Cycles: svn merge -r41225:41232 ^/trunk/blender
[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 /* initialise 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                 VECMUL(shi->vn, -1.0f);
131                 VECMUL(shi->facenor, -1.0f);
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 /*
217         if(1 || shi->passflag & SCE_PASS_RAYHITS)
218         {
219                 shr->rayhits[0] = (float)shi->raycounter.faces.test;
220                 shr->rayhits[1] = (float)shi->raycounter.bb.hit;
221                 shr->rayhits[2] = 0.0;
222                 shr->rayhits[3] = 1.0;
223         }
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 optimizations
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                                 }
473
474                                 if(shi->totcol) {
475                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
476                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
477                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
478                                 }
479                                 else {
480                                         shi->vcol[0]= 0.0f;
481                                         shi->vcol[1]= 0.0f;
482                                         shi->vcol[2]= 0.0f;
483                                 }
484                         }
485
486                         for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
487                                 ShadeInputUV *suv= &shi->uv[i];
488
489                                 shi->totuv++;
490                                 suv->name= name;
491
492                                 if(strandbuf->overrideuv == i) {
493                                         suv->uv[0]= -1.0f;
494                                         suv->uv[1]= spoint->strandco;
495                                         suv->uv[2]= 0.0f;
496                                 }
497                                 else {
498                                         suv->uv[0]= -1.0f + 2.0f*uv[0];
499                                         suv->uv[1]= -1.0f + 2.0f*uv[1];
500                                         suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
501                                 }
502
503                                 if(shi->osatex) {
504                                         suv->dxuv[0]= 0.0f;
505                                         suv->dxuv[1]= 0.0f;
506                                         suv->dyuv[0]= 0.0f;
507                                         suv->dyuv[1]= 0.0f;
508                                 }
509
510                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
511                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
512                                                 shi->vcol[0]= 1.0f;
513                                                 shi->vcol[1]= 1.0f;
514                                                 shi->vcol[2]= 1.0f;
515                                         }
516                                 }
517                         }
518
519                         if(shi->totuv == 0) {
520                                 ShadeInputUV *suv= &shi->uv[0];
521
522                                 suv->uv[0]= 0.0f;
523                                 suv->uv[1]= spoint->strandco;
524                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
525                                 
526                                 if(mode & MA_FACETEXTURE) {
527                                         /* no tface? set at 1.0f */
528                                         shi->vcol[0]= 1.0f;
529                                         shi->vcol[1]= 1.0f;
530                                         shi->vcol[2]= 1.0f;
531                                 }
532                         }
533
534                 }
535
536                 if(texco & TEXCO_NORM) {
537                         shi->orn[0]= -shi->vn[0];
538                         shi->orn[1]= -shi->vn[1];
539                         shi->orn[2]= -shi->vn[2];
540                 }
541
542                 if(texco & TEXCO_STRESS) {
543                         /* not supported */
544                 }
545
546                 if(texco & TEXCO_TANGENT) {
547                         if((mode & MA_TANGENT_V)==0) {
548                                 /* just prevent surprises */
549                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
550                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
551                         }
552                 }
553         }
554
555         /* this only avalailable for scanline renders */
556         if(shi->depth==0) {
557                 if(texco & TEXCO_WINDOW) {
558                         shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
559                         shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
560                         shi->winco[2]= 0.0f;
561
562                         /* not supported */
563                         if(shi->osatex) {
564                                 shi->dxwin[0]= 0.0f;
565                                 shi->dywin[1]= 0.0f;
566                                 shi->dxwin[0]= 0.0f;
567                                 shi->dywin[1]= 0.0f;
568                         }
569                 }
570
571                 if(texco & TEXCO_STICKY) {
572                         /* not supported */
573                 }
574         }
575         
576         if (shi->do_manage) {
577                 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
578                         srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
579                 }
580         }
581         
582 }
583
584 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
585 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])
586 {
587         /* returns not normalized, so is in viewplane coords */
588         calc_view_vector(view, x, y);
589         
590         if(shi->mat->material_type == MA_TYPE_WIRE) {
591                 /* wire cannot use normal for calculating shi->co, so
592                  * we reconstruct the coordinate less accurate */
593                 if(R.r.mode & R_ORTHO)
594                         calc_renderco_ortho(co, x, y, z);
595                 else
596                         calc_renderco_zbuf(co, view, z);
597         }
598         else {
599                 /* for non-wire, intersect with the triangle to get the exact coord */
600                 float fac, dface, v1[3];
601                 
602                 copy_v3_v3(v1, shi->v1->co);
603                 if(shi->obi->flag & R_TRANSFORMED)
604                         mul_m4_v3(shi->obi->mat, v1);
605                 
606                 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
607                 
608                 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
609                 if(R.r.mode & R_ORTHO) {
610                         /* x and y 3d coordinate can be derived from pixel coord and winmat */
611                         float fx= 2.0f/(R.winx*R.winmat[0][0]);
612                         float fy= 2.0f/(R.winy*R.winmat[1][1]);
613                         
614                         co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
615                         co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
616                         
617                         /* using a*x + b*y + c*z = d equation, (a b c) is normal */
618                         if(shi->facenor[2]!=0.0f)
619                                 co[2]= (dface - shi->facenor[0]*co[0] - shi->facenor[1]*co[1])/shi->facenor[2];
620                         else
621                                 co[2]= 0.0f;
622                         
623                         if(dxco && dyco) {
624                                 dxco[0]= fx;
625                                 dxco[1]= 0.0f;
626                                 if(shi->facenor[2]!=0.0f)
627                                         dxco[2]= -(shi->facenor[0]*fx)/shi->facenor[2];
628                                 else 
629                                         dxco[2]= 0.0f;
630                                 
631                                 dyco[0]= 0.0f;
632                                 dyco[1]= fy;
633                                 if(shi->facenor[2]!=0.0f)
634                                         dyco[2]= -(shi->facenor[1]*fy)/shi->facenor[2];
635                                 else 
636                                         dyco[2]= 0.0f;
637                                 
638                                 if(dxyview) {
639                                         if(co[2]!=0.0f) fac= 1.0f/co[2]; else fac= 0.0f;
640                                         dxyview[0]= -R.viewdx*fac;
641                                         dxyview[1]= -R.viewdy*fac;
642                                 }
643                         }
644                 }
645                 else {
646                         float div;
647                         
648                         div= shi->facenor[0]*view[0] + shi->facenor[1]*view[1] + shi->facenor[2]*view[2];
649                         if (div!=0.0f) fac= dface/div;
650                         else fac= 0.0f;
651                         
652                         co[0]= fac*view[0];
653                         co[1]= fac*view[1];
654                         co[2]= fac*view[2];
655                         
656                         /* pixel dx/dy for render coord */
657                         if(dxco && dyco) {
658                                 float u= dface/(div - R.viewdx*shi->facenor[0]);
659                                 float v= dface/(div - R.viewdy*shi->facenor[1]);
660                                 
661                                 dxco[0]= co[0]- (view[0]-R.viewdx)*u;
662                                 dxco[1]= co[1]- (view[1])*u;
663                                 dxco[2]= co[2]- (view[2])*u;
664                                 
665                                 dyco[0]= co[0]- (view[0])*v;
666                                 dyco[1]= co[1]- (view[1]-R.viewdy)*v;
667                                 dyco[2]= co[2]- (view[2])*v;
668                                 
669                                 if(dxyview) {
670                                         if(fac!=0.0f) fac= 1.0f/fac;
671                                         dxyview[0]= -R.viewdx*fac;
672                                         dxyview[1]= -R.viewdy*fac;
673                                 }
674                         }
675                 }
676         }
677         
678         /* set camera coords - for scanline, it's always 0.0,0.0,0.0 (render is in camera space)
679          * however for raytrace it can be different - the position of the last intersection */
680         shi->camera_co[0] = shi->camera_co[1] = shi->camera_co[2] = 0.0f;
681         
682         /* cannot normalize earlier, code above needs it at viewplane level */
683         normalize_v3(view);
684 }
685
686 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
687 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
688 {
689         float *dxyview= NULL, *dxco= NULL, *dyco= NULL;
690         
691         /* currently in use for dithering (soft shadow), node preview, irregular shad */
692         shi->xs= (int)xs;
693         shi->ys= (int)ys;
694
695         /* original scanline coordinate without jitter */
696         shi->scanco[0]= x;
697         shi->scanco[1]= y;
698         shi->scanco[2]= z;
699
700         /* check if we need derivatives */
701         if(shi->osatex || (R.r.mode & R_SHADOW)) {
702                 dxco= shi->dxco;
703                 dyco= shi->dyco;
704
705                 if((shi->mat->texco & TEXCO_REFL))
706                         dxyview= &shi->dxview;
707         }
708
709         shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
710 }
711
712 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
713 void shade_input_set_uv(ShadeInput *shi)
714 {
715         VlakRen *vlr= shi->vlr;
716         
717         if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
718                 float v1[3], v2[3], v3[3];
719
720                 copy_v3_v3(v1, shi->v1->co);
721                 copy_v3_v3(v2, shi->v2->co);
722                 copy_v3_v3(v3, shi->v3->co);
723
724                 if(shi->obi->flag & R_TRANSFORMED) {
725                         mul_m4_v3(shi->obi->mat, v1);
726                         mul_m4_v3(shi->obi->mat, v2);
727                         mul_m4_v3(shi->obi->mat, v3);
728                 }
729
730                 /* exception case for wire render of edge */
731                 if(vlr->v2==vlr->v3) {
732                         float lend, lenc;
733                         
734                         lend= len_v3v3(v2, v1);
735                         lenc= len_v3v3(shi->co, v1);
736                         
737                         if(lend==0.0f) {
738                                 shi->u=shi->v= 0.0f;
739                         }
740                         else {
741                                 shi->u= - (1.0f - lenc/lend);
742                                 shi->v= 0.0f;
743                         }
744                         
745                         if(shi->osatex) {
746                                 shi->dx_u=  0.0f;
747                                 shi->dx_v=  0.0f;
748                                 shi->dy_u=  0.0f;
749                                 shi->dy_v=  0.0f;
750                         }
751                 }
752                 else {
753                         /* most of this could become re-used for faces */
754                         float detsh, t00, t10, t01, t11, xn, yn, zn;
755                         int axis1, axis2;
756
757                         /* find most stable axis to project */
758                         xn= fabs(shi->facenor[0]);
759                         yn= fabs(shi->facenor[1]);
760                         zn= fabs(shi->facenor[2]);
761
762                         if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
763                         else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
764                         else { axis1= 1; axis2= 2; }
765
766                         /* compute u,v and derivatives */
767                         t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
768                         t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
769
770                         detsh= (t00*t11-t10*t01);
771                         detsh= (detsh != 0.0f)? 1.0f/detsh: 0.0f;
772                         t00*= detsh; t01*=detsh; 
773                         t10*=detsh; t11*=detsh;
774
775                         shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
776                         shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
777                         if(shi->osatex) {
778                                 shi->dx_u=  shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
779                                 shi->dx_v=  shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
780                                 shi->dy_u=  shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
781                                 shi->dy_v=  shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
782                         }
783
784                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
785                         CLAMP(shi->u, -2.0f, 1.0f);
786                         CLAMP(shi->v, -2.0f, 1.0f);
787                 }
788         }       
789 }
790
791 void shade_input_set_normals(ShadeInput *shi)
792 {
793         float u= shi->u, v= shi->v;
794         float l= 1.0f+u+v;
795
796         shi->flippednor = 0;
797
798         /* test flip normals to viewing direction */
799         if(!(shi->vlr->flag & R_TANGENT)) {
800                 if(dot_v3v3(shi->facenor, shi->view) < 0.0f) {
801                         negate_v3(shi->facenor);
802                         shi->flippednor= 1;
803                 }
804         }
805         
806         /* calculate vertexnormals */
807         if(shi->vlr->flag & R_SMOOTH) {
808                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
809
810                 if(shi->flippednor) {
811                         negate_v3(n1);
812                         negate_v3(n2);
813                         negate_v3(n3);
814                 }
815                 
816                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
817                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
818                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
819
820                 // use unnormalized normal (closer to games)
821                 copy_v3_v3(shi->nmapnorm, shi->vn);
822                 
823                 normalize_v3(shi->vn);
824         }
825         else
826         {
827                 copy_v3_v3(shi->vn, shi->facenor);
828                 copy_v3_v3(shi->nmapnorm, shi->vn);
829         }
830         
831         /* used in nodes */
832         copy_v3_v3(shi->vno, shi->vn);
833
834         /* flip normals to viewing direction */
835         if(!(shi->vlr->flag & R_TANGENT))
836                 if(dot_v3v3(shi->facenor, shi->view) < 0.0f)
837                         shade_input_flip_normals(shi);
838 }
839
840 /* XXX shi->flippednor messes up otherwise */
841 void shade_input_set_vertex_normals(ShadeInput *shi)
842 {
843         float u= shi->u, v= shi->v;
844         float l= 1.0f+u+v;
845         
846         /* calculate vertexnormals */
847         if(shi->vlr->flag & R_SMOOTH) {
848                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
849                 
850                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
851                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
852                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
853                 
854                 // use unnormalized normal (closer to games)
855                 copy_v3_v3(shi->nmapnorm, shi->vn);
856                 
857                 normalize_v3(shi->vn);
858         }
859         else
860         {
861                 copy_v3_v3(shi->vn, shi->facenor);
862                 copy_v3_v3(shi->nmapnorm, shi->vn);
863         }
864         
865         /* used in nodes */
866         copy_v3_v3(shi->vno, shi->vn);
867 }
868
869
870 /* use by raytrace, sss, bake to flip into the right direction */
871 void shade_input_flip_normals(ShadeInput *shi)
872 {
873         shi->facenor[0]= -shi->facenor[0];
874         shi->facenor[1]= -shi->facenor[1];
875         shi->facenor[2]= -shi->facenor[2];
876
877         shi->vn[0]= -shi->vn[0];
878         shi->vn[1]= -shi->vn[1];
879         shi->vn[2]= -shi->vn[2];
880
881         shi->vno[0]= -shi->vno[0];
882         shi->vno[1]= -shi->vno[1];
883         shi->vno[2]= -shi->vno[2];
884
885         shi->nmapnorm[0] = -shi->nmapnorm[0];
886         shi->nmapnorm[1] = -shi->nmapnorm[1];
887         shi->nmapnorm[2] = -shi->nmapnorm[2];
888
889         shi->flippednor= !shi->flippednor;
890 }
891
892 void shade_input_set_shade_texco(ShadeInput *shi)
893 {
894         ObjectInstanceRen *obi= shi->obi;
895         ObjectRen *obr= shi->obr;
896         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
897         float u= shi->u, v= shi->v;
898         float l= 1.0f+u+v, dl;
899         int mode= shi->mode;            /* or-ed result for all nodes */
900         short texco= shi->mat->texco;
901
902         /* calculate dxno */
903         if(shi->vlr->flag & R_SMOOTH) {
904                 
905                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
906                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
907                         
908                         dl= shi->dx_u+shi->dx_v;
909                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
910                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
911                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
912                         dl= shi->dy_u+shi->dy_v;
913                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
914                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
915                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
916                         
917                 }
918         }
919
920         /* calc tangents */
921         if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
922                 float *tangent, *s1, *s2, *s3;
923                 float tl, tu, tv;
924
925                 if(shi->vlr->flag & R_SMOOTH) {
926                         tl= l;
927                         tu= u;
928                         tv= v;
929                 }
930                 else {
931                         /* qdn: flat faces have tangents too,
932                            could pick either one, using average here */
933                         tl= 1.0f/3.0f;
934                         tu= -1.0f/3.0f;
935                         tv= -1.0f/3.0f;
936                 }
937
938                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
939                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
940
941                 if(mode & MA_TANGENT_V) {
942                         s1 = RE_vertren_get_tangent(obr, v1, 0);
943                         s2 = RE_vertren_get_tangent(obr, v2, 0);
944                         s3 = RE_vertren_get_tangent(obr, v3, 0);
945
946                         if(s1 && s2 && s3) {
947                                 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
948                                 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
949                                 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
950
951                                 if(obi->flag & R_TRANSFORMED)
952                                         mul_m3_v3(obi->nmat, shi->tang);
953
954                                 normalize_v3(shi->tang);
955                                 copy_v3_v3(shi->nmaptang, shi->tang);
956                         }
957                 }
958
959                 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
960                         tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
961
962                         if(tangent) {
963                                 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
964                                 float c0[3], c1[3], c2[3];
965
966                                 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
967
968                                 copy_v3_v3(c0, &tangent[j1*4]);
969                                 copy_v3_v3(c1, &tangent[j2*4]);
970                                 copy_v3_v3(c2, &tangent[j3*4]);
971
972                                 // keeping tangents normalized at vertex level
973                                 // corresponds better to how it's done in game engines
974                                 if(obi->flag & R_TRANSFORMED)
975                                 {
976                                         mul_mat3_m4_v3(obi->mat, c0); normalize_v3(c0);
977                                         mul_mat3_m4_v3(obi->mat, c1); normalize_v3(c1);
978                                         mul_mat3_m4_v3(obi->mat, c2); normalize_v3(c2);
979                                 }
980                                 
981                                 // we don't normalize the interpolated TBN tangent
982                                 // corresponds better to how it's done in game engines
983                                 shi->nmaptang[0]= (tl*c2[0] - tu*c0[0] - tv*c1[0]);
984                                 shi->nmaptang[1]= (tl*c2[1] - tu*c0[1] - tv*c1[1]);
985                                 shi->nmaptang[2]= (tl*c2[2] - tu*c0[2] - tv*c1[2]);
986
987                                 // the sign is the same for all 3 vertices of any
988                                 // non degenerate triangle.
989                                 shi->nmaptang[3]= tangent[j1*4+3];
990                         }
991                 }
992         }
993
994         if(mode & MA_STR_SURFDIFF) {
995                 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
996
997                 if(surfnor) {
998                         copy_v3_v3(shi->surfnor, surfnor);
999                         if(obi->flag & R_TRANSFORMED)
1000                                 mul_m3_v3(obi->nmat, shi->surfnor);
1001                 }
1002                 else
1003                         copy_v3_v3(shi->surfnor, shi->vn);
1004
1005                 shi->surfdist= 0.0f;
1006         }
1007         
1008         if(R.r.mode & R_SPEED) {
1009                 float *s1, *s2, *s3;
1010                 
1011                 s1= RE_vertren_get_winspeed(obi, v1, 0);
1012                 s2= RE_vertren_get_winspeed(obi, v2, 0);
1013                 s3= RE_vertren_get_winspeed(obi, v3, 0);
1014                 if(s1 && s2 && s3) {
1015                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
1016                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
1017                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
1018                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
1019                 }
1020                 else {
1021                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
1022                 }
1023         }
1024
1025         /* pass option forces UV calc */
1026         if(shi->passflag & SCE_PASS_UV)
1027                 texco |= (NEED_UV|TEXCO_UV);
1028         
1029         /* texture coordinates. shi->dxuv shi->dyuv have been set */
1030         if(texco & NEED_UV) {
1031                 
1032                 if(texco & TEXCO_ORCO) {
1033                         if(v1->orco) {
1034                                 float *o1, *o2, *o3;
1035                                 
1036                                 o1= v1->orco;
1037                                 o2= v2->orco;
1038                                 o3= v3->orco;
1039                                 
1040                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
1041                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
1042                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
1043                                 
1044                                 if(shi->osatex) {
1045                                         dl= shi->dx_u+shi->dx_v;
1046                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
1047                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
1048                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
1049                                         dl= shi->dy_u+shi->dy_v;
1050                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
1051                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
1052                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
1053                                 }
1054                         }
1055
1056                         copy_v3_v3(shi->duplilo, obi->dupliorco);
1057                 }
1058                 
1059                 if(texco & TEXCO_GLOB) {
1060                         copy_v3_v3(shi->gl, shi->co);
1061                         mul_m4_v3(R.viewinv, shi->gl);
1062                         if(shi->osatex) {
1063                                 copy_v3_v3(shi->dxgl, shi->dxco);
1064                                 mul_mat3_m4_v3(R.viewinv, shi->dxgl); 
1065                                 copy_v3_v3(shi->dygl, shi->dyco);
1066                                 mul_mat3_m4_v3(R.viewinv, shi->dygl);
1067                         }
1068                 }
1069                 
1070                 if(texco & TEXCO_STRAND) {
1071                         shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
1072                         if(shi->osatex) {
1073                                 dl= shi->dx_u+shi->dx_v;
1074                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
1075                                 dl= shi->dy_u+shi->dy_v;
1076                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
1077                         }
1078                 }
1079                                 
1080                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
1081                         VlakRen *vlr= shi->vlr;
1082                         MTFace *tface;
1083                         MCol *mcol;
1084                         char *name;
1085                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
1086
1087                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
1088                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1089
1090                         shi->totuv= 0;
1091                         shi->totcol= 0;
1092                         shi->actuv= obr->actmtface;
1093                         shi->actcol= obr->actmcol;
1094
1095                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1096                                 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1097                                         ShadeInputCol *scol= &shi->col[i];
1098                                         char *cp1, *cp2, *cp3;
1099                                         
1100                                         shi->totcol++;
1101                                         scol->name= name;
1102
1103                                         cp1= (char *)(mcol+j1);
1104                                         cp2= (char *)(mcol+j2);
1105                                         cp3= (char *)(mcol+j3);
1106                                         
1107                                         scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
1108                                         scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
1109                                         scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
1110                                 }
1111
1112                                 if(shi->totcol) {
1113                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
1114                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
1115                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
1116                                         shi->vcol[3]= 1.0f;
1117                                 }
1118                                 else {
1119                                         shi->vcol[0]= 0.0f;
1120                                         shi->vcol[1]= 0.0f;
1121                                         shi->vcol[2]= 0.0f;
1122                                         shi->vcol[3]= 1.0f;
1123                                 }
1124                         }
1125
1126                         for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1127                                 ShadeInputUV *suv= &shi->uv[i];
1128                                 float *uv1, *uv2, *uv3;
1129
1130                                 shi->totuv++;
1131                                 suv->name= name;
1132                                 
1133                                 uv1= tface->uv[j1];
1134                                 uv2= tface->uv[j2];
1135                                 uv3= tface->uv[j3];
1136                                 
1137                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1138                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1139                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1140
1141                                 if(shi->osatex) {
1142                                         float duv[2];
1143                                         
1144                                         dl= shi->dx_u+shi->dx_v;
1145                                         duv[0]= shi->dx_u; 
1146                                         duv[1]= shi->dx_v;
1147                                         
1148                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1149                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1150                                         
1151                                         dl= shi->dy_u+shi->dy_v;
1152                                         duv[0]= shi->dy_u; 
1153                                         duv[1]= shi->dy_v;
1154                                         
1155                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1156                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1157                                 }
1158
1159                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1160                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1161                                                 shi->vcol[0]= 1.0f;
1162                                                 shi->vcol[1]= 1.0f;
1163                                                 shi->vcol[2]= 1.0f;
1164                                                 shi->vcol[3]= 1.0f;
1165                                         }
1166                                         if(tface->tpage) {
1167                                                 render_realtime_texture(shi, tface->tpage);
1168                                         }
1169                                 }
1170
1171
1172                         }
1173
1174                         shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1175                         shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1176                         shi->dupliuv[2]= 0.0f;
1177
1178                         if(shi->totuv == 0) {
1179                                 ShadeInputUV *suv= &shi->uv[0];
1180
1181                                 suv->uv[0]= 2.0f*(u+.5f);
1182                                 suv->uv[1]= 2.0f*(v+.5f);
1183                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1184                                 
1185                                 if(mode & MA_FACETEXTURE) {
1186                                         /* no tface? set at 1.0f */
1187                                         shi->vcol[0]= 1.0f;
1188                                         shi->vcol[1]= 1.0f;
1189                                         shi->vcol[2]= 1.0f;
1190                                         shi->vcol[3]= 1.0f;
1191                                 }
1192                         }
1193                 }
1194                 
1195                 if(texco & TEXCO_NORM) {
1196                         shi->orn[0]= -shi->vn[0];
1197                         shi->orn[1]= -shi->vn[1];
1198                         shi->orn[2]= -shi->vn[2];
1199                 }
1200                 
1201                 if(texco & TEXCO_STRESS) {
1202                         float *s1, *s2, *s3;
1203                         
1204                         s1= RE_vertren_get_stress(obr, v1, 0);
1205                         s2= RE_vertren_get_stress(obr, v2, 0);
1206                         s3= RE_vertren_get_stress(obr, v3, 0);
1207                         if(s1 && s2 && s3) {
1208                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1209                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
1210                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
1211                         }
1212                         else shi->stress= 0.0f;
1213                 }
1214                 
1215                 if(texco & TEXCO_TANGENT) {
1216                         if((mode & MA_TANGENT_V)==0) {
1217                                 /* just prevent surprises */
1218                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1219                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1220                         }
1221                 }
1222         }
1223         
1224         /* this only avalailable for scanline renders */
1225         if(shi->depth==0) {
1226                 float x= shi->xs;
1227                 float y= shi->ys;
1228                 
1229                 if(texco & TEXCO_WINDOW) {
1230                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1231                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1232                         shi->winco[2]= 0.0f;
1233                         if(shi->osatex) {
1234                                 shi->dxwin[0]= 2.0f/(float)R.winx;
1235                                 shi->dywin[1]= 2.0f/(float)R.winy;
1236                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1237                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
1238                         }
1239                 }
1240
1241                 if(texco & TEXCO_STICKY) {
1242                         float *s1, *s2, *s3;
1243                         
1244                         s1= RE_vertren_get_sticky(obr, v1, 0);
1245                         s2= RE_vertren_get_sticky(obr, v2, 0);
1246                         s3= RE_vertren_get_sticky(obr, v3, 0);
1247                         
1248                         if(s1 && s2 && s3) {
1249                                 float obwinmat[4][4], winmat[4][4], ho1[4], ho2[4], ho3[4];
1250                                 float Zmulx, Zmuly;
1251                                 float hox, hoy, l, dl, u, v;
1252                                 float s00, s01, s10, s11, detsh;
1253                                 
1254                                 /* old globals, localized now */
1255                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
1256
1257                                 zbuf_make_winmat(&R, winmat);
1258                                 if(shi->obi->flag & R_TRANSFORMED)
1259                                         mul_m4_m4m4(obwinmat, obi->mat, winmat);
1260                                 else
1261                                         copy_m4_m4(obwinmat, winmat);
1262
1263                                 zbuf_render_project(obwinmat, v1->co, ho1);
1264                                 zbuf_render_project(obwinmat, v2->co, ho2);
1265                                 zbuf_render_project(obwinmat, v3->co, ho3);
1266                                 
1267                                 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1268                                 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1269                                 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1270                                 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1271                                 
1272                                 detsh= s00*s11-s10*s01;
1273                                 detsh= (detsh != 0.0f)? 1.0f/detsh: 0.0f;
1274                                 s00*= detsh; s01*=detsh; 
1275                                 s10*=detsh; s11*=detsh;
1276                                 
1277                                 /* recalc u and v again */
1278                                 hox= x/Zmulx -1.0f;
1279                                 hoy= y/Zmuly -1.0f;
1280                                 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1281                                 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1282                                 l= 1.0f+u+v;
1283                                 
1284                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1285                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1286                                 shi->sticky[2]= 0.0f;
1287                                 
1288                                 if(shi->osatex) {
1289                                         float dxuv[2], dyuv[2];
1290                                         dxuv[0]=  s11/Zmulx;
1291                                         dxuv[1]=  - s01/Zmulx;
1292                                         dyuv[0]=  - s10/Zmuly;
1293                                         dyuv[1]=  s00/Zmuly;
1294                                         
1295                                         dl= dxuv[0] + dxuv[1];
1296                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1297                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1298                                         dl= dyuv[0] + dyuv[1];
1299                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1300                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1301                                 }
1302                         }
1303                 }
1304         } /* else {
1305          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
1306         */
1307         if (shi->do_manage) {
1308                 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
1309                         srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
1310                 }
1311         }
1312         
1313 }
1314
1315 /* ****************** ShadeSample ************************************** */
1316
1317 /* initialize per part, not per pixel! */
1318 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1319 {
1320         
1321         memset(shi, 0, sizeof(ShadeInput));
1322         
1323         shi->sample= sample;
1324         shi->thread= pa->thread;
1325         shi->do_preview= (R.r.scemode & R_MATNODE_PREVIEW) != 0;
1326         shi->do_manage= (R.r.color_mgt_flag & R_COLOR_MANAGEMENT);
1327         shi->lay= rl->lay;
1328         shi->layflag= rl->layflag;
1329         shi->passflag= rl->passflag;
1330         shi->combinedflag= ~rl->pass_xor;
1331         shi->mat_override= rl->mat_override;
1332         shi->light_override= rl->light_override;
1333 //      shi->rl= rl;
1334         /* note shi.depth==0  means first hit, not raytracing */
1335         
1336 }
1337
1338 /* initialize per part, not per pixel! */
1339 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1340 {
1341         int a, tot;
1342         
1343         tot= R.osa==0?1:R.osa;
1344         
1345         for(a=0; a<tot; a++) {
1346                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1347                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1348         }
1349         
1350         get_sample_layers(pa, rl, ssamp->rlpp);
1351 }
1352
1353 /* Do AO or (future) GI */
1354 void shade_samples_do_AO(ShadeSample *ssamp)
1355 {
1356         if(!(R.r.mode & R_SHADOW))
1357                 return;
1358         if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1359                 return;
1360         
1361         if(R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) {
1362                 ShadeInput *shi= &ssamp->shi[0];
1363                 int sample;
1364
1365                 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1366                         || (shi->passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1367                         for(sample=0; sample<ssamp->tot; shi++, sample++)
1368                                 if(!(shi->mode & MA_SHLESS))
1369                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
1370         }
1371 }
1372
1373
1374 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1375 {
1376         ShadeInput *shi;
1377         float xs, ys;
1378         
1379         ssamp->tot= 0;
1380         
1381         for(shi= ssamp->shi; ps; ps= ps->next) {
1382                 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1383                 
1384                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
1385                         unsigned short curmask= ps->mask;
1386                         
1387                         /* full osa is only set for OSA renders */
1388                         if(shi->vlr->flag & R_FULL_OSA) {
1389                                 short shi_cp= 0, samp;
1390                                 
1391                                 for(samp=0; samp<R.osa; samp++) {
1392                                         if(curmask & (1<<samp)) {
1393                                                 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1394                                                 xs= (float)x + R.jit[samp][0] + 0.5f;
1395                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
1396                                                 
1397                                                 if(shi_cp)
1398                                                         shade_input_copy_triangle(shi, shi-1);
1399                                                 
1400                                                 shi->mask= (1<<samp);
1401 //                                              shi->rl= ssamp->rlpp[samp];
1402                                                 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1403                                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1404                                                 shade_input_set_uv(shi);
1405                                                 if(shi_cp==0)
1406                                                         shade_input_set_normals(shi);
1407                                                 else  /* XXX shi->flippednor messes up otherwise */
1408                                                         shade_input_set_vertex_normals(shi);
1409                                                 
1410                                                 shi_cp= 1;
1411                                                 shi++;
1412                                         }
1413                                 }
1414                         }
1415                         else {
1416                                 if(R.osa) {
1417                                         short b= R.samples->centmask[curmask];
1418                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1419                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1420                                 }
1421                                 else {
1422                                         xs= (float)x + 0.5f;
1423                                         ys= (float)y + 0.5f;
1424                                 }
1425
1426                                 shi->mask= curmask;
1427                                 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1428                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1429                                 shade_input_set_uv(shi);
1430                                 shade_input_set_normals(shi);
1431                                 shi++;
1432                         }
1433                         
1434                         /* total sample amount, shi->sample is static set in initialize */
1435                         if(shi!=ssamp->shi)
1436                                 ssamp->tot= (shi-1)->sample + 1;
1437                 }
1438         }
1439 }
1440
1441 /* shades samples, returns true if anything happened */
1442 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1443 {
1444         shade_samples_fill_with_ps(ssamp, ps, x, y);
1445         
1446         if(ssamp->tot) {
1447                 ShadeInput *shi= ssamp->shi;
1448                 ShadeResult *shr= ssamp->shr;
1449                 int samp;
1450                 
1451                 /* if shadow or AO? */
1452                 shade_samples_do_AO(ssamp);
1453                 
1454                 /* if shade (all shadepinputs have same passflag) */
1455                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB|SCE_PASS_INDEXMA)) {
1456
1457                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1458                                 shade_input_set_shade_texco(shi);
1459                                 shade_input_do_shade(shi, shr);
1460                         }
1461                 }
1462                 else if(shi->passflag & SCE_PASS_Z) {
1463                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++)
1464                                 shr->z= -shi->co[2];
1465                 }
1466                 
1467                 return 1;
1468         }
1469         return 0;
1470 }
1471