Subsurface scattering:
[blender.git] / source / blender / render / intern / source / shadeinput.c
1 /**
2 * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation
21  * All rights reserved.
22  *
23  * Contributors: Hos, Robert Wenzlaff.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stdio.h>
29 #include <math.h>
30 #include <string.h>
31
32 #include "MTC_matrixops.h"
33 #include "BLI_arithb.h"
34
35 #include "DNA_curve_types.h"
36 #include "DNA_group_types.h"
37 #include "DNA_lamp_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_material_types.h"
40
41 #include "BKE_utildefines.h"
42 #include "BKE_node.h"
43
44 /* local include */
45 #include "renderpipeline.h"
46 #include "render_types.h"
47 #include "renderdatabase.h"
48 #include "rendercore.h"
49 #include "shadbuf.h"
50 #include "shading.h"
51 #include "texture.h"
52
53 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
54 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
55 /* only to be used here in this file, it's for speed */
56 extern struct Render R;
57 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
58
59
60 #define VECADDISFAC(v1,v3,fac) {*(v1)+= *(v3)*(fac); *(v1+1)+= *(v3+1)*(fac); *(v1+2)+= *(v3+2)*(fac);}
61
62
63
64 /* Shade Sample order:
65
66 - shade_samples_fill_with_ps()
67         - for each sample
68                 - shade_input_set_triangle()  <- if prev sample-face is same, use shade_input_copy_triangle()
69                 - if vlr
70                         - shade_input_set_viewco()    <- not for ray or bake
71                         - shade_input_set_uv()        <- not for ray or bake
72                         - shade_input_set_normals()
73 - shade_samples()
74         - if AO
75                 - shade_samples_do_AO()
76         - if shading happens
77                 - for each sample
78                         - shade_input_set_shade_texco()
79                         - shade_samples_do_shade()
80 - OSA: distribute sample result with filter masking
81
82         */
83
84
85 /* also used as callback for nodes */
86 /* delivers a fully filled in ShadeResult, for all passes */
87 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
88 {
89         
90         shade_lamp_loop(shi, shr);      /* clears shr */
91         
92         if(shi->translucency!=0.0f) {
93                 ShadeResult shr_t;
94                 float fac= shi->translucency;
95                 
96                 /* gotta copy it again */
97                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
98                 shi->har= shi->mat->har;
99
100                 VECCOPY(shi->vn, shi->vno);
101                 VECMUL(shi->vn, -1.0f);
102                 VECMUL(shi->facenor, -1.0f);
103                 shi->depth++;   /* hack to get real shadow now */
104                 shade_lamp_loop(shi, &shr_t);
105                 shi->depth--;
106
107                 /* a couple of passes */
108                 VECADDISFAC(shr->combined, shr_t.combined, fac);
109                 if(shi->passflag & SCE_PASS_SPEC)
110                         VECADDISFAC(shr->spec, shr_t.spec, fac);
111                 if(shi->passflag & SCE_PASS_DIFFUSE)
112                         VECADDISFAC(shr->diff, shr_t.diff, fac);
113                 if(shi->passflag & SCE_PASS_SHADOW)
114                         VECADDISFAC(shr->shad, shr_t.shad, fac);
115
116                 VECMUL(shi->vn, -1.0f);
117                 VECMUL(shi->facenor, -1.0f);
118         }
119         
120         /* depth >= 1 when ray-shading */
121         if(shi->depth==0) {
122                 if(R.r.mode & R_RAYTRACE) {
123                         if(shi->ray_mirror!=0.0f || ((shi->mat->mode & MA_RAYTRANSP) && shr->alpha!=1.0f)) {
124                                 
125                                 /* ray trace works on combined, but gives pass info */
126                                 ray_trace(shi, shr);
127                         }
128                 }
129                 /* disable adding of sky for raytransp */
130                 if(shi->mat->mode & MA_RAYTRANSP) 
131                         if(shi->layflag & SCE_LAY_SKY)
132                                 shr->alpha= 1.0f;
133         }       
134 }
135
136
137 /* do a shade, finish up some passes, apply mist */
138 void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
139 {
140         float alpha;
141         
142         /* ------  main shading loop -------- */
143         
144         if(shi->mat->nodetree && shi->mat->use_nodes) {
145                 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
146         }
147         else {
148                 /* copy all relevant material vars, note, keep this synced with render_types.h */
149                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
150                 shi->har= shi->mat->har;
151                 
152                 shade_material_loop(shi, shr);
153         }
154         
155         /* copy additional passes */
156         if(shi->passflag & (SCE_PASS_VECTOR|SCE_PASS_NORMAL|SCE_PASS_RADIO)) {
157                 QUATCOPY(shr->winspeed, shi->winspeed);
158                 VECCOPY(shr->nor, shi->vn);
159                 VECCOPY(shr->rad, shi->rad);
160         }
161         
162         /* MIST */
163         if((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0 ) {
164                 if(R.r.mode & R_ORTHO)
165                         alpha= mistfactor(-shi->co[2], shi->co);
166                 else
167                         alpha= mistfactor(VecLength(shi->co), shi->co);
168         }
169         else alpha= 1.0f;
170         
171         /* add mist and premul color */
172         if(shr->alpha!=1.0f || alpha!=1.0f) {
173                 float fac= alpha*(shr->alpha);
174                 
175                 shr->combined[3]= fac;
176                 shr->combined[0]*= fac;
177                 shr->combined[1]*= fac;
178                 shr->combined[2]*= fac;
179         }
180         else shr->combined[3]= 1.0f;
181         
182 }
183
184 /* **************************************************************************** */
185 /*                    ShadeInput                                                */
186 /* **************************************************************************** */
187
188
189 void vlr_set_uv_indices(VlakRen *vlr, int *i1, int *i2, int *i3)
190 {
191         /* to prevent storing new tfaces or vcols, we check a split runtime */
192         /*              4---3           4---3 */
193         /*              |\ 1|   or  |1 /| */
194         /*              |0\ |           |/ 0| */
195         /*              1---2           1---2   0 = orig face, 1 = new face */
196         
197         /* Update vert nums to point to correct verts of original face */
198         if(vlr->flag & R_DIVIDE_24) {  
199                 if(vlr->flag & R_FACE_SPLIT) {
200                         (*i1)++; (*i2)++; (*i3)++;
201                 }
202                 else {
203                         (*i3)++;
204                 }
205         }
206         else if(vlr->flag & R_FACE_SPLIT) {
207                 (*i2)++; (*i3)++; 
208         }
209 }
210
211
212 /* copy data from face to ShadeInput, general case */
213 /* indices 0 1 2 3 only. shi->puno should be set! */
214 void shade_input_set_triangle_i(ShadeInput *shi, VlakRen *vlr, short i1, short i2, short i3)
215 {
216         VertRen **vpp= &vlr->v1;
217         
218         shi->vlr= vlr;
219         
220         shi->v1= vpp[i1];
221         shi->v2= vpp[i2];
222         shi->v3= vpp[i3];
223         
224         shi->i1= i1;
225         shi->i2= i2;
226         shi->i3= i3;
227         
228         /* note, shi->mat is set in node shaders */
229         shi->mat= shi->mat_override?shi->mat_override:vlr->mat;
230         
231         shi->osatex= (shi->mat->texco & TEXCO_OSA);
232         shi->mode= shi->mat->mode_l;            /* or-ed result for all nodes */
233         
234         /* calculate vertexnormals */
235         if(vlr->flag & R_SMOOTH) {
236                 float *n1= shi->v1->n, *n2= shi->v2->n, *n3= shi->v3->n;
237                 char p1, p2, p3;
238                 
239                 p1= 1<<i1;
240                 p2= 1<<i2;
241                 p3= 1<<i3;
242         
243                 if(shi->puno & p1) {
244                         shi->n1[0]= -n1[0]; shi->n1[1]= -n1[1]; shi->n1[2]= -n1[2];
245                 } else {
246                         VECCOPY(shi->n1, n1);
247                 }
248                 if(shi->puno & p2) {
249                         shi->n2[0]= -n2[0]; shi->n2[1]= -n2[1]; shi->n2[2]= -n2[2];
250                 } else {
251                         VECCOPY(shi->n2, n2);
252                 }
253                 if(shi->puno & p3) {
254                         shi->n3[0]= -n3[0]; shi->n3[1]= -n3[1]; shi->n3[2]= -n3[2];
255                 } else {
256                         VECCOPY(shi->n3, n3);
257                 }
258         }
259         /* facenormal copy, can get flipped */
260         VECCOPY(shi->facenor, vlr->n);
261         
262 }
263
264 /* note, facenr declared volatile due to over-eager -O2 optimizations
265  * on cygwin (particularly -frerun-cse-after-loop)
266  */
267
268 /* copy data from face to ShadeInput, scanline case */
269 void shade_input_set_triangle(ShadeInput *shi, volatile int facenr, int normal_flip)
270 {
271         if(facenr>0) {
272                 shi->facenr= (facenr-1) & RE_QUAD_MASK;
273                 if( shi->facenr < R.totvlak ) {
274                         VlakRen *vlr= RE_findOrAddVlak(&R, shi->facenr);
275                         
276                         shi->puno= normal_flip?vlr->puno:0;
277                         
278                         if(facenr & RE_QUAD_OFFS)
279                                 shade_input_set_triangle_i(shi, vlr, 0, 2, 3);
280                         else
281                                 shade_input_set_triangle_i(shi, vlr, 0, 1, 2);
282                 }
283                 else
284                         shi->vlr= NULL; /* general signal we got sky */
285         }
286         else
287                 shi->vlr= NULL; /* general signal we got sky */
288         
289 }
290
291 /* full osa case: copy static info */
292 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
293 {
294         /* not so nice, but works... warning is in RE_shader_ext.h */
295         memcpy(shi, from, sizeof(struct ShadeInputCopy));
296 }
297
298
299 /* scanline pixel coordinates */
300 /* requires set_triangle */
301 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float z)
302 {
303         float fac;
304         
305         /* currently in use for dithering (soft shadow), node preview, irregular shad */
306         shi->xs= (int)(x);
307         shi->ys= (int)(y);
308         
309         calc_view_vector(shi->view, x, y);      /* returns not normalized, so is in viewplane coords */
310         
311         /* wire cannot use normal for calculating shi->co */
312         if(shi->mat->mode & MA_WIRE) {
313                 
314                 if(R.r.mode & R_ORTHO)
315                         calc_renderco_ortho(shi->co, x, y, z);
316                 else
317                         calc_renderco_zbuf(shi->co, shi->view, z);
318         }
319         else {
320                 float dface, *v1= shi->v1->co;
321                 
322                 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
323                 
324                 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
325                 if(R.r.mode & R_ORTHO) {
326                         /* x and y 3d coordinate can be derived from pixel coord and winmat */
327                         float fx= 2.0f/(R.winx*R.winmat[0][0]);
328                         float fy= 2.0f/(R.winy*R.winmat[1][1]);
329                         
330                         shi->co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
331                         shi->co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
332                         
333                         /* using a*x + b*y + c*z = d equation, (a b c) is normal */
334                         if(shi->facenor[2]!=0.0f)
335                                 shi->co[2]= (dface - shi->facenor[0]*shi->co[0] - shi->facenor[1]*shi->co[1])/shi->facenor[2];
336                         else
337                                 shi->co[2]= 0.0f;
338                         
339                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
340                                 shi->dxco[0]= fx;
341                                 shi->dxco[1]= 0.0f;
342                                 if(shi->facenor[2]!=0.0f)
343                                         shi->dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
344                                 else 
345                                         shi->dxco[2]= 0.0f;
346                                 
347                                 shi->dyco[0]= 0.0f;
348                                 shi->dyco[1]= fy;
349                                 if(shi->facenor[2]!=0.0f)
350                                         shi->dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
351                                 else 
352                                         shi->dyco[2]= 0.0f;
353                                 
354                                 if( (shi->mat->texco & TEXCO_REFL) ) {
355                                         if(shi->co[2]!=0.0f) fac= 1.0f/shi->co[2]; else fac= 0.0f;
356                                         shi->dxview= -R.viewdx*fac;
357                                         shi->dyview= -R.viewdy*fac;
358                                 }
359                         }
360                 }
361                 else {
362                         float div;
363                         
364                         div= shi->facenor[0]*shi->view[0] + shi->facenor[1]*shi->view[1] + shi->facenor[2]*shi->view[2];
365                         if (div!=0.0f) fac= dface/div;
366                         else fac= 0.0f;
367                         
368                         shi->co[0]= fac*shi->view[0];
369                         shi->co[1]= fac*shi->view[1];
370                         shi->co[2]= fac*shi->view[2];
371                         
372                         /* pixel dx/dy for render coord */
373                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
374                                 float u= dface/(div - R.viewdx*shi->facenor[0]);
375                                 float v= dface/(div - R.viewdy*shi->facenor[1]);
376                                 
377                                 shi->dxco[0]= shi->co[0]- (shi->view[0]-R.viewdx)*u;
378                                 shi->dxco[1]= shi->co[1]- (shi->view[1])*u;
379                                 shi->dxco[2]= shi->co[2]- (shi->view[2])*u;
380                                 
381                                 shi->dyco[0]= shi->co[0]- (shi->view[0])*v;
382                                 shi->dyco[1]= shi->co[1]- (shi->view[1]-R.viewdy)*v;
383                                 shi->dyco[2]= shi->co[2]- (shi->view[2])*v;
384                                 
385                                 if( (shi->mat->texco & TEXCO_REFL) ) {
386                                         if(fac!=0.0f) fac= 1.0f/fac;
387                                         shi->dxview= -R.viewdx*fac;
388                                         shi->dyview= -R.viewdy*fac;
389                                 }
390                         }
391                 }
392         }
393         
394         /* cannot normalize earlier, code above needs it at viewplane level */
395         Normalize(shi->view);
396 }
397
398 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
399 void shade_input_set_uv(ShadeInput *shi)
400 {
401         VlakRen *vlr= shi->vlr;
402         
403         if( (vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
404                 float *v1= shi->v1->co, *v2= shi->v2->co, *v3= shi->v3->co;
405                 
406                 /* exception case for wire render of edge */
407                 if(vlr->v2==vlr->v3) {
408                         float lend, lenc;
409                         
410                         lend= VecLenf(v2, v1);
411                         lenc= VecLenf(shi->co, v1);
412                         
413                         if(lend==0.0f) {
414                                 shi->u=shi->v= 0.0f;
415                         }
416                         else {
417                                 shi->u= - (1.0f - lenc/lend);
418                                 shi->v= 0.0f;
419                         }
420                         
421                         if(shi->osatex) {
422                                 shi->dx_u=  0.0f;
423                                 shi->dx_v=  0.0f;
424                                 shi->dy_u=  0.0f;
425                                 shi->dy_v=  0.0f;
426                         }
427                 }
428                 else {
429                         /* most of this could become re-used for faces */
430                         float detsh, t00, t10, t01, t11;
431                         
432                         if(vlr->noflag & R_SNPROJ_X) {
433                                 t00= v3[0]-v1[0]; t01= v3[1]-v1[1];
434                                 t10= v3[0]-v2[0]; t11= v3[1]-v2[1];
435                         }
436                         else if(vlr->noflag & R_SNPROJ_Y) {
437                                 t00= v3[0]-v1[0]; t01= v3[2]-v1[2];
438                                 t10= v3[0]-v2[0]; t11= v3[2]-v2[2];
439                         }
440                         else {
441                                 t00= v3[1]-v1[1]; t01= v3[2]-v1[2];
442                                 t10= v3[1]-v2[1]; t11= v3[2]-v2[2];
443                         }
444                         
445                         detsh= 1.0f/(t00*t11-t10*t01);
446                         t00*= detsh; t01*=detsh; 
447                         t10*=detsh; t11*=detsh;
448                         
449                         if(vlr->noflag & R_SNPROJ_X) {
450                                 shi->u= (shi->co[0]-v3[0])*t11-(shi->co[1]-v3[1])*t10;
451                                 shi->v= (shi->co[1]-v3[1])*t00-(shi->co[0]-v3[0])*t01;
452                                 if(shi->osatex) {
453                                         shi->dx_u=  shi->dxco[0]*t11- shi->dxco[1]*t10;
454                                         shi->dx_v=  shi->dxco[1]*t00- shi->dxco[0]*t01;
455                                         shi->dy_u=  shi->dyco[0]*t11- shi->dyco[1]*t10;
456                                         shi->dy_v=  shi->dyco[1]*t00- shi->dyco[0]*t01;
457                                 }
458                         }
459                         else if(vlr->noflag & R_SNPROJ_Y) {
460                                 shi->u= (shi->co[0]-v3[0])*t11-(shi->co[2]-v3[2])*t10;
461                                 shi->v= (shi->co[2]-v3[2])*t00-(shi->co[0]-v3[0])*t01;
462                                 if(shi->osatex) {
463                                         shi->dx_u=  shi->dxco[0]*t11- shi->dxco[2]*t10;
464                                         shi->dx_v=  shi->dxco[2]*t00- shi->dxco[0]*t01;
465                                         shi->dy_u=  shi->dyco[0]*t11- shi->dyco[2]*t10;
466                                         shi->dy_v=  shi->dyco[2]*t00- shi->dyco[0]*t01;
467                                 }
468                         }
469                         else {
470                                 shi->u= (shi->co[1]-v3[1])*t11-(shi->co[2]-v3[2])*t10;
471                                 shi->v= (shi->co[2]-v3[2])*t00-(shi->co[1]-v3[1])*t01;
472                                 if(shi->osatex) {
473                                         shi->dx_u=  shi->dxco[1]*t11- shi->dxco[2]*t10;
474                                         shi->dx_v=  shi->dxco[2]*t00- shi->dxco[1]*t01;
475                                         shi->dy_u=  shi->dyco[1]*t11- shi->dyco[2]*t10;
476                                         shi->dy_v=  shi->dyco[2]*t00- shi->dyco[1]*t01;
477                                 }
478                         }
479                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
480                         CLAMP(shi->u, -2.0f, 1.0f);
481                         CLAMP(shi->v, -2.0f, 1.0f);
482                 }
483         }       
484 }
485
486 void shade_input_set_normals(ShadeInput *shi)
487 {
488         float u= shi->u, v= shi->v;
489         float l= 1.0f+u+v;
490         
491         /* calculate vertexnormals */
492         if(shi->vlr->flag & R_SMOOTH) {
493                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
494                 
495                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
496                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
497                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
498                 
499                 Normalize(shi->vn);
500         }
501         else {
502                 VECCOPY(shi->vn, shi->facenor);
503         }
504         
505         /* used in nodes */
506         VECCOPY(shi->vno, shi->vn);
507
508 }
509
510 void shade_input_set_shade_texco(ShadeInput *shi)
511 {
512         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
513         float u= shi->u, v= shi->v;
514         float l= 1.0f+u+v, dl;
515         int mode= shi->mode;            /* or-ed result for all nodes */
516         short texco= shi->mat->texco;
517
518         /* calculate dxno and tangents */
519         if(shi->vlr->flag & R_SMOOTH) {
520                 
521                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
522                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
523                         
524                         dl= shi->dx_u+shi->dx_v;
525                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
526                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
527                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
528                         dl= shi->dy_u+shi->dy_v;
529                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
530                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
531                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
532                         
533                 }
534                 
535                 /* qdn: normalmap tangent space */
536                 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
537                         float *s1, *s2, *s3;
538                         
539                         s1= RE_vertren_get_tangent(&R, v1, 0);
540                         s2= RE_vertren_get_tangent(&R, v2, 0);
541                         s3= RE_vertren_get_tangent(&R, v3, 0);
542                         if(s1 && s2 && s3) {
543                                 shi->tang[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
544                                 shi->tang[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
545                                 shi->tang[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
546                                 /* qdn: normalize just in case */
547                                 Normalize(shi->tang);
548                         }
549                         else shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
550                 }
551         }
552         else {
553                 /* qdn: normalmap tangent space */
554                 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
555                         /* qdn: flat faces have tangents too,
556                            could pick either one, using average here */
557                         float *s1 = RE_vertren_get_tangent(&R, v1, 0);
558                         float *s2 = RE_vertren_get_tangent(&R, v2, 0);
559                         float *s3 = RE_vertren_get_tangent(&R, v3, 0);
560                         if (s1 && s2 && s3) {
561                                 shi->tang[0] = (s1[0] + s2[0] + s3[0]);
562                                 shi->tang[1] = (s1[1] + s2[1] + s3[1]);
563                                 shi->tang[2] = (s1[2] + s2[2] + s3[2]);
564                                 Normalize(shi->tang);
565                         }
566                 }
567         }
568         
569         if(R.r.mode & R_SPEED) {
570                 float *s1, *s2, *s3;
571                 
572                 s1= RE_vertren_get_winspeed(&R, v1, 0);
573                 s2= RE_vertren_get_winspeed(&R, v2, 0);
574                 s3= RE_vertren_get_winspeed(&R, v3, 0);
575                 if(s1 && s2 && s3) {
576                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
577                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
578                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
579                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
580                 }
581                 else {
582                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
583                 }
584         }
585
586         /* pass option forces UV calc */
587         if(shi->passflag & SCE_PASS_UV)
588                 texco |= (NEED_UV|TEXCO_UV);
589         
590         /* texture coordinates. shi->dxuv shi->dyuv have been set */
591         if(texco & NEED_UV) {
592                 
593                 if(texco & TEXCO_ORCO) {
594                         if(v1->orco) {
595                                 float *o1, *o2, *o3;
596                                 
597                                 o1= v1->orco;
598                                 o2= v2->orco;
599                                 o3= v3->orco;
600                                 
601                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
602                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
603                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
604                                 
605                                 if(shi->osatex) {
606                                         dl= shi->dx_u+shi->dx_v;
607                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
608                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
609                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
610                                         dl= shi->dy_u+shi->dy_v;
611                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
612                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
613                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
614                                 }
615                         }
616                 }
617                 
618                 if(texco & TEXCO_GLOB) {
619                         VECCOPY(shi->gl, shi->co);
620                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
621                         if(shi->osatex) {
622                                 VECCOPY(shi->dxgl, shi->dxco);
623                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
624                                 VECCOPY(shi->dygl, shi->dyco);
625                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
626                         }
627                 }
628                 
629                 if(texco & TEXCO_STRAND) {
630                         shi->strand= (l*v3->accum - u*v1->accum - v*v2->accum);
631                         if(shi->osatex) {
632                                 dl= shi->dx_u+shi->dx_v;
633                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
634                                 dl= shi->dy_u+shi->dy_v;
635                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
636                         }
637                 }
638                                 
639                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
640                         VlakRen *vlr= shi->vlr;
641                         MTFace *tface;
642                         MCol *mcol;
643                         char *name;
644                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
645
646                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
647                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
648
649                         shi->totuv= 0;
650                         shi->totcol= 0;
651
652                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
653                                 for (i=0; (mcol=RE_vlakren_get_mcol(&R, vlr, i, &name, 0)); i++) {
654                                         ShadeInputCol *scol= &shi->col[i];
655                                         char *cp1, *cp2, *cp3;
656                                         
657                                         shi->totcol++;
658                                         scol->name= name;
659
660                                         cp1= (char *)(mcol+j1);
661                                         cp2= (char *)(mcol+j2);
662                                         cp3= (char *)(mcol+j3);
663                                         
664                                         scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
665                                         scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
666                                         scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
667                                 }
668
669                                 if(shi->totcol) {
670                                         shi->vcol[0]= shi->col[0].col[0];
671                                         shi->vcol[1]= shi->col[0].col[1];
672                                         shi->vcol[2]= shi->col[0].col[2];
673                                 }
674                                 else {
675                                         shi->vcol[0]= 0.0f;
676                                         shi->vcol[1]= 0.0f;
677                                         shi->vcol[2]= 0.0f;
678                                 }
679                         }
680
681                         for (i=0; (tface=RE_vlakren_get_tface(&R, vlr, i, &name, 0)); i++) {
682                                 ShadeInputUV *suv= &shi->uv[i];
683                                 float *uv1, *uv2, *uv3;
684
685                                 shi->totuv++;
686                                 suv->name= name;
687                                 
688                                 uv1= tface->uv[j1];
689                                 uv2= tface->uv[j2];
690                                 uv3= tface->uv[j3];
691                                 
692                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
693                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
694                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
695
696                                 if(shi->osatex) {
697                                         float duv[2];
698                                         
699                                         dl= shi->dx_u+shi->dx_v;
700                                         duv[0]= shi->dx_u; 
701                                         duv[1]= shi->dx_v;
702                                         
703                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
704                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
705                                         
706                                         dl= shi->dy_u+shi->dy_v;
707                                         duv[0]= shi->dy_u; 
708                                         duv[1]= shi->dy_v;
709                                         
710                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
711                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
712                                 }
713
714                                 if((mode & MA_FACETEXTURE) && i==0) {
715                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
716                                                 shi->vcol[0]= 1.0f;
717                                                 shi->vcol[1]= 1.0f;
718                                                 shi->vcol[2]= 1.0f;
719                                         }
720                                         if(tface && tface->tpage)
721                                                 render_realtime_texture(shi, tface->tpage);
722                                 }
723                         }
724
725                         if(shi->totuv == 0) {
726                                 ShadeInputUV *suv= &shi->uv[0];
727
728                                 suv->uv[0]= 2.0f*(u+.5f);
729                                 suv->uv[1]= 2.0f*(v+.5f);
730                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
731                                 
732                                 if(mode & MA_FACETEXTURE) {
733                                         /* no tface? set at 1.0f */
734                                         shi->vcol[0]= 1.0f;
735                                         shi->vcol[1]= 1.0f;
736                                         shi->vcol[2]= 1.0f;
737                                 }
738                         }
739                 }
740                 
741                 if(texco & TEXCO_NORM) {
742                         shi->orn[0]= -shi->vn[0];
743                         shi->orn[1]= -shi->vn[1];
744                         shi->orn[2]= -shi->vn[2];
745                 }
746                 
747                 if(mode & MA_RADIO) {
748                         float *r1, *r2, *r3;
749                         
750                         r1= RE_vertren_get_rad(&R, v1, 0);
751                         r2= RE_vertren_get_rad(&R, v2, 0);
752                         r3= RE_vertren_get_rad(&R, v3, 0);
753                         
754                         if(r1 && r2 && r3) {
755                                 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
756                                 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
757                                 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
758                         }
759                         else {
760                                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
761                         }
762                 }
763                 else {
764                         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
765                 }
766                 
767                 if(texco & TEXCO_REFL) {
768                         /* mirror reflection color textures (and envmap) */
769                         calc_R_ref(shi);        /* wrong location for normal maps! XXXXXXXXXXXXXX */
770                 }
771                 
772                 if(texco & TEXCO_STRESS) {
773                         float *s1, *s2, *s3;
774                         
775                         s1= RE_vertren_get_stress(&R, v1, 0);
776                         s2= RE_vertren_get_stress(&R, v2, 0);
777                         s3= RE_vertren_get_stress(&R, v3, 0);
778                         if(s1 && s2 && s3) {
779                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
780                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
781                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
782                         }
783                         else shi->stress= 0.0f;
784                 }
785                 
786                 if(texco & TEXCO_TANGENT) {
787                         if((mode & MA_TANGENT_V)==0) {
788                                 /* just prevent surprises */
789                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
790                         }
791                 }
792         }
793         else {
794                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
795         }
796         
797         /* this only avalailable for scanline renders */
798         if(shi->depth==0) {
799                 float x= shi->xs;
800                 float y= shi->ys;
801                 
802                 if(texco & TEXCO_WINDOW) {
803                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
804                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
805                         shi->winco[2]= 0.0f;
806                         if(shi->osatex) {
807                                 shi->dxwin[0]= 2.0f/(float)R.winx;
808                                 shi->dywin[1]= 2.0f/(float)R.winy;
809                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
810                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
811                         }
812                 }
813
814                 if(texco & TEXCO_STICKY) {
815                         float *s1, *s2, *s3;
816                         
817                         s1= RE_vertren_get_sticky(&R, v1, 0);
818                         s2= RE_vertren_get_sticky(&R, v2, 0);
819                         s3= RE_vertren_get_sticky(&R, v3, 0);
820                         
821                         if(s1 && s2 && s3) {
822                                 float Zmulx, Zmuly;
823                                 float hox, hoy, l, dl, u, v;
824                                 float s00, s01, s10, s11, detsh;
825                                 
826                                 /* old globals, localized now */
827                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
828                                 
829                                 s00= v3->ho[0]/v3->ho[3] - v1->ho[0]/v1->ho[3];
830                                 s01= v3->ho[1]/v3->ho[3] - v1->ho[1]/v1->ho[3];
831                                 s10= v3->ho[0]/v3->ho[3] - v2->ho[0]/v2->ho[3];
832                                 s11= v3->ho[1]/v3->ho[3] - v2->ho[1]/v2->ho[3];
833                                 
834                                 detsh= s00*s11-s10*s01;
835                                 s00/= detsh; s01/=detsh; 
836                                 s10/=detsh; s11/=detsh;
837                                 
838                                 /* recalc u and v again */
839                                 hox= x/Zmulx -1.0f;
840                                 hoy= y/Zmuly -1.0f;
841                                 u= (hox - v3->ho[0]/v3->ho[3])*s11 - (hoy - v3->ho[1]/v3->ho[3])*s10;
842                                 v= (hoy - v3->ho[1]/v3->ho[3])*s00 - (hox - v3->ho[0]/v3->ho[3])*s01;
843                                 l= 1.0f+u+v;
844                                 
845                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
846                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
847                                 shi->sticky[2]= 0.0f;
848                                 
849                                 if(shi->osatex) {
850                                         float dxuv[2], dyuv[2];
851                                         dxuv[0]=  s11/Zmulx;
852                                         dxuv[1]=  - s01/Zmulx;
853                                         dyuv[0]=  - s10/Zmuly;
854                                         dyuv[1]=  s00/Zmuly;
855                                         
856                                         dl= dxuv[0] + dxuv[1];
857                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
858                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
859                                         dl= dyuv[0] + dyuv[1];
860                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
861                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
862                                 }
863                         }
864                 }
865         }       
866 }
867
868 /* ****************** ShadeSample ************************************** */
869
870 /* initialize per part, not per pixel! */
871 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
872 {
873         
874         memset(shi, 0, sizeof(ShadeInput));
875         
876         shi->sample= sample;
877         shi->thread= pa->thread;
878         shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
879         shi->lay= rl->lay;
880         shi->layflag= rl->layflag;
881         shi->passflag= rl->passflag;
882         shi->combinedflag= ~rl->pass_xor;
883         shi->mat_override= rl->mat_override;
884         shi->light_override= rl->light_override;
885         
886         /* note shi.depth==0  means first hit, not raytracing */
887 }
888
889 /* initialize per part, not per pixel! */
890 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
891 {
892         int a, tot;
893         
894         tot= R.osa==0?1:R.osa;
895         
896         for(a=0; a<tot; a++) {
897                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
898                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
899         }
900         
901         ssamp->samplenr= 0; /* counter, detect shadow-reuse for shaders */
902 }
903
904 /* Do AO or (future) GI */
905 void shade_samples_do_AO(ShadeSample *ssamp)
906 {
907         ShadeInput *shi;
908         int sample;
909         
910         if(!(R.r.mode & R_SHADOW))
911                 return;
912         if(!(R.r.mode & R_RAYTRACE))
913                 return;
914         
915         if(R.wrld.mode & WO_AMB_OCC)
916                 if(ssamp->shi[0].passflag & (SCE_PASS_COMBINED|SCE_PASS_AO))
917                         for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
918                                 if(!(shi->mode & MA_SHLESS))
919                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
920                 
921 }
922
923
924 static void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
925 {
926         ShadeInput *shi;
927         float xs, ys;
928         
929         ssamp->tot= 0;
930         
931         for(shi= ssamp->shi; ps; ps= ps->next) {
932                 shade_input_set_triangle(shi, ps->facenr, 1);
933                 
934                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
935                         unsigned short curmask= ps->mask;
936                         
937                         /* full osa is only set for OSA renders */
938                         if(shi->vlr->flag & R_FULL_OSA) {
939                                 short shi_cp= 0, samp;
940                                 
941                                 for(samp=0; samp<R.osa; samp++) {
942                                         if(curmask & (1<<samp)) {
943                                                 xs= (float)x + R.jit[samp][0] + 0.5f;   /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
944                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
945                                                 
946                                                 if(shi_cp)
947                                                         shade_input_copy_triangle(shi, shi-1);
948                                                 
949                                                 shi->mask= (1<<samp);
950                                                 shi->samplenr= ssamp->samplenr++;
951                                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
952                                                 shade_input_set_uv(shi);
953                                                 shade_input_set_normals(shi);
954                                                 
955                                                 shi_cp= 1;
956                                                 shi++;
957                                         }
958                                 }
959                         }
960                         else {
961                                 if(R.osa) {
962                                         short b= R.samples->centmask[curmask];
963                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
964                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
965                                 }
966                                 else {
967                                         xs= (float)x + 0.5f;
968                                         ys= (float)y + 0.5f;
969                                 }
970                                 shi->mask= curmask;
971                                 shi->samplenr= ssamp->samplenr++;
972                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
973                                 shade_input_set_uv(shi);
974                                 shade_input_set_normals(shi);
975                                 shi++;
976                         }
977                         
978                         /* total sample amount, shi->sample is static set in initialize */
979                         if(shi!=ssamp->shi)
980                                 ssamp->tot= (shi-1)->sample + 1;
981                 }
982         }
983 }
984
985 /* shades samples, returns true if anything happened */
986 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
987 {
988         shade_samples_fill_with_ps(ssamp, ps, x, y);
989         
990         if(ssamp->tot) {
991                 ShadeInput *shi= ssamp->shi;
992                 ShadeResult *shr= ssamp->shr;
993                 int samp;
994                 
995                 /* if shadow or AO? */
996                 shade_samples_do_AO(ssamp);
997                 
998                 /* if shade (all shadepinputs have same passflag) */
999                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1000
1001                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1002                                 shade_input_set_shade_texco(shi);
1003                                 shade_input_do_shade(shi, shr);
1004                         }
1005                 }
1006                 
1007                 return 1;
1008         }
1009         return 0;
1010 }
1011