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