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