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