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))
139 /* do a shade, finish up some passes, apply mist */
140 void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
144 /* ------ main shading loop -------- */
146 if(shi->mat->nodetree && shi->mat->use_nodes) {
147 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
150 /* copy all relevant material vars, note, keep this synced with render_types.h */
151 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
152 shi->har= shi->mat->har;
154 shade_material_loop(shi, shr);
157 /* copy additional passes */
158 if(shi->passflag & (SCE_PASS_VECTOR|SCE_PASS_NORMAL|SCE_PASS_RADIO)) {
159 QUATCOPY(shr->winspeed, shi->winspeed);
160 VECCOPY(shr->nor, shi->vn);
161 VECCOPY(shr->rad, shi->rad);
165 if((shi->passflag & SCE_PASS_MIST) || ((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0)) {
166 if(R.r.mode & R_ORTHO)
167 shr->mist= mistfactor(-shi->co[2], shi->co);
169 shr->mist= mistfactor(VecLength(shi->co), shi->co);
171 else shr->mist= 0.0f;
173 if((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0 ) {
178 /* add mist and premul color */
179 if(shr->alpha!=1.0f || alpha!=1.0f) {
180 float fac= alpha*(shr->alpha);
182 shr->combined[3]= fac;
183 shr->combined[0]*= fac;
184 shr->combined[1]*= fac;
185 shr->combined[2]*= fac;
187 else shr->combined[3]= 1.0f;
193 /* **************************************************************************** */
195 /* **************************************************************************** */
198 void vlr_set_uv_indices(VlakRen *vlr, int *i1, int *i2, int *i3)
200 /* to prevent storing new tfaces or vcols, we check a split runtime */
204 /* 1---2 1---2 0 = orig face, 1 = new face */
206 /* Update vert nums to point to correct verts of original face */
207 if(vlr->flag & R_DIVIDE_24) {
208 if(vlr->flag & R_FACE_SPLIT) {
209 (*i1)++; (*i2)++; (*i3)++;
215 else if(vlr->flag & R_FACE_SPLIT) {
220 /* copy data from face to ShadeInput, general case */
221 /* indices 0 1 2 3 only */
222 void shade_input_set_triangle_i(ShadeInput *shi, ObjectInstanceRen *obi, VlakRen *vlr, short i1, short i2, short i3)
224 VertRen **vpp= &vlr->v1;
238 /* note, shi->mat is set in node shaders */
239 shi->mat= shi->mat_override?shi->mat_override:vlr->mat;
241 shi->osatex= (shi->mat->texco & TEXCO_OSA);
242 shi->mode= shi->mat->mode_l; /* or-ed result for all nodes */
244 /* facenormal copy, can get flipped */
245 shi->flippednor= RE_vlakren_get_normal(&R, obi, vlr, shi->facenor);
247 /* copy of original pre-flipped normal, for geometry->front/back node output */
248 VECCOPY(shi->orignor, shi->facenor);
250 VECMUL(shi->orignor, -1.0f);
252 /* calculate vertexnormals */
253 if(vlr->flag & R_SMOOTH) {
254 VECCOPY(shi->n1, shi->v1->n);
255 VECCOPY(shi->n2, shi->v2->n);
256 VECCOPY(shi->n3, shi->v3->n);
258 if(obi->flag & R_TRANSFORMED) {
259 Mat3MulVecfl(obi->nmat, shi->n1);
260 Mat3MulVecfl(obi->nmat, shi->n2);
261 Mat3MulVecfl(obi->nmat, shi->n3);
264 if(!(vlr->flag & (R_NOPUNOFLIP|R_TANGENT))) {
265 if(INPR(shi->facenor, shi->n1) < 0.0f) {
266 shi->n1[0]= -shi->n1[0];
267 shi->n1[1]= -shi->n1[1];
268 shi->n1[2]= -shi->n1[2];
270 if(INPR(shi->facenor, shi->n2) < 0.0f) {
271 shi->n2[0]= -shi->n2[0];
272 shi->n2[1]= -shi->n2[1];
273 shi->n2[2]= -shi->n2[2];
275 if(INPR(shi->facenor, shi->n3) < 0.0f) {
276 shi->n3[0]= -shi->n3[0];
277 shi->n3[1]= -shi->n3[1];
278 shi->n3[2]= -shi->n3[2];
284 /* note, facenr declared volatile due to over-eager -O2 optimizations
285 * on cygwin (particularly -frerun-cse-after-loop)
288 /* copy data from face to ShadeInput, scanline case */
289 void shade_input_set_triangle(ShadeInput *shi, volatile int obi, volatile int facenr, int normal_flip)
292 shi->obi= &R.objectinstance[obi];
293 shi->obr= shi->obi->obr;
294 shi->facenr= (facenr-1) & RE_QUAD_MASK;
295 if( shi->facenr < shi->obr->totvlak ) {
296 VlakRen *vlr= RE_findOrAddVlak(shi->obr, shi->facenr);
298 if(facenr & RE_QUAD_OFFS)
299 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 2, 3);
301 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 1, 2);
304 shi->vlr= NULL; /* general signal we got sky */
307 shi->vlr= NULL; /* general signal we got sky */
310 /* full osa case: copy static info */
311 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
313 /* not so nice, but works... warning is in RE_shader_ext.h */
314 memcpy(shi, from, sizeof(struct ShadeInputCopy));
317 /* copy data from strand to shadeinput */
318 void shade_input_set_strand(ShadeInput *shi, StrandRen *strand, StrandPoint *spoint)
320 /* note, shi->mat is set in node shaders */
321 shi->mat= shi->mat_override? shi->mat_override: strand->buffer->ma;
323 shi->osatex= (shi->mat->texco & TEXCO_OSA);
324 shi->mode= shi->mat->mode_l; /* or-ed result for all nodes */
326 /* shade_input_set_viewco equivalent */
327 VECCOPY(shi->co, spoint->co);
328 VECCOPY(shi->view, shi->co);
329 Normalize(shi->view);
331 shi->xs= (int)spoint->x;
332 shi->ys= (int)spoint->y;
334 if(shi->osatex || (R.r.mode & R_SHADOW)) {
335 VECCOPY(shi->dxco, spoint->dtco);
336 VECCOPY(shi->dyco, spoint->dsco);
339 /* dxview, dyview, not supported */
341 /* facenormal, simply viewco flipped */
342 VECCOPY(shi->facenor, spoint->nor);
343 VECCOPY(shi->orignor, shi->facenor);
345 /* shade_input_set_normals equivalent */
346 if(shi->mat->mode & MA_TANGENT_STR) {
347 VECCOPY(shi->vn, spoint->tan)
352 Crossf(cross, spoint->co, spoint->tan);
353 Crossf(shi->vn, cross, spoint->tan);
356 if(INPR(shi->vn, shi->view) < 0.0f)
357 VecMulf(shi->vn, -1.0f);
360 VECCOPY(shi->vno, shi->vn);
363 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
365 StrandBuffer *strandbuf= strand->buffer;
366 ObjectRen *obr= strandbuf->obr;
368 int mode= shi->mode; /* or-ed result for all nodes */
369 short texco= shi->mat->texco;
371 if((shi->mat->texco & TEXCO_REFL)) {
372 /* shi->dxview, shi->dyview, not supported */
375 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL))) {
379 if(mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
380 VECCOPY(shi->tang, spoint->tan);
381 VECCOPY(shi->nmaptang, spoint->tan);
384 if(mode & MA_STR_SURFDIFF) {
385 float *surfnor= RE_strandren_get_surfnor(obr, strand, 0);
388 VECCOPY(shi->surfnor, surfnor)
390 VECCOPY(shi->surfnor, shi->vn)
392 if(shi->mat->strand_surfnor > 0.0f) {
394 for(sv=strand->vert; sv!=svert; sv++)
395 shi->surfdist+=VecLenf(sv->co, (sv+1)->co);
396 shi->surfdist += spoint->t*VecLenf(sv->co, (sv+1)->co);
400 if(R.r.mode & R_SPEED) {
403 speed= RE_strandren_get_winspeed(shi->obi, strand, 0);
405 QUATCOPY(shi->winspeed, speed)
407 shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
410 /* shade_input_set_shade_texco equivalent */
411 if(texco & NEED_UV) {
412 if(texco & TEXCO_ORCO) {
413 VECCOPY(shi->lo, strand->orco);
414 /* no shi->osatex, orco derivatives are zero */
417 if(texco & TEXCO_GLOB) {
418 VECCOPY(shi->gl, shi->co);
419 MTC_Mat4MulVecfl(R.viewinv, shi->gl);
422 VECCOPY(shi->dxgl, shi->dxco);
423 MTC_Mat3MulVecfl(R.imat, shi->dxco);
424 VECCOPY(shi->dygl, shi->dyco);
425 MTC_Mat3MulVecfl(R.imat, shi->dyco);
429 if(texco & TEXCO_STRAND) {
430 shi->strandco= spoint->strandco;
433 shi->dxstrand= spoint->dtstrandco;
438 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
446 shi->actuv= obr->actmtface;
447 shi->actcol= obr->actmcol;
449 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
450 for (i=0; (mcol=RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
451 ShadeInputCol *scol= &shi->col[i];
452 char *cp= (char*)mcol;
457 scol->col[0]= cp[3]/255.0f;
458 scol->col[1]= cp[2]/255.0f;
459 scol->col[2]= cp[1]/255.0f;
463 shi->vcol[0]= shi->col[shi->actcol].col[0];
464 shi->vcol[1]= shi->col[shi->actcol].col[1];
465 shi->vcol[2]= shi->col[shi->actcol].col[2];
474 for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
475 ShadeInputUV *suv= &shi->uv[i];
480 if(strandbuf->overrideuv == i) {
482 suv->uv[1]= spoint->strandco;
486 suv->uv[0]= -1.0f + 2.0f*uv[0];
487 suv->uv[1]= -1.0f + 2.0f*uv[1];
488 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
498 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
499 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
507 if(shi->totuv == 0) {
508 ShadeInputUV *suv= &shi->uv[0];
511 suv->uv[1]= spoint->strandco;
512 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
514 if(mode & MA_FACETEXTURE) {
515 /* no tface? set at 1.0f */
524 if(texco & TEXCO_NORM) {
525 shi->orn[0]= -shi->vn[0];
526 shi->orn[1]= -shi->vn[1];
527 shi->orn[2]= -shi->vn[2];
530 if(mode & MA_RADIO) {
534 if(texco & TEXCO_REFL) {
535 /* mirror reflection color textures (and envmap) */
536 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
539 if(texco & TEXCO_STRESS) {
543 if(texco & TEXCO_TANGENT) {
544 if((mode & MA_TANGENT_V)==0) {
545 /* just prevent surprises */
546 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
547 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
552 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
554 /* this only avalailable for scanline renders */
556 if(texco & TEXCO_WINDOW) {
557 shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
558 shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
570 if(texco & TEXCO_STICKY) {
576 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
577 void shade_input_calc_viewco(ShadeInput *shi, float x, float y, float z, float *view, float *dxyview, float *co, float *dxco, float *dyco)
579 /* returns not normalized, so is in viewplane coords */
580 calc_view_vector(view, x, y);
582 if(shi->mat->mode & MA_WIRE) {
583 /* wire cannot use normal for calculating shi->co, so
584 * we reconstruct the coordinate less accurate */
585 if(R.r.mode & R_ORTHO)
586 calc_renderco_ortho(co, x, y, z);
588 calc_renderco_zbuf(co, view, z);
591 /* for non-wire, intersect with the triangle to get the exact coord */
592 float fac, dface, v1[3];
594 VECCOPY(v1, shi->v1->co);
595 if(shi->obi->flag & R_TRANSFORMED)
596 Mat4MulVecfl(shi->obi->mat, v1);
598 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
600 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
601 if(R.r.mode & R_ORTHO) {
602 /* x and y 3d coordinate can be derived from pixel coord and winmat */
603 float fx= 2.0f/(R.winx*R.winmat[0][0]);
604 float fy= 2.0f/(R.winy*R.winmat[1][1]);
606 co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
607 co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
609 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
610 if(shi->facenor[2]!=0.0f)
611 co[2]= (dface - shi->facenor[0]*co[0] - shi->facenor[1]*co[1])/shi->facenor[2];
618 if(shi->facenor[2]!=0.0f)
619 dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
625 if(shi->facenor[2]!=0.0f)
626 dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
631 if(co[2]!=0.0f) fac= 1.0f/co[2]; else fac= 0.0f;
632 dxyview[0]= -R.viewdx*fac;
633 dxyview[1]= -R.viewdy*fac;
640 div= shi->facenor[0]*view[0] + shi->facenor[1]*view[1] + shi->facenor[2]*view[2];
641 if (div!=0.0f) fac= dface/div;
648 /* pixel dx/dy for render coord */
650 float u= dface/(div - R.viewdx*shi->facenor[0]);
651 float v= dface/(div - R.viewdy*shi->facenor[1]);
653 dxco[0]= co[0]- (view[0]-R.viewdx)*u;
654 dxco[1]= co[1]- (view[1])*u;
655 dxco[2]= co[2]- (view[2])*u;
657 dyco[0]= co[0]- (view[0])*v;
658 dyco[1]= co[1]- (view[1]-R.viewdy)*v;
659 dyco[2]= co[2]- (view[2])*v;
662 if(fac!=0.0f) fac= 1.0f/fac;
663 dxyview[0]= -R.viewdx*fac;
664 dxyview[1]= -R.viewdy*fac;
670 /* cannot normalize earlier, code above needs it at viewplane level */
674 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
675 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
677 float *dxyview= NULL, *dxco= NULL, *dyco= NULL;
679 /* currently in use for dithering (soft shadow), node preview, irregular shad */
683 /* original scanline coordinate without jitter */
688 /* check if we need derivatives */
689 if(shi->osatex || (R.r.mode & R_SHADOW)) {
693 if((shi->mat->texco & TEXCO_REFL))
694 dxyview= &shi->dxview;
697 shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
700 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
701 void shade_input_set_uv(ShadeInput *shi)
703 VlakRen *vlr= shi->vlr;
705 if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
706 float v1[3], v2[3], v3[3];
708 VECCOPY(v1, shi->v1->co);
709 VECCOPY(v2, shi->v2->co);
710 VECCOPY(v3, shi->v3->co);
712 if(shi->obi->flag & R_TRANSFORMED) {
713 Mat4MulVecfl(shi->obi->mat, v1);
714 Mat4MulVecfl(shi->obi->mat, v2);
715 Mat4MulVecfl(shi->obi->mat, v3);
718 /* exception case for wire render of edge */
719 if(vlr->v2==vlr->v3) {
722 lend= VecLenf(v2, v1);
723 lenc= VecLenf(shi->co, v1);
729 shi->u= - (1.0f - lenc/lend);
741 /* most of this could become re-used for faces */
742 float detsh, t00, t10, t01, t11, xn, yn, zn;
745 /* find most stable axis to project */
746 xn= fabs(shi->facenor[0]);
747 yn= fabs(shi->facenor[1]);
748 zn= fabs(shi->facenor[2]);
750 if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
751 else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
752 else { axis1= 1; axis2= 2; }
754 /* compute u,v and derivatives */
755 t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
756 t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
758 detsh= 1.0f/(t00*t11-t10*t01);
759 t00*= detsh; t01*=detsh;
760 t10*=detsh; t11*=detsh;
762 shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
763 shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
765 shi->dx_u= shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
766 shi->dx_v= shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
767 shi->dy_u= shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
768 shi->dy_v= shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
771 /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
772 CLAMP(shi->u, -2.0f, 1.0f);
773 CLAMP(shi->v, -2.0f, 1.0f);
778 void shade_input_set_normals(ShadeInput *shi)
780 float u= shi->u, v= shi->v;
783 /* calculate vertexnormals */
784 if(shi->vlr->flag & R_SMOOTH) {
785 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
787 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
788 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
789 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
794 VECCOPY(shi->vn, shi->facenor);
797 VECCOPY(shi->vno, shi->vn);
801 /* use by raytrace, sss, bake to flip into the right direction */
802 void shade_input_flip_normals(ShadeInput *shi)
804 shi->facenor[0]= -shi->facenor[0];
805 shi->facenor[1]= -shi->facenor[1];
806 shi->facenor[2]= -shi->facenor[2];
808 shi->vn[0]= -shi->vn[0];
809 shi->vn[1]= -shi->vn[1];
810 shi->vn[2]= -shi->vn[2];
812 shi->vno[0]= -shi->vno[0];
813 shi->vno[1]= -shi->vno[1];
814 shi->vno[2]= -shi->vno[2];
816 shi->flippednor= !shi->flippednor;
819 void shade_input_set_shade_texco(ShadeInput *shi)
821 ObjectInstanceRen *obi= shi->obi;
822 ObjectRen *obr= shi->obr;
823 VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
824 float u= shi->u, v= shi->v;
825 float l= 1.0f+u+v, dl;
826 int mode= shi->mode; /* or-ed result for all nodes */
827 short texco= shi->mat->texco;
830 if(shi->vlr->flag & R_SMOOTH) {
832 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
833 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
835 dl= shi->dx_u+shi->dx_v;
836 shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
837 shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
838 shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
839 dl= shi->dy_u+shi->dy_v;
840 shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
841 shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
842 shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
848 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
849 float *tangent, *s1, *s2, *s3;
852 if(shi->vlr->flag & R_SMOOTH) {
858 /* qdn: flat faces have tangents too,
859 could pick either one, using average here */
865 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
866 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
868 if(mode & MA_TANGENT_V) {
869 s1 = RE_vertren_get_tangent(obr, v1, 0);
870 s2 = RE_vertren_get_tangent(obr, v2, 0);
871 s3 = RE_vertren_get_tangent(obr, v3, 0);
874 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
875 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
876 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
878 if(obi->flag & R_TRANSFORMED)
879 Mat3MulVecfl(obi->nmat, shi->tang);
881 Normalize(shi->tang);
882 VECCOPY(shi->nmaptang, shi->tang);
886 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
887 tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
890 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
892 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
898 shi->nmaptang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
899 shi->nmaptang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
900 shi->nmaptang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
902 if(obi->flag & R_TRANSFORMED)
903 Mat3MulVecfl(obi->nmat, shi->nmaptang);
905 Normalize(shi->nmaptang);
910 if(mode & MA_STR_SURFDIFF) {
911 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
914 VECCOPY(shi->surfnor, surfnor)
915 if(obi->flag & R_TRANSFORMED)
916 Mat3MulVecfl(obi->nmat, shi->surfnor);
919 VECCOPY(shi->surfnor, shi->vn)
924 if(R.r.mode & R_SPEED) {
927 s1= RE_vertren_get_winspeed(obi, v1, 0);
928 s2= RE_vertren_get_winspeed(obi, v2, 0);
929 s3= RE_vertren_get_winspeed(obi, v3, 0);
931 shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
932 shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
933 shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
934 shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
937 shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
941 /* pass option forces UV calc */
942 if(shi->passflag & SCE_PASS_UV)
943 texco |= (NEED_UV|TEXCO_UV);
945 /* texture coordinates. shi->dxuv shi->dyuv have been set */
946 if(texco & NEED_UV) {
948 if(texco & TEXCO_ORCO) {
956 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
957 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
958 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
961 dl= shi->dx_u+shi->dx_v;
962 shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
963 shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
964 shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
965 dl= shi->dy_u+shi->dy_v;
966 shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
967 shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
968 shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
972 VECCOPY(shi->duplilo, obi->dupliorco);
975 if(texco & TEXCO_GLOB) {
976 VECCOPY(shi->gl, shi->co);
977 MTC_Mat4MulVecfl(R.viewinv, shi->gl);
979 VECCOPY(shi->dxgl, shi->dxco);
980 MTC_Mat3MulVecfl(R.imat, shi->dxco);
981 VECCOPY(shi->dygl, shi->dyco);
982 MTC_Mat3MulVecfl(R.imat, shi->dyco);
986 if(texco & TEXCO_STRAND) {
987 shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
989 dl= shi->dx_u+shi->dx_v;
990 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
991 dl= shi->dy_u+shi->dy_v;
992 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
996 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
997 VlakRen *vlr= shi->vlr;
1001 int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
1003 /* uv and vcols are not copied on split, so set them according vlr divide flag */
1004 vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1008 shi->actuv= obr->actmtface;
1009 shi->actcol= obr->actmcol;
1011 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1012 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1013 ShadeInputCol *scol= &shi->col[i];
1014 char *cp1, *cp2, *cp3;
1019 cp1= (char *)(mcol+j1);
1020 cp2= (char *)(mcol+j2);
1021 cp3= (char *)(mcol+j3);
1023 scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
1024 scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
1025 scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
1029 shi->vcol[0]= shi->col[shi->actcol].col[0];
1030 shi->vcol[1]= shi->col[shi->actcol].col[1];
1031 shi->vcol[2]= shi->col[shi->actcol].col[2];
1042 for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1043 ShadeInputUV *suv= &shi->uv[i];
1044 float *uv1, *uv2, *uv3;
1053 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1054 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1055 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1060 dl= shi->dx_u+shi->dx_v;
1064 suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1065 suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1067 dl= shi->dy_u+shi->dy_v;
1071 suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1072 suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1075 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1076 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1082 if(tface && tface->tpage)
1083 render_realtime_texture(shi, tface->tpage);
1087 shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1088 shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1089 shi->dupliuv[2]= 0.0f;
1091 if(shi->totuv == 0) {
1092 ShadeInputUV *suv= &shi->uv[0];
1094 suv->uv[0]= 2.0f*(u+.5f);
1095 suv->uv[1]= 2.0f*(v+.5f);
1096 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1098 if(mode & MA_FACETEXTURE) {
1099 /* no tface? set at 1.0f */
1108 if(texco & TEXCO_NORM) {
1109 shi->orn[0]= -shi->vn[0];
1110 shi->orn[1]= -shi->vn[1];
1111 shi->orn[2]= -shi->vn[2];
1114 if(mode & MA_RADIO) {
1115 float *r1, *r2, *r3;
1117 r1= RE_vertren_get_rad(obr, v1, 0);
1118 r2= RE_vertren_get_rad(obr, v2, 0);
1119 r3= RE_vertren_get_rad(obr, v3, 0);
1121 if(r1 && r2 && r3) {
1122 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
1123 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
1124 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
1127 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1130 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1132 if(texco & TEXCO_REFL) {
1133 /* mirror reflection color textures (and envmap) */
1134 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
1137 if(texco & TEXCO_STRESS) {
1138 float *s1, *s2, *s3;
1140 s1= RE_vertren_get_stress(obr, v1, 0);
1141 s2= RE_vertren_get_stress(obr, v2, 0);
1142 s3= RE_vertren_get_stress(obr, v3, 0);
1143 if(s1 && s2 && s3) {
1144 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1145 if(shi->stress<1.0f) shi->stress-= 1.0f;
1146 else shi->stress= (shi->stress-1.0f)/shi->stress;
1148 else shi->stress= 0.0f;
1151 if(texco & TEXCO_TANGENT) {
1152 if((mode & MA_TANGENT_V)==0) {
1153 /* just prevent surprises */
1154 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1155 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1160 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1162 /* this only avalailable for scanline renders */
1167 if(texco & TEXCO_WINDOW) {
1168 shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1169 shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1170 shi->winco[2]= 0.0f;
1172 shi->dxwin[0]= 2.0f/(float)R.winx;
1173 shi->dywin[1]= 2.0f/(float)R.winy;
1174 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1175 shi->dywin[0]= shi->dywin[2]= 0.0f;
1179 if(texco & TEXCO_STICKY) {
1180 float *s1, *s2, *s3;
1182 s1= RE_vertren_get_sticky(obr, v1, 0);
1183 s2= RE_vertren_get_sticky(obr, v2, 0);
1184 s3= RE_vertren_get_sticky(obr, v3, 0);
1186 if(s1 && s2 && s3) {
1187 float winmat[4][4], ho1[4], ho2[4], ho3[4];
1189 float hox, hoy, l, dl, u, v;
1190 float s00, s01, s10, s11, detsh;
1192 /* old globals, localized now */
1193 Zmulx= ((float)R.winx)/2.0f; Zmuly= ((float)R.winy)/2.0f;
1195 if(shi->obi->flag & R_TRANSFORMED)
1196 zbuf_make_winmat(&R, shi->obi->mat, winmat);
1198 zbuf_make_winmat(&R, NULL, winmat);
1200 zbuf_render_project(winmat, v1->co, ho1);
1201 zbuf_render_project(winmat, v2->co, ho2);
1202 zbuf_render_project(winmat, v3->co, ho3);
1204 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1205 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1206 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1207 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1209 detsh= s00*s11-s10*s01;
1210 s00/= detsh; s01/=detsh;
1211 s10/=detsh; s11/=detsh;
1213 /* recalc u and v again */
1216 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1217 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1220 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1221 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1222 shi->sticky[2]= 0.0f;
1225 float dxuv[2], dyuv[2];
1227 dxuv[1]= - s01/Zmulx;
1228 dyuv[0]= - s10/Zmuly;
1231 dl= dxuv[0] + dxuv[1];
1232 shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1233 shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1234 dl= dyuv[0] + dyuv[1];
1235 shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1236 shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1241 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
1245 /* ****************** ShadeSample ************************************** */
1247 /* initialize per part, not per pixel! */
1248 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1251 memset(shi, 0, sizeof(ShadeInput));
1253 shi->sample= sample;
1254 shi->thread= pa->thread;
1255 shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
1257 shi->layflag= rl->layflag;
1258 shi->passflag= rl->passflag;
1259 shi->combinedflag= ~rl->pass_xor;
1260 shi->mat_override= rl->mat_override;
1261 shi->light_override= rl->light_override;
1263 /* note shi.depth==0 means first hit, not raytracing */
1267 /* initialize per part, not per pixel! */
1268 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1272 tot= R.osa==0?1:R.osa;
1274 for(a=0; a<tot; a++) {
1275 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1276 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1279 get_sample_layers(pa, rl, ssamp->rlpp);
1282 /* Do AO or (future) GI */
1283 void shade_samples_do_AO(ShadeSample *ssamp)
1288 if(!(R.r.mode & R_SHADOW))
1290 if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1293 if(R.wrld.mode & WO_AMB_OCC) {
1294 shi= &ssamp->shi[0];
1296 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & SCE_PASS_AO))
1297 || (shi->passflag & SCE_PASS_AO))
1298 for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
1299 if(!(shi->mode & MA_SHLESS))
1300 ambient_occlusion(shi); /* stores in shi->ao[] */
1305 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1312 for(shi= ssamp->shi; ps; ps= ps->next) {
1313 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1315 if(shi->vlr) { /* NULL happens for env material or for 'all z' */
1316 unsigned short curmask= ps->mask;
1318 /* full osa is only set for OSA renders */
1319 if(shi->vlr->flag & R_FULL_OSA) {
1320 short shi_cp= 0, samp;
1322 for(samp=0; samp<R.osa; samp++) {
1323 if(curmask & (1<<samp)) {
1324 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1325 xs= (float)x + R.jit[samp][0] + 0.5f;
1326 ys= (float)y + R.jit[samp][1] + 0.5f;
1329 shade_input_copy_triangle(shi, shi-1);
1331 shi->mask= (1<<samp);
1332 // shi->rl= ssamp->rlpp[samp];
1333 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1334 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1335 shade_input_set_uv(shi);
1336 shade_input_set_normals(shi);
1345 short b= R.samples->centmask[curmask];
1346 xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1347 ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1350 xs= (float)x + 0.5f;
1351 ys= (float)y + 0.5f;
1355 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1356 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1357 shade_input_set_uv(shi);
1358 shade_input_set_normals(shi);
1362 /* total sample amount, shi->sample is static set in initialize */
1364 ssamp->tot= (shi-1)->sample + 1;
1369 /* shades samples, returns true if anything happened */
1370 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1372 shade_samples_fill_with_ps(ssamp, ps, x, y);
1375 ShadeInput *shi= ssamp->shi;
1376 ShadeResult *shr= ssamp->shr;
1379 /* if shadow or AO? */
1380 shade_samples_do_AO(ssamp);
1382 /* if shade (all shadepinputs have same passflag) */
1383 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1385 for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1386 shade_input_set_shade_texco(shi);
1387 shade_input_do_shade(shi, shr);