4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2006 Blender Foundation
21 * All rights reserved.
23 * Contributors: Hos, Robert Wenzlaff.
25 * ***** END GPL LICENSE BLOCK *****
32 #include "MTC_matrixops.h"
33 #include "BLI_arithb.h"
34 #include "BLI_blenlib.h"
36 #include "DNA_curve_types.h"
37 #include "DNA_group_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_material_types.h"
42 #include "BKE_utildefines.h"
46 #include "renderpipeline.h"
47 #include "render_types.h"
48 #include "renderdatabase.h"
49 #include "rendercore.h"
56 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
57 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
58 /* only to be used here in this file, it's for speed */
59 extern struct Render R;
60 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
63 #define VECADDISFAC(v1,v3,fac) {*(v1)+= *(v3)*(fac); *(v1+1)+= *(v3+1)*(fac); *(v1+2)+= *(v3+2)*(fac);}
67 /* Shade Sample order:
69 - shade_samples_fill_with_ps()
71 - shade_input_set_triangle() <- if prev sample-face is same, use shade_input_copy_triangle()
73 - shade_input_set_viewco() <- not for ray or bake
74 - shade_input_set_uv() <- not for ray or bake
75 - shade_input_set_normals()
78 - shade_samples_do_AO()
81 - shade_input_set_shade_texco()
82 - shade_samples_do_shade()
83 - OSA: distribute sample result with filter masking
88 /* also used as callback for nodes */
89 /* delivers a fully filled in ShadeResult, for all passes */
90 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
93 shade_lamp_loop(shi, shr); /* clears shr */
95 if(shi->translucency!=0.0f) {
97 float fac= shi->translucency;
99 /* gotta copy it again */
100 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
101 shi->har= shi->mat->har;
103 VECCOPY(shi->vn, shi->vno);
104 VECMUL(shi->vn, -1.0f);
105 VECMUL(shi->facenor, -1.0f);
106 shi->depth++; /* hack to get real shadow now */
107 shade_lamp_loop(shi, &shr_t);
110 /* a couple of passes */
111 VECADDISFAC(shr->combined, shr_t.combined, fac);
112 if(shi->passflag & SCE_PASS_SPEC)
113 VECADDISFAC(shr->spec, shr_t.spec, fac);
114 if(shi->passflag & SCE_PASS_DIFFUSE)
115 VECADDISFAC(shr->diff, shr_t.diff, fac);
116 if(shi->passflag & SCE_PASS_SHADOW)
117 VECADDISFAC(shr->shad, shr_t.shad, fac);
119 VECMUL(shi->vn, -1.0f);
120 VECMUL(shi->facenor, -1.0f);
123 /* depth >= 1 when ray-shading */
125 if(R.r.mode & R_RAYTRACE) {
126 if(shi->ray_mirror!=0.0f || ((shi->mat->mode & MA_RAYTRANSP) && shr->alpha!=1.0f)) {
127 /* ray trace works on combined, but gives pass info */
131 /* disable adding of sky for raytransp */
132 if(shi->mat->mode & MA_RAYTRANSP)
133 if((shi->layflag & SCE_LAY_SKY) && (R.r.alphamode==R_ADDSKY))
138 /* delivers a fully filled in ShadeResult, for all passes */
139 void shade_volume_loop(ShadeInput *shi, ShadeResult *shr)
141 if(R.r.mode & R_RAYTRACE) volume_trace(shi, shr);
145 /* do a shade, finish up some passes, apply mist */
146 void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
150 /* ------ main shading loop -------- */
152 if(shi->mat->nodetree && shi->mat->use_nodes) {
153 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
156 /* copy all relevant material vars, note, keep this synced with render_types.h */
157 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
158 shi->har= shi->mat->har;
160 if (shi->mat->material_type == MA_SOLID) shade_material_loop(shi, shr);
161 else if (shi->mat->material_type == MA_VOLUME) shade_volume_loop(shi, shr);
164 /* copy additional passes */
165 if(shi->passflag & (SCE_PASS_VECTOR|SCE_PASS_NORMAL|SCE_PASS_RADIO)) {
166 QUATCOPY(shr->winspeed, shi->winspeed);
167 VECCOPY(shr->nor, shi->vn);
168 VECCOPY(shr->rad, shi->rad);
172 if((shi->passflag & SCE_PASS_MIST) || ((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0)) {
173 if(R.r.mode & R_ORTHO)
174 shr->mist= mistfactor(-shi->co[2], shi->co);
176 shr->mist= mistfactor(VecLength(shi->co), shi->co);
178 else shr->mist= 0.0f;
180 if((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0 ) {
185 /* add mist and premul color */
186 if(shr->alpha!=1.0f || alpha!=1.0f) {
187 float fac= alpha*(shr->alpha);
189 shr->combined[3]= fac;
190 shr->combined[0]*= fac;
191 shr->combined[1]*= fac;
192 shr->combined[2]*= fac;
194 else shr->combined[3]= 1.0f;
200 /* **************************************************************************** */
202 /* **************************************************************************** */
205 void vlr_set_uv_indices(VlakRen *vlr, int *i1, int *i2, int *i3)
207 /* to prevent storing new tfaces or vcols, we check a split runtime */
211 /* 1---2 1---2 0 = orig face, 1 = new face */
213 /* Update vert nums to point to correct verts of original face */
214 if(vlr->flag & R_DIVIDE_24) {
215 if(vlr->flag & R_FACE_SPLIT) {
216 (*i1)++; (*i2)++; (*i3)++;
222 else if(vlr->flag & R_FACE_SPLIT) {
227 /* copy data from face to ShadeInput, general case */
228 /* indices 0 1 2 3 only */
229 void shade_input_set_triangle_i(ShadeInput *shi, ObjectInstanceRen *obi, VlakRen *vlr, short i1, short i2, short i3)
231 VertRen **vpp= &vlr->v1;
245 /* note, shi->mat is set in node shaders */
246 shi->mat= shi->mat_override?shi->mat_override:vlr->mat;
248 shi->osatex= (shi->mat->texco & TEXCO_OSA);
249 shi->mode= shi->mat->mode_l; /* or-ed result for all nodes */
251 /* facenormal copy, can get flipped */
252 shi->flippednor= RE_vlakren_get_normal(&R, obi, vlr, shi->facenor);
254 /* copy of original pre-flipped normal, for geometry->front/back node output */
255 VECCOPY(shi->orignor, shi->facenor);
257 VECMUL(shi->orignor, -1.0f);
259 /* calculate vertexnormals */
260 if(vlr->flag & R_SMOOTH) {
261 VECCOPY(shi->n1, shi->v1->n);
262 VECCOPY(shi->n2, shi->v2->n);
263 VECCOPY(shi->n3, shi->v3->n);
265 if(obi->flag & R_TRANSFORMED) {
266 Mat3MulVecfl(obi->nmat, shi->n1);
267 Mat3MulVecfl(obi->nmat, shi->n2);
268 Mat3MulVecfl(obi->nmat, shi->n3);
271 if(!(vlr->flag & (R_NOPUNOFLIP|R_TANGENT))) {
272 if(INPR(shi->facenor, shi->n1) < 0.0f) {
273 shi->n1[0]= -shi->n1[0];
274 shi->n1[1]= -shi->n1[1];
275 shi->n1[2]= -shi->n1[2];
277 if(INPR(shi->facenor, shi->n2) < 0.0f) {
278 shi->n2[0]= -shi->n2[0];
279 shi->n2[1]= -shi->n2[1];
280 shi->n2[2]= -shi->n2[2];
282 if(INPR(shi->facenor, shi->n3) < 0.0f) {
283 shi->n3[0]= -shi->n3[0];
284 shi->n3[1]= -shi->n3[1];
285 shi->n3[2]= -shi->n3[2];
291 /* note, facenr declared volatile due to over-eager -O2 optimizations
292 * on cygwin (particularly -frerun-cse-after-loop)
295 /* copy data from face to ShadeInput, scanline case */
296 void shade_input_set_triangle(ShadeInput *shi, volatile int obi, volatile int facenr, int normal_flip)
299 shi->obi= &R.objectinstance[obi];
300 shi->obr= shi->obi->obr;
301 shi->facenr= (facenr-1) & RE_QUAD_MASK;
302 if( shi->facenr < shi->obr->totvlak ) {
303 VlakRen *vlr= RE_findOrAddVlak(shi->obr, shi->facenr);
305 if(facenr & RE_QUAD_OFFS)
306 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 2, 3);
308 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 1, 2);
311 shi->vlr= NULL; /* general signal we got sky */
314 shi->vlr= NULL; /* general signal we got sky */
317 /* full osa case: copy static info */
318 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
320 /* not so nice, but works... warning is in RE_shader_ext.h */
321 memcpy(shi, from, sizeof(struct ShadeInputCopy));
324 /* copy data from strand to shadeinput */
325 void shade_input_set_strand(ShadeInput *shi, StrandRen *strand, StrandPoint *spoint)
327 /* note, shi->mat is set in node shaders */
328 shi->mat= shi->mat_override? shi->mat_override: strand->buffer->ma;
330 shi->osatex= (shi->mat->texco & TEXCO_OSA);
331 shi->mode= shi->mat->mode_l; /* or-ed result for all nodes */
333 /* shade_input_set_viewco equivalent */
334 VECCOPY(shi->co, spoint->co);
335 VECCOPY(shi->view, shi->co);
336 Normalize(shi->view);
338 shi->xs= (int)spoint->x;
339 shi->ys= (int)spoint->y;
341 if(shi->osatex || (R.r.mode & R_SHADOW)) {
342 VECCOPY(shi->dxco, spoint->dtco);
343 VECCOPY(shi->dyco, spoint->dsco);
346 /* dxview, dyview, not supported */
348 /* facenormal, simply viewco flipped */
349 VECCOPY(shi->facenor, spoint->nor);
350 VECCOPY(shi->orignor, shi->facenor);
352 /* shade_input_set_normals equivalent */
353 if(shi->mat->mode & MA_TANGENT_STR) {
354 VECCOPY(shi->vn, spoint->tan)
359 Crossf(cross, spoint->co, spoint->tan);
360 Crossf(shi->vn, cross, spoint->tan);
363 if(INPR(shi->vn, shi->view) < 0.0f)
364 VecMulf(shi->vn, -1.0f);
367 VECCOPY(shi->vno, shi->vn);
370 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
372 StrandBuffer *strandbuf= strand->buffer;
373 ObjectRen *obr= strandbuf->obr;
375 int mode= shi->mode; /* or-ed result for all nodes */
376 short texco= shi->mat->texco;
378 if((shi->mat->texco & TEXCO_REFL)) {
379 /* shi->dxview, shi->dyview, not supported */
382 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL))) {
386 if(mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
387 VECCOPY(shi->tang, spoint->tan);
388 VECCOPY(shi->nmaptang, spoint->tan);
391 if(mode & MA_STR_SURFDIFF) {
392 float *surfnor= RE_strandren_get_surfnor(obr, strand, 0);
395 VECCOPY(shi->surfnor, surfnor)
397 VECCOPY(shi->surfnor, shi->vn)
399 if(shi->mat->strand_surfnor > 0.0f) {
401 for(sv=strand->vert; sv!=svert; sv++)
402 shi->surfdist+=VecLenf(sv->co, (sv+1)->co);
403 shi->surfdist += spoint->t*VecLenf(sv->co, (sv+1)->co);
407 if(R.r.mode & R_SPEED) {
410 speed= RE_strandren_get_winspeed(shi->obi, strand, 0);
412 QUATCOPY(shi->winspeed, speed)
414 shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
417 /* shade_input_set_shade_texco equivalent */
418 if(texco & NEED_UV) {
419 if(texco & TEXCO_ORCO) {
420 VECCOPY(shi->lo, strand->orco);
421 /* no shi->osatex, orco derivatives are zero */
424 if(texco & TEXCO_GLOB) {
425 VECCOPY(shi->gl, shi->co);
426 MTC_Mat4MulVecfl(R.viewinv, shi->gl);
429 VECCOPY(shi->dxgl, shi->dxco);
430 MTC_Mat3MulVecfl(R.imat, shi->dxco);
431 VECCOPY(shi->dygl, shi->dyco);
432 MTC_Mat3MulVecfl(R.imat, shi->dyco);
436 if(texco & TEXCO_STRAND) {
437 shi->strandco= spoint->strandco;
440 shi->dxstrand= spoint->dtstrandco;
445 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
453 shi->actuv= obr->actmtface;
454 shi->actcol= obr->actmcol;
456 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
457 for (i=0; (mcol=RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
458 ShadeInputCol *scol= &shi->col[i];
459 char *cp= (char*)mcol;
464 scol->col[0]= cp[3]/255.0f;
465 scol->col[1]= cp[2]/255.0f;
466 scol->col[2]= cp[1]/255.0f;
470 shi->vcol[0]= shi->col[shi->actcol].col[0];
471 shi->vcol[1]= shi->col[shi->actcol].col[1];
472 shi->vcol[2]= shi->col[shi->actcol].col[2];
481 for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
482 ShadeInputUV *suv= &shi->uv[i];
487 if(strandbuf->overrideuv == i) {
489 suv->uv[1]= spoint->strandco;
493 suv->uv[0]= -1.0f + 2.0f*uv[0];
494 suv->uv[1]= -1.0f + 2.0f*uv[1];
495 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
505 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
506 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
514 if(shi->totuv == 0) {
515 ShadeInputUV *suv= &shi->uv[0];
518 suv->uv[1]= spoint->strandco;
519 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
521 if(mode & MA_FACETEXTURE) {
522 /* no tface? set at 1.0f */
531 if(texco & TEXCO_NORM) {
532 shi->orn[0]= -shi->vn[0];
533 shi->orn[1]= -shi->vn[1];
534 shi->orn[2]= -shi->vn[2];
537 if(mode & MA_RADIO) {
541 if(texco & TEXCO_REFL) {
542 /* mirror reflection color textures (and envmap) */
543 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
546 if(texco & TEXCO_STRESS) {
550 if(texco & TEXCO_TANGENT) {
551 if((mode & MA_TANGENT_V)==0) {
552 /* just prevent surprises */
553 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
554 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
559 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
561 /* this only avalailable for scanline renders */
563 if(texco & TEXCO_WINDOW) {
564 shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
565 shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
577 if(texco & TEXCO_STICKY) {
583 /* scanline pixel coordinates */
584 /* requires set_triangle */
585 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float z)
589 /* currently in use for dithering (soft shadow), node preview, irregular shad */
593 calc_view_vector(shi->view, x, y); /* returns not normalized, so is in viewplane coords */
595 /* wire cannot use normal for calculating shi->co */
596 if(shi->mat->mode & MA_WIRE) {
598 if(R.r.mode & R_ORTHO)
599 calc_renderco_ortho(shi->co, x, y, z);
601 calc_renderco_zbuf(shi->co, shi->view, z);
606 VECCOPY(v1, shi->v1->co);
608 if(shi->obi->flag & R_TRANSFORMED)
609 Mat4MulVecfl(shi->obi->mat, v1);
611 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
613 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
614 if(R.r.mode & R_ORTHO) {
615 /* x and y 3d coordinate can be derived from pixel coord and winmat */
616 float fx= 2.0f/(R.winx*R.winmat[0][0]);
617 float fy= 2.0f/(R.winy*R.winmat[1][1]);
619 shi->co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
620 shi->co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
622 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
623 if(shi->facenor[2]!=0.0f)
624 shi->co[2]= (dface - shi->facenor[0]*shi->co[0] - shi->facenor[1]*shi->co[1])/shi->facenor[2];
628 if(shi->osatex || (R.r.mode & R_SHADOW) ) {
631 if(shi->facenor[2]!=0.0f)
632 shi->dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
638 if(shi->facenor[2]!=0.0f)
639 shi->dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
643 if( (shi->mat->texco & TEXCO_REFL) ) {
644 if(shi->co[2]!=0.0f) fac= 1.0f/shi->co[2]; else fac= 0.0f;
645 shi->dxview= -R.viewdx*fac;
646 shi->dyview= -R.viewdy*fac;
653 div= shi->facenor[0]*shi->view[0] + shi->facenor[1]*shi->view[1] + shi->facenor[2]*shi->view[2];
654 if (div!=0.0f) fac= dface/div;
657 shi->co[0]= fac*shi->view[0];
658 shi->co[1]= fac*shi->view[1];
659 shi->co[2]= fac*shi->view[2];
661 /* pixel dx/dy for render coord */
662 if(shi->osatex || (R.r.mode & R_SHADOW) ) {
663 float u= dface/(div - R.viewdx*shi->facenor[0]);
664 float v= dface/(div - R.viewdy*shi->facenor[1]);
666 shi->dxco[0]= shi->co[0]- (shi->view[0]-R.viewdx)*u;
667 shi->dxco[1]= shi->co[1]- (shi->view[1])*u;
668 shi->dxco[2]= shi->co[2]- (shi->view[2])*u;
670 shi->dyco[0]= shi->co[0]- (shi->view[0])*v;
671 shi->dyco[1]= shi->co[1]- (shi->view[1]-R.viewdy)*v;
672 shi->dyco[2]= shi->co[2]- (shi->view[2])*v;
674 if( (shi->mat->texco & TEXCO_REFL) ) {
675 if(fac!=0.0f) fac= 1.0f/fac;
676 shi->dxview= -R.viewdx*fac;
677 shi->dyview= -R.viewdy*fac;
683 /* set camera coords - for scanline, it's always 0.0,0.0,0.0 (render is in camera space)
684 * however for raytrace it can be different - the position of the last intersection */
685 shi->camera_co[0] = shi->camera_co[1] = shi->camera_co[2] = 0.0f;
687 /* cannot normalize earlier, code above needs it at viewplane level */
688 Normalize(shi->view);
691 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
692 void shade_input_set_uv(ShadeInput *shi)
694 VlakRen *vlr= shi->vlr;
696 if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
697 float v1[3], v2[3], v3[3];
699 VECCOPY(v1, shi->v1->co);
700 VECCOPY(v2, shi->v2->co);
701 VECCOPY(v3, shi->v3->co);
703 if(shi->obi->flag & R_TRANSFORMED) {
704 Mat4MulVecfl(shi->obi->mat, v1);
705 Mat4MulVecfl(shi->obi->mat, v2);
706 Mat4MulVecfl(shi->obi->mat, v3);
709 /* exception case for wire render of edge */
710 if(vlr->v2==vlr->v3) {
713 lend= VecLenf(v2, v1);
714 lenc= VecLenf(shi->co, v1);
720 shi->u= - (1.0f - lenc/lend);
732 /* most of this could become re-used for faces */
733 float detsh, t00, t10, t01, t11, xn, yn, zn;
736 /* find most stable axis to project */
737 xn= fabs(shi->facenor[0]);
738 yn= fabs(shi->facenor[1]);
739 zn= fabs(shi->facenor[2]);
741 if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
742 else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
743 else { axis1= 1; axis2= 2; }
745 /* compute u,v and derivatives */
746 t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
747 t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
749 detsh= 1.0f/(t00*t11-t10*t01);
750 t00*= detsh; t01*=detsh;
751 t10*=detsh; t11*=detsh;
753 shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
754 shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
756 shi->dx_u= shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
757 shi->dx_v= shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
758 shi->dy_u= shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
759 shi->dy_v= shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
762 /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
763 CLAMP(shi->u, -2.0f, 1.0f);
764 CLAMP(shi->v, -2.0f, 1.0f);
769 void shade_input_set_normals(ShadeInput *shi)
771 float u= shi->u, v= shi->v;
774 /* calculate vertexnormals */
775 if(shi->vlr->flag & R_SMOOTH) {
776 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
778 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
779 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
780 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
785 VECCOPY(shi->vn, shi->facenor);
788 VECCOPY(shi->vno, shi->vn);
792 /* use by raytrace, sss, bake to flip into the right direction */
793 void shade_input_flip_normals(ShadeInput *shi)
795 shi->facenor[0]= -shi->facenor[0];
796 shi->facenor[1]= -shi->facenor[1];
797 shi->facenor[2]= -shi->facenor[2];
799 shi->vn[0]= -shi->vn[0];
800 shi->vn[1]= -shi->vn[1];
801 shi->vn[2]= -shi->vn[2];
803 shi->vno[0]= -shi->vno[0];
804 shi->vno[1]= -shi->vno[1];
805 shi->vno[2]= -shi->vno[2];
807 shi->flippednor= !shi->flippednor;
810 void shade_input_set_shade_texco(ShadeInput *shi)
812 ObjectInstanceRen *obi= shi->obi;
813 ObjectRen *obr= shi->obr;
814 VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
815 float u= shi->u, v= shi->v;
816 float l= 1.0f+u+v, dl;
817 int mode= shi->mode; /* or-ed result for all nodes */
818 short texco= shi->mat->texco;
821 if(shi->vlr->flag & R_SMOOTH) {
823 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
824 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
826 dl= shi->dx_u+shi->dx_v;
827 shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
828 shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
829 shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
830 dl= shi->dy_u+shi->dy_v;
831 shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
832 shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
833 shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
839 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
840 float *tangent, *s1, *s2, *s3;
843 if(shi->vlr->flag & R_SMOOTH) {
849 /* qdn: flat faces have tangents too,
850 could pick either one, using average here */
856 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
857 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
859 if(mode & MA_TANGENT_V) {
860 s1 = RE_vertren_get_tangent(obr, v1, 0);
861 s2 = RE_vertren_get_tangent(obr, v2, 0);
862 s3 = RE_vertren_get_tangent(obr, v3, 0);
865 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
866 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
867 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
869 if(obi->flag & R_TRANSFORMED)
870 Mat3MulVecfl(obi->nmat, shi->tang);
872 Normalize(shi->tang);
873 VECCOPY(shi->nmaptang, shi->tang);
877 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
878 tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
881 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
883 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
889 shi->nmaptang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
890 shi->nmaptang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
891 shi->nmaptang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
893 if(obi->flag & R_TRANSFORMED)
894 Mat3MulVecfl(obi->nmat, shi->nmaptang);
896 Normalize(shi->nmaptang);
901 if(mode & MA_STR_SURFDIFF) {
902 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
905 VECCOPY(shi->surfnor, surfnor)
906 if(obi->flag & R_TRANSFORMED)
907 Mat3MulVecfl(obi->nmat, shi->surfnor);
910 VECCOPY(shi->surfnor, shi->vn)
915 if(R.r.mode & R_SPEED) {
918 s1= RE_vertren_get_winspeed(obi, v1, 0);
919 s2= RE_vertren_get_winspeed(obi, v2, 0);
920 s3= RE_vertren_get_winspeed(obi, v3, 0);
922 shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
923 shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
924 shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
925 shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
928 shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
932 /* pass option forces UV calc */
933 if(shi->passflag & SCE_PASS_UV)
934 texco |= (NEED_UV|TEXCO_UV);
936 /* texture coordinates. shi->dxuv shi->dyuv have been set */
937 if(texco & NEED_UV) {
939 if(texco & TEXCO_ORCO) {
947 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
948 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
949 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
952 dl= shi->dx_u+shi->dx_v;
953 shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
954 shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
955 shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
956 dl= shi->dy_u+shi->dy_v;
957 shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
958 shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
959 shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
963 VECCOPY(shi->duplilo, obi->dupliorco);
966 if(texco & TEXCO_GLOB) {
967 VECCOPY(shi->gl, shi->co);
968 MTC_Mat4MulVecfl(R.viewinv, shi->gl);
970 VECCOPY(shi->dxgl, shi->dxco);
971 MTC_Mat3MulVecfl(R.imat, shi->dxco);
972 VECCOPY(shi->dygl, shi->dyco);
973 MTC_Mat3MulVecfl(R.imat, shi->dyco);
977 if(texco & TEXCO_STRAND) {
978 shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
980 dl= shi->dx_u+shi->dx_v;
981 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
982 dl= shi->dy_u+shi->dy_v;
983 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
987 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
988 VlakRen *vlr= shi->vlr;
992 int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
994 /* uv and vcols are not copied on split, so set them according vlr divide flag */
995 vlr_set_uv_indices(vlr, &j1, &j2, &j3);
999 shi->actuv= obr->actmtface;
1000 shi->actcol= obr->actmcol;
1002 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1003 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1004 ShadeInputCol *scol= &shi->col[i];
1005 char *cp1, *cp2, *cp3;
1010 cp1= (char *)(mcol+j1);
1011 cp2= (char *)(mcol+j2);
1012 cp3= (char *)(mcol+j3);
1014 scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
1015 scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
1016 scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
1020 shi->vcol[0]= shi->col[shi->actcol].col[0];
1021 shi->vcol[1]= shi->col[shi->actcol].col[1];
1022 shi->vcol[2]= shi->col[shi->actcol].col[2];
1033 for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1034 ShadeInputUV *suv= &shi->uv[i];
1035 float *uv1, *uv2, *uv3;
1044 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1045 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1046 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1051 dl= shi->dx_u+shi->dx_v;
1055 suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1056 suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1058 dl= shi->dy_u+shi->dy_v;
1062 suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1063 suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1066 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1067 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1073 if(tface && tface->tpage)
1074 render_realtime_texture(shi, tface->tpage);
1078 shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1079 shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1080 shi->dupliuv[2]= 0.0f;
1082 if(shi->totuv == 0) {
1083 ShadeInputUV *suv= &shi->uv[0];
1085 suv->uv[0]= 2.0f*(u+.5f);
1086 suv->uv[1]= 2.0f*(v+.5f);
1087 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1089 if(mode & MA_FACETEXTURE) {
1090 /* no tface? set at 1.0f */
1099 if(texco & TEXCO_NORM) {
1100 shi->orn[0]= -shi->vn[0];
1101 shi->orn[1]= -shi->vn[1];
1102 shi->orn[2]= -shi->vn[2];
1105 if(mode & MA_RADIO) {
1106 float *r1, *r2, *r3;
1108 r1= RE_vertren_get_rad(obr, v1, 0);
1109 r2= RE_vertren_get_rad(obr, v2, 0);
1110 r3= RE_vertren_get_rad(obr, v3, 0);
1112 if(r1 && r2 && r3) {
1113 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
1114 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
1115 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
1118 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1121 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1123 if(texco & TEXCO_REFL) {
1124 /* mirror reflection color textures (and envmap) */
1125 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
1128 if(texco & TEXCO_STRESS) {
1129 float *s1, *s2, *s3;
1131 s1= RE_vertren_get_stress(obr, v1, 0);
1132 s2= RE_vertren_get_stress(obr, v2, 0);
1133 s3= RE_vertren_get_stress(obr, v3, 0);
1134 if(s1 && s2 && s3) {
1135 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1136 if(shi->stress<1.0f) shi->stress-= 1.0f;
1137 else shi->stress= (shi->stress-1.0f)/shi->stress;
1139 else shi->stress= 0.0f;
1142 if(texco & TEXCO_TANGENT) {
1143 if((mode & MA_TANGENT_V)==0) {
1144 /* just prevent surprises */
1145 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1146 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1151 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1153 /* this only avalailable for scanline renders */
1158 if(texco & TEXCO_WINDOW) {
1159 shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1160 shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1161 shi->winco[2]= 0.0f;
1163 shi->dxwin[0]= 2.0f/(float)R.winx;
1164 shi->dywin[1]= 2.0f/(float)R.winy;
1165 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1166 shi->dywin[0]= shi->dywin[2]= 0.0f;
1170 if(texco & TEXCO_STICKY) {
1171 float *s1, *s2, *s3;
1173 s1= RE_vertren_get_sticky(obr, v1, 0);
1174 s2= RE_vertren_get_sticky(obr, v2, 0);
1175 s3= RE_vertren_get_sticky(obr, v3, 0);
1177 if(s1 && s2 && s3) {
1178 float winmat[4][4], ho1[4], ho2[4], ho3[4];
1180 float hox, hoy, l, dl, u, v;
1181 float s00, s01, s10, s11, detsh;
1183 /* old globals, localized now */
1184 Zmulx= ((float)R.winx)/2.0f; Zmuly= ((float)R.winy)/2.0f;
1186 if(shi->obi->flag & R_TRANSFORMED)
1187 zbuf_make_winmat(&R, shi->obi->mat, winmat);
1189 zbuf_make_winmat(&R, NULL, winmat);
1191 zbuf_render_project(winmat, v1->co, ho1);
1192 zbuf_render_project(winmat, v2->co, ho2);
1193 zbuf_render_project(winmat, v3->co, ho3);
1195 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1196 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1197 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1198 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1200 detsh= s00*s11-s10*s01;
1201 s00/= detsh; s01/=detsh;
1202 s10/=detsh; s11/=detsh;
1204 /* recalc u and v again */
1207 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1208 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1211 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1212 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1213 shi->sticky[2]= 0.0f;
1216 float dxuv[2], dyuv[2];
1218 dxuv[1]= - s01/Zmulx;
1219 dyuv[0]= - s10/Zmuly;
1222 dl= dxuv[0] + dxuv[1];
1223 shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1224 shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1225 dl= dyuv[0] + dyuv[1];
1226 shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1227 shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1232 Note! For raytracing winco is not set, important because thus means all shader input's need to have their variables set to zero else in-initialized values are used
1236 /* ****************** ShadeSample ************************************** */
1238 /* initialize per part, not per pixel! */
1239 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1242 memset(shi, 0, sizeof(ShadeInput));
1244 shi->sample= sample;
1245 shi->thread= pa->thread;
1246 shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
1248 shi->layflag= rl->layflag;
1249 shi->passflag= rl->passflag;
1250 shi->combinedflag= ~rl->pass_xor;
1251 shi->mat_override= rl->mat_override;
1252 shi->light_override= rl->light_override;
1254 /* note shi.depth==0 means first hit, not raytracing */
1258 /* initialize per part, not per pixel! */
1259 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1263 tot= R.osa==0?1:R.osa;
1265 for(a=0; a<tot; a++) {
1266 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1267 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1270 get_sample_layers(pa, rl, ssamp->rlpp);
1273 /* Do AO or (future) GI */
1274 void shade_samples_do_AO(ShadeSample *ssamp)
1279 if(!(R.r.mode & R_SHADOW))
1281 if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1284 if(R.wrld.mode & WO_AMB_OCC) {
1285 shi= &ssamp->shi[0];
1287 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & SCE_PASS_AO))
1288 || (shi->passflag & SCE_PASS_AO))
1289 for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
1290 if(!(shi->mode & MA_SHLESS))
1291 ambient_occlusion(shi); /* stores in shi->ao[] */
1296 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1303 for(shi= ssamp->shi; ps; ps= ps->next) {
1304 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1306 if(shi->vlr) { /* NULL happens for env material or for 'all z' */
1307 unsigned short curmask= ps->mask;
1309 /* full osa is only set for OSA renders */
1310 if(shi->vlr->flag & R_FULL_OSA) {
1311 short shi_cp= 0, samp;
1313 for(samp=0; samp<R.osa; samp++) {
1314 if(curmask & (1<<samp)) {
1315 xs= (float)x + R.jit[samp][0] + 0.5f; /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1316 ys= (float)y + R.jit[samp][1] + 0.5f;
1319 shade_input_copy_triangle(shi, shi-1);
1321 shi->mask= (1<<samp);
1322 // shi->rl= ssamp->rlpp[samp];
1323 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1324 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
1325 shade_input_set_uv(shi);
1326 shade_input_set_normals(shi);
1335 short b= R.samples->centmask[curmask];
1336 xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1337 ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1340 xs= (float)x + 0.5f;
1341 ys= (float)y + 0.5f;
1344 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1345 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
1346 shade_input_set_uv(shi);
1347 shade_input_set_normals(shi);
1351 /* total sample amount, shi->sample is static set in initialize */
1353 ssamp->tot= (shi-1)->sample + 1;
1358 /* shades samples, returns true if anything happened */
1359 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1361 shade_samples_fill_with_ps(ssamp, ps, x, y);
1364 ShadeInput *shi= ssamp->shi;
1365 ShadeResult *shr= ssamp->shr;
1368 /* if shadow or AO? */
1369 shade_samples_do_AO(ssamp);
1371 /* if shade (all shadepinputs have same passflag) */
1372 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1374 for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1375 shade_input_set_shade_texco(shi);
1376 shade_input_do_shade(shi, shr);