Fix for bug #13363: ray (qmc) shadows had some light leaking issues,
[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 #include "BLI_blenlib.h"
35
36 #include "DNA_curve_types.h"
37 #include "DNA_group_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_material_types.h"
41
42 #include "BKE_utildefines.h"
43 #include "BKE_node.h"
44
45 /* local include */
46 #include "renderpipeline.h"
47 #include "render_types.h"
48 #include "renderdatabase.h"
49 #include "rendercore.h"
50 #include "shadbuf.h"
51 #include "shading.h"
52 #include "strand.h"
53 #include "texture.h"
54 #include "zbuf.h"
55
56 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
57 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
58 /* only to be used here in this file, it's for speed */
59 extern struct Render R;
60 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
61
62
63 #define VECADDISFAC(v1,v3,fac) {*(v1)+= *(v3)*(fac); *(v1+1)+= *(v3+1)*(fac); *(v1+2)+= *(v3+2)*(fac);}
64
65
66
67 /* Shade Sample order:
68
69 - shade_samples_fill_with_ps()
70         - for each sample
71                 - shade_input_set_triangle()  <- if prev sample-face is same, use shade_input_copy_triangle()
72                 - if vlr
73                         - shade_input_set_viewco()    <- not for ray or bake
74                         - shade_input_set_uv()        <- not for ray or bake
75                         - shade_input_set_normals()
76 - shade_samples()
77         - if AO
78                 - shade_samples_do_AO()
79         - if shading happens
80                 - for each sample
81                         - shade_input_set_shade_texco()
82                         - shade_samples_do_shade()
83 - OSA: distribute sample result with filter masking
84
85         */
86
87
88 /* also used as callback for nodes */
89 /* delivers a fully filled in ShadeResult, for all passes */
90 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
91 {
92         
93         shade_lamp_loop(shi, shr);      /* clears shr */
94         
95         if(shi->translucency!=0.0f) {
96                 ShadeResult shr_t;
97                 float fac= shi->translucency;
98                 
99                 /* gotta copy it again */
100                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
101                 shi->har= shi->mat->har;
102
103                 VECCOPY(shi->vn, shi->vno);
104                 VECMUL(shi->vn, -1.0f);
105                 VECMUL(shi->facenor, -1.0f);
106                 shi->depth++;   /* hack to get real shadow now */
107                 shade_lamp_loop(shi, &shr_t);
108                 shi->depth--;
109
110                 /* a couple of passes */
111                 VECADDISFAC(shr->combined, shr_t.combined, fac);
112                 if(shi->passflag & SCE_PASS_SPEC)
113                         VECADDISFAC(shr->spec, shr_t.spec, fac);
114                 if(shi->passflag & SCE_PASS_DIFFUSE)
115                         VECADDISFAC(shr->diff, shr_t.diff, fac);
116                 if(shi->passflag & SCE_PASS_SHADOW)
117                         VECADDISFAC(shr->shad, shr_t.shad, fac);
118
119                 VECMUL(shi->vn, -1.0f);
120                 VECMUL(shi->facenor, -1.0f);
121         }
122         
123         /* depth >= 1 when ray-shading */
124         if(shi->depth==0) {
125                 if(R.r.mode & R_RAYTRACE) {
126                         if(shi->ray_mirror!=0.0f || ((shi->mat->mode & MA_RAYTRANSP) && shr->alpha!=1.0f)) {
127                                 /* ray trace works on combined, but gives pass info */
128                                 ray_trace(shi, shr);
129                         }
130                 }
131                 /* disable adding of sky for raytransp */
132                 if(shi->mat->mode & MA_RAYTRANSP) 
133                         if((shi->layflag & SCE_LAY_SKY) && (R.r.alphamode==R_ADDSKY))
134                                 shr->alpha= 1.0f;
135         }       
136 }
137
138
139 /* do a shade, finish up some passes, apply mist */
140 void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
141 {
142         float alpha;
143         
144         /* ------  main shading loop -------- */
145         
146         if(shi->mat->nodetree && shi->mat->use_nodes) {
147                 ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
148         }
149         else {
150                 /* copy all relevant material vars, note, keep this synced with render_types.h */
151                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
152                 shi->har= shi->mat->har;
153                 
154                 shade_material_loop(shi, shr);
155         }
156         
157         /* copy additional passes */
158         if(shi->passflag & (SCE_PASS_VECTOR|SCE_PASS_NORMAL|SCE_PASS_RADIO)) {
159                 QUATCOPY(shr->winspeed, shi->winspeed);
160                 VECCOPY(shr->nor, shi->vn);
161                 VECCOPY(shr->rad, shi->rad);
162         }
163         
164         /* MIST */
165         if((shi->passflag & SCE_PASS_MIST) || ((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0))  {
166                 if(R.r.mode & R_ORTHO)
167                         shr->mist= mistfactor(-shi->co[2], shi->co);
168                 else
169                         shr->mist= mistfactor(VecLength(shi->co), shi->co);
170         }
171         else shr->mist= 0.0f;
172         
173         if((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST)==0 ) {
174                 alpha= shr->mist;
175         }
176         else alpha= 1.0f;
177         
178         /* add mist and premul color */
179         if(shr->alpha!=1.0f || alpha!=1.0f) {
180                 float fac= alpha*(shr->alpha);
181                 
182                 shr->combined[3]= fac;
183                 shr->combined[0]*= fac;
184                 shr->combined[1]*= fac;
185                 shr->combined[2]*= fac;
186         }
187         else shr->combined[3]= 1.0f;
188         
189         /* add z */
190         shr->z= -shi->co[2];
191 }
192
193 /* **************************************************************************** */
194 /*                    ShadeInput                                                */
195 /* **************************************************************************** */
196
197
198 void vlr_set_uv_indices(VlakRen *vlr, int *i1, int *i2, int *i3)
199 {
200         /* to prevent storing new tfaces or vcols, we check a split runtime */
201         /*              4---3           4---3 */
202         /*              |\ 1|   or  |1 /| */
203         /*              |0\ |           |/ 0| */
204         /*              1---2           1---2   0 = orig face, 1 = new face */
205         
206         /* Update vert nums to point to correct verts of original face */
207         if(vlr->flag & R_DIVIDE_24) {  
208                 if(vlr->flag & R_FACE_SPLIT) {
209                         (*i1)++; (*i2)++; (*i3)++;
210                 }
211                 else {
212                         (*i3)++;
213                 }
214         }
215         else if(vlr->flag & R_FACE_SPLIT) {
216                 (*i2)++; (*i3)++; 
217         }
218 }
219
220 /* copy data from face to ShadeInput, general case */
221 /* indices 0 1 2 3 only */
222 void shade_input_set_triangle_i(ShadeInput *shi, ObjectInstanceRen *obi, VlakRen *vlr, short i1, short i2, short i3)
223 {
224         VertRen **vpp= &vlr->v1;
225         
226         shi->vlr= vlr;
227         shi->obi= obi;
228         shi->obr= obi->obr;
229
230         shi->v1= vpp[i1];
231         shi->v2= vpp[i2];
232         shi->v3= vpp[i3];
233         
234         shi->i1= i1;
235         shi->i2= i2;
236         shi->i3= i3;
237         
238         /* note, shi->mat is set in node shaders */
239         shi->mat= shi->mat_override?shi->mat_override:vlr->mat;
240         
241         shi->osatex= (shi->mat->texco & TEXCO_OSA);
242         shi->mode= shi->mat->mode_l;            /* or-ed result for all nodes */
243
244         /* facenormal copy, can get flipped */
245         shi->flippednor= RE_vlakren_get_normal(&R, obi, vlr, shi->facenor);
246         
247         /* copy of original pre-flipped normal, for geometry->front/back node output */
248         VECCOPY(shi->orignor, shi->facenor);
249         if(shi->flippednor)
250                 VECMUL(shi->orignor, -1.0f);
251         
252         /* calculate vertexnormals */
253         if(vlr->flag & R_SMOOTH) {
254                 VECCOPY(shi->n1, shi->v1->n);
255                 VECCOPY(shi->n2, shi->v2->n);
256                 VECCOPY(shi->n3, shi->v3->n);
257
258                 if(obi->flag & R_TRANSFORMED) {
259                         Mat3MulVecfl(obi->nmat, shi->n1);
260                         Mat3MulVecfl(obi->nmat, shi->n2);
261                         Mat3MulVecfl(obi->nmat, shi->n3);
262                 }
263
264                 if(!(vlr->flag & (R_NOPUNOFLIP|R_TANGENT))) {
265                         if(INPR(shi->facenor, shi->n1) < 0.0f) {
266                                 shi->n1[0]= -shi->n1[0];
267                                 shi->n1[1]= -shi->n1[1];
268                                 shi->n1[2]= -shi->n1[2];
269                         }
270                         if(INPR(shi->facenor, shi->n2) < 0.0f) {
271                                 shi->n2[0]= -shi->n2[0];
272                                 shi->n2[1]= -shi->n2[1];
273                                 shi->n2[2]= -shi->n2[2];
274                         }
275                         if(INPR(shi->facenor, shi->n3) < 0.0f) {
276                                 shi->n3[0]= -shi->n3[0];
277                                 shi->n3[1]= -shi->n3[1];
278                                 shi->n3[2]= -shi->n3[2];
279                         }
280                 }
281         }
282 }
283
284 /* note, facenr declared volatile due to over-eager -O2 optimizations
285  * on cygwin (particularly -frerun-cse-after-loop)
286  */
287
288 /* copy data from face to ShadeInput, scanline case */
289 void shade_input_set_triangle(ShadeInput *shi, volatile int obi, volatile int facenr, int normal_flip)
290 {
291         if(facenr>0) {
292                 shi->obi= &R.objectinstance[obi];
293                 shi->obr= shi->obi->obr;
294                 shi->facenr= (facenr-1) & RE_QUAD_MASK;
295                 if( shi->facenr < shi->obr->totvlak ) {
296                         VlakRen *vlr= RE_findOrAddVlak(shi->obr, shi->facenr);
297                         
298                         if(facenr & RE_QUAD_OFFS)
299                                 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 2, 3);
300                         else
301                                 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 1, 2);
302                 }
303                 else
304                         shi->vlr= NULL; /* general signal we got sky */
305         }
306         else
307                 shi->vlr= NULL; /* general signal we got sky */
308 }
309
310 /* full osa case: copy static info */
311 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
312 {
313         /* not so nice, but works... warning is in RE_shader_ext.h */
314         memcpy(shi, from, sizeof(struct ShadeInputCopy));
315 }
316
317 /* copy data from strand to shadeinput */
318 void shade_input_set_strand(ShadeInput *shi, StrandRen *strand, StrandPoint *spoint)
319 {
320         /* note, shi->mat is set in node shaders */
321         shi->mat= shi->mat_override? shi->mat_override: strand->buffer->ma;
322         
323         shi->osatex= (shi->mat->texco & TEXCO_OSA);
324         shi->mode= shi->mat->mode_l;            /* or-ed result for all nodes */
325
326         /* shade_input_set_viewco equivalent */
327         VECCOPY(shi->co, spoint->co);
328         VECCOPY(shi->view, shi->co);
329         Normalize(shi->view);
330
331         shi->xs= (int)spoint->x;
332         shi->ys= (int)spoint->y;
333
334         if(shi->osatex || (R.r.mode & R_SHADOW)) {
335                 VECCOPY(shi->dxco, spoint->dtco);
336                 VECCOPY(shi->dyco, spoint->dsco);
337         }
338
339         /* dxview, dyview, not supported */
340
341         /* facenormal, simply viewco flipped */
342         VECCOPY(shi->facenor, spoint->nor);
343         VECCOPY(shi->orignor, shi->facenor);
344
345         /* shade_input_set_normals equivalent */
346         if(shi->mat->mode & MA_TANGENT_STR) {
347                 VECCOPY(shi->vn, spoint->tan)
348         }
349         else {
350                 float cross[3];
351
352                 Crossf(cross, spoint->co, spoint->tan);
353                 Crossf(shi->vn, cross, spoint->tan);
354                 Normalize(shi->vn);
355
356                 if(INPR(shi->vn, shi->view) < 0.0f)
357                         VecMulf(shi->vn, -1.0f);
358         }
359
360         VECCOPY(shi->vno, shi->vn);
361 }
362
363 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
364 {
365         StrandBuffer *strandbuf= strand->buffer;
366         ObjectRen *obr= strandbuf->obr;
367         StrandVert *sv;
368         int mode= shi->mode;            /* or-ed result for all nodes */
369         short texco= shi->mat->texco;
370
371         if((shi->mat->texco & TEXCO_REFL)) {
372                 /* shi->dxview, shi->dyview, not supported */
373         }
374
375         if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL))) {
376                 /* not supported */
377         }
378
379         if(mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
380                 VECCOPY(shi->tang, spoint->tan);
381                 VECCOPY(shi->nmaptang, spoint->tan);
382         }
383
384         if(mode & MA_STR_SURFDIFF) {
385                 float *surfnor= RE_strandren_get_surfnor(obr, strand, 0);
386
387                 if(surfnor)
388                         VECCOPY(shi->surfnor, surfnor)
389                 else
390                         VECCOPY(shi->surfnor, shi->vn)
391
392                 if(shi->mat->strand_surfnor > 0.0f) {
393                         shi->surfdist= 0.0f;
394                         for(sv=strand->vert; sv!=svert; sv++)
395                                 shi->surfdist+=VecLenf(sv->co, (sv+1)->co);
396                         shi->surfdist += spoint->t*VecLenf(sv->co, (sv+1)->co);
397                 }
398         }
399
400         if(R.r.mode & R_SPEED) {
401                 float *speed;
402                 
403                 speed= RE_strandren_get_winspeed(shi->obi, strand, 0);
404                 if(speed)
405                         QUATCOPY(shi->winspeed, speed)
406                 else
407                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
408         }
409
410         /* shade_input_set_shade_texco equivalent */
411         if(texco & NEED_UV) {
412                 if(texco & TEXCO_ORCO) {
413                         VECCOPY(shi->lo, strand->orco);
414                         /* no shi->osatex, orco derivatives are zero */
415                 }
416
417                 if(texco & TEXCO_GLOB) {
418                         VECCOPY(shi->gl, shi->co);
419                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
420                         
421                         if(shi->osatex) {
422                                 VECCOPY(shi->dxgl, shi->dxco);
423                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
424                                 VECCOPY(shi->dygl, shi->dyco);
425                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
426                         }
427                 }
428
429                 if(texco & TEXCO_STRAND) {
430                         shi->strandco= spoint->strandco;
431
432                         if(shi->osatex) {
433                                 shi->dxstrand= spoint->dtstrandco;
434                                 shi->dystrand= 0.0f;
435                         }
436                 }
437
438                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
439                         MCol *mcol;
440                         float *uv;
441                         char *name;
442                         int i;
443
444                         shi->totuv= 0;
445                         shi->totcol= 0;
446                         shi->actuv= obr->actmtface;
447                         shi->actcol= obr->actmcol;
448
449                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
450                                 for (i=0; (mcol=RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
451                                         ShadeInputCol *scol= &shi->col[i];
452                                         char *cp= (char*)mcol;
453                                         
454                                         shi->totcol++;
455                                         scol->name= name;
456
457                                         scol->col[0]= cp[3]/255.0f;
458                                         scol->col[1]= cp[2]/255.0f;
459                                         scol->col[2]= cp[1]/255.0f;
460                                 }
461
462                                 if(shi->totcol) {
463                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
464                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
465                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
466                                 }
467                                 else {
468                                         shi->vcol[0]= 0.0f;
469                                         shi->vcol[1]= 0.0f;
470                                         shi->vcol[2]= 0.0f;
471                                 }
472                         }
473
474                         for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
475                                 ShadeInputUV *suv= &shi->uv[i];
476
477                                 shi->totuv++;
478                                 suv->name= name;
479
480                                 if(strandbuf->overrideuv == i) {
481                                         suv->uv[0]= -1.0f;
482                                         suv->uv[1]= spoint->strandco;
483                                         suv->uv[2]= 0.0f;
484                                 }
485                                 else {
486                                         suv->uv[0]= -1.0f + 2.0f*uv[0];
487                                         suv->uv[1]= -1.0f + 2.0f*uv[1];
488                                         suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
489                                 }
490
491                                 if(shi->osatex) {
492                                         suv->dxuv[0]= 0.0f;
493                                         suv->dxuv[1]= 0.0f;
494                                         suv->dyuv[0]= 0.0f;
495                                         suv->dyuv[1]= 0.0f;
496                                 }
497
498                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
499                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
500                                                 shi->vcol[0]= 1.0f;
501                                                 shi->vcol[1]= 1.0f;
502                                                 shi->vcol[2]= 1.0f;
503                                         }
504                                 }
505                         }
506
507                         if(shi->totuv == 0) {
508                                 ShadeInputUV *suv= &shi->uv[0];
509
510                                 suv->uv[0]= 0.0f;
511                                 suv->uv[1]= spoint->strandco;
512                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
513                                 
514                                 if(mode & MA_FACETEXTURE) {
515                                         /* no tface? set at 1.0f */
516                                         shi->vcol[0]= 1.0f;
517                                         shi->vcol[1]= 1.0f;
518                                         shi->vcol[2]= 1.0f;
519                                 }
520                         }
521
522                 }
523
524                 if(texco & TEXCO_NORM) {
525                         shi->orn[0]= -shi->vn[0];
526                         shi->orn[1]= -shi->vn[1];
527                         shi->orn[2]= -shi->vn[2];
528                 }
529
530                 if(mode & MA_RADIO) {
531                         /* not supported */
532                 }
533
534                 if(texco & TEXCO_REFL) {
535                         /* mirror reflection color textures (and envmap) */
536                         calc_R_ref(shi);    /* wrong location for normal maps! XXXXXXXXXXXXXX */
537                 }
538
539                 if(texco & TEXCO_STRESS) {
540                         /* not supported */
541                 }
542
543                 if(texco & TEXCO_TANGENT) {
544                         if((mode & MA_TANGENT_V)==0) {
545                                 /* just prevent surprises */
546                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
547                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
548                         }
549                 }
550         }
551
552         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
553
554         /* this only avalailable for scanline renders */
555         if(shi->depth==0) {
556                 if(texco & TEXCO_WINDOW) {
557                         shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
558                         shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
559                         shi->winco[2]= 0.0f;
560
561                         /* not supported */
562                         if(shi->osatex) {
563                                 shi->dxwin[0]= 0.0f;
564                                 shi->dywin[1]= 0.0f;
565                                 shi->dxwin[0]= 0.0f;
566                                 shi->dywin[1]= 0.0f;
567                         }
568                 }
569
570                 if(texco & TEXCO_STICKY) {
571                         /* not supported */
572                 }
573         }
574 }
575
576 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
577 void shade_input_calc_viewco(ShadeInput *shi, float x, float y, float z, float *view, float *dxyview, float *co, float *dxco, float *dyco)
578 {
579         /* returns not normalized, so is in viewplane coords */
580         calc_view_vector(view, x, y);
581         
582         if(shi->mat->mode & MA_WIRE) {
583                 /* wire cannot use normal for calculating shi->co, so
584                  * we reconstruct the coordinate less accurate */
585                 if(R.r.mode & R_ORTHO)
586                         calc_renderco_ortho(co, x, y, z);
587                 else
588                         calc_renderco_zbuf(co, view, z);
589         }
590         else {
591                 /* for non-wire, intersect with the triangle to get the exact coord */
592                 float fac, dface, v1[3];
593                 
594                 VECCOPY(v1, shi->v1->co);
595                 if(shi->obi->flag & R_TRANSFORMED)
596                         Mat4MulVecfl(shi->obi->mat, v1);
597                 
598                 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
599                 
600                 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
601                 if(R.r.mode & R_ORTHO) {
602                         /* x and y 3d coordinate can be derived from pixel coord and winmat */
603                         float fx= 2.0f/(R.winx*R.winmat[0][0]);
604                         float fy= 2.0f/(R.winy*R.winmat[1][1]);
605                         
606                         co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
607                         co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
608                         
609                         /* using a*x + b*y + c*z = d equation, (a b c) is normal */
610                         if(shi->facenor[2]!=0.0f)
611                                 co[2]= (dface - shi->facenor[0]*co[0] - shi->facenor[1]*co[1])/shi->facenor[2];
612                         else
613                                 co[2]= 0.0f;
614                         
615                         if(dxco && dyco) {
616                                 dxco[0]= fx;
617                                 dxco[1]= 0.0f;
618                                 if(shi->facenor[2]!=0.0f)
619                                         dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
620                                 else 
621                                         dxco[2]= 0.0f;
622                                 
623                                 dyco[0]= 0.0f;
624                                 dyco[1]= fy;
625                                 if(shi->facenor[2]!=0.0f)
626                                         dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
627                                 else 
628                                         dyco[2]= 0.0f;
629                                 
630                                 if(dxyview) {
631                                         if(co[2]!=0.0f) fac= 1.0f/co[2]; else fac= 0.0f;
632                                         dxyview[0]= -R.viewdx*fac;
633                                         dxyview[1]= -R.viewdy*fac;
634                                 }
635                         }
636                 }
637                 else {
638                         float div;
639                         
640                         div= shi->facenor[0]*view[0] + shi->facenor[1]*view[1] + shi->facenor[2]*view[2];
641                         if (div!=0.0f) fac= dface/div;
642                         else fac= 0.0f;
643                         
644                         co[0]= fac*view[0];
645                         co[1]= fac*view[1];
646                         co[2]= fac*view[2];
647                         
648                         /* pixel dx/dy for render coord */
649                         if(dxco && dyco) {
650                                 float u= dface/(div - R.viewdx*shi->facenor[0]);
651                                 float v= dface/(div - R.viewdy*shi->facenor[1]);
652                                 
653                                 dxco[0]= co[0]- (view[0]-R.viewdx)*u;
654                                 dxco[1]= co[1]- (view[1])*u;
655                                 dxco[2]= co[2]- (view[2])*u;
656                                 
657                                 dyco[0]= co[0]- (view[0])*v;
658                                 dyco[1]= co[1]- (view[1]-R.viewdy)*v;
659                                 dyco[2]= co[2]- (view[2])*v;
660                                 
661                                 if(dxyview) {
662                                         if(fac!=0.0f) fac= 1.0f/fac;
663                                         dxyview[0]= -R.viewdx*fac;
664                                         dxyview[1]= -R.viewdy*fac;
665                                 }
666                         }
667                 }
668         }
669         
670         /* cannot normalize earlier, code above needs it at viewplane level */
671         Normalize(view);
672 }
673
674 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
675 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
676 {
677         float *dxyview= NULL, *dxco= NULL, *dyco= NULL;
678         
679         /* currently in use for dithering (soft shadow), node preview, irregular shad */
680         shi->xs= (int)xs;
681         shi->ys= (int)ys;
682
683         /* original scanline coordinate without jitter */
684         shi->scanco[0]= x;
685         shi->scanco[1]= y;
686         shi->scanco[2]= z;
687
688         /* check if we need derivatives */
689         if(shi->osatex || (R.r.mode & R_SHADOW)) {
690                 dxco= shi->dxco;
691                 dyco= shi->dyco;
692
693                 if((shi->mat->texco & TEXCO_REFL))
694                         dxyview= &shi->dxview;
695         }
696
697         shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
698 }
699
700 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
701 void shade_input_set_uv(ShadeInput *shi)
702 {
703         VlakRen *vlr= shi->vlr;
704         
705         if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
706                 float v1[3], v2[3], v3[3];
707
708                 VECCOPY(v1, shi->v1->co);
709                 VECCOPY(v2, shi->v2->co);
710                 VECCOPY(v3, shi->v3->co);
711
712                 if(shi->obi->flag & R_TRANSFORMED) {
713                         Mat4MulVecfl(shi->obi->mat, v1);
714                         Mat4MulVecfl(shi->obi->mat, v2);
715                         Mat4MulVecfl(shi->obi->mat, v3);
716                 }
717
718                 /* exception case for wire render of edge */
719                 if(vlr->v2==vlr->v3) {
720                         float lend, lenc;
721                         
722                         lend= VecLenf(v2, v1);
723                         lenc= VecLenf(shi->co, v1);
724                         
725                         if(lend==0.0f) {
726                                 shi->u=shi->v= 0.0f;
727                         }
728                         else {
729                                 shi->u= - (1.0f - lenc/lend);
730                                 shi->v= 0.0f;
731                         }
732                         
733                         if(shi->osatex) {
734                                 shi->dx_u=  0.0f;
735                                 shi->dx_v=  0.0f;
736                                 shi->dy_u=  0.0f;
737                                 shi->dy_v=  0.0f;
738                         }
739                 }
740                 else {
741                         /* most of this could become re-used for faces */
742                         float detsh, t00, t10, t01, t11, xn, yn, zn;
743                         int axis1, axis2;
744
745                         /* find most stable axis to project */
746                         xn= fabs(shi->facenor[0]);
747                         yn= fabs(shi->facenor[1]);
748                         zn= fabs(shi->facenor[2]);
749
750                         if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
751                         else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
752                         else { axis1= 1; axis2= 2; }
753
754                         /* compute u,v and derivatives */
755                         t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
756                         t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
757
758                         detsh= 1.0f/(t00*t11-t10*t01);
759                         t00*= detsh; t01*=detsh; 
760                         t10*=detsh; t11*=detsh;
761
762                         shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
763                         shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
764                         if(shi->osatex) {
765                                 shi->dx_u=  shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
766                                 shi->dx_v=  shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
767                                 shi->dy_u=  shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
768                                 shi->dy_v=  shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
769                         }
770
771                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
772                         CLAMP(shi->u, -2.0f, 1.0f);
773                         CLAMP(shi->v, -2.0f, 1.0f);
774                 }
775         }       
776 }
777
778 void shade_input_set_normals(ShadeInput *shi)
779 {
780         float u= shi->u, v= shi->v;
781         float l= 1.0f+u+v;
782         
783         /* calculate vertexnormals */
784         if(shi->vlr->flag & R_SMOOTH) {
785                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
786                 
787                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
788                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
789                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
790                 
791                 Normalize(shi->vn);
792         }
793         else
794                 VECCOPY(shi->vn, shi->facenor);
795         
796         /* used in nodes */
797         VECCOPY(shi->vno, shi->vn);
798
799 }
800
801 /* use by raytrace, sss, bake to flip into the right direction */
802 void shade_input_flip_normals(ShadeInput *shi)
803 {
804         shi->facenor[0]= -shi->facenor[0];
805         shi->facenor[1]= -shi->facenor[1];
806         shi->facenor[2]= -shi->facenor[2];
807
808         shi->vn[0]= -shi->vn[0];
809         shi->vn[1]= -shi->vn[1];
810         shi->vn[2]= -shi->vn[2];
811
812         shi->vno[0]= -shi->vno[0];
813         shi->vno[1]= -shi->vno[1];
814         shi->vno[2]= -shi->vno[2];
815
816         shi->flippednor= !shi->flippednor;
817 }
818
819 void shade_input_set_shade_texco(ShadeInput *shi)
820 {
821         ObjectInstanceRen *obi= shi->obi;
822         ObjectRen *obr= shi->obr;
823         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
824         float u= shi->u, v= shi->v;
825         float l= 1.0f+u+v, dl;
826         int mode= shi->mode;            /* or-ed result for all nodes */
827         short texco= shi->mat->texco;
828
829         /* calculate dxno */
830         if(shi->vlr->flag & R_SMOOTH) {
831                 
832                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
833                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
834                         
835                         dl= shi->dx_u+shi->dx_v;
836                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
837                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
838                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
839                         dl= shi->dy_u+shi->dy_v;
840                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
841                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
842                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
843                         
844                 }
845         }
846
847         /* calc tangents */
848         if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
849                 float *tangent, *s1, *s2, *s3;
850                 float tl, tu, tv;
851
852                 if(shi->vlr->flag & R_SMOOTH) {
853                         tl= l;
854                         tu= u;
855                         tv= v;
856                 }
857                 else {
858                         /* qdn: flat faces have tangents too,
859                            could pick either one, using average here */
860                         tl= 1.0f/3.0f;
861                         tu= -1.0f/3.0f;
862                         tv= -1.0f/3.0f;
863                 }
864
865                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
866                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
867
868                 if(mode & MA_TANGENT_V) {
869                         s1 = RE_vertren_get_tangent(obr, v1, 0);
870                         s2 = RE_vertren_get_tangent(obr, v2, 0);
871                         s3 = RE_vertren_get_tangent(obr, v3, 0);
872
873                         if(s1 && s2 && s3) {
874                                 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
875                                 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
876                                 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
877
878                                 if(obi->flag & R_TRANSFORMED)
879                                         Mat3MulVecfl(obi->nmat, shi->tang);
880
881                                 Normalize(shi->tang);
882                                 VECCOPY(shi->nmaptang, shi->tang);
883                         }
884                 }
885
886                 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
887                         tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
888
889                         if(tangent) {
890                                 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
891
892                                 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
893
894                                 s1= &tangent[j1*3];
895                                 s2= &tangent[j2*3];
896                                 s3= &tangent[j3*3];
897
898                                 shi->nmaptang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
899                                 shi->nmaptang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
900                                 shi->nmaptang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
901
902                                 if(obi->flag & R_TRANSFORMED)
903                                         Mat3MulVecfl(obi->nmat, shi->nmaptang);
904
905                                 Normalize(shi->nmaptang);
906                         }
907                 }
908         }
909
910         if(mode & MA_STR_SURFDIFF) {
911                 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
912
913                 if(surfnor) {
914                         VECCOPY(shi->surfnor, surfnor)
915                         if(obi->flag & R_TRANSFORMED)
916                                 Mat3MulVecfl(obi->nmat, shi->surfnor);
917                 }
918                 else
919                         VECCOPY(shi->surfnor, shi->vn)
920
921                 shi->surfdist= 0.0f;
922         }
923         
924         if(R.r.mode & R_SPEED) {
925                 float *s1, *s2, *s3;
926                 
927                 s1= RE_vertren_get_winspeed(obi, v1, 0);
928                 s2= RE_vertren_get_winspeed(obi, v2, 0);
929                 s3= RE_vertren_get_winspeed(obi, v3, 0);
930                 if(s1 && s2 && s3) {
931                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
932                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
933                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
934                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
935                 }
936                 else {
937                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
938                 }
939         }
940
941         /* pass option forces UV calc */
942         if(shi->passflag & SCE_PASS_UV)
943                 texco |= (NEED_UV|TEXCO_UV);
944         
945         /* texture coordinates. shi->dxuv shi->dyuv have been set */
946         if(texco & NEED_UV) {
947                 
948                 if(texco & TEXCO_ORCO) {
949                         if(v1->orco) {
950                                 float *o1, *o2, *o3;
951                                 
952                                 o1= v1->orco;
953                                 o2= v2->orco;
954                                 o3= v3->orco;
955                                 
956                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
957                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
958                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
959                                 
960                                 if(shi->osatex) {
961                                         dl= shi->dx_u+shi->dx_v;
962                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
963                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
964                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
965                                         dl= shi->dy_u+shi->dy_v;
966                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
967                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
968                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
969                                 }
970                         }
971
972                         VECCOPY(shi->duplilo, obi->dupliorco);
973                 }
974                 
975                 if(texco & TEXCO_GLOB) {
976                         VECCOPY(shi->gl, shi->co);
977                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
978                         if(shi->osatex) {
979                                 VECCOPY(shi->dxgl, shi->dxco);
980                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
981                                 VECCOPY(shi->dygl, shi->dyco);
982                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
983                         }
984                 }
985                 
986                 if(texco & TEXCO_STRAND) {
987                         shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
988                         if(shi->osatex) {
989                                 dl= shi->dx_u+shi->dx_v;
990                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
991                                 dl= shi->dy_u+shi->dy_v;
992                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
993                         }
994                 }
995                                 
996                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
997                         VlakRen *vlr= shi->vlr;
998                         MTFace *tface;
999                         MCol *mcol;
1000                         char *name;
1001                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
1002
1003                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
1004                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1005
1006                         shi->totuv= 0;
1007                         shi->totcol= 0;
1008                         shi->actuv= obr->actmtface;
1009                         shi->actcol= obr->actmcol;
1010
1011                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1012                                 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1013                                         ShadeInputCol *scol= &shi->col[i];
1014                                         char *cp1, *cp2, *cp3;
1015                                         
1016                                         shi->totcol++;
1017                                         scol->name= name;
1018
1019                                         cp1= (char *)(mcol+j1);
1020                                         cp2= (char *)(mcol+j2);
1021                                         cp3= (char *)(mcol+j3);
1022                                         
1023                                         scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
1024                                         scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
1025                                         scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
1026                                 }
1027
1028                                 if(shi->totcol) {
1029                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
1030                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
1031                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
1032                                         shi->vcol[3]= 1.0f;
1033                                 }
1034                                 else {
1035                                         shi->vcol[0]= 0.0f;
1036                                         shi->vcol[1]= 0.0f;
1037                                         shi->vcol[2]= 0.0f;
1038                                         shi->vcol[3]= 1.0f;
1039                                 }
1040                         }
1041
1042                         for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1043                                 ShadeInputUV *suv= &shi->uv[i];
1044                                 float *uv1, *uv2, *uv3;
1045
1046                                 shi->totuv++;
1047                                 suv->name= name;
1048                                 
1049                                 uv1= tface->uv[j1];
1050                                 uv2= tface->uv[j2];
1051                                 uv3= tface->uv[j3];
1052                                 
1053                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1054                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1055                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1056
1057                                 if(shi->osatex) {
1058                                         float duv[2];
1059                                         
1060                                         dl= shi->dx_u+shi->dx_v;
1061                                         duv[0]= shi->dx_u; 
1062                                         duv[1]= shi->dx_v;
1063                                         
1064                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1065                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1066                                         
1067                                         dl= shi->dy_u+shi->dy_v;
1068                                         duv[0]= shi->dy_u; 
1069                                         duv[1]= shi->dy_v;
1070                                         
1071                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1072                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1073                                 }
1074
1075                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1076                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1077                                                 shi->vcol[0]= 1.0f;
1078                                                 shi->vcol[1]= 1.0f;
1079                                                 shi->vcol[2]= 1.0f;
1080                                                 shi->vcol[3]= 1.0f;
1081                                         }
1082                                         if(tface && tface->tpage)
1083                                                 render_realtime_texture(shi, tface->tpage);
1084                                 }
1085                         }
1086
1087                         shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1088                         shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1089                         shi->dupliuv[2]= 0.0f;
1090
1091                         if(shi->totuv == 0) {
1092                                 ShadeInputUV *suv= &shi->uv[0];
1093
1094                                 suv->uv[0]= 2.0f*(u+.5f);
1095                                 suv->uv[1]= 2.0f*(v+.5f);
1096                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1097                                 
1098                                 if(mode & MA_FACETEXTURE) {
1099                                         /* no tface? set at 1.0f */
1100                                         shi->vcol[0]= 1.0f;
1101                                         shi->vcol[1]= 1.0f;
1102                                         shi->vcol[2]= 1.0f;
1103                                         shi->vcol[3]= 1.0f;
1104                                 }
1105                         }
1106                 }
1107                 
1108                 if(texco & TEXCO_NORM) {
1109                         shi->orn[0]= -shi->vn[0];
1110                         shi->orn[1]= -shi->vn[1];
1111                         shi->orn[2]= -shi->vn[2];
1112                 }
1113                 
1114                 if(mode & MA_RADIO) {
1115                         float *r1, *r2, *r3;
1116                         
1117                         r1= RE_vertren_get_rad(obr, v1, 0);
1118                         r2= RE_vertren_get_rad(obr, v2, 0);
1119                         r3= RE_vertren_get_rad(obr, v3, 0);
1120                         
1121                         if(r1 && r2 && r3) {
1122                                 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
1123                                 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
1124                                 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
1125                         }
1126                         else
1127                                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1128                 }
1129                 else
1130                         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1131                 
1132                 if(texco & TEXCO_REFL) {
1133                         /* mirror reflection color textures (and envmap) */
1134                         calc_R_ref(shi);        /* wrong location for normal maps! XXXXXXXXXXXXXX */
1135                 }
1136                 
1137                 if(texco & TEXCO_STRESS) {
1138                         float *s1, *s2, *s3;
1139                         
1140                         s1= RE_vertren_get_stress(obr, v1, 0);
1141                         s2= RE_vertren_get_stress(obr, v2, 0);
1142                         s3= RE_vertren_get_stress(obr, v3, 0);
1143                         if(s1 && s2 && s3) {
1144                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1145                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
1146                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
1147                         }
1148                         else shi->stress= 0.0f;
1149                 }
1150                 
1151                 if(texco & TEXCO_TANGENT) {
1152                         if((mode & MA_TANGENT_V)==0) {
1153                                 /* just prevent surprises */
1154                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1155                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1156                         }
1157                 }
1158         }
1159         else
1160                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1161         
1162         /* this only avalailable for scanline renders */
1163         if(shi->depth==0) {
1164                 float x= shi->xs;
1165                 float y= shi->ys;
1166                 
1167                 if(texco & TEXCO_WINDOW) {
1168                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1169                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1170                         shi->winco[2]= 0.0f;
1171                         if(shi->osatex) {
1172                                 shi->dxwin[0]= 2.0f/(float)R.winx;
1173                                 shi->dywin[1]= 2.0f/(float)R.winy;
1174                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1175                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
1176                         }
1177                 }
1178
1179                 if(texco & TEXCO_STICKY) {
1180                         float *s1, *s2, *s3;
1181                         
1182                         s1= RE_vertren_get_sticky(obr, v1, 0);
1183                         s2= RE_vertren_get_sticky(obr, v2, 0);
1184                         s3= RE_vertren_get_sticky(obr, v3, 0);
1185                         
1186                         if(s1 && s2 && s3) {
1187                                 float winmat[4][4], ho1[4], ho2[4], ho3[4];
1188                                 float Zmulx, Zmuly;
1189                                 float hox, hoy, l, dl, u, v;
1190                                 float s00, s01, s10, s11, detsh;
1191                                 
1192                                 /* old globals, localized now */
1193                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
1194
1195                                 if(shi->obi->flag & R_TRANSFORMED)
1196                                         zbuf_make_winmat(&R, shi->obi->mat, winmat);
1197                                 else
1198                                         zbuf_make_winmat(&R, NULL, winmat);
1199
1200                                 zbuf_render_project(winmat, v1->co, ho1);
1201                                 zbuf_render_project(winmat, v2->co, ho2);
1202                                 zbuf_render_project(winmat, v3->co, ho3);
1203                                 
1204                                 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1205                                 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1206                                 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1207                                 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1208                                 
1209                                 detsh= s00*s11-s10*s01;
1210                                 s00/= detsh; s01/=detsh; 
1211                                 s10/=detsh; s11/=detsh;
1212                                 
1213                                 /* recalc u and v again */
1214                                 hox= x/Zmulx -1.0f;
1215                                 hoy= y/Zmuly -1.0f;
1216                                 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1217                                 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1218                                 l= 1.0f+u+v;
1219                                 
1220                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1221                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1222                                 shi->sticky[2]= 0.0f;
1223                                 
1224                                 if(shi->osatex) {
1225                                         float dxuv[2], dyuv[2];
1226                                         dxuv[0]=  s11/Zmulx;
1227                                         dxuv[1]=  - s01/Zmulx;
1228                                         dyuv[0]=  - s10/Zmuly;
1229                                         dyuv[1]=  s00/Zmuly;
1230                                         
1231                                         dl= dxuv[0] + dxuv[1];
1232                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1233                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1234                                         dl= dyuv[0] + dyuv[1];
1235                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1236                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1237                                 }
1238                         }
1239                 }
1240         } /* else {
1241          Note! For raytracing winco is not set, important because thus means all shader input's need to have their variables set to zero else in-initialized values are used
1242         */
1243 }
1244
1245 /* ****************** ShadeSample ************************************** */
1246
1247 /* initialize per part, not per pixel! */
1248 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1249 {
1250         
1251         memset(shi, 0, sizeof(ShadeInput));
1252         
1253         shi->sample= sample;
1254         shi->thread= pa->thread;
1255         shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
1256         shi->lay= rl->lay;
1257         shi->layflag= rl->layflag;
1258         shi->passflag= rl->passflag;
1259         shi->combinedflag= ~rl->pass_xor;
1260         shi->mat_override= rl->mat_override;
1261         shi->light_override= rl->light_override;
1262 //      shi->rl= rl;
1263         /* note shi.depth==0  means first hit, not raytracing */
1264         
1265 }
1266
1267 /* initialize per part, not per pixel! */
1268 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1269 {
1270         int a, tot;
1271         
1272         tot= R.osa==0?1:R.osa;
1273         
1274         for(a=0; a<tot; a++) {
1275                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1276                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1277         }
1278         
1279         get_sample_layers(pa, rl, ssamp->rlpp);
1280 }
1281
1282 /* Do AO or (future) GI */
1283 void shade_samples_do_AO(ShadeSample *ssamp)
1284 {
1285         ShadeInput *shi;
1286         int sample;
1287         
1288         if(!(R.r.mode & R_SHADOW))
1289                 return;
1290         if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1291                 return;
1292         
1293         if(R.wrld.mode & WO_AMB_OCC) {
1294                 shi= &ssamp->shi[0];
1295
1296                 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & SCE_PASS_AO))
1297                         || (shi->passflag & SCE_PASS_AO))
1298                         for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
1299                                 if(!(shi->mode & MA_SHLESS))
1300                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
1301         }
1302 }
1303
1304
1305 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1306 {
1307         ShadeInput *shi;
1308         float xs, ys;
1309         
1310         ssamp->tot= 0;
1311         
1312         for(shi= ssamp->shi; ps; ps= ps->next) {
1313                 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1314                 
1315                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
1316                         unsigned short curmask= ps->mask;
1317                         
1318                         /* full osa is only set for OSA renders */
1319                         if(shi->vlr->flag & R_FULL_OSA) {
1320                                 short shi_cp= 0, samp;
1321                                 
1322                                 for(samp=0; samp<R.osa; samp++) {
1323                                         if(curmask & (1<<samp)) {
1324                                                 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1325                                                 xs= (float)x + R.jit[samp][0] + 0.5f;
1326                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
1327                                                 
1328                                                 if(shi_cp)
1329                                                         shade_input_copy_triangle(shi, shi-1);
1330                                                 
1331                                                 shi->mask= (1<<samp);
1332 //                                              shi->rl= ssamp->rlpp[samp];
1333                                                 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1334                                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1335                                                 shade_input_set_uv(shi);
1336                                                 shade_input_set_normals(shi);
1337                                                 
1338                                                 shi_cp= 1;
1339                                                 shi++;
1340                                         }
1341                                 }
1342                         }
1343                         else {
1344                                 if(R.osa) {
1345                                         short b= R.samples->centmask[curmask];
1346                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1347                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1348                                 }
1349                                 else {
1350                                         xs= (float)x + 0.5f;
1351                                         ys= (float)y + 0.5f;
1352                                 }
1353
1354                                 shi->mask= curmask;
1355                                 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1356                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1357                                 shade_input_set_uv(shi);
1358                                 shade_input_set_normals(shi);
1359                                 shi++;
1360                         }
1361                         
1362                         /* total sample amount, shi->sample is static set in initialize */
1363                         if(shi!=ssamp->shi)
1364                                 ssamp->tot= (shi-1)->sample + 1;
1365                 }
1366         }
1367 }
1368
1369 /* shades samples, returns true if anything happened */
1370 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1371 {
1372         shade_samples_fill_with_ps(ssamp, ps, x, y);
1373         
1374         if(ssamp->tot) {
1375                 ShadeInput *shi= ssamp->shi;
1376                 ShadeResult *shr= ssamp->shr;
1377                 int samp;
1378                 
1379                 /* if shadow or AO? */
1380                 shade_samples_do_AO(ssamp);
1381                 
1382                 /* if shade (all shadepinputs have same passflag) */
1383                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1384
1385                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1386                                 shade_input_set_shade_texco(shi);
1387                                 shade_input_do_shade(shi, shr);
1388                         }
1389                 }
1390                 
1391                 return 1;
1392         }
1393         return 0;
1394 }
1395