* New material option: TexFace Alpha
[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         /* copy of original pre-flipped normal, for geometry->front/back node output */
263         VECCOPY(shi->orignor, vlr->n);
264         if (vlr->noflag & R_FLIPPED_NO) {
265                 VECMUL(shi->orignor, -1.0f);
266         }
267 }
268
269 /* note, facenr declared volatile due to over-eager -O2 optimizations
270  * on cygwin (particularly -frerun-cse-after-loop)
271  */
272
273 /* copy data from face to ShadeInput, scanline case */
274 void shade_input_set_triangle(ShadeInput *shi, volatile int facenr, int normal_flip)
275 {
276         if(facenr>0) {
277                 shi->facenr= (facenr-1) & RE_QUAD_MASK;
278                 if( shi->facenr < R.totvlak ) {
279                         VlakRen *vlr= RE_findOrAddVlak(&R, shi->facenr);
280                         
281                         shi->puno= normal_flip?vlr->puno:0;
282                         
283                         if(facenr & RE_QUAD_OFFS)
284                                 shade_input_set_triangle_i(shi, vlr, 0, 2, 3);
285                         else
286                                 shade_input_set_triangle_i(shi, vlr, 0, 1, 2);
287                 }
288                 else
289                         shi->vlr= NULL; /* general signal we got sky */
290         }
291         else
292                 shi->vlr= NULL; /* general signal we got sky */
293         
294 }
295
296 /* full osa case: copy static info */
297 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
298 {
299         /* not so nice, but works... warning is in RE_shader_ext.h */
300         memcpy(shi, from, sizeof(struct ShadeInputCopy));
301 }
302
303
304 /* scanline pixel coordinates */
305 /* requires set_triangle */
306 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float z)
307 {
308         float fac;
309         
310         /* currently in use for dithering (soft shadow), node preview, irregular shad */
311         shi->xs= (int)(x);
312         shi->ys= (int)(y);
313         
314         calc_view_vector(shi->view, x, y);      /* returns not normalized, so is in viewplane coords */
315         
316         /* wire cannot use normal for calculating shi->co */
317         if(shi->mat->mode & MA_WIRE) {
318                 
319                 if(R.r.mode & R_ORTHO)
320                         calc_renderco_ortho(shi->co, x, y, z);
321                 else
322                         calc_renderco_zbuf(shi->co, shi->view, z);
323         }
324         else {
325                 float dface, *v1= shi->v1->co;
326                 
327                 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
328                 
329                 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
330                 if(R.r.mode & R_ORTHO) {
331                         /* x and y 3d coordinate can be derived from pixel coord and winmat */
332                         float fx= 2.0f/(R.winx*R.winmat[0][0]);
333                         float fy= 2.0f/(R.winy*R.winmat[1][1]);
334                         
335                         shi->co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
336                         shi->co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
337                         
338                         /* using a*x + b*y + c*z = d equation, (a b c) is normal */
339                         if(shi->facenor[2]!=0.0f)
340                                 shi->co[2]= (dface - shi->facenor[0]*shi->co[0] - shi->facenor[1]*shi->co[1])/shi->facenor[2];
341                         else
342                                 shi->co[2]= 0.0f;
343                         
344                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
345                                 shi->dxco[0]= fx;
346                                 shi->dxco[1]= 0.0f;
347                                 if(shi->facenor[2]!=0.0f)
348                                         shi->dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
349                                 else 
350                                         shi->dxco[2]= 0.0f;
351                                 
352                                 shi->dyco[0]= 0.0f;
353                                 shi->dyco[1]= fy;
354                                 if(shi->facenor[2]!=0.0f)
355                                         shi->dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
356                                 else 
357                                         shi->dyco[2]= 0.0f;
358                                 
359                                 if( (shi->mat->texco & TEXCO_REFL) ) {
360                                         if(shi->co[2]!=0.0f) fac= 1.0f/shi->co[2]; else fac= 0.0f;
361                                         shi->dxview= -R.viewdx*fac;
362                                         shi->dyview= -R.viewdy*fac;
363                                 }
364                         }
365                 }
366                 else {
367                         float div;
368                         
369                         div= shi->facenor[0]*shi->view[0] + shi->facenor[1]*shi->view[1] + shi->facenor[2]*shi->view[2];
370                         if (div!=0.0f) fac= dface/div;
371                         else fac= 0.0f;
372                         
373                         shi->co[0]= fac*shi->view[0];
374                         shi->co[1]= fac*shi->view[1];
375                         shi->co[2]= fac*shi->view[2];
376                         
377                         /* pixel dx/dy for render coord */
378                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
379                                 float u= dface/(div - R.viewdx*shi->facenor[0]);
380                                 float v= dface/(div - R.viewdy*shi->facenor[1]);
381                                 
382                                 shi->dxco[0]= shi->co[0]- (shi->view[0]-R.viewdx)*u;
383                                 shi->dxco[1]= shi->co[1]- (shi->view[1])*u;
384                                 shi->dxco[2]= shi->co[2]- (shi->view[2])*u;
385                                 
386                                 shi->dyco[0]= shi->co[0]- (shi->view[0])*v;
387                                 shi->dyco[1]= shi->co[1]- (shi->view[1]-R.viewdy)*v;
388                                 shi->dyco[2]= shi->co[2]- (shi->view[2])*v;
389                                 
390                                 if( (shi->mat->texco & TEXCO_REFL) ) {
391                                         if(fac!=0.0f) fac= 1.0f/fac;
392                                         shi->dxview= -R.viewdx*fac;
393                                         shi->dyview= -R.viewdy*fac;
394                                 }
395                         }
396                 }
397         }
398         
399         /* cannot normalize earlier, code above needs it at viewplane level */
400         Normalize(shi->view);
401 }
402
403 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
404 void shade_input_set_uv(ShadeInput *shi)
405 {
406         VlakRen *vlr= shi->vlr;
407         
408         if( (vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
409                 float *v1= shi->v1->co, *v2= shi->v2->co, *v3= shi->v3->co;
410                 
411                 /* exception case for wire render of edge */
412                 if(vlr->v2==vlr->v3) {
413                         float lend, lenc;
414                         
415                         lend= VecLenf(v2, v1);
416                         lenc= VecLenf(shi->co, v1);
417                         
418                         if(lend==0.0f) {
419                                 shi->u=shi->v= 0.0f;
420                         }
421                         else {
422                                 shi->u= - (1.0f - lenc/lend);
423                                 shi->v= 0.0f;
424                         }
425                         
426                         if(shi->osatex) {
427                                 shi->dx_u=  0.0f;
428                                 shi->dx_v=  0.0f;
429                                 shi->dy_u=  0.0f;
430                                 shi->dy_v=  0.0f;
431                         }
432                 }
433                 else {
434                         /* most of this could become re-used for faces */
435                         float detsh, t00, t10, t01, t11;
436                         
437                         if(vlr->noflag & R_SNPROJ_X) {
438                                 t00= v3[0]-v1[0]; t01= v3[1]-v1[1];
439                                 t10= v3[0]-v2[0]; t11= v3[1]-v2[1];
440                         }
441                         else if(vlr->noflag & R_SNPROJ_Y) {
442                                 t00= v3[0]-v1[0]; t01= v3[2]-v1[2];
443                                 t10= v3[0]-v2[0]; t11= v3[2]-v2[2];
444                         }
445                         else {
446                                 t00= v3[1]-v1[1]; t01= v3[2]-v1[2];
447                                 t10= v3[1]-v2[1]; t11= v3[2]-v2[2];
448                         }
449                         
450                         detsh= 1.0f/(t00*t11-t10*t01);
451                         t00*= detsh; t01*=detsh; 
452                         t10*=detsh; t11*=detsh;
453                         
454                         if(vlr->noflag & R_SNPROJ_X) {
455                                 shi->u= (shi->co[0]-v3[0])*t11-(shi->co[1]-v3[1])*t10;
456                                 shi->v= (shi->co[1]-v3[1])*t00-(shi->co[0]-v3[0])*t01;
457                                 if(shi->osatex) {
458                                         shi->dx_u=  shi->dxco[0]*t11- shi->dxco[1]*t10;
459                                         shi->dx_v=  shi->dxco[1]*t00- shi->dxco[0]*t01;
460                                         shi->dy_u=  shi->dyco[0]*t11- shi->dyco[1]*t10;
461                                         shi->dy_v=  shi->dyco[1]*t00- shi->dyco[0]*t01;
462                                 }
463                         }
464                         else if(vlr->noflag & R_SNPROJ_Y) {
465                                 shi->u= (shi->co[0]-v3[0])*t11-(shi->co[2]-v3[2])*t10;
466                                 shi->v= (shi->co[2]-v3[2])*t00-(shi->co[0]-v3[0])*t01;
467                                 if(shi->osatex) {
468                                         shi->dx_u=  shi->dxco[0]*t11- shi->dxco[2]*t10;
469                                         shi->dx_v=  shi->dxco[2]*t00- shi->dxco[0]*t01;
470                                         shi->dy_u=  shi->dyco[0]*t11- shi->dyco[2]*t10;
471                                         shi->dy_v=  shi->dyco[2]*t00- shi->dyco[0]*t01;
472                                 }
473                         }
474                         else {
475                                 shi->u= (shi->co[1]-v3[1])*t11-(shi->co[2]-v3[2])*t10;
476                                 shi->v= (shi->co[2]-v3[2])*t00-(shi->co[1]-v3[1])*t01;
477                                 if(shi->osatex) {
478                                         shi->dx_u=  shi->dxco[1]*t11- shi->dxco[2]*t10;
479                                         shi->dx_v=  shi->dxco[2]*t00- shi->dxco[1]*t01;
480                                         shi->dy_u=  shi->dyco[1]*t11- shi->dyco[2]*t10;
481                                         shi->dy_v=  shi->dyco[2]*t00- shi->dyco[1]*t01;
482                                 }
483                         }
484                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
485                         CLAMP(shi->u, -2.0f, 1.0f);
486                         CLAMP(shi->v, -2.0f, 1.0f);
487                 }
488         }       
489 }
490
491 void shade_input_set_normals(ShadeInput *shi)
492 {
493         float u= shi->u, v= shi->v;
494         float l= 1.0f+u+v;
495         
496         /* calculate vertexnormals */
497         if(shi->vlr->flag & R_SMOOTH) {
498                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
499                 
500                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
501                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
502                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
503                 
504                 Normalize(shi->vn);
505         }
506         else {
507                 VECCOPY(shi->vn, shi->facenor);
508         }
509         
510         /* used in nodes */
511         VECCOPY(shi->vno, shi->vn);
512
513 }
514
515 void shade_input_set_shade_texco(ShadeInput *shi)
516 {
517         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
518         float u= shi->u, v= shi->v;
519         float l= 1.0f+u+v, dl;
520         int mode= shi->mode;            /* or-ed result for all nodes */
521         short texco= shi->mat->texco;
522
523         /* calculate dxno and tangents */
524         if(shi->vlr->flag & R_SMOOTH) {
525                 
526                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
527                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
528                         
529                         dl= shi->dx_u+shi->dx_v;
530                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
531                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
532                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
533                         dl= shi->dy_u+shi->dy_v;
534                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
535                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
536                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
537                         
538                 }
539                 
540                 /* qdn: normalmap tangent space */
541                 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
542                         float *s1, *s2, *s3;
543                         
544                         s1= RE_vertren_get_tangent(&R, v1, 0);
545                         s2= RE_vertren_get_tangent(&R, v2, 0);
546                         s3= RE_vertren_get_tangent(&R, v3, 0);
547                         if(s1 && s2 && s3) {
548                                 shi->tang[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
549                                 shi->tang[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
550                                 shi->tang[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
551                                 /* qdn: normalize just in case */
552                                 Normalize(shi->tang);
553                         }
554                         else shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
555                 }
556         }
557         else {
558                 /* qdn: normalmap tangent space */
559                 if (mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
560                         /* qdn: flat faces have tangents too,
561                            could pick either one, using average here */
562                         float *s1 = RE_vertren_get_tangent(&R, v1, 0);
563                         float *s2 = RE_vertren_get_tangent(&R, v2, 0);
564                         float *s3 = RE_vertren_get_tangent(&R, v3, 0);
565                         if (s1 && s2 && s3) {
566                                 shi->tang[0] = (s1[0] + s2[0] + s3[0]);
567                                 shi->tang[1] = (s1[1] + s2[1] + s3[1]);
568                                 shi->tang[2] = (s1[2] + s2[2] + s3[2]);
569                                 Normalize(shi->tang);
570                         }
571                 }
572         }
573         
574         if(R.r.mode & R_SPEED) {
575                 float *s1, *s2, *s3;
576                 
577                 s1= RE_vertren_get_winspeed(&R, v1, 0);
578                 s2= RE_vertren_get_winspeed(&R, v2, 0);
579                 s3= RE_vertren_get_winspeed(&R, v3, 0);
580                 if(s1 && s2 && s3) {
581                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
582                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
583                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
584                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
585                 }
586                 else {
587                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
588                 }
589         }
590
591         /* pass option forces UV calc */
592         if(shi->passflag & SCE_PASS_UV)
593                 texco |= (NEED_UV|TEXCO_UV);
594         
595         /* texture coordinates. shi->dxuv shi->dyuv have been set */
596         if(texco & NEED_UV) {
597                 
598                 if(texco & TEXCO_ORCO) {
599                         if(v1->orco) {
600                                 float *o1, *o2, *o3;
601                                 
602                                 o1= v1->orco;
603                                 o2= v2->orco;
604                                 o3= v3->orco;
605                                 
606                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
607                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
608                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
609                                 
610                                 if(shi->osatex) {
611                                         dl= shi->dx_u+shi->dx_v;
612                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
613                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
614                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
615                                         dl= shi->dy_u+shi->dy_v;
616                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
617                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
618                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
619                                 }
620                         }
621                 }
622                 
623                 if(texco & TEXCO_GLOB) {
624                         VECCOPY(shi->gl, shi->co);
625                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
626                         if(shi->osatex) {
627                                 VECCOPY(shi->dxgl, shi->dxco);
628                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
629                                 VECCOPY(shi->dygl, shi->dyco);
630                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
631                         }
632                 }
633                 
634                 if(texco & TEXCO_STRAND) {
635                         shi->strand= (l*v3->accum - u*v1->accum - v*v2->accum);
636                         if(shi->osatex) {
637                                 dl= shi->dx_u+shi->dx_v;
638                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
639                                 dl= shi->dy_u+shi->dy_v;
640                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
641                         }
642                 }
643                                 
644                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
645                         VlakRen *vlr= shi->vlr;
646                         MTFace *tface;
647                         MCol *mcol;
648                         char *name;
649                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
650
651                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
652                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
653
654                         shi->totuv= 0;
655                         shi->totcol= 0;
656
657                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
658                                 for (i=0; (mcol=RE_vlakren_get_mcol(&R, vlr, i, &name, 0)); i++) {
659                                         ShadeInputCol *scol= &shi->col[i];
660                                         char *cp1, *cp2, *cp3;
661                                         
662                                         shi->totcol++;
663                                         scol->name= name;
664
665                                         cp1= (char *)(mcol+j1);
666                                         cp2= (char *)(mcol+j2);
667                                         cp3= (char *)(mcol+j3);
668                                         
669                                         scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
670                                         scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
671                                         scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
672                                 }
673
674                                 if(shi->totcol) {
675                                         shi->vcol[0]= shi->col[0].col[0];
676                                         shi->vcol[1]= shi->col[0].col[1];
677                                         shi->vcol[2]= shi->col[0].col[2];
678                                         shi->vcol[3]= 1.0f;
679                                 }
680                                 else {
681                                         shi->vcol[0]= 0.0f;
682                                         shi->vcol[1]= 0.0f;
683                                         shi->vcol[2]= 0.0f;
684                                         shi->vcol[3]= 1.0f;
685                                 }
686                         }
687
688                         for (i=0; (tface=RE_vlakren_get_tface(&R, vlr, i, &name, 0)); i++) {
689                                 ShadeInputUV *suv= &shi->uv[i];
690                                 float *uv1, *uv2, *uv3;
691
692                                 shi->totuv++;
693                                 suv->name= name;
694                                 
695                                 uv1= tface->uv[j1];
696                                 uv2= tface->uv[j2];
697                                 uv3= tface->uv[j3];
698                                 
699                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
700                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
701                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
702
703                                 if(shi->osatex) {
704                                         float duv[2];
705                                         
706                                         dl= shi->dx_u+shi->dx_v;
707                                         duv[0]= shi->dx_u; 
708                                         duv[1]= shi->dx_v;
709                                         
710                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
711                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
712                                         
713                                         dl= shi->dy_u+shi->dy_v;
714                                         duv[0]= shi->dy_u; 
715                                         duv[1]= shi->dy_v;
716                                         
717                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
718                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
719                                 }
720
721                                 if((mode & MA_FACETEXTURE) && i==0) {
722                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
723                                                 shi->vcol[0]= 1.0f;
724                                                 shi->vcol[1]= 1.0f;
725                                                 shi->vcol[2]= 1.0f;
726                                                 shi->vcol[3]= 1.0f;
727                                         }
728                                         if(tface && tface->tpage)
729                                                 render_realtime_texture(shi, tface->tpage);
730                                 }
731                         }
732
733                         if(shi->totuv == 0) {
734                                 ShadeInputUV *suv= &shi->uv[0];
735
736                                 suv->uv[0]= 2.0f*(u+.5f);
737                                 suv->uv[1]= 2.0f*(v+.5f);
738                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
739                                 
740                                 if(mode & MA_FACETEXTURE) {
741                                         /* no tface? set at 1.0f */
742                                         shi->vcol[0]= 1.0f;
743                                         shi->vcol[1]= 1.0f;
744                                         shi->vcol[2]= 1.0f;
745                                         shi->vcol[3]= 1.0f;
746                                 }
747                         }
748                 }
749                 
750                 if(texco & TEXCO_NORM) {
751                         shi->orn[0]= -shi->vn[0];
752                         shi->orn[1]= -shi->vn[1];
753                         shi->orn[2]= -shi->vn[2];
754                 }
755                 
756                 if(mode & MA_RADIO) {
757                         float *r1, *r2, *r3;
758                         
759                         r1= RE_vertren_get_rad(&R, v1, 0);
760                         r2= RE_vertren_get_rad(&R, v2, 0);
761                         r3= RE_vertren_get_rad(&R, v3, 0);
762                         
763                         if(r1 && r2 && r3) {
764                                 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
765                                 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
766                                 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
767                         }
768                         else {
769                                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
770                         }
771                 }
772                 else {
773                         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
774                 }
775                 
776                 if(texco & TEXCO_REFL) {
777                         /* mirror reflection color textures (and envmap) */
778                         calc_R_ref(shi);        /* wrong location for normal maps! XXXXXXXXXXXXXX */
779                 }
780                 
781                 if(texco & TEXCO_STRESS) {
782                         float *s1, *s2, *s3;
783                         
784                         s1= RE_vertren_get_stress(&R, v1, 0);
785                         s2= RE_vertren_get_stress(&R, v2, 0);
786                         s3= RE_vertren_get_stress(&R, v3, 0);
787                         if(s1 && s2 && s3) {
788                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
789                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
790                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
791                         }
792                         else shi->stress= 0.0f;
793                 }
794                 
795                 if(texco & TEXCO_TANGENT) {
796                         if((mode & MA_TANGENT_V)==0) {
797                                 /* just prevent surprises */
798                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
799                         }
800                 }
801         }
802         else {
803                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
804         }
805         
806         /* this only avalailable for scanline renders */
807         if(shi->depth==0) {
808                 float x= shi->xs;
809                 float y= shi->ys;
810                 
811                 if(texco & TEXCO_WINDOW) {
812                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
813                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
814                         shi->winco[2]= 0.0f;
815                         if(shi->osatex) {
816                                 shi->dxwin[0]= 2.0f/(float)R.winx;
817                                 shi->dywin[1]= 2.0f/(float)R.winy;
818                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
819                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
820                         }
821                 }
822
823                 if(texco & TEXCO_STICKY) {
824                         float *s1, *s2, *s3;
825                         
826                         s1= RE_vertren_get_sticky(&R, v1, 0);
827                         s2= RE_vertren_get_sticky(&R, v2, 0);
828                         s3= RE_vertren_get_sticky(&R, v3, 0);
829                         
830                         if(s1 && s2 && s3) {
831                                 float Zmulx, Zmuly;
832                                 float hox, hoy, l, dl, u, v;
833                                 float s00, s01, s10, s11, detsh;
834                                 
835                                 /* old globals, localized now */
836                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
837                                 
838                                 s00= v3->ho[0]/v3->ho[3] - v1->ho[0]/v1->ho[3];
839                                 s01= v3->ho[1]/v3->ho[3] - v1->ho[1]/v1->ho[3];
840                                 s10= v3->ho[0]/v3->ho[3] - v2->ho[0]/v2->ho[3];
841                                 s11= v3->ho[1]/v3->ho[3] - v2->ho[1]/v2->ho[3];
842                                 
843                                 detsh= s00*s11-s10*s01;
844                                 s00/= detsh; s01/=detsh; 
845                                 s10/=detsh; s11/=detsh;
846                                 
847                                 /* recalc u and v again */
848                                 hox= x/Zmulx -1.0f;
849                                 hoy= y/Zmuly -1.0f;
850                                 u= (hox - v3->ho[0]/v3->ho[3])*s11 - (hoy - v3->ho[1]/v3->ho[3])*s10;
851                                 v= (hoy - v3->ho[1]/v3->ho[3])*s00 - (hox - v3->ho[0]/v3->ho[3])*s01;
852                                 l= 1.0f+u+v;
853                                 
854                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
855                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
856                                 shi->sticky[2]= 0.0f;
857                                 
858                                 if(shi->osatex) {
859                                         float dxuv[2], dyuv[2];
860                                         dxuv[0]=  s11/Zmulx;
861                                         dxuv[1]=  - s01/Zmulx;
862                                         dyuv[0]=  - s10/Zmuly;
863                                         dyuv[1]=  s00/Zmuly;
864                                         
865                                         dl= dxuv[0] + dxuv[1];
866                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
867                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
868                                         dl= dyuv[0] + dyuv[1];
869                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
870                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
871                                 }
872                         }
873                 }
874         }       
875 }
876
877 /* ****************** ShadeSample ************************************** */
878
879 /* initialize per part, not per pixel! */
880 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
881 {
882         
883         memset(shi, 0, sizeof(ShadeInput));
884         
885         shi->sample= sample;
886         shi->thread= pa->thread;
887         shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
888         shi->lay= rl->lay;
889         shi->layflag= rl->layflag;
890         shi->passflag= rl->passflag;
891         shi->combinedflag= ~rl->pass_xor;
892         shi->mat_override= rl->mat_override;
893         shi->light_override= rl->light_override;
894         
895         /* note shi.depth==0  means first hit, not raytracing */
896 }
897
898 /* initialize per part, not per pixel! */
899 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
900 {
901         int a, tot;
902         
903         tot= R.osa==0?1:R.osa;
904         
905         for(a=0; a<tot; a++) {
906                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
907                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
908         }
909         
910         ssamp->samplenr= 0; /* counter, detect shadow-reuse for shaders */
911 }
912
913 /* Do AO or (future) GI */
914 void shade_samples_do_AO(ShadeSample *ssamp)
915 {
916         ShadeInput *shi;
917         int sample;
918         
919         if(!(R.r.mode & R_SHADOW))
920                 return;
921         if(!(R.r.mode & R_RAYTRACE))
922                 return;
923         
924         if(R.wrld.mode & WO_AMB_OCC)
925                 if(ssamp->shi[0].passflag & (SCE_PASS_COMBINED|SCE_PASS_AO))
926                         for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
927                                 if(!(shi->mode & MA_SHLESS))
928                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
929                 
930 }
931
932
933 static void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
934 {
935         ShadeInput *shi;
936         float xs, ys;
937         
938         ssamp->tot= 0;
939         
940         for(shi= ssamp->shi; ps; ps= ps->next) {
941                 shade_input_set_triangle(shi, ps->facenr, 1);
942                 
943                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
944                         unsigned short curmask= ps->mask;
945                         
946                         /* full osa is only set for OSA renders */
947                         if(shi->vlr->flag & R_FULL_OSA) {
948                                 short shi_cp= 0, samp;
949                                 
950                                 for(samp=0; samp<R.osa; samp++) {
951                                         if(curmask & (1<<samp)) {
952                                                 xs= (float)x + R.jit[samp][0] + 0.5f;   /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
953                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
954                                                 
955                                                 if(shi_cp)
956                                                         shade_input_copy_triangle(shi, shi-1);
957                                                 
958                                                 shi->mask= (1<<samp);
959                                                 shi->samplenr= ssamp->samplenr++;
960                                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
961                                                 shade_input_set_uv(shi);
962                                                 shade_input_set_normals(shi);
963                                                 
964                                                 shi_cp= 1;
965                                                 shi++;
966                                         }
967                                 }
968                         }
969                         else {
970                                 if(R.osa) {
971                                         short b= R.samples->centmask[curmask];
972                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
973                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
974                                 }
975                                 else {
976                                         xs= (float)x + 0.5f;
977                                         ys= (float)y + 0.5f;
978                                 }
979                                 shi->mask= curmask;
980                                 shi->samplenr= ssamp->samplenr++;
981                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
982                                 shade_input_set_uv(shi);
983                                 shade_input_set_normals(shi);
984                                 shi++;
985                         }
986                         
987                         /* total sample amount, shi->sample is static set in initialize */
988                         if(shi!=ssamp->shi)
989                                 ssamp->tot= (shi-1)->sample + 1;
990                 }
991         }
992 }
993
994 /* shades samples, returns true if anything happened */
995 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
996 {
997         shade_samples_fill_with_ps(ssamp, ps, x, y);
998         
999         if(ssamp->tot) {
1000                 ShadeInput *shi= ssamp->shi;
1001                 ShadeResult *shr= ssamp->shr;
1002                 int samp;
1003                 
1004                 /* if shadow or AO? */
1005                 shade_samples_do_AO(ssamp);
1006                 
1007                 /* if shade (all shadepinputs have same passflag) */
1008                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1009
1010                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1011                                 shade_input_set_shade_texco(shi);
1012                                 shade_input_do_shade(shi, shr);
1013                         }
1014                 }
1015                 
1016                 return 1;
1017         }
1018         return 0;
1019 }
1020