bf2ee3dbdd96036e82d17728dd0fda42cf06df51
[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         else
349                 VECCOPY(shi->vn, spoint->nor)
350
351         VECCOPY(shi->vno, shi->vn);
352 }
353
354 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
355 {
356         StrandBuffer *strandbuf= strand->buffer;
357         ObjectRen *obr= strandbuf->obr;
358         StrandVert *sv;
359         int mode= shi->mode;            /* or-ed result for all nodes */
360         short texco= shi->mat->texco;
361
362         if((shi->mat->texco & TEXCO_REFL)) {
363                 /* shi->dxview, shi->dyview, not supported */
364         }
365
366         if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL))) {
367                 /* not supported */
368         }
369
370         if(mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
371                 VECCOPY(shi->tang, spoint->tan);
372                 VECCOPY(shi->nmaptang, spoint->tan);
373         }
374
375         if(mode & MA_STR_SURFDIFF) {
376                 float *surfnor= RE_strandren_get_surfnor(obr, strand, 0);
377
378                 if(surfnor)
379                         VECCOPY(shi->surfnor, surfnor)
380                 else
381                         VECCOPY(shi->surfnor, shi->vn)
382
383                 if(shi->mat->strand_surfnor > 0.0f) {
384                         shi->surfdist= 0.0f;
385                         for(sv=strand->vert; sv!=svert; sv++)
386                                 shi->surfdist+=VecLenf(sv->co, (sv+1)->co);
387                         shi->surfdist += spoint->t*VecLenf(sv->co, (sv+1)->co);
388                 }
389         }
390
391         if(R.r.mode & R_SPEED) {
392                 float *speed;
393                 
394                 speed= RE_strandren_get_winspeed(shi->obi, strand, 0);
395                 if(speed)
396                         QUATCOPY(shi->winspeed, speed)
397                 else
398                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
399         }
400
401         /* shade_input_set_shade_texco equivalent */
402         if(texco & NEED_UV) {
403                 if(texco & TEXCO_ORCO) {
404                         VECCOPY(shi->lo, strand->orco);
405                         /* no shi->osatex, orco derivatives are zero */
406                 }
407
408                 if(texco & TEXCO_GLOB) {
409                         VECCOPY(shi->gl, shi->co);
410                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
411                         
412                         if(shi->osatex) {
413                                 VECCOPY(shi->dxgl, shi->dxco);
414                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
415                                 VECCOPY(shi->dygl, shi->dyco);
416                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
417                         }
418                 }
419
420                 if(texco & TEXCO_STRAND) {
421                         shi->strandco= spoint->strandco;
422
423                         if(shi->osatex) {
424                                 shi->dxstrand= spoint->dtstrandco;
425                                 shi->dystrand= 0.0f;
426                         }
427                 }
428
429                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
430                         MCol *mcol;
431                         float *uv;
432                         char *name;
433                         int i;
434
435                         shi->totuv= 0;
436                         shi->totcol= 0;
437
438                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
439                                 for (i=0; (mcol=RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
440                                         ShadeInputCol *scol= &shi->col[i];
441                                         char *cp= (char*)mcol;
442                                         
443                                         shi->totcol++;
444                                         scol->name= name;
445
446                                         scol->col[0]= cp[3]/255.0f;
447                                         scol->col[1]= cp[2]/255.0f;
448                                         scol->col[2]= cp[1]/255.0f;
449                                 }
450
451                                 if(shi->totcol) {
452                                         shi->vcol[0]= shi->col[0].col[0];
453                                         shi->vcol[1]= shi->col[0].col[1];
454                                         shi->vcol[2]= shi->col[0].col[2];
455                                 }
456                                 else {
457                                         shi->vcol[0]= 0.0f;
458                                         shi->vcol[1]= 0.0f;
459                                         shi->vcol[2]= 0.0f;
460                                 }
461                         }
462
463                         for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
464                                 ShadeInputUV *suv= &shi->uv[i];
465
466                                 shi->totuv++;
467                                 suv->name= name;
468
469                                 if(strandbuf->overrideuv == i) {
470                                         suv->uv[0]= -1.0f;
471                                         suv->uv[1]= spoint->strandco;
472                                         suv->uv[2]= 0.0f;
473                                 }
474                                 else {
475                                         suv->uv[0]= -1.0f + 2.0f*uv[0];
476                                         suv->uv[1]= -1.0f + 2.0f*uv[1];
477                                         suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
478                                 }
479
480                                 if(shi->osatex) {
481                                         suv->dxuv[0]= 0.0f;
482                                         suv->dxuv[1]= 0.0f;
483                                         suv->dyuv[0]= 0.0f;
484                                         suv->dyuv[1]= 0.0f;
485                                 }
486
487                                 if((mode & MA_FACETEXTURE) && i==0) {
488                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
489                                                 shi->vcol[0]= 1.0f;
490                                                 shi->vcol[1]= 1.0f;
491                                                 shi->vcol[2]= 1.0f;
492                                         }
493                                 }
494                         }
495
496                         if(shi->totuv == 0) {
497                                 ShadeInputUV *suv= &shi->uv[0];
498
499                                 suv->uv[0]= 0.0f;
500                                 suv->uv[1]= spoint->strandco;
501                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
502                                 
503                                 if(mode & MA_FACETEXTURE) {
504                                         /* no tface? set at 1.0f */
505                                         shi->vcol[0]= 1.0f;
506                                         shi->vcol[1]= 1.0f;
507                                         shi->vcol[2]= 1.0f;
508                                 }
509                         }
510
511                 }
512
513                 if(texco & TEXCO_NORM) {
514                         shi->orn[0]= -shi->vn[0];
515                         shi->orn[1]= -shi->vn[1];
516                         shi->orn[2]= -shi->vn[2];
517                 }
518
519                 if(mode & MA_RADIO) {
520                         /* not supported */
521                 }
522
523                 if(texco & TEXCO_REFL) {
524                         /* mirror reflection color textures (and envmap) */
525                         calc_R_ref(shi);    /* wrong location for normal maps! XXXXXXXXXXXXXX */
526                 }
527
528                 if(texco & TEXCO_STRESS) {
529                         /* not supported */
530                 }
531
532                 if(texco & TEXCO_TANGENT) {
533                         if((mode & MA_TANGENT_V)==0) {
534                                 /* just prevent surprises */
535                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
536                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
537                         }
538                 }
539         }
540
541         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
542
543         /* this only avalailable for scanline renders */
544         if(shi->depth==0) {
545                 if(texco & TEXCO_WINDOW) {
546                         shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
547                         shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
548                         shi->winco[2]= 0.0f;
549
550                         /* not supported */
551                         if(shi->osatex) {
552                                 shi->dxwin[0]= 0.0f;
553                                 shi->dywin[1]= 0.0f;
554                                 shi->dxwin[0]= 0.0f;
555                                 shi->dywin[1]= 0.0f;
556                         }
557                 }
558
559                 if(texco & TEXCO_STICKY) {
560                         /* not supported */
561                 }
562         }
563 }
564
565 /* scanline pixel coordinates */
566 /* requires set_triangle */
567 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float z)
568 {
569         float fac;
570         
571         /* currently in use for dithering (soft shadow), node preview, irregular shad */
572         shi->xs= (int)(x);
573         shi->ys= (int)(y);
574         
575         calc_view_vector(shi->view, x, y);      /* returns not normalized, so is in viewplane coords */
576         
577         /* wire cannot use normal for calculating shi->co */
578         if(shi->mat->mode & MA_WIRE) {
579                 
580                 if(R.r.mode & R_ORTHO)
581                         calc_renderco_ortho(shi->co, x, y, z);
582                 else
583                         calc_renderco_zbuf(shi->co, shi->view, z);
584         }
585         else {
586                 float dface, v1[3];
587                 
588                 VECCOPY(v1, shi->v1->co);
589
590                 if(shi->obi->flag & R_TRANSFORMED)
591                         Mat4MulVecfl(shi->obi->mat, v1);
592                 
593                 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
594                 
595                 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
596                 if(R.r.mode & R_ORTHO) {
597                         /* x and y 3d coordinate can be derived from pixel coord and winmat */
598                         float fx= 2.0f/(R.winx*R.winmat[0][0]);
599                         float fy= 2.0f/(R.winy*R.winmat[1][1]);
600                         
601                         shi->co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
602                         shi->co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
603                         
604                         /* using a*x + b*y + c*z = d equation, (a b c) is normal */
605                         if(shi->facenor[2]!=0.0f)
606                                 shi->co[2]= (dface - shi->facenor[0]*shi->co[0] - shi->facenor[1]*shi->co[1])/shi->facenor[2];
607                         else
608                                 shi->co[2]= 0.0f;
609                         
610                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
611                                 shi->dxco[0]= fx;
612                                 shi->dxco[1]= 0.0f;
613                                 if(shi->facenor[2]!=0.0f)
614                                         shi->dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
615                                 else 
616                                         shi->dxco[2]= 0.0f;
617                                 
618                                 shi->dyco[0]= 0.0f;
619                                 shi->dyco[1]= fy;
620                                 if(shi->facenor[2]!=0.0f)
621                                         shi->dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
622                                 else 
623                                         shi->dyco[2]= 0.0f;
624                                 
625                                 if( (shi->mat->texco & TEXCO_REFL) ) {
626                                         if(shi->co[2]!=0.0f) fac= 1.0f/shi->co[2]; else fac= 0.0f;
627                                         shi->dxview= -R.viewdx*fac;
628                                         shi->dyview= -R.viewdy*fac;
629                                 }
630                         }
631                 }
632                 else {
633                         float div;
634                         
635                         div= shi->facenor[0]*shi->view[0] + shi->facenor[1]*shi->view[1] + shi->facenor[2]*shi->view[2];
636                         if (div!=0.0f) fac= dface/div;
637                         else fac= 0.0f;
638                         
639                         shi->co[0]= fac*shi->view[0];
640                         shi->co[1]= fac*shi->view[1];
641                         shi->co[2]= fac*shi->view[2];
642                         
643                         /* pixel dx/dy for render coord */
644                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
645                                 float u= dface/(div - R.viewdx*shi->facenor[0]);
646                                 float v= dface/(div - R.viewdy*shi->facenor[1]);
647                                 
648                                 shi->dxco[0]= shi->co[0]- (shi->view[0]-R.viewdx)*u;
649                                 shi->dxco[1]= shi->co[1]- (shi->view[1])*u;
650                                 shi->dxco[2]= shi->co[2]- (shi->view[2])*u;
651                                 
652                                 shi->dyco[0]= shi->co[0]- (shi->view[0])*v;
653                                 shi->dyco[1]= shi->co[1]- (shi->view[1]-R.viewdy)*v;
654                                 shi->dyco[2]= shi->co[2]- (shi->view[2])*v;
655                                 
656                                 if( (shi->mat->texco & TEXCO_REFL) ) {
657                                         if(fac!=0.0f) fac= 1.0f/fac;
658                                         shi->dxview= -R.viewdx*fac;
659                                         shi->dyview= -R.viewdy*fac;
660                                 }
661                         }
662                 }
663         }
664         
665         /* cannot normalize earlier, code above needs it at viewplane level */
666         Normalize(shi->view);
667 }
668
669 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
670 void shade_input_set_uv(ShadeInput *shi)
671 {
672         VlakRen *vlr= shi->vlr;
673         
674         if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
675                 float v1[3], v2[3], v3[3];
676
677                 VECCOPY(v1, shi->v1->co);
678                 VECCOPY(v2, shi->v2->co);
679                 VECCOPY(v3, shi->v3->co);
680
681                 if(shi->obi->flag & R_TRANSFORMED) {
682                         Mat4MulVecfl(shi->obi->mat, v1);
683                         Mat4MulVecfl(shi->obi->mat, v2);
684                         Mat4MulVecfl(shi->obi->mat, v3);
685                 }
686
687                 /* exception case for wire render of edge */
688                 if(vlr->v2==vlr->v3) {
689                         float lend, lenc;
690                         
691                         lend= VecLenf(v2, v1);
692                         lenc= VecLenf(shi->co, v1);
693                         
694                         if(lend==0.0f) {
695                                 shi->u=shi->v= 0.0f;
696                         }
697                         else {
698                                 shi->u= - (1.0f - lenc/lend);
699                                 shi->v= 0.0f;
700                         }
701                         
702                         if(shi->osatex) {
703                                 shi->dx_u=  0.0f;
704                                 shi->dx_v=  0.0f;
705                                 shi->dy_u=  0.0f;
706                                 shi->dy_v=  0.0f;
707                         }
708                 }
709                 else {
710                         /* most of this could become re-used for faces */
711                         float detsh, t00, t10, t01, t11, xn, yn, zn;
712                         int axis1, axis2;
713
714                         /* find most stable axis to project */
715                         xn= fabs(shi->facenor[0]);
716                         yn= fabs(shi->facenor[1]);
717                         zn= fabs(shi->facenor[2]);
718
719                         if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
720                         else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
721                         else { axis1= 1; axis2= 2; }
722
723                         /* compute u,v and derivatives */
724                         t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
725                         t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
726
727                         detsh= 1.0f/(t00*t11-t10*t01);
728                         t00*= detsh; t01*=detsh; 
729                         t10*=detsh; t11*=detsh;
730
731                         shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
732                         shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
733                         if(shi->osatex) {
734                                 shi->dx_u=  shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
735                                 shi->dx_v=  shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
736                                 shi->dy_u=  shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
737                                 shi->dy_v=  shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
738                         }
739
740                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
741                         CLAMP(shi->u, -2.0f, 1.0f);
742                         CLAMP(shi->v, -2.0f, 1.0f);
743                 }
744         }       
745 }
746
747 void shade_input_set_normals(ShadeInput *shi)
748 {
749         float u= shi->u, v= shi->v;
750         float l= 1.0f+u+v;
751         
752         /* calculate vertexnormals */
753         if(shi->vlr->flag & R_SMOOTH) {
754                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
755                 
756                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
757                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
758                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
759                 
760                 Normalize(shi->vn);
761         }
762         else
763                 VECCOPY(shi->vn, shi->facenor);
764         
765         /* used in nodes */
766         VECCOPY(shi->vno, shi->vn);
767
768 }
769
770 /* use by raytrace, sss, bake to flip into the right direction */
771 void shade_input_flip_normals(ShadeInput *shi)
772 {
773         shi->facenor[0]= -shi->facenor[0];
774         shi->facenor[1]= -shi->facenor[1];
775         shi->facenor[2]= -shi->facenor[2];
776
777         shi->vn[0]= -shi->vn[0];
778         shi->vn[1]= -shi->vn[1];
779         shi->vn[2]= -shi->vn[2];
780
781         shi->vno[0]= -shi->vno[0];
782         shi->vno[1]= -shi->vno[1];
783         shi->vno[2]= -shi->vno[2];
784
785         shi->flippednor= !shi->flippednor;
786 }
787
788 void shade_input_set_shade_texco(ShadeInput *shi)
789 {
790         ObjectInstanceRen *obi= shi->obi;
791         ObjectRen *obr= shi->obr;
792         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
793         float u= shi->u, v= shi->v;
794         float l= 1.0f+u+v, dl;
795         int mode= shi->mode;            /* or-ed result for all nodes */
796         short texco= shi->mat->texco;
797
798         /* calculate dxno */
799         if(shi->vlr->flag & R_SMOOTH) {
800                 
801                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
802                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
803                         
804                         dl= shi->dx_u+shi->dx_v;
805                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
806                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
807                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
808                         dl= shi->dy_u+shi->dy_v;
809                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
810                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
811                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
812                         
813                 }
814         }
815
816         /* calc tangents */
817         if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
818                 float *tangent, *s1, *s2, *s3;
819                 float tl, tu, tv;
820
821                 if(shi->vlr->flag & R_SMOOTH) {
822                         tl= l;
823                         tu= u;
824                         tv= v;
825                 }
826                 else {
827                         /* qdn: flat faces have tangents too,
828                            could pick either one, using average here */
829                         tl= 1.0f/3.0f;
830                         tu= -1.0f/3.0f;
831                         tv= -1.0f/3.0f;
832                 }
833
834                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
835                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
836
837                 if(mode & MA_TANGENT_V) {
838                         s1 = RE_vertren_get_tangent(obr, v1, 0);
839                         s2 = RE_vertren_get_tangent(obr, v2, 0);
840                         s3 = RE_vertren_get_tangent(obr, v3, 0);
841
842                         if(s1 && s2 && s3) {
843                                 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
844                                 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
845                                 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
846
847                                 if(obi->flag & R_TRANSFORMED)
848                                         Mat3MulVecfl(obi->nmat, shi->tang);
849
850                                 Normalize(shi->tang);
851                                 VECCOPY(shi->nmaptang, shi->tang);
852                         }
853                 }
854
855                 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
856                         tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
857
858                         if(tangent) {
859                                 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
860
861                                 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
862
863                                 s1= &tangent[j1*3];
864                                 s2= &tangent[j2*3];
865                                 s3= &tangent[j3*3];
866
867                                 shi->nmaptang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
868                                 shi->nmaptang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
869                                 shi->nmaptang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
870
871                                 if(obi->flag & R_TRANSFORMED)
872                                         Mat3MulVecfl(obi->nmat, shi->nmaptang);
873
874                                 Normalize(shi->nmaptang);
875                         }
876                 }
877         }
878
879         if(mode & MA_STR_SURFDIFF) {
880                 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
881
882                 if(surfnor) {
883                         VECCOPY(shi->surfnor, surfnor)
884                         if(obi->flag & R_TRANSFORMED)
885                                 Mat3MulVecfl(obi->nmat, shi->surfnor);
886                 }
887                 else
888                         VECCOPY(shi->surfnor, shi->vn)
889
890                 shi->surfdist= 0.0f;
891         }
892         
893         if(R.r.mode & R_SPEED) {
894                 float *s1, *s2, *s3;
895                 
896                 s1= RE_vertren_get_winspeed(obi, v1, 0);
897                 s2= RE_vertren_get_winspeed(obi, v2, 0);
898                 s3= RE_vertren_get_winspeed(obi, v3, 0);
899                 if(s1 && s2 && s3) {
900                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
901                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
902                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
903                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
904                 }
905                 else {
906                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
907                 }
908         }
909
910         /* pass option forces UV calc */
911         if(shi->passflag & SCE_PASS_UV)
912                 texco |= (NEED_UV|TEXCO_UV);
913         
914         /* texture coordinates. shi->dxuv shi->dyuv have been set */
915         if(texco & NEED_UV) {
916                 
917                 if(texco & TEXCO_ORCO) {
918                         if(v1->orco) {
919                                 float *o1, *o2, *o3;
920                                 
921                                 o1= v1->orco;
922                                 o2= v2->orco;
923                                 o3= v3->orco;
924                                 
925                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
926                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
927                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
928                                 
929                                 if(shi->osatex) {
930                                         dl= shi->dx_u+shi->dx_v;
931                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
932                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
933                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
934                                         dl= shi->dy_u+shi->dy_v;
935                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
936                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
937                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
938                                 }
939                         }
940
941                         VECCOPY(shi->duplilo, obi->dupliorco);
942                 }
943                 
944                 if(texco & TEXCO_GLOB) {
945                         VECCOPY(shi->gl, shi->co);
946                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
947                         if(shi->osatex) {
948                                 VECCOPY(shi->dxgl, shi->dxco);
949                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
950                                 VECCOPY(shi->dygl, shi->dyco);
951                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
952                         }
953                 }
954                 
955                 if(texco & TEXCO_STRAND) {
956                         shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
957                         if(shi->osatex) {
958                                 dl= shi->dx_u+shi->dx_v;
959                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
960                                 dl= shi->dy_u+shi->dy_v;
961                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
962                         }
963                 }
964                                 
965                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
966                         VlakRen *vlr= shi->vlr;
967                         MTFace *tface;
968                         MCol *mcol;
969                         char *name;
970                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
971
972                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
973                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
974
975                         shi->totuv= 0;
976                         shi->totcol= 0;
977                         shi->actuv= obr->actmtface;
978                         shi->actcol= obr->actmcol;
979
980                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
981                                 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
982                                         ShadeInputCol *scol= &shi->col[i];
983                                         char *cp1, *cp2, *cp3;
984                                         
985                                         shi->totcol++;
986                                         scol->name= name;
987
988                                         cp1= (char *)(mcol+j1);
989                                         cp2= (char *)(mcol+j2);
990                                         cp3= (char *)(mcol+j3);
991                                         
992                                         scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
993                                         scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
994                                         scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
995                                 }
996
997                                 if(shi->totcol) {
998                                         shi->vcol[0]= shi->col[0].col[0];
999                                         shi->vcol[1]= shi->col[0].col[1];
1000                                         shi->vcol[2]= shi->col[0].col[2];
1001                                         shi->vcol[3]= 1.0f;
1002                                 }
1003                                 else {
1004                                         shi->vcol[0]= 0.0f;
1005                                         shi->vcol[1]= 0.0f;
1006                                         shi->vcol[2]= 0.0f;
1007                                         shi->vcol[3]= 1.0f;
1008                                 }
1009                         }
1010
1011                         for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1012                                 ShadeInputUV *suv= &shi->uv[i];
1013                                 float *uv1, *uv2, *uv3;
1014
1015                                 shi->totuv++;
1016                                 suv->name= name;
1017                                 
1018                                 uv1= tface->uv[j1];
1019                                 uv2= tface->uv[j2];
1020                                 uv3= tface->uv[j3];
1021                                 
1022                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1023                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1024                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1025
1026                                 if(shi->osatex) {
1027                                         float duv[2];
1028                                         
1029                                         dl= shi->dx_u+shi->dx_v;
1030                                         duv[0]= shi->dx_u; 
1031                                         duv[1]= shi->dx_v;
1032                                         
1033                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1034                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1035                                         
1036                                         dl= shi->dy_u+shi->dy_v;
1037                                         duv[0]= shi->dy_u; 
1038                                         duv[1]= shi->dy_v;
1039                                         
1040                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1041                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1042                                 }
1043
1044                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1045                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1046                                                 shi->vcol[0]= 1.0f;
1047                                                 shi->vcol[1]= 1.0f;
1048                                                 shi->vcol[2]= 1.0f;
1049                                                 shi->vcol[3]= 1.0f;
1050                                         }
1051                                         if(tface && tface->tpage)
1052                                                 render_realtime_texture(shi, tface->tpage);
1053                                 }
1054                         }
1055
1056                         shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1057                         shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1058                         shi->dupliuv[2]= 0.0f;
1059
1060                         if(shi->totuv == 0) {
1061                                 ShadeInputUV *suv= &shi->uv[0];
1062
1063                                 suv->uv[0]= 2.0f*(u+.5f);
1064                                 suv->uv[1]= 2.0f*(v+.5f);
1065                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1066                                 
1067                                 if(mode & MA_FACETEXTURE) {
1068                                         /* no tface? set at 1.0f */
1069                                         shi->vcol[0]= 1.0f;
1070                                         shi->vcol[1]= 1.0f;
1071                                         shi->vcol[2]= 1.0f;
1072                                         shi->vcol[3]= 1.0f;
1073                                 }
1074                         }
1075                 }
1076                 
1077                 if(texco & TEXCO_NORM) {
1078                         shi->orn[0]= -shi->vn[0];
1079                         shi->orn[1]= -shi->vn[1];
1080                         shi->orn[2]= -shi->vn[2];
1081                 }
1082                 
1083                 if(mode & MA_RADIO) {
1084                         float *r1, *r2, *r3;
1085                         
1086                         r1= RE_vertren_get_rad(obr, v1, 0);
1087                         r2= RE_vertren_get_rad(obr, v2, 0);
1088                         r3= RE_vertren_get_rad(obr, v3, 0);
1089                         
1090                         if(r1 && r2 && r3) {
1091                                 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
1092                                 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
1093                                 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
1094                         }
1095                         else
1096                                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1097                 }
1098                 else
1099                         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1100                 
1101                 if(texco & TEXCO_REFL) {
1102                         /* mirror reflection color textures (and envmap) */
1103                         calc_R_ref(shi);        /* wrong location for normal maps! XXXXXXXXXXXXXX */
1104                 }
1105                 
1106                 if(texco & TEXCO_STRESS) {
1107                         float *s1, *s2, *s3;
1108                         
1109                         s1= RE_vertren_get_stress(obr, v1, 0);
1110                         s2= RE_vertren_get_stress(obr, v2, 0);
1111                         s3= RE_vertren_get_stress(obr, v3, 0);
1112                         if(s1 && s2 && s3) {
1113                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1114                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
1115                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
1116                         }
1117                         else shi->stress= 0.0f;
1118                 }
1119                 
1120                 if(texco & TEXCO_TANGENT) {
1121                         if((mode & MA_TANGENT_V)==0) {
1122                                 /* just prevent surprises */
1123                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1124                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1125                         }
1126                 }
1127         }
1128         else
1129                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1130         
1131         /* this only avalailable for scanline renders */
1132         if(shi->depth==0) {
1133                 float x= shi->xs;
1134                 float y= shi->ys;
1135                 
1136                 if(texco & TEXCO_WINDOW) {
1137                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1138                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1139                         shi->winco[2]= 0.0f;
1140                         if(shi->osatex) {
1141                                 shi->dxwin[0]= 2.0f/(float)R.winx;
1142                                 shi->dywin[1]= 2.0f/(float)R.winy;
1143                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1144                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
1145                         }
1146                 }
1147
1148                 if(texco & TEXCO_STICKY) {
1149                         float *s1, *s2, *s3;
1150                         
1151                         s1= RE_vertren_get_sticky(obr, v1, 0);
1152                         s2= RE_vertren_get_sticky(obr, v2, 0);
1153                         s3= RE_vertren_get_sticky(obr, v3, 0);
1154                         
1155                         if(s1 && s2 && s3) {
1156                                 float winmat[4][4], ho1[4], ho2[4], ho3[4];
1157                                 float Zmulx, Zmuly;
1158                                 float hox, hoy, l, dl, u, v;
1159                                 float s00, s01, s10, s11, detsh;
1160                                 
1161                                 /* old globals, localized now */
1162                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
1163
1164                                 if(shi->obi->flag & R_TRANSFORMED)
1165                                         zbuf_make_winmat(&R, shi->obi->mat, winmat);
1166                                 else
1167                                         zbuf_make_winmat(&R, NULL, winmat);
1168
1169                                 zbuf_render_project(winmat, v1->co, ho1);
1170                                 zbuf_render_project(winmat, v2->co, ho2);
1171                                 zbuf_render_project(winmat, v3->co, ho3);
1172                                 
1173                                 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1174                                 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1175                                 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1176                                 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1177                                 
1178                                 detsh= s00*s11-s10*s01;
1179                                 s00/= detsh; s01/=detsh; 
1180                                 s10/=detsh; s11/=detsh;
1181                                 
1182                                 /* recalc u and v again */
1183                                 hox= x/Zmulx -1.0f;
1184                                 hoy= y/Zmuly -1.0f;
1185                                 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1186                                 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1187                                 l= 1.0f+u+v;
1188                                 
1189                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1190                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1191                                 shi->sticky[2]= 0.0f;
1192                                 
1193                                 if(shi->osatex) {
1194                                         float dxuv[2], dyuv[2];
1195                                         dxuv[0]=  s11/Zmulx;
1196                                         dxuv[1]=  - s01/Zmulx;
1197                                         dyuv[0]=  - s10/Zmuly;
1198                                         dyuv[1]=  s00/Zmuly;
1199                                         
1200                                         dl= dxuv[0] + dxuv[1];
1201                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1202                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1203                                         dl= dyuv[0] + dyuv[1];
1204                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1205                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1206                                 }
1207                         }
1208                 }
1209         } /* else {
1210          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
1211         */
1212 }
1213
1214 /* ****************** ShadeSample ************************************** */
1215
1216 /* initialize per part, not per pixel! */
1217 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1218 {
1219         
1220         memset(shi, 0, sizeof(ShadeInput));
1221         
1222         shi->sample= sample;
1223         shi->thread= pa->thread;
1224         shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
1225         shi->lay= rl->lay;
1226         shi->layflag= rl->layflag;
1227         shi->passflag= rl->passflag;
1228         shi->combinedflag= ~rl->pass_xor;
1229         shi->mat_override= rl->mat_override;
1230         shi->light_override= rl->light_override;
1231 //      shi->rl= rl;
1232         /* note shi.depth==0  means first hit, not raytracing */
1233         
1234 }
1235
1236 /* initialize per part, not per pixel! */
1237 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1238 {
1239         int a, tot;
1240         
1241         tot= R.osa==0?1:R.osa;
1242         
1243         for(a=0; a<tot; a++) {
1244                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1245                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1246         }
1247         
1248         get_sample_layers(pa, rl, ssamp->rlpp);
1249 }
1250
1251 /* Do AO or (future) GI */
1252 void shade_samples_do_AO(ShadeSample *ssamp)
1253 {
1254         ShadeInput *shi;
1255         int sample;
1256         
1257         if(!(R.r.mode & R_SHADOW))
1258                 return;
1259         if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1260                 return;
1261         
1262         if(R.wrld.mode & WO_AMB_OCC) {
1263                 shi= &ssamp->shi[0];
1264
1265                 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & SCE_PASS_AO))
1266                         || (shi->passflag & SCE_PASS_AO))
1267                         for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
1268                                 if(!(shi->mode & MA_SHLESS))
1269                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
1270         }
1271 }
1272
1273
1274 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1275 {
1276         ShadeInput *shi;
1277         float xs, ys;
1278         
1279         ssamp->tot= 0;
1280         
1281         for(shi= ssamp->shi; ps; ps= ps->next) {
1282                 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1283                 
1284                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
1285                         unsigned short curmask= ps->mask;
1286                         
1287                         /* full osa is only set for OSA renders */
1288                         if(shi->vlr->flag & R_FULL_OSA) {
1289                                 short shi_cp= 0, samp;
1290                                 
1291                                 for(samp=0; samp<R.osa; samp++) {
1292                                         if(curmask & (1<<samp)) {
1293                                                 xs= (float)x + R.jit[samp][0] + 0.5f;   /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1294                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
1295                                                 
1296                                                 if(shi_cp)
1297                                                         shade_input_copy_triangle(shi, shi-1);
1298                                                 
1299                                                 shi->mask= (1<<samp);
1300 //                                              shi->rl= ssamp->rlpp[samp];
1301                                                 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1302                                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
1303                                                 shade_input_set_uv(shi);
1304                                                 shade_input_set_normals(shi);
1305                                                 
1306                                                 shi_cp= 1;
1307                                                 shi++;
1308                                         }
1309                                 }
1310                         }
1311                         else {
1312                                 if(R.osa) {
1313                                         short b= R.samples->centmask[curmask];
1314                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1315                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1316                                 }
1317                                 else {
1318                                         xs= (float)x + 0.5f;
1319                                         ys= (float)y + 0.5f;
1320                                 }
1321                                 shi->mask= curmask;
1322                                 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1323                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
1324                                 shade_input_set_uv(shi);
1325                                 shade_input_set_normals(shi);
1326                                 shi++;
1327                         }
1328                         
1329                         /* total sample amount, shi->sample is static set in initialize */
1330                         if(shi!=ssamp->shi)
1331                                 ssamp->tot= (shi-1)->sample + 1;
1332                 }
1333         }
1334 }
1335
1336 /* shades samples, returns true if anything happened */
1337 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1338 {
1339         shade_samples_fill_with_ps(ssamp, ps, x, y);
1340         
1341         if(ssamp->tot) {
1342                 ShadeInput *shi= ssamp->shi;
1343                 ShadeResult *shr= ssamp->shr;
1344                 int samp;
1345                 
1346                 /* if shadow or AO? */
1347                 shade_samples_do_AO(ssamp);
1348                 
1349                 /* if shade (all shadepinputs have same passflag) */
1350                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1351
1352                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1353                                 shade_input_set_shade_texco(shi);
1354                                 shade_input_do_shade(shi, shr);
1355                         }
1356                 }
1357                 
1358                 return 1;
1359         }
1360         return 0;
1361 }
1362