287
/* ***************************** shade displist. note colors now are in rgb(a) order ******************** */
289
/* create default shade input... save cpu cycles with ugly global */
290
/* XXXX bad code warning: local ShadeInput initialize... */
291
static ShadeInput shi;
292
static void init_fastshade_shadeinput(Render *re)
294
memset(&shi, 0, sizeof(ShadeInput));
295
shi.lay= RE_GetScene(re)->lay;
297
shi.passflag= SCE_PASS_COMBINED;
298
shi.combinedflag= -1;
301
static Render *fastshade_get_render(Scene *UNUSED(scene))
303
// XXX 2.5: this crashes combined with previewrender
304
// due to global R so disabled for now
306
/* XXX ugly global still, but we can't do preview while rendering */
309
Render *re= RE_GetRender("_Shade View_");
311
re= RE_NewRender("_Shade View_");
313
RE_Database_Baking(re, scene, 0, 0); /* 0= no faces */
322
/* called on file reading */
323
void fastshade_free_render(void)
325
Render *re= RE_GetRender("_Shade View_");
328
RE_Database_Free(re);
333
static int fastshade_customdata_layer_num(int n, int active)
335
/* make the active layer the first */
336
if (n == active) return 0;
337
else if (n < active) return n+1;
341
static void fastshade_customdata(CustomData *fdata, int a, int j, Material *ma)
343
CustomDataLayer *layer;
345
int index, n, needuv= ma->texco & TEXCO_UV;
351
for(index=0; index<fdata->totlayer; index++) {
352
layer= &fdata->layers[index];
354
if(needuv && layer->type == CD_MTFACE && shi.totuv < MAX_MTFACE) {
355
n= fastshade_customdata_layer_num(shi.totuv, layer->active_rnd);
356
mtface= &((MTFace*)layer->data)[a];
358
shi.uv[shi.totuv].uv[0]= 2.0f*mtface->uv[j][0]-1.0f;
359
shi.uv[shi.totuv].uv[1]= 2.0f*mtface->uv[j][1]-1.0f;
360
shi.uv[shi.totuv].uv[2]= 1.0f;
362
shi.uv[shi.totuv].name= layer->name;
365
else if(layer->type == CD_MCOL && shi.totcol < MAX_MCOL) {
366
n= fastshade_customdata_layer_num(shi.totcol, layer->active_rnd);
367
vertcol= (char*)&((MCol*)layer->data)[a*4 + j];
369
shi.col[shi.totcol].col[0]= ((float)vertcol[3])/255.0f;
370
shi.col[shi.totcol].col[1]= ((float)vertcol[2])/255.0f;
371
shi.col[shi.totcol].col[2]= ((float)vertcol[1])/255.0f;
373
shi.col[shi.totcol].name= layer->name;
378
if(needuv && shi.totuv == 0)
379
VECCOPY(shi.uv[0].uv, shi.lo);
382
VECCOPY(shi.vcol, shi.col[0].col);
385
static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2)
394
VECCOPY(shi.vno, shi.vn);
395
VECCOPY(shi.facenor, shi.vn);
398
VECCOPY(shi.lo, orco);
400
if(ma->texco & TEXCO_GLOB) {
401
VECCOPY(shi.gl, shi.lo);
403
if(ma->texco & TEXCO_WINDOW) {
404
VECCOPY(shi.winco, shi.lo);
406
if(ma->texco & TEXCO_STICKY) {
407
VECCOPY(shi.sticky, shi.lo);
409
if(ma->texco & TEXCO_OBJECT) {
410
VECCOPY(shi.co, shi.lo);
412
if(ma->texco & TEXCO_NORM) {
413
VECCOPY(shi.orn, shi.vn);
415
if(ma->texco & TEXCO_REFL) {
416
float inp= 2.0f * (shi.vn[2]);
417
shi.ref[0]= (inp*shi.vn[0]);
418
shi.ref[1]= (inp*shi.vn[1]);
419
shi.ref[2]= (-1.0f + inp*shi.vn[2]);
423
shi.mat= ma; /* set each time... node shaders change it */
424
RE_shade_external(NULL, &shi, &shr);
426
a= 256.0f*(shr.combined[0]);
427
col1[0]= CLAMPIS(a, 0, 255);
428
a= 256.0f*(shr.combined[1]);
429
col1[1]= CLAMPIS(a, 0, 255);
430
a= 256.0f*(shr.combined[2]);
431
col1[2]= CLAMPIS(a, 0, 255);
434
shi.vn[0]= -shi.vn[0];
435
shi.vn[1]= -shi.vn[1];
436
shi.vn[2]= -shi.vn[2];
438
shi.mat= ma; /* set each time... node shaders change it */
439
RE_shade_external(NULL, &shi, &shr);
441
a= 256.0f*(shr.combined[0]);
442
col2[0]= CLAMPIS(a, 0, 255);
443
a= 256.0f*(shr.combined[1]);
444
col2[1]= CLAMPIS(a, 0, 255);
445
a= 256.0f*(shr.combined[2]);
446
col2[2]= CLAMPIS(a, 0, 255);
450
static void init_fastshade_for_ob(Render *re, Object *ob, int *need_orco_r, float mat[4][4], float imat[3][3])
453
float amb[3]= {0.0f, 0.0f, 0.0f};
456
/* initialize globals in render */
457
RE_shade_external(re, NULL, NULL);
459
/* initialize global here */
460
init_fastshade_shadeinput(re);
462
RE_DataBase_GetView(re, tmat);
463
mul_m4_m4m4(mat, ob->obmat, tmat);
465
invert_m4_m4(tmat, mat);
466
copy_m3_m4(imat, tmat);
467
if(ob->transflag & OB_NEG_SCALE) mul_m3_fl(imat, -1.0);
469
if (need_orco_r) *need_orco_r= 0;
470
for(a=0; a<ob->totcol; a++) {
471
Material *ma= give_current_material(ob, a+1);
473
init_render_material(ma, 0, amb);
475
if(ma->texco & TEXCO_ORCO) {
476
if (need_orco_r) *need_orco_r= 1;
482
static void end_fastshade_for_ob(Object *ob)
486
for(a=0; a<ob->totcol; a++) {
487
Material *ma= give_current_material(ob, a+1);
489
end_render_material(ma);
494
static void mesh_create_shadedColors(Render *re, Object *ob, int onlyForMesh, unsigned int **col1_r, unsigned int **col2_r)
500
unsigned int *col1, *col2;
501
float *orco, *vnors, *nors, imat[3][3], mat[4][4], vec[3];
502
int a, i, need_orco, totface, totvert;
503
CustomDataMask dataMask = CD_MASK_BAREMESH | CD_MASK_MCOL
504
| CD_MASK_MTFACE | CD_MASK_NORMAL;
507
init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
510
dataMask |= CD_MASK_ORCO;
513
dm = mesh_get_derived_deform(RE_GetScene(re), ob, dataMask);
515
dm = mesh_get_derived_final(RE_GetScene(re), ob, dataMask);
517
mvert = dm->getVertArray(dm);
518
mface = dm->getFaceArray(dm);
519
nors = dm->getFaceDataArray(dm, CD_NORMAL);
520
totvert = dm->getNumVerts(dm);
521
totface = dm->getNumFaces(dm);
522
orco= dm->getVertDataArray(dm, CD_ORCO);
528
*col1_r = col1 = MEM_mallocN(sizeof(*col1)*totface*4, "col1");
530
if (col2_r && (me->flag & ME_TWOSIDED))
531
col2 = MEM_mallocN(sizeof(*col2)*totface*4, "col2");
535
if (col2_r) *col2_r = col2;
539
vnors= MEM_mallocN(totvert*3*sizeof(float), "vnors disp");
540
for (a=0; a<totvert; a++) {
541
MVert *mv = &mvert[a];
542
float *vn= &vnors[a*3];
548
vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
549
vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
550
vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
554
for (i=0; i<totface; i++) {
555
extern Material defmaterial; /* material.c */
556
MFace *mf= &mface[i];
557
Material *ma= give_current_material(ob, mf->mat_nr+1);
558
int j, vidx[4], nverts= mf->v4?4:3;
559
unsigned char *col1base= (unsigned char*) &col1[i*4];
560
unsigned char *col2base= (unsigned char*) (col2?&col2[i*4]:NULL);
563
if(ma==NULL) ma= &defmaterial;
571
VECCOPY(nor, &nors[i*3]);
574
normal_quad_v3( nor,mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co, mvert[mf->v4].co);
576
normal_tri_v3( nor,mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co);
579
n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
580
n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
581
n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
584
for (j=0; j<nverts; j++) {
585
MVert *mv= &mvert[vidx[j]];
586
char *col1= (char*)&col1base[j*4];
587
char *col2= (char*)(col2base?&col2base[j*4]:NULL);
588
float *vn = (mf->flag & ME_SMOOTH)?&vnors[3*vidx[j]]:n1;
590
mul_v3_m4v3(vec, mat, mv->co);
592
mul_v3_v3fl(vec, vn, 0.001f);
594
fastshade_customdata(&dm->faceData, i, j, ma);
595
fastshade(vec, vn, orco?&orco[vidx[j]*3]:mv->co, ma, col1, col2);
602
end_fastshade_for_ob(ob);
605
void shadeMeshMCol(Scene *scene, Object *ob, Mesh *me)
607
Render *re= fastshade_get_render(scene);
610
unsigned int *mcol= (unsigned int*)me->mcol;
613
mesh_create_shadedColors(re, ob, 1, &mcol, NULL);
614
me->mcol= (MCol*)mcol;
617
for(cp= (char *)me->mcol, a= 4*me->totface; a>0; a--, cp+=4) {
618
SWAP(char, cp[0], cp[3]);
619
SWAP(char, cp[1], cp[2]);
624
/* has base pointer, to check for layer */
625
/* called from drawobject.c */
626
void shadeDispList(Scene *scene, Base *base)
628
Object *ob= base->object;
632
float imat[3][3], mat[4][4], vec[3];
633
float *fp, *nor, n1[3];
637
re= fastshade_get_render(scene);
641
dl = find_displist(&ob->disp, DL_VERTCOL);
643
BLI_remlink(&ob->disp, dl);
647
if(ob->type==OB_MESH) {
648
dl= MEM_callocN(sizeof(DispList), "displistshade");
649
dl->type= DL_VERTCOL;
651
mesh_create_shadedColors(re, ob, 0, &dl->col1, &dl->col2);
653
/* add dl to ob->disp after mesh_create_shadedColors, because it
654
might indirectly free ob->disp */
655
BLI_addtail(&ob->disp, dl);
659
init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
661
if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
663
/* now we need the normals */
667
extern Material defmaterial; /* material.c */
669
dlob= MEM_callocN(sizeof(DispList), "displistshade");
670
BLI_addtail(&ob->disp, dlob);
671
dlob->type= DL_VERTCOL;
672
dlob->parts= dl->parts;
675
if(dl->type==DL_INDEX3) {
676
col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
679
col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->parts*dl->nr, "col1");
683
ma= give_current_material(ob, dl->col+1);
684
if(ma==NULL) ma= &defmaterial;
686
if(dl->type==DL_INDEX3) {
688
/* there's just one normal */
689
n1[0]= imat[0][0]*dl->nors[0]+imat[0][1]*dl->nors[1]+imat[0][2]*dl->nors[2];
690
n1[1]= imat[1][0]*dl->nors[0]+imat[1][1]*dl->nors[1]+imat[1][2]*dl->nors[2];
691
n1[2]= imat[2][0]*dl->nors[0]+imat[2][1]*dl->nors[1]+imat[2][2]*dl->nors[2];
698
mul_v3_m4v3(vec, mat, fp);
700
fastshade(vec, n1, fp, ma, (char *)col1, NULL);
706
else if(dl->type==DL_SURF) {
713
mul_v3_m4v3(vec, mat, fp);
715
n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
716
n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
717
n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
720
fastshade(vec, n1, fp, ma, (char *)col1, NULL);
722
fp+= 3; nor+= 3; col1++;
729
else if(ob->type==OB_MBALL) {
730
/* there are normals already */
735
if(dl->type==DL_INDEX4) {
737
extern Material defmaterial; /* material.c */
739
if(dl->col1) MEM_freeN(dl->col1);
740
col1= dl->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
742
ma= give_current_material(ob, dl->col+1);
743
if(ma==NULL) ma= &defmaterial;
750
mul_v3_m4v3(vec, mat, fp);
753
n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
754
n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
755
n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
758
fastshade(vec, n1, fp, ma, (char *)col1, NULL);
760
fp+= 3; col1++; nor+= 3;
768
end_fastshade_for_ob(ob);
772
/* frees render and shade part of displists */
773
/* note: dont do a shade again, until a redraw happens */
774
void reshadeall_displist(Scene *scene)
779
fastshade_free_render();
781
for(base= scene->base.first; base; base= base->next) {
784
if(ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
785
freedisplist(&ob->disp);
787
if(base->lay & scene->lay) {
788
/* Metaballs have standard displist at the Object */
789
if(ob->type==OB_MBALL) shadeDispList(scene, base);
794
286
/* ****************** make displists ********************* */
796
288
static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase, int forRender)