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