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 *****
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_colortools.h"
43 #include "BKE_utildefines.h"
47 #include "raycounter.h"
48 #include "renderpipeline.h"
49 #include "render_types.h"
50 #include "renderdatabase.h"
51 #include "rendercore.h"
56 #include "volumetric.h"
59 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
60 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
61 /* only to be used here in this file, it's for speed */
62 extern struct Render R;
63 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
66 #define VECADDISFAC(v1,v3,fac) {*(v1)+= *(v3)*(fac); *(v1+1)+= *(v3+1)*(fac); *(v1+2)+= *(v3+2)*(fac);}
70 /* Shade Sample order:
72 - shade_samples_fill_with_ps()
74 - shade_input_set_triangle() <- if prev sample-face is same, use shade_input_copy_triangle()
76 - shade_input_set_viewco() <- not for ray or bake
77 - shade_input_set_uv() <- not for ray or bake
78 - shade_input_set_normals()
81 - shade_samples_do_AO()
84 - shade_input_set_shade_texco()
85 - shade_samples_do_shade()
86 - OSA: distribute sample result with filter masking
90 /* initialise material variables in shadeinput,
91 * doing inverse gamma correction where applicable */
92 void shade_input_init_material(ShadeInput *shi)
94 if (R.r.color_mgt_flag & R_COLOR_MANAGEMENT) {
95 color_manage_linearize(&shi->r, &shi->mat->r);
96 color_manage_linearize(&shi->specr, &shi->mat->specr);
97 color_manage_linearize(&shi->mirr, &shi->mat->mirr);
99 /* material ambr / ambg / ambb is overwritten from world
100 color_manage_linearize(shi->ambr, shi->mat->ambr);
103 /* note, keep this synced with render_types.h */
104 memcpy(&shi->amb, &shi->mat->amb, 11*sizeof(float));
105 shi->har= shi->mat->har;
107 /* note, keep this synced with render_types.h */
108 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
109 shi->har= shi->mat->har;
114 static void shadeinput_colors_linearize(ShadeInput *shi)
116 color_manage_linearize(&shi->r, &shi->r);
117 color_manage_linearize(&shi->specr, &shi->specr);
118 color_manage_linearize(&shi->mirr, &shi->mirr);
121 /* also used as callback for nodes */
122 /* delivers a fully filled in ShadeResult, for all passes */
123 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
125 /* because node materials don't have access to rendering context,
126 * inverse gamma correction must happen here. evil. */
127 if (R.r.color_mgt_flag & R_COLOR_MANAGEMENT && shi->nodes == 1)
128 shadeinput_colors_linearize(shi);
130 shade_lamp_loop(shi, shr); /* clears shr */
132 if(shi->translucency!=0.0f) {
134 float fac= shi->translucency;
136 shade_input_init_material(shi);
138 VECCOPY(shi->vn, shi->vno);
139 VECMUL(shi->vn, -1.0f);
140 VECMUL(shi->facenor, -1.0f);
141 shi->depth++; /* hack to get real shadow now */
142 shade_lamp_loop(shi, &shr_t);
145 /* a couple of passes */
146 VECADDISFAC(shr->combined, shr_t.combined, fac);
147 if(shi->passflag & SCE_PASS_SPEC)
148 VECADDISFAC(shr->spec, shr_t.spec, fac);
149 if(shi->passflag & SCE_PASS_DIFFUSE)
150 VECADDISFAC(shr->diff, shr_t.diff, fac);
151 if(shi->passflag & SCE_PASS_SHADOW)
152 VECADDISFAC(shr->shad, shr_t.shad, fac);
154 VECMUL(shi->vn, -1.0f);
155 VECMUL(shi->facenor, -1.0f);
158 /* depth >= 1 when ray-shading */
160 if(R.r.mode & R_RAYTRACE) {
161 if(shi->ray_mirror!=0.0f || ((shi->mat->mode & MA_TRANSP) && (shi->mat->mode & MA_RAYTRANSP) && shr->alpha!=1.0f)) {
162 /* ray trace works on combined, but gives pass info */
166 /* disable adding of sky for raytransp */
167 if((shi->mat->mode & MA_TRANSP) && (shi->mat->mode & MA_RAYTRANSP))
168 if((shi->layflag & SCE_LAY_SKY) && (R.r.alphamode==R_ADDSKY))
172 if(R.r.mode & R_RAYTRACE) {
173 if (R.render_volumes_inside.first)
174 shade_volume_inside(shi, shr);
179 /* do a shade, finish up some passes, apply mist */
180 void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
184 /* ------ main shading loop -------- */
186 memset(&shi->raycounter, 0, sizeof(shi->raycounter));
189 if(shi->mat->nodetree && shi->mat->use_nodes) {
190 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
193 /* copy all relevant material vars, note, keep this synced with render_types.h */
194 shade_input_init_material(shi);
196 if (shi->mat->material_type == MA_TYPE_VOLUME) {
197 if(R.r.mode & R_RAYTRACE)
198 shade_volume_outside(shi, shr);
199 } else { /* MA_TYPE_SURFACE, MA_TYPE_WIRE */
200 shade_material_loop(shi, shr);
204 /* copy additional passes */
205 if(shi->passflag & (SCE_PASS_VECTOR|SCE_PASS_NORMAL|SCE_PASS_RADIO)) {
206 QUATCOPY(shr->winspeed, shi->winspeed);
207 VECCOPY(shr->nor, shi->vn);
211 if((shi->passflag & SCE_PASS_MIST) || ((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0)) {
212 if(R.r.mode & R_ORTHO)
213 shr->mist= mistfactor(-shi->co[2], shi->co);
215 shr->mist= mistfactor(VecLength(shi->co), shi->co);
217 else shr->mist= 0.0f;
219 if((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0 ) {
224 /* add mist and premul color */
225 if(shr->alpha!=1.0f || alpha!=1.0f) {
226 float fac= alpha*(shr->alpha);
227 shr->combined[3]= fac;
229 if (shi->mat->material_type!= MA_TYPE_VOLUME)
230 VecMulf(shr->combined, fac);
233 shr->combined[3]= 1.0f;
240 if(1 || shi->passflag & SCE_PASS_RAYHITS)
242 shr->rayhits[0] = (float)shi->raycounter.faces.test;
243 shr->rayhits[1] = (float)shi->raycounter.bb.hit;
244 shr->rayhits[2] = 0.0;
245 shr->rayhits[3] = 1.0;
248 RE_RC_MERGE(&re_rc_counter[shi->thread], &shi->raycounter);
251 /* **************************************************************************** */
253 /* **************************************************************************** */
256 void vlr_set_uv_indices(VlakRen *vlr, int *i1, int *i2, int *i3)
258 /* to prevent storing new tfaces or vcols, we check a split runtime */
262 /* 1---2 1---2 0 = orig face, 1 = new face */
264 /* Update vert nums to point to correct verts of original face */
265 if(vlr->flag & R_DIVIDE_24) {
266 if(vlr->flag & R_FACE_SPLIT) {
267 (*i1)++; (*i2)++; (*i3)++;
273 else if(vlr->flag & R_FACE_SPLIT) {
278 /* copy data from face to ShadeInput, general case */
279 /* indices 0 1 2 3 only */
280 void shade_input_set_triangle_i(ShadeInput *shi, ObjectInstanceRen *obi, VlakRen *vlr, short i1, short i2, short i3)
282 VertRen **vpp= &vlr->v1;
296 /* note, shi->mat is set in node shaders */
297 shi->mat= shi->mat_override?shi->mat_override:vlr->mat;
299 shi->osatex= (shi->mat->texco & TEXCO_OSA);
300 shi->mode= shi->mat->mode_l; /* or-ed result for all nodes */
302 /* facenormal copy, can get flipped */
303 shi->flippednor= RE_vlakren_get_normal(&R, obi, vlr, shi->facenor);
305 /* copy of original pre-flipped normal, for geometry->front/back node output */
306 VECCOPY(shi->orignor, shi->facenor);
308 VECMUL(shi->orignor, -1.0f);
310 /* calculate vertexnormals */
311 if(vlr->flag & R_SMOOTH) {
312 VECCOPY(shi->n1, shi->v1->n);
313 VECCOPY(shi->n2, shi->v2->n);
314 VECCOPY(shi->n3, shi->v3->n);
316 if(obi->flag & R_TRANSFORMED) {
317 Mat3MulVecfl(obi->nmat, shi->n1);
318 Mat3MulVecfl(obi->nmat, shi->n2);
319 Mat3MulVecfl(obi->nmat, shi->n3);
322 if(!(vlr->flag & (R_NOPUNOFLIP|R_TANGENT))) {
323 if(INPR(shi->facenor, shi->n1) < 0.0f) {
324 shi->n1[0]= -shi->n1[0];
325 shi->n1[1]= -shi->n1[1];
326 shi->n1[2]= -shi->n1[2];
328 if(INPR(shi->facenor, shi->n2) < 0.0f) {
329 shi->n2[0]= -shi->n2[0];
330 shi->n2[1]= -shi->n2[1];
331 shi->n2[2]= -shi->n2[2];
333 if(INPR(shi->facenor, shi->n3) < 0.0f) {
334 shi->n3[0]= -shi->n3[0];
335 shi->n3[1]= -shi->n3[1];
336 shi->n3[2]= -shi->n3[2];
342 /* note, facenr declared volatile due to over-eager -O2 optimizations
343 * on cygwin (particularly -frerun-cse-after-loop)
346 /* copy data from face to ShadeInput, scanline case */
347 void shade_input_set_triangle(ShadeInput *shi, volatile int obi, volatile int facenr, int normal_flip)
350 shi->obi= &R.objectinstance[obi];
351 shi->obr= shi->obi->obr;
352 shi->facenr= (facenr-1) & RE_QUAD_MASK;
353 if( shi->facenr < shi->obr->totvlak ) {
354 VlakRen *vlr= RE_findOrAddVlak(shi->obr, shi->facenr);
356 if(facenr & RE_QUAD_OFFS)
357 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 2, 3);
359 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 1, 2);
362 shi->vlr= NULL; /* general signal we got sky */
365 shi->vlr= NULL; /* general signal we got sky */
368 /* full osa case: copy static info */
369 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
371 /* not so nice, but works... warning is in RE_shader_ext.h */
372 memcpy(shi, from, sizeof(struct ShadeInputCopy));
375 /* copy data from strand to shadeinput */
376 void shade_input_set_strand(ShadeInput *shi, StrandRen *strand, StrandPoint *spoint)
378 /* note, shi->mat is set in node shaders */
379 shi->mat= shi->mat_override? shi->mat_override: strand->buffer->ma;
381 shi->osatex= (shi->mat->texco & TEXCO_OSA);
382 shi->mode= shi->mat->mode_l; /* or-ed result for all nodes */
384 /* shade_input_set_viewco equivalent */
385 VECCOPY(shi->co, spoint->co);
386 VECCOPY(shi->view, shi->co);
387 Normalize(shi->view);
389 shi->xs= (int)spoint->x;
390 shi->ys= (int)spoint->y;
392 if(shi->osatex || (R.r.mode & R_SHADOW)) {
393 VECCOPY(shi->dxco, spoint->dtco);
394 VECCOPY(shi->dyco, spoint->dsco);
397 /* dxview, dyview, not supported */
399 /* facenormal, simply viewco flipped */
400 VECCOPY(shi->facenor, spoint->nor);
401 VECCOPY(shi->orignor, shi->facenor);
403 /* shade_input_set_normals equivalent */
404 if(shi->mat->mode & MA_TANGENT_STR) {
405 VECCOPY(shi->vn, spoint->tan)
410 Crossf(cross, spoint->co, spoint->tan);
411 Crossf(shi->vn, cross, spoint->tan);
414 if(INPR(shi->vn, shi->view) < 0.0f)
418 VECCOPY(shi->vno, shi->vn);
421 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
423 StrandBuffer *strandbuf= strand->buffer;
424 ObjectRen *obr= strandbuf->obr;
426 int mode= shi->mode; /* or-ed result for all nodes */
427 short texco= shi->mat->texco;
429 if((shi->mat->texco & TEXCO_REFL)) {
430 /* shi->dxview, shi->dyview, not supported */
433 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL))) {
437 if(mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
438 VECCOPY(shi->tang, spoint->tan);
439 VECCOPY(shi->nmaptang, spoint->tan);
442 if(mode & MA_STR_SURFDIFF) {
443 float *surfnor= RE_strandren_get_surfnor(obr, strand, 0);
446 VECCOPY(shi->surfnor, surfnor)
448 VECCOPY(shi->surfnor, shi->vn)
450 if(shi->mat->strand_surfnor > 0.0f) {
452 for(sv=strand->vert; sv!=svert; sv++)
453 shi->surfdist+=VecLenf(sv->co, (sv+1)->co);
454 shi->surfdist += spoint->t*VecLenf(sv->co, (sv+1)->co);
458 if(R.r.mode & R_SPEED) {
461 speed= RE_strandren_get_winspeed(shi->obi, strand, 0);
463 QUATCOPY(shi->winspeed, speed)
465 shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
468 /* shade_input_set_shade_texco equivalent */
469 if(texco & NEED_UV) {
470 if(texco & TEXCO_ORCO) {
471 VECCOPY(shi->lo, strand->orco);
472 /* no shi->osatex, orco derivatives are zero */
475 if(texco & TEXCO_GLOB) {
476 VECCOPY(shi->gl, shi->co);
477 Mat4MulVecfl(R.viewinv, shi->gl);
480 VECCOPY(shi->dxgl, shi->dxco);
481 Mat3MulVecfl(R.imat, shi->dxco);
482 VECCOPY(shi->dygl, shi->dyco);
483 Mat3MulVecfl(R.imat, shi->dyco);
487 if(texco & TEXCO_STRAND) {
488 shi->strandco= spoint->strandco;
491 shi->dxstrand= spoint->dtstrandco;
496 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
504 shi->actuv= obr->actmtface;
505 shi->actcol= obr->actmcol;
507 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
508 for (i=0; (mcol=RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
509 ShadeInputCol *scol= &shi->col[i];
510 char *cp= (char*)mcol;
515 scol->col[0]= cp[3]/255.0f;
516 scol->col[1]= cp[2]/255.0f;
517 scol->col[2]= cp[1]/255.0f;
521 shi->vcol[0]= shi->col[shi->actcol].col[0];
522 shi->vcol[1]= shi->col[shi->actcol].col[1];
523 shi->vcol[2]= shi->col[shi->actcol].col[2];
532 for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
533 ShadeInputUV *suv= &shi->uv[i];
538 if(strandbuf->overrideuv == i) {
540 suv->uv[1]= spoint->strandco;
544 suv->uv[0]= -1.0f + 2.0f*uv[0];
545 suv->uv[1]= -1.0f + 2.0f*uv[1];
546 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
556 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
557 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
565 if(shi->totuv == 0) {
566 ShadeInputUV *suv= &shi->uv[0];
569 suv->uv[1]= spoint->strandco;
570 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
572 if(mode & MA_FACETEXTURE) {
573 /* no tface? set at 1.0f */
582 if(texco & TEXCO_NORM) {
583 shi->orn[0]= -shi->vn[0];
584 shi->orn[1]= -shi->vn[1];
585 shi->orn[2]= -shi->vn[2];
588 if(texco & TEXCO_REFL) {
589 /* mirror reflection color textures (and envmap) */
590 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
593 if(texco & TEXCO_STRESS) {
597 if(texco & TEXCO_TANGENT) {
598 if((mode & MA_TANGENT_V)==0) {
599 /* just prevent surprises */
600 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
601 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
606 /* this only avalailable for scanline renders */
608 if(texco & TEXCO_WINDOW) {
609 shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
610 shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
622 if(texco & TEXCO_STICKY) {
627 if (R.r.color_mgt_flag & R_COLOR_MANAGEMENT) {
628 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
629 color_manage_linearize(shi->vcol, shi->vcol);
635 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
636 void shade_input_calc_viewco(ShadeInput *shi, float x, float y, float z, float *view, float *dxyview, float *co, float *dxco, float *dyco)
638 /* returns not normalized, so is in viewplane coords */
639 calc_view_vector(view, x, y);
641 if(shi->mat->material_type == MA_TYPE_WIRE) {
642 /* wire cannot use normal for calculating shi->co, so
643 * we reconstruct the coordinate less accurate */
644 if(R.r.mode & R_ORTHO)
645 calc_renderco_ortho(co, x, y, z);
647 calc_renderco_zbuf(co, view, z);
650 /* for non-wire, intersect with the triangle to get the exact coord */
651 float fac, dface, v1[3];
653 VECCOPY(v1, shi->v1->co);
654 if(shi->obi->flag & R_TRANSFORMED)
655 Mat4MulVecfl(shi->obi->mat, v1);
657 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
659 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
660 if(R.r.mode & R_ORTHO) {
661 /* x and y 3d coordinate can be derived from pixel coord and winmat */
662 float fx= 2.0f/(R.winx*R.winmat[0][0]);
663 float fy= 2.0f/(R.winy*R.winmat[1][1]);
665 co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
666 co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
668 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
669 if(shi->facenor[2]!=0.0f)
670 co[2]= (dface - shi->facenor[0]*co[0] - shi->facenor[1]*co[1])/shi->facenor[2];
677 if(shi->facenor[2]!=0.0f)
678 dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
684 if(shi->facenor[2]!=0.0f)
685 dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
690 if(co[2]!=0.0f) fac= 1.0f/co[2]; else fac= 0.0f;
691 dxyview[0]= -R.viewdx*fac;
692 dxyview[1]= -R.viewdy*fac;
699 div= shi->facenor[0]*view[0] + shi->facenor[1]*view[1] + shi->facenor[2]*view[2];
700 if (div!=0.0f) fac= dface/div;
707 /* pixel dx/dy for render coord */
709 float u= dface/(div - R.viewdx*shi->facenor[0]);
710 float v= dface/(div - R.viewdy*shi->facenor[1]);
712 dxco[0]= co[0]- (view[0]-R.viewdx)*u;
713 dxco[1]= co[1]- (view[1])*u;
714 dxco[2]= co[2]- (view[2])*u;
716 dyco[0]= co[0]- (view[0])*v;
717 dyco[1]= co[1]- (view[1]-R.viewdy)*v;
718 dyco[2]= co[2]- (view[2])*v;
721 if(fac!=0.0f) fac= 1.0f/fac;
722 dxyview[0]= -R.viewdx*fac;
723 dxyview[1]= -R.viewdy*fac;
729 /* set camera coords - for scanline, it's always 0.0,0.0,0.0 (render is in camera space)
730 * however for raytrace it can be different - the position of the last intersection */
731 shi->camera_co[0] = shi->camera_co[1] = shi->camera_co[2] = 0.0f;
733 /* cannot normalize earlier, code above needs it at viewplane level */
737 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
738 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
740 float *dxyview= NULL, *dxco= NULL, *dyco= NULL;
742 /* currently in use for dithering (soft shadow), node preview, irregular shad */
746 /* original scanline coordinate without jitter */
751 /* check if we need derivatives */
752 if(shi->osatex || (R.r.mode & R_SHADOW)) {
756 if((shi->mat->texco & TEXCO_REFL))
757 dxyview= &shi->dxview;
760 shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
763 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
764 void shade_input_set_uv(ShadeInput *shi)
766 VlakRen *vlr= shi->vlr;
768 if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
769 float v1[3], v2[3], v3[3];
771 VECCOPY(v1, shi->v1->co);
772 VECCOPY(v2, shi->v2->co);
773 VECCOPY(v3, shi->v3->co);
775 if(shi->obi->flag & R_TRANSFORMED) {
776 Mat4MulVecfl(shi->obi->mat, v1);
777 Mat4MulVecfl(shi->obi->mat, v2);
778 Mat4MulVecfl(shi->obi->mat, v3);
781 /* exception case for wire render of edge */
782 if(vlr->v2==vlr->v3) {
785 lend= VecLenf(v2, v1);
786 lenc= VecLenf(shi->co, v1);
792 shi->u= - (1.0f - lenc/lend);
804 /* most of this could become re-used for faces */
805 float detsh, t00, t10, t01, t11, xn, yn, zn;
808 /* find most stable axis to project */
809 xn= fabs(shi->facenor[0]);
810 yn= fabs(shi->facenor[1]);
811 zn= fabs(shi->facenor[2]);
813 if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
814 else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
815 else { axis1= 1; axis2= 2; }
817 /* compute u,v and derivatives */
818 t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
819 t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
821 detsh= 1.0f/(t00*t11-t10*t01);
822 t00*= detsh; t01*=detsh;
823 t10*=detsh; t11*=detsh;
825 shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
826 shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
828 shi->dx_u= shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
829 shi->dx_v= shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
830 shi->dy_u= shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
831 shi->dy_v= shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
834 /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
835 CLAMP(shi->u, -2.0f, 1.0f);
836 CLAMP(shi->v, -2.0f, 1.0f);
841 void shade_input_set_normals(ShadeInput *shi)
843 float u= shi->u, v= shi->v;
846 /* calculate vertexnormals */
847 if(shi->vlr->flag & R_SMOOTH) {
848 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
850 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
851 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
852 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
857 VECCOPY(shi->vn, shi->facenor);
860 VECCOPY(shi->vno, shi->vn);
864 /* use by raytrace, sss, bake to flip into the right direction */
865 void shade_input_flip_normals(ShadeInput *shi)
867 shi->facenor[0]= -shi->facenor[0];
868 shi->facenor[1]= -shi->facenor[1];
869 shi->facenor[2]= -shi->facenor[2];
871 shi->vn[0]= -shi->vn[0];
872 shi->vn[1]= -shi->vn[1];
873 shi->vn[2]= -shi->vn[2];
875 shi->vno[0]= -shi->vno[0];
876 shi->vno[1]= -shi->vno[1];
877 shi->vno[2]= -shi->vno[2];
879 shi->flippednor= !shi->flippednor;
882 void shade_input_set_shade_texco(ShadeInput *shi)
884 ObjectInstanceRen *obi= shi->obi;
885 ObjectRen *obr= shi->obr;
886 VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
887 float u= shi->u, v= shi->v;
888 float l= 1.0f+u+v, dl;
889 int mode= shi->mode; /* or-ed result for all nodes */
890 short texco= shi->mat->texco;
893 if(shi->vlr->flag & R_SMOOTH) {
895 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
896 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
898 dl= shi->dx_u+shi->dx_v;
899 shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
900 shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
901 shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
902 dl= shi->dy_u+shi->dy_v;
903 shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
904 shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
905 shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
911 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
912 float *tangent, *s1, *s2, *s3;
915 if(shi->vlr->flag & R_SMOOTH) {
921 /* qdn: flat faces have tangents too,
922 could pick either one, using average here */
928 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
929 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
931 if(mode & MA_TANGENT_V) {
932 s1 = RE_vertren_get_tangent(obr, v1, 0);
933 s2 = RE_vertren_get_tangent(obr, v2, 0);
934 s3 = RE_vertren_get_tangent(obr, v3, 0);
937 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
938 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
939 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
941 if(obi->flag & R_TRANSFORMED)
942 Mat3MulVecfl(obi->nmat, shi->tang);
944 Normalize(shi->tang);
945 VECCOPY(shi->nmaptang, shi->tang);
949 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
950 tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
953 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
955 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
961 shi->nmaptang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
962 shi->nmaptang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
963 shi->nmaptang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
965 if(obi->flag & R_TRANSFORMED)
966 Mat3MulVecfl(obi->nmat, shi->nmaptang);
968 Normalize(shi->nmaptang);
973 if(mode & MA_STR_SURFDIFF) {
974 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
977 VECCOPY(shi->surfnor, surfnor)
978 if(obi->flag & R_TRANSFORMED)
979 Mat3MulVecfl(obi->nmat, shi->surfnor);
982 VECCOPY(shi->surfnor, shi->vn)
987 if(R.r.mode & R_SPEED) {
990 s1= RE_vertren_get_winspeed(obi, v1, 0);
991 s2= RE_vertren_get_winspeed(obi, v2, 0);
992 s3= RE_vertren_get_winspeed(obi, v3, 0);
994 shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
995 shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
996 shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
997 shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
1000 shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
1004 /* pass option forces UV calc */
1005 if(shi->passflag & SCE_PASS_UV)
1006 texco |= (NEED_UV|TEXCO_UV);
1008 /* texture coordinates. shi->dxuv shi->dyuv have been set */
1009 if(texco & NEED_UV) {
1011 if(texco & TEXCO_ORCO) {
1013 float *o1, *o2, *o3;
1019 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
1020 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
1021 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
1024 dl= shi->dx_u+shi->dx_v;
1025 shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
1026 shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
1027 shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
1028 dl= shi->dy_u+shi->dy_v;
1029 shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
1030 shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
1031 shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
1035 VECCOPY(shi->duplilo, obi->dupliorco);
1038 if(texco & TEXCO_GLOB) {
1039 VECCOPY(shi->gl, shi->co);
1040 Mat4MulVecfl(R.viewinv, shi->gl);
1042 VECCOPY(shi->dxgl, shi->dxco);
1043 // TXF: bug was here, but probably should be in convertblender.c, R.imat only valid if there is a world
1044 //Mat3MulVecfl(R.imat, shi->dxco);
1045 Mat4Mul3Vecfl(R.viewinv, shi->dxco);
1046 VECCOPY(shi->dygl, shi->dyco);
1047 //Mat3MulVecfl(R.imat, shi->dyco);
1048 Mat4Mul3Vecfl(R.viewinv, shi->dyco);
1052 if(texco & TEXCO_STRAND) {
1053 shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
1055 dl= shi->dx_u+shi->dx_v;
1056 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
1057 dl= shi->dy_u+shi->dy_v;
1058 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
1062 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
1063 VlakRen *vlr= shi->vlr;
1067 int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
1069 /* uv and vcols are not copied on split, so set them according vlr divide flag */
1070 vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1074 shi->actuv= obr->actmtface;
1075 shi->actcol= obr->actmcol;
1077 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1078 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1079 ShadeInputCol *scol= &shi->col[i];
1080 char *cp1, *cp2, *cp3;
1085 cp1= (char *)(mcol+j1);
1086 cp2= (char *)(mcol+j2);
1087 cp3= (char *)(mcol+j3);
1089 scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
1090 scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
1091 scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
1095 shi->vcol[0]= shi->col[shi->actcol].col[0];
1096 shi->vcol[1]= shi->col[shi->actcol].col[1];
1097 shi->vcol[2]= shi->col[shi->actcol].col[2];
1108 for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1109 ShadeInputUV *suv= &shi->uv[i];
1110 float *uv1, *uv2, *uv3;
1119 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1120 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1121 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1126 dl= shi->dx_u+shi->dx_v;
1130 suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1131 suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1133 dl= shi->dy_u+shi->dy_v;
1137 suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1138 suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1141 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1142 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1148 if(tface && tface->tpage)
1149 render_realtime_texture(shi, tface->tpage);
1155 shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1156 shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1157 shi->dupliuv[2]= 0.0f;
1159 if(shi->totuv == 0) {
1160 ShadeInputUV *suv= &shi->uv[0];
1162 suv->uv[0]= 2.0f*(u+.5f);
1163 suv->uv[1]= 2.0f*(v+.5f);
1164 suv->uv[2]= 0.0f; /* texture.c assumes there are 3 coords */
1166 if(mode & MA_FACETEXTURE) {
1167 /* no tface? set at 1.0f */
1176 if(texco & TEXCO_NORM) {
1177 shi->orn[0]= -shi->vn[0];
1178 shi->orn[1]= -shi->vn[1];
1179 shi->orn[2]= -shi->vn[2];
1182 if(texco & TEXCO_REFL) {
1183 /* mirror reflection color textures (and envmap) */
1184 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
1187 if(texco & TEXCO_STRESS) {
1188 float *s1, *s2, *s3;
1190 s1= RE_vertren_get_stress(obr, v1, 0);
1191 s2= RE_vertren_get_stress(obr, v2, 0);
1192 s3= RE_vertren_get_stress(obr, v3, 0);
1193 if(s1 && s2 && s3) {
1194 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1195 if(shi->stress<1.0f) shi->stress-= 1.0f;
1196 else shi->stress= (shi->stress-1.0f)/shi->stress;
1198 else shi->stress= 0.0f;
1201 if(texco & TEXCO_TANGENT) {
1202 if((mode & MA_TANGENT_V)==0) {
1203 /* just prevent surprises */
1204 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1205 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1210 /* this only avalailable for scanline renders */
1215 if(texco & TEXCO_WINDOW) {
1216 shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1217 shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1218 shi->winco[2]= 0.0f;
1220 shi->dxwin[0]= 2.0f/(float)R.winx;
1221 shi->dywin[1]= 2.0f/(float)R.winy;
1222 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1223 shi->dywin[0]= shi->dywin[2]= 0.0f;
1227 if(texco & TEXCO_STICKY) {
1228 float *s1, *s2, *s3;
1230 s1= RE_vertren_get_sticky(obr, v1, 0);
1231 s2= RE_vertren_get_sticky(obr, v2, 0);
1232 s3= RE_vertren_get_sticky(obr, v3, 0);
1234 if(s1 && s2 && s3) {
1235 float winmat[4][4], ho1[4], ho2[4], ho3[4];
1237 float hox, hoy, l, dl, u, v;
1238 float s00, s01, s10, s11, detsh;
1240 /* old globals, localized now */
1241 Zmulx= ((float)R.winx)/2.0f; Zmuly= ((float)R.winy)/2.0f;
1243 if(shi->obi->flag & R_TRANSFORMED)
1244 zbuf_make_winmat(&R, shi->obi->mat, winmat);
1246 zbuf_make_winmat(&R, NULL, winmat);
1248 zbuf_render_project(winmat, v1->co, ho1);
1249 zbuf_render_project(winmat, v2->co, ho2);
1250 zbuf_render_project(winmat, v3->co, ho3);
1252 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1253 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1254 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1255 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1257 detsh= s00*s11-s10*s01;
1258 s00/= detsh; s01/=detsh;
1259 s10/=detsh; s11/=detsh;
1261 /* recalc u and v again */
1264 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1265 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1268 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1269 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1270 shi->sticky[2]= 0.0f;
1273 float dxuv[2], dyuv[2];
1275 dxuv[1]= - s01/Zmulx;
1276 dyuv[0]= - s10/Zmuly;
1279 dl= dxuv[0] + dxuv[1];
1280 shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1281 shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1282 dl= dyuv[0] + dyuv[1];
1283 shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1284 shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1289 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
1291 if (R.r.color_mgt_flag & R_COLOR_MANAGEMENT) {
1292 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
1293 color_manage_linearize(shi->vcol, shi->vcol);
1299 /* ****************** ShadeSample ************************************** */
1301 /* initialize per part, not per pixel! */
1302 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1305 memset(shi, 0, sizeof(ShadeInput));
1307 shi->sample= sample;
1308 shi->thread= pa->thread;
1309 shi->do_preview= (R.r.scemode & R_MATNODE_PREVIEW) != 0;
1311 shi->layflag= rl->layflag;
1312 shi->passflag= rl->passflag;
1313 shi->combinedflag= ~rl->pass_xor;
1314 shi->mat_override= rl->mat_override;
1315 shi->light_override= rl->light_override;
1317 /* note shi.depth==0 means first hit, not raytracing */
1321 /* initialize per part, not per pixel! */
1322 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1326 tot= R.osa==0?1:R.osa;
1328 for(a=0; a<tot; a++) {
1329 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1330 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1333 get_sample_layers(pa, rl, ssamp->rlpp);
1336 /* Do AO or (future) GI */
1337 void shade_samples_do_AO(ShadeSample *ssamp)
1342 if(!(R.r.mode & R_SHADOW))
1344 if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1347 if(R.wrld.mode & WO_AMB_OCC) {
1348 shi= &ssamp->shi[0];
1350 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & SCE_PASS_AO))
1351 || (shi->passflag & SCE_PASS_AO))
1352 for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
1353 if(!(shi->mode & MA_SHLESS))
1354 ambient_occlusion(shi); /* stores in shi->ao[] */
1359 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1366 for(shi= ssamp->shi; ps; ps= ps->next) {
1367 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1369 if(shi->vlr) { /* NULL happens for env material or for 'all z' */
1370 unsigned short curmask= ps->mask;
1372 /* full osa is only set for OSA renders */
1373 if(shi->vlr->flag & R_FULL_OSA) {
1374 short shi_cp= 0, samp;
1376 for(samp=0; samp<R.osa; samp++) {
1377 if(curmask & (1<<samp)) {
1378 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1379 xs= (float)x + R.jit[samp][0] + 0.5f;
1380 ys= (float)y + R.jit[samp][1] + 0.5f;
1383 shade_input_copy_triangle(shi, shi-1);
1385 shi->mask= (1<<samp);
1386 // shi->rl= ssamp->rlpp[samp];
1387 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1388 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1389 shade_input_set_uv(shi);
1390 shade_input_set_normals(shi);
1399 short b= R.samples->centmask[curmask];
1400 xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1401 ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1404 xs= (float)x + 0.5f;
1405 ys= (float)y + 0.5f;
1409 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1410 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1411 shade_input_set_uv(shi);
1412 shade_input_set_normals(shi);
1416 /* total sample amount, shi->sample is static set in initialize */
1418 ssamp->tot= (shi-1)->sample + 1;
1423 /* shades samples, returns true if anything happened */
1424 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1426 shade_samples_fill_with_ps(ssamp, ps, x, y);
1429 ShadeInput *shi= ssamp->shi;
1430 ShadeResult *shr= ssamp->shr;
1433 /* if shadow or AO? */
1434 shade_samples_do_AO(ssamp);
1436 /* if shade (all shadepinputs have same passflag) */
1437 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1439 for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1440 shade_input_set_shade_texco(shi);
1441 shade_input_do_shade(shi, shr);