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