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