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