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