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 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
584 void shade_input_calc_viewco(ShadeInput *shi, float x, float y, float z, float *view, float *dxyview, float *co, float *dxco, float *dyco)
586 /* returns not normalized, so is in viewplane coords */
587 calc_view_vector(view, x, y);
589 if(shi->mat->mode & MA_WIRE) {
590 /* wire cannot use normal for calculating shi->co, so
591 * we reconstruct the coordinate less accurate */
592 if(R.r.mode & R_ORTHO)
593 calc_renderco_ortho(co, x, y, z);
595 calc_renderco_zbuf(co, view, z);
598 /* for non-wire, intersect with the triangle to get the exact coord */
599 float fac, dface, v1[3];
601 VECCOPY(v1, shi->v1->co);
602 if(shi->obi->flag & R_TRANSFORMED)
603 Mat4MulVecfl(shi->obi->mat, v1);
605 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
607 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
608 if(R.r.mode & R_ORTHO) {
609 /* x and y 3d coordinate can be derived from pixel coord and winmat */
610 float fx= 2.0f/(R.winx*R.winmat[0][0]);
611 float fy= 2.0f/(R.winy*R.winmat[1][1]);
613 co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
614 co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
616 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
617 if(shi->facenor[2]!=0.0f)
618 co[2]= (dface - shi->facenor[0]*co[0] - shi->facenor[1]*co[1])/shi->facenor[2];
625 if(shi->facenor[2]!=0.0f)
626 dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
632 if(shi->facenor[2]!=0.0f)
633 dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
638 if(co[2]!=0.0f) fac= 1.0f/co[2]; else fac= 0.0f;
639 dxyview[0]= -R.viewdx*fac;
640 dxyview[1]= -R.viewdy*fac;
647 div= shi->facenor[0]*view[0] + shi->facenor[1]*view[1] + shi->facenor[2]*view[2];
648 if (div!=0.0f) fac= dface/div;
655 /* pixel dx/dy for render coord */
657 float u= dface/(div - R.viewdx*shi->facenor[0]);
658 float v= dface/(div - R.viewdy*shi->facenor[1]);
660 dxco[0]= co[0]- (view[0]-R.viewdx)*u;
661 dxco[1]= co[1]- (view[1])*u;
662 dxco[2]= co[2]- (view[2])*u;
664 dyco[0]= co[0]- (view[0])*v;
665 dyco[1]= co[1]- (view[1]-R.viewdy)*v;
666 dyco[2]= co[2]- (view[2])*v;
669 if(fac!=0.0f) fac= 1.0f/fac;
670 dxyview[0]= -R.viewdx*fac;
671 dxyview[1]= -R.viewdy*fac;
677 /* set camera coords - for scanline, it's always 0.0,0.0,0.0 (render is in camera space)
678 * however for raytrace it can be different - the position of the last intersection */
679 shi->camera_co[0] = shi->camera_co[1] = shi->camera_co[2] = 0.0f;
681 /* cannot normalize earlier, code above needs it at viewplane level */
685 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
686 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
688 float *dxyview= NULL, *dxco= NULL, *dyco= NULL;
690 /* currently in use for dithering (soft shadow), node preview, irregular shad */
694 /* original scanline coordinate without jitter */
699 /* check if we need derivatives */
700 if(shi->osatex || (R.r.mode & R_SHADOW)) {
704 if((shi->mat->texco & TEXCO_REFL))
705 dxyview= &shi->dxview;
708 shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
711 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
712 void shade_input_set_uv(ShadeInput *shi)
714 VlakRen *vlr= shi->vlr;
716 if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
717 float v1[3], v2[3], v3[3];
719 VECCOPY(v1, shi->v1->co);
720 VECCOPY(v2, shi->v2->co);
721 VECCOPY(v3, shi->v3->co);
723 if(shi->obi->flag & R_TRANSFORMED) {
724 Mat4MulVecfl(shi->obi->mat, v1);
725 Mat4MulVecfl(shi->obi->mat, v2);
726 Mat4MulVecfl(shi->obi->mat, v3);
729 /* exception case for wire render of edge */
730 if(vlr->v2==vlr->v3) {
733 lend= VecLenf(v2, v1);
734 lenc= VecLenf(shi->co, v1);
740 shi->u= - (1.0f - lenc/lend);
752 /* most of this could become re-used for faces */
753 float detsh, t00, t10, t01, t11, xn, yn, zn;
756 /* find most stable axis to project */
757 xn= fabs(shi->facenor[0]);
758 yn= fabs(shi->facenor[1]);
759 zn= fabs(shi->facenor[2]);
761 if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
762 else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
763 else { axis1= 1; axis2= 2; }
765 /* compute u,v and derivatives */
766 t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
767 t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
769 detsh= 1.0f/(t00*t11-t10*t01);
770 t00*= detsh; t01*=detsh;
771 t10*=detsh; t11*=detsh;
773 shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
774 shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
776 shi->dx_u= shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
777 shi->dx_v= shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
778 shi->dy_u= shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
779 shi->dy_v= shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
782 /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
783 CLAMP(shi->u, -2.0f, 1.0f);
784 CLAMP(shi->v, -2.0f, 1.0f);
789 void shade_input_set_normals(ShadeInput *shi)
791 float u= shi->u, v= shi->v;
794 /* calculate vertexnormals */
795 if(shi->vlr->flag & R_SMOOTH) {
796 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
798 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
799 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
800 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
805 VECCOPY(shi->vn, shi->facenor);
808 VECCOPY(shi->vno, shi->vn);
812 /* use by raytrace, sss, bake to flip into the right direction */
813 void shade_input_flip_normals(ShadeInput *shi)
815 shi->facenor[0]= -shi->facenor[0];
816 shi->facenor[1]= -shi->facenor[1];
817 shi->facenor[2]= -shi->facenor[2];
819 shi->vn[0]= -shi->vn[0];
820 shi->vn[1]= -shi->vn[1];
821 shi->vn[2]= -shi->vn[2];
823 shi->vno[0]= -shi->vno[0];
824 shi->vno[1]= -shi->vno[1];
825 shi->vno[2]= -shi->vno[2];
827 shi->flippednor= !shi->flippednor;
830 void shade_input_set_shade_texco(ShadeInput *shi)
832 ObjectInstanceRen *obi= shi->obi;
833 ObjectRen *obr= shi->obr;
834 VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
835 float u= shi->u, v= shi->v;
836 float l= 1.0f+u+v, dl;
837 int mode= shi->mode; /* or-ed result for all nodes */
838 short texco= shi->mat->texco;
841 if(shi->vlr->flag & R_SMOOTH) {
843 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
844 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
846 dl= shi->dx_u+shi->dx_v;
847 shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
848 shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
849 shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
850 dl= shi->dy_u+shi->dy_v;
851 shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
852 shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
853 shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
859 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
860 float *tangent, *s1, *s2, *s3;
863 if(shi->vlr->flag & R_SMOOTH) {
869 /* qdn: flat faces have tangents too,
870 could pick either one, using average here */
876 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
877 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
879 if(mode & MA_TANGENT_V) {
880 s1 = RE_vertren_get_tangent(obr, v1, 0);
881 s2 = RE_vertren_get_tangent(obr, v2, 0);
882 s3 = RE_vertren_get_tangent(obr, v3, 0);
885 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
886 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
887 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
889 if(obi->flag & R_TRANSFORMED)
890 Mat3MulVecfl(obi->nmat, shi->tang);
892 Normalize(shi->tang);
893 VECCOPY(shi->nmaptang, shi->tang);
897 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
898 tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
901 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
903 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
909 shi->nmaptang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
910 shi->nmaptang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
911 shi->nmaptang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
913 if(obi->flag & R_TRANSFORMED)
914 Mat3MulVecfl(obi->nmat, shi->nmaptang);
916 Normalize(shi->nmaptang);
921 if(mode & MA_STR_SURFDIFF) {
922 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
925 VECCOPY(shi->surfnor, surfnor)
926 if(obi->flag & R_TRANSFORMED)
927 Mat3MulVecfl(obi->nmat, shi->surfnor);
930 VECCOPY(shi->surfnor, shi->vn)
935 if(R.r.mode & R_SPEED) {
938 s1= RE_vertren_get_winspeed(obi, v1, 0);
939 s2= RE_vertren_get_winspeed(obi, v2, 0);
940 s3= RE_vertren_get_winspeed(obi, v3, 0);
942 shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
943 shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
944 shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
945 shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
948 shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
952 /* pass option forces UV calc */
953 if(shi->passflag & SCE_PASS_UV)
954 texco |= (NEED_UV|TEXCO_UV);
956 /* texture coordinates. shi->dxuv shi->dyuv have been set */
957 if(texco & NEED_UV) {
959 if(texco & TEXCO_ORCO) {
967 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
968 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
969 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
972 dl= shi->dx_u+shi->dx_v;
973 shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
974 shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
975 shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
976 dl= shi->dy_u+shi->dy_v;
977 shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
978 shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
979 shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
983 VECCOPY(shi->duplilo, obi->dupliorco);
986 if(texco & TEXCO_GLOB) {
987 VECCOPY(shi->gl, shi->co);
988 MTC_Mat4MulVecfl(R.viewinv, shi->gl);
990 VECCOPY(shi->dxgl, shi->dxco);
991 MTC_Mat3MulVecfl(R.imat, shi->dxco);
992 VECCOPY(shi->dygl, shi->dyco);
993 MTC_Mat3MulVecfl(R.imat, shi->dyco);
997 if(texco & TEXCO_STRAND) {
998 shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
1000 dl= shi->dx_u+shi->dx_v;
1001 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
1002 dl= shi->dy_u+shi->dy_v;
1003 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
1007 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
1008 VlakRen *vlr= shi->vlr;
1012 int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
1014 /* uv and vcols are not copied on split, so set them according vlr divide flag */
1015 vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1019 shi->actuv= obr->actmtface;
1020 shi->actcol= obr->actmcol;
1022 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1023 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1024 ShadeInputCol *scol= &shi->col[i];
1025 char *cp1, *cp2, *cp3;
1030 cp1= (char *)(mcol+j1);
1031 cp2= (char *)(mcol+j2);
1032 cp3= (char *)(mcol+j3);
1034 scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
1035 scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
1036 scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
1040 shi->vcol[0]= shi->col[shi->actcol].col[0];
1041 shi->vcol[1]= shi->col[shi->actcol].col[1];
1042 shi->vcol[2]= shi->col[shi->actcol].col[2];
1053 for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1054 ShadeInputUV *suv= &shi->uv[i];
1055 float *uv1, *uv2, *uv3;
1064 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1065 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1066 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1071 dl= shi->dx_u+shi->dx_v;
1075 suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1076 suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1078 dl= shi->dy_u+shi->dy_v;
1082 suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1083 suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1086 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1087 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1093 if(tface && tface->tpage)
1094 render_realtime_texture(shi, tface->tpage);
1098 shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1099 shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1100 shi->dupliuv[2]= 0.0f;
1102 if(shi->totuv == 0) {
1103 ShadeInputUV *suv= &shi->uv[0];
1105 suv->uv[0]= 2.0f*(u+.5f);
1106 suv->uv[1]= 2.0f*(v+.5f);
1107 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1109 if(mode & MA_FACETEXTURE) {
1110 /* no tface? set at 1.0f */
1119 if(texco & TEXCO_NORM) {
1120 shi->orn[0]= -shi->vn[0];
1121 shi->orn[1]= -shi->vn[1];
1122 shi->orn[2]= -shi->vn[2];
1125 if(mode & MA_RADIO) {
1126 float *r1, *r2, *r3;
1128 r1= RE_vertren_get_rad(obr, v1, 0);
1129 r2= RE_vertren_get_rad(obr, v2, 0);
1130 r3= RE_vertren_get_rad(obr, v3, 0);
1132 if(r1 && r2 && r3) {
1133 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
1134 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
1135 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
1138 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1141 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1143 if(texco & TEXCO_REFL) {
1144 /* mirror reflection color textures (and envmap) */
1145 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
1148 if(texco & TEXCO_STRESS) {
1149 float *s1, *s2, *s3;
1151 s1= RE_vertren_get_stress(obr, v1, 0);
1152 s2= RE_vertren_get_stress(obr, v2, 0);
1153 s3= RE_vertren_get_stress(obr, v3, 0);
1154 if(s1 && s2 && s3) {
1155 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1156 if(shi->stress<1.0f) shi->stress-= 1.0f;
1157 else shi->stress= (shi->stress-1.0f)/shi->stress;
1159 else shi->stress= 0.0f;
1162 if(texco & TEXCO_TANGENT) {
1163 if((mode & MA_TANGENT_V)==0) {
1164 /* just prevent surprises */
1165 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1166 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1171 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1173 /* this only avalailable for scanline renders */
1178 if(texco & TEXCO_WINDOW) {
1179 shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1180 shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1181 shi->winco[2]= 0.0f;
1183 shi->dxwin[0]= 2.0f/(float)R.winx;
1184 shi->dywin[1]= 2.0f/(float)R.winy;
1185 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1186 shi->dywin[0]= shi->dywin[2]= 0.0f;
1190 if(texco & TEXCO_STICKY) {
1191 float *s1, *s2, *s3;
1193 s1= RE_vertren_get_sticky(obr, v1, 0);
1194 s2= RE_vertren_get_sticky(obr, v2, 0);
1195 s3= RE_vertren_get_sticky(obr, v3, 0);
1197 if(s1 && s2 && s3) {
1198 float winmat[4][4], ho1[4], ho2[4], ho3[4];
1200 float hox, hoy, l, dl, u, v;
1201 float s00, s01, s10, s11, detsh;
1203 /* old globals, localized now */
1204 Zmulx= ((float)R.winx)/2.0f; Zmuly= ((float)R.winy)/2.0f;
1206 if(shi->obi->flag & R_TRANSFORMED)
1207 zbuf_make_winmat(&R, shi->obi->mat, winmat);
1209 zbuf_make_winmat(&R, NULL, winmat);
1211 zbuf_render_project(winmat, v1->co, ho1);
1212 zbuf_render_project(winmat, v2->co, ho2);
1213 zbuf_render_project(winmat, v3->co, ho3);
1215 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1216 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1217 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1218 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1220 detsh= s00*s11-s10*s01;
1221 s00/= detsh; s01/=detsh;
1222 s10/=detsh; s11/=detsh;
1224 /* recalc u and v again */
1227 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1228 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1231 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1232 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1233 shi->sticky[2]= 0.0f;
1236 float dxuv[2], dyuv[2];
1238 dxuv[1]= - s01/Zmulx;
1239 dyuv[0]= - s10/Zmuly;
1242 dl= dxuv[0] + dxuv[1];
1243 shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1244 shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1245 dl= dyuv[0] + dyuv[1];
1246 shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1247 shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1252 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
1256 /* ****************** ShadeSample ************************************** */
1258 /* initialize per part, not per pixel! */
1259 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1262 memset(shi, 0, sizeof(ShadeInput));
1264 shi->sample= sample;
1265 shi->thread= pa->thread;
1266 shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
1268 shi->layflag= rl->layflag;
1269 shi->passflag= rl->passflag;
1270 shi->combinedflag= ~rl->pass_xor;
1271 shi->mat_override= rl->mat_override;
1272 shi->light_override= rl->light_override;
1274 /* note shi.depth==0 means first hit, not raytracing */
1278 /* initialize per part, not per pixel! */
1279 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1283 tot= R.osa==0?1:R.osa;
1285 for(a=0; a<tot; a++) {
1286 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1287 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1290 get_sample_layers(pa, rl, ssamp->rlpp);
1293 /* Do AO or (future) GI */
1294 void shade_samples_do_AO(ShadeSample *ssamp)
1299 if(!(R.r.mode & R_SHADOW))
1301 if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1304 if(R.wrld.mode & WO_AMB_OCC) {
1305 shi= &ssamp->shi[0];
1307 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & SCE_PASS_AO))
1308 || (shi->passflag & SCE_PASS_AO))
1309 for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
1310 if(!(shi->mode & MA_SHLESS))
1311 ambient_occlusion(shi); /* stores in shi->ao[] */
1316 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1323 for(shi= ssamp->shi; ps; ps= ps->next) {
1324 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1326 if(shi->vlr) { /* NULL happens for env material or for 'all z' */
1327 unsigned short curmask= ps->mask;
1329 /* full osa is only set for OSA renders */
1330 if(shi->vlr->flag & R_FULL_OSA) {
1331 short shi_cp= 0, samp;
1333 for(samp=0; samp<R.osa; samp++) {
1334 if(curmask & (1<<samp)) {
1335 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1336 xs= (float)x + R.jit[samp][0] + 0.5f;
1337 ys= (float)y + R.jit[samp][1] + 0.5f;
1340 shade_input_copy_triangle(shi, shi-1);
1342 shi->mask= (1<<samp);
1343 // shi->rl= ssamp->rlpp[samp];
1344 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1345 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1346 shade_input_set_uv(shi);
1347 shade_input_set_normals(shi);
1356 short b= R.samples->centmask[curmask];
1357 xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1358 ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1361 xs= (float)x + 0.5f;
1362 ys= (float)y + 0.5f;
1366 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1367 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1368 shade_input_set_uv(shi);
1369 shade_input_set_normals(shi);
1373 /* total sample amount, shi->sample is static set in initialize */
1375 ssamp->tot= (shi-1)->sample + 1;
1380 /* shades samples, returns true if anything happened */
1381 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1383 shade_samples_fill_with_ps(ssamp, ps, x, y);
1386 ShadeInput *shi= ssamp->shi;
1387 ShadeResult *shr= ssamp->shr;
1390 /* if shadow or AO? */
1391 shade_samples_do_AO(ssamp);
1393 /* if shade (all shadepinputs have same passflag) */
1394 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1396 for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1397 shade_input_set_shade_texco(shi);
1398 shade_input_do_shade(shi, shr);