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