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