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