Merge with trunk r41545
[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                                         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= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
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= shi->facenor[0]*view[0] + shi->facenor[1]*view[1] + shi->facenor[2]*view[2];
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, xn, yn, zn;
760                         int axis1, axis2;
761
762                         /* find most stable axis to project */
763                         xn= fabs(shi->facenor[0]);
764                         yn= fabs(shi->facenor[1]);
765                         zn= fabs(shi->facenor[2]);
766
767                         if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
768                         else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
769                         else { axis1= 1; axis2= 2; }
770
771                         /* compute u,v and derivatives */
772                         t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
773                         t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
774
775                         detsh= (t00*t11-t10*t01);
776                         detsh= (detsh != 0.0f)? 1.0f/detsh: 0.0f;
777                         t00*= detsh; t01*=detsh; 
778                         t10*=detsh; t11*=detsh;
779
780                         shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
781                         shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
782                         if(shi->osatex) {
783                                 shi->dx_u=  shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
784                                 shi->dx_v=  shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
785                                 shi->dy_u=  shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
786                                 shi->dy_v=  shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
787                         }
788
789                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
790                         CLAMP(shi->u, -2.0f, 1.0f);
791                         CLAMP(shi->v, -2.0f, 1.0f);
792                 }
793         }       
794 }
795
796 void shade_input_set_normals(ShadeInput *shi)
797 {
798         float u= shi->u, v= shi->v;
799         float l= 1.0f+u+v;
800
801         shi->flippednor = 0;
802
803         /* test flip normals to viewing direction */
804         if(!(shi->vlr->flag & R_TANGENT)) {
805                 if(dot_v3v3(shi->facenor, shi->view) < 0.0f) {
806                         negate_v3(shi->facenor);
807                         shi->flippednor= 1;
808                 }
809         }
810         
811         /* calculate vertexnormals */
812         if(shi->vlr->flag & R_SMOOTH) {
813                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
814
815                 if(shi->flippednor) {
816                         negate_v3(n1);
817                         negate_v3(n2);
818                         negate_v3(n3);
819                 }
820                 
821                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
822                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
823                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
824
825                 // use unnormalized normal (closer to games)
826                 copy_v3_v3(shi->nmapnorm, shi->vn);
827                 
828                 normalize_v3(shi->vn);
829         }
830         else
831         {
832                 copy_v3_v3(shi->vn, shi->facenor);
833                 copy_v3_v3(shi->nmapnorm, shi->vn);
834         }
835         
836         /* used in nodes */
837         copy_v3_v3(shi->vno, shi->vn);
838
839         /* flip normals to viewing direction */
840         if(!(shi->vlr->flag & R_TANGENT))
841                 if(dot_v3v3(shi->facenor, shi->view) < 0.0f)
842                         shade_input_flip_normals(shi);
843 }
844
845 /* XXX shi->flippednor messes up otherwise */
846 void shade_input_set_vertex_normals(ShadeInput *shi)
847 {
848         float u= shi->u, v= shi->v;
849         float l= 1.0f+u+v;
850         
851         /* calculate vertexnormals */
852         if(shi->vlr->flag & R_SMOOTH) {
853                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
854                 
855                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
856                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
857                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
858                 
859                 // use unnormalized normal (closer to games)
860                 copy_v3_v3(shi->nmapnorm, shi->vn);
861                 
862                 normalize_v3(shi->vn);
863         }
864         else
865         {
866                 copy_v3_v3(shi->vn, shi->facenor);
867                 copy_v3_v3(shi->nmapnorm, shi->vn);
868         }
869         
870         /* used in nodes */
871         copy_v3_v3(shi->vno, shi->vn);
872 }
873
874
875 /* use by raytrace, sss, bake to flip into the right direction */
876 void shade_input_flip_normals(ShadeInput *shi)
877 {
878         shi->facenor[0]= -shi->facenor[0];
879         shi->facenor[1]= -shi->facenor[1];
880         shi->facenor[2]= -shi->facenor[2];
881
882         shi->vn[0]= -shi->vn[0];
883         shi->vn[1]= -shi->vn[1];
884         shi->vn[2]= -shi->vn[2];
885
886         shi->vno[0]= -shi->vno[0];
887         shi->vno[1]= -shi->vno[1];
888         shi->vno[2]= -shi->vno[2];
889
890         shi->nmapnorm[0] = -shi->nmapnorm[0];
891         shi->nmapnorm[1] = -shi->nmapnorm[1];
892         shi->nmapnorm[2] = -shi->nmapnorm[2];
893
894         shi->flippednor= !shi->flippednor;
895 }
896
897 void shade_input_set_shade_texco(ShadeInput *shi)
898 {
899         ObjectInstanceRen *obi= shi->obi;
900         ObjectRen *obr= shi->obr;
901         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
902         float u= shi->u, v= shi->v;
903         float l= 1.0f+u+v, dl;
904         int mode= shi->mode;            /* or-ed result for all nodes */
905         short texco= shi->mat->texco;
906
907         /* calculate dxno */
908         if(shi->vlr->flag & R_SMOOTH) {
909                 
910                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
911                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
912                         
913                         dl= shi->dx_u+shi->dx_v;
914                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
915                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
916                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
917                         dl= shi->dy_u+shi->dy_v;
918                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
919                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
920                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
921                         
922                 }
923         }
924
925         /* calc tangents */
926         if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
927                 float *tangent, *s1, *s2, *s3;
928                 float tl, tu, tv;
929
930                 if(shi->vlr->flag & R_SMOOTH) {
931                         tl= l;
932                         tu= u;
933                         tv= v;
934                 }
935                 else {
936                         /* qdn: flat faces have tangents too,
937                            could pick either one, using average here */
938                         tl= 1.0f/3.0f;
939                         tu= -1.0f/3.0f;
940                         tv= -1.0f/3.0f;
941                 }
942
943                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
944                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
945
946                 if(mode & MA_TANGENT_V) {
947                         s1 = RE_vertren_get_tangent(obr, v1, 0);
948                         s2 = RE_vertren_get_tangent(obr, v2, 0);
949                         s3 = RE_vertren_get_tangent(obr, v3, 0);
950
951                         if(s1 && s2 && s3) {
952                                 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
953                                 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
954                                 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
955
956                                 if(obi->flag & R_TRANSFORMED)
957                                         mul_m3_v3(obi->nmat, shi->tang);
958
959                                 normalize_v3(shi->tang);
960                                 copy_v3_v3(shi->nmaptang, shi->tang);
961                         }
962                 }
963
964                 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
965                         tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
966
967                         if(tangent) {
968                                 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
969                                 float c0[3], c1[3], c2[3];
970
971                                 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
972
973                                 copy_v3_v3(c0, &tangent[j1*4]);
974                                 copy_v3_v3(c1, &tangent[j2*4]);
975                                 copy_v3_v3(c2, &tangent[j3*4]);
976
977                                 // keeping tangents normalized at vertex level
978                                 // corresponds better to how it's done in game engines
979                                 if(obi->flag & R_TRANSFORMED)
980                                 {
981                                         mul_mat3_m4_v3(obi->mat, c0); normalize_v3(c0);
982                                         mul_mat3_m4_v3(obi->mat, c1); normalize_v3(c1);
983                                         mul_mat3_m4_v3(obi->mat, c2); normalize_v3(c2);
984                                 }
985                                 
986                                 // we don't normalize the interpolated TBN tangent
987                                 // corresponds better to how it's done in game engines
988                                 shi->nmaptang[0]= (tl*c2[0] - tu*c0[0] - tv*c1[0]);
989                                 shi->nmaptang[1]= (tl*c2[1] - tu*c0[1] - tv*c1[1]);
990                                 shi->nmaptang[2]= (tl*c2[2] - tu*c0[2] - tv*c1[2]);
991
992                                 // the sign is the same for all 3 vertices of any
993                                 // non degenerate triangle.
994                                 shi->nmaptang[3]= tangent[j1*4+3];
995                         }
996                 }
997         }
998
999         if(mode & MA_STR_SURFDIFF) {
1000                 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
1001
1002                 if(surfnor) {
1003                         copy_v3_v3(shi->surfnor, surfnor);
1004                         if(obi->flag & R_TRANSFORMED)
1005                                 mul_m3_v3(obi->nmat, shi->surfnor);
1006                 }
1007                 else
1008                         copy_v3_v3(shi->surfnor, shi->vn);
1009
1010                 shi->surfdist= 0.0f;
1011         }
1012         
1013         if(R.r.mode & R_SPEED) {
1014                 float *s1, *s2, *s3;
1015                 
1016                 s1= RE_vertren_get_winspeed(obi, v1, 0);
1017                 s2= RE_vertren_get_winspeed(obi, v2, 0);
1018                 s3= RE_vertren_get_winspeed(obi, v3, 0);
1019                 if(s1 && s2 && s3) {
1020                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
1021                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
1022                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
1023                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
1024                 }
1025                 else {
1026                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
1027                 }
1028         }
1029
1030         /* pass option forces UV calc */
1031         if(shi->passflag & SCE_PASS_UV)
1032                 texco |= (NEED_UV|TEXCO_UV);
1033         
1034         /* texture coordinates. shi->dxuv shi->dyuv have been set */
1035         if(texco & NEED_UV) {
1036                 
1037                 if(texco & TEXCO_ORCO) {
1038                         if(v1->orco) {
1039                                 float *o1, *o2, *o3;
1040                                 
1041                                 o1= v1->orco;
1042                                 o2= v2->orco;
1043                                 o3= v3->orco;
1044                                 
1045                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
1046                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
1047                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
1048                                 
1049                                 if(shi->osatex) {
1050                                         dl= shi->dx_u+shi->dx_v;
1051                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
1052                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
1053                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
1054                                         dl= shi->dy_u+shi->dy_v;
1055                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
1056                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
1057                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
1058                                 }
1059                         }
1060
1061                         copy_v3_v3(shi->duplilo, obi->dupliorco);
1062                 }
1063                 
1064                 if(texco & TEXCO_GLOB) {
1065                         copy_v3_v3(shi->gl, shi->co);
1066                         mul_m4_v3(R.viewinv, shi->gl);
1067                         if(shi->osatex) {
1068                                 copy_v3_v3(shi->dxgl, shi->dxco);
1069                                 mul_mat3_m4_v3(R.viewinv, shi->dxgl); 
1070                                 copy_v3_v3(shi->dygl, shi->dyco);
1071                                 mul_mat3_m4_v3(R.viewinv, shi->dygl);
1072                         }
1073                 }
1074                 
1075                 if(texco & TEXCO_STRAND) {
1076                         shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
1077                         if(shi->osatex) {
1078                                 dl= shi->dx_u+shi->dx_v;
1079                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
1080                                 dl= shi->dy_u+shi->dy_v;
1081                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
1082                         }
1083                 }
1084                                 
1085                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
1086                         VlakRen *vlr= shi->vlr;
1087                         MTFace *tface;
1088                         MCol *mcol;
1089                         char *name;
1090                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
1091
1092                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
1093                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1094
1095                         shi->totuv= 0;
1096                         shi->totcol= 0;
1097                         shi->actuv= obr->actmtface;
1098                         shi->actcol= obr->actmcol;
1099
1100                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1101                                 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1102                                         ShadeInputCol *scol= &shi->col[i];
1103                                         char *cp1, *cp2, *cp3;
1104                                         float a[3];
1105                                         
1106                                         shi->totcol++;
1107                                         scol->name= name;
1108
1109                                         cp1= (char *)(mcol+j1);
1110                                         cp2= (char *)(mcol+j2);
1111                                         cp3= (char *)(mcol+j3);
1112
1113                                         /* alpha values */
1114                                         a[0] = ((float)cp1[0])/255.f;
1115                                         a[1] = ((float)cp2[0])/255.f;
1116                                         a[2] = ((float)cp3[0])/255.f;
1117                                         scol->col[3]= l*a[2] - u*a[0] - v*a[1];
1118
1119                                         /* sample premultiplied color value */
1120                                         scol->col[0]= (l*((float)cp3[3])*a[2] - u*((float)cp1[3])*a[0] - v*((float)cp2[3])*a[1])/255.f;
1121                                         scol->col[1]= (l*((float)cp3[2])*a[2] - u*((float)cp1[2])*a[0] - v*((float)cp2[2])*a[1])/255.f;
1122                                         scol->col[2]= (l*((float)cp3[1])*a[2] - u*((float)cp1[1])*a[0] - v*((float)cp2[1])*a[1])/255.f;
1123
1124                                         /* if not zero alpha, restore non-multiplied color */
1125                                         if (scol->col[3]) {
1126                                                 mul_v3_fl(scol->col, 1.0f/scol->col[3]);
1127                                         }
1128                                 }
1129
1130                                 if(shi->totcol) {
1131                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
1132                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
1133                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
1134                                         shi->vcol[3]= shi->col[shi->actcol].col[3];
1135                                 }
1136                                 else {
1137                                         shi->vcol[0]= 0.0f;
1138                                         shi->vcol[1]= 0.0f;
1139                                         shi->vcol[2]= 0.0f;
1140                                         shi->vcol[3]= 1.0f;
1141                                 }
1142                         }
1143
1144                         for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1145                                 ShadeInputUV *suv= &shi->uv[i];
1146                                 float *uv1, *uv2, *uv3;
1147
1148                                 shi->totuv++;
1149                                 suv->name= name;
1150                                 
1151                                 uv1= tface->uv[j1];
1152                                 uv2= tface->uv[j2];
1153                                 uv3= tface->uv[j3];
1154                                 
1155                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1156                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1157                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1158
1159                                 if(shi->osatex) {
1160                                         float duv[2];
1161                                         
1162                                         dl= shi->dx_u+shi->dx_v;
1163                                         duv[0]= shi->dx_u; 
1164                                         duv[1]= shi->dx_v;
1165                                         
1166                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1167                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1168                                         
1169                                         dl= shi->dy_u+shi->dy_v;
1170                                         duv[0]= shi->dy_u; 
1171                                         duv[1]= shi->dy_v;
1172                                         
1173                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1174                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1175                                 }
1176
1177                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1178                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1179                                                 shi->vcol[0]= 1.0f;
1180                                                 shi->vcol[1]= 1.0f;
1181                                                 shi->vcol[2]= 1.0f;
1182                                                 shi->vcol[3]= 1.0f;
1183                                         }
1184                                         if(tface->tpage) {
1185                                                 render_realtime_texture(shi, tface->tpage);
1186                                         }
1187                                 }
1188
1189
1190                         }
1191
1192                         shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1193                         shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1194                         shi->dupliuv[2]= 0.0f;
1195
1196                         if(shi->totuv == 0) {
1197                                 ShadeInputUV *suv= &shi->uv[0];
1198
1199                                 suv->uv[0]= 2.0f*(u+.5f);
1200                                 suv->uv[1]= 2.0f*(v+.5f);
1201                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1202                                 
1203                                 if(mode & MA_FACETEXTURE) {
1204                                         /* no tface? set at 1.0f */
1205                                         shi->vcol[0]= 1.0f;
1206                                         shi->vcol[1]= 1.0f;
1207                                         shi->vcol[2]= 1.0f;
1208                                         shi->vcol[3]= 1.0f;
1209                                 }
1210                         }
1211                 }
1212                 
1213                 if(texco & TEXCO_NORM) {
1214                         shi->orn[0]= -shi->vn[0];
1215                         shi->orn[1]= -shi->vn[1];
1216                         shi->orn[2]= -shi->vn[2];
1217                 }
1218                 
1219                 if(texco & TEXCO_STRESS) {
1220                         float *s1, *s2, *s3;
1221                         
1222                         s1= RE_vertren_get_stress(obr, v1, 0);
1223                         s2= RE_vertren_get_stress(obr, v2, 0);
1224                         s3= RE_vertren_get_stress(obr, v3, 0);
1225                         if(s1 && s2 && s3) {
1226                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1227                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
1228                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
1229                         }
1230                         else shi->stress= 0.0f;
1231                 }
1232                 
1233                 if(texco & TEXCO_TANGENT) {
1234                         if((mode & MA_TANGENT_V)==0) {
1235                                 /* just prevent surprises */
1236                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1237                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1238                         }
1239                 }
1240         }
1241         
1242         /* this only avalailable for scanline renders */
1243         if(shi->depth==0) {
1244                 float x= shi->xs;
1245                 float y= shi->ys;
1246                 
1247                 if(texco & TEXCO_WINDOW) {
1248                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1249                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1250                         shi->winco[2]= 0.0f;
1251                         if(shi->osatex) {
1252                                 shi->dxwin[0]= 2.0f/(float)R.winx;
1253                                 shi->dywin[1]= 2.0f/(float)R.winy;
1254                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1255                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
1256                         }
1257                 }
1258
1259                 if(texco & TEXCO_STICKY) {
1260                         float *s1, *s2, *s3;
1261                         
1262                         s1= RE_vertren_get_sticky(obr, v1, 0);
1263                         s2= RE_vertren_get_sticky(obr, v2, 0);
1264                         s3= RE_vertren_get_sticky(obr, v3, 0);
1265                         
1266                         if(s1 && s2 && s3) {
1267                                 float obwinmat[4][4], winmat[4][4], ho1[4], ho2[4], ho3[4];
1268                                 float Zmulx, Zmuly;
1269                                 float hox, hoy, l, dl, u, v;
1270                                 float s00, s01, s10, s11, detsh;
1271                                 
1272                                 /* old globals, localized now */
1273                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
1274
1275                                 zbuf_make_winmat(&R, winmat);
1276                                 if(shi->obi->flag & R_TRANSFORMED)
1277                                         mul_m4_m4m4(obwinmat, obi->mat, winmat);
1278                                 else
1279                                         copy_m4_m4(obwinmat, winmat);
1280
1281                                 zbuf_render_project(obwinmat, v1->co, ho1);
1282                                 zbuf_render_project(obwinmat, v2->co, ho2);
1283                                 zbuf_render_project(obwinmat, v3->co, ho3);
1284                                 
1285                                 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1286                                 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1287                                 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1288                                 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1289                                 
1290                                 detsh= s00*s11-s10*s01;
1291                                 detsh= (detsh != 0.0f)? 1.0f/detsh: 0.0f;
1292                                 s00*= detsh; s01*=detsh; 
1293                                 s10*=detsh; s11*=detsh;
1294                                 
1295                                 /* recalc u and v again */
1296                                 hox= x/Zmulx -1.0f;
1297                                 hoy= y/Zmuly -1.0f;
1298                                 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1299                                 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1300                                 l= 1.0f+u+v;
1301                                 
1302                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1303                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1304                                 shi->sticky[2]= 0.0f;
1305                                 
1306                                 if(shi->osatex) {
1307                                         float dxuv[2], dyuv[2];
1308                                         dxuv[0]=  s11/Zmulx;
1309                                         dxuv[1]=  - s01/Zmulx;
1310                                         dyuv[0]=  - s10/Zmuly;
1311                                         dyuv[1]=  s00/Zmuly;
1312                                         
1313                                         dl= dxuv[0] + dxuv[1];
1314                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1315                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1316                                         dl= dyuv[0] + dyuv[1];
1317                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1318                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1319                                 }
1320                         }
1321                 }
1322         } /* else {
1323          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
1324         */
1325         if (shi->do_manage) {
1326                 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
1327                         srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
1328                 }
1329         }
1330         
1331 }
1332
1333 /* ****************** ShadeSample ************************************** */
1334
1335 /* initialize per part, not per pixel! */
1336 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1337 {
1338         
1339         memset(shi, 0, sizeof(ShadeInput));
1340         
1341         shi->sample= sample;
1342         shi->thread= pa->thread;
1343         shi->do_preview= (R.r.scemode & R_MATNODE_PREVIEW) != 0;
1344         shi->do_manage= (R.r.color_mgt_flag & R_COLOR_MANAGEMENT);
1345         shi->lay= rl->lay;
1346         shi->layflag= rl->layflag;
1347         shi->passflag= rl->passflag;
1348         shi->combinedflag= ~rl->pass_xor;
1349         shi->mat_override= rl->mat_override;
1350         shi->light_override= rl->light_override;
1351 //      shi->rl= rl;
1352         /* note shi.depth==0  means first hit, not raytracing */
1353         
1354 }
1355
1356 /* initialize per part, not per pixel! */
1357 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1358 {
1359         int a, tot;
1360         
1361         tot= R.osa==0?1:R.osa;
1362         
1363         for(a=0; a<tot; a++) {
1364                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1365                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1366         }
1367         
1368         get_sample_layers(pa, rl, ssamp->rlpp);
1369 }
1370
1371 /* Do AO or (future) GI */
1372 void shade_samples_do_AO(ShadeSample *ssamp)
1373 {
1374         if(!(R.r.mode & R_SHADOW))
1375                 return;
1376         if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1377                 return;
1378         
1379         if(R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) {
1380                 ShadeInput *shi= &ssamp->shi[0];
1381                 int sample;
1382
1383                 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1384                         || (shi->passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1385                         for(sample=0; sample<ssamp->tot; shi++, sample++)
1386                                 if(!(shi->mode & MA_SHLESS))
1387                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
1388         }
1389 }
1390
1391
1392 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1393 {
1394         ShadeInput *shi;
1395         float xs, ys;
1396         
1397         ssamp->tot= 0;
1398         
1399         for(shi= ssamp->shi; ps; ps= ps->next) {
1400                 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1401                 
1402                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
1403                         unsigned short curmask= ps->mask;
1404                         
1405                         /* full osa is only set for OSA renders */
1406                         if(shi->vlr->flag & R_FULL_OSA) {
1407                                 short shi_cp= 0, samp;
1408                                 
1409                                 for(samp=0; samp<R.osa; samp++) {
1410                                         if(curmask & (1<<samp)) {
1411                                                 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1412                                                 xs= (float)x + R.jit[samp][0] + 0.5f;
1413                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
1414                                                 
1415                                                 if(shi_cp)
1416                                                         shade_input_copy_triangle(shi, shi-1);
1417                                                 
1418                                                 shi->mask= (1<<samp);
1419 //                                              shi->rl= ssamp->rlpp[samp];
1420                                                 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1421                                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1422                                                 shade_input_set_uv(shi);
1423                                                 if(shi_cp==0)
1424                                                         shade_input_set_normals(shi);
1425                                                 else  /* XXX shi->flippednor messes up otherwise */
1426                                                         shade_input_set_vertex_normals(shi);
1427                                                 
1428                                                 shi_cp= 1;
1429                                                 shi++;
1430                                         }
1431                                 }
1432                         }
1433                         else {
1434                                 if(R.osa) {
1435                                         short b= R.samples->centmask[curmask];
1436                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1437                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1438                                 }
1439                                 else {
1440                                         xs= (float)x + 0.5f;
1441                                         ys= (float)y + 0.5f;
1442                                 }
1443
1444                                 shi->mask= curmask;
1445                                 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1446                                 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1447                                 shade_input_set_uv(shi);
1448                                 shade_input_set_normals(shi);
1449                                 shi++;
1450                         }
1451                         
1452                         /* total sample amount, shi->sample is static set in initialize */
1453                         if(shi!=ssamp->shi)
1454                                 ssamp->tot= (shi-1)->sample + 1;
1455                 }
1456         }
1457 }
1458
1459 /* shades samples, returns true if anything happened */
1460 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1461 {
1462         shade_samples_fill_with_ps(ssamp, ps, x, y);
1463         
1464         if(ssamp->tot) {
1465                 ShadeInput *shi= ssamp->shi;
1466                 ShadeResult *shr= ssamp->shr;
1467                 int samp;
1468                 
1469                 /* if shadow or AO? */
1470                 shade_samples_do_AO(ssamp);
1471                 
1472                 /* if shade (all shadepinputs have same passflag) */
1473                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB|SCE_PASS_INDEXMA)) {
1474
1475                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1476                                 shade_input_set_shade_texco(shi);
1477                                 shade_input_do_shade(shi, shr);
1478                         }
1479                 }
1480                 else if(shi->passflag & SCE_PASS_Z) {
1481                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++)
1482                                 shr->z= -shi->co[2];
1483                 }
1484                 
1485                 return 1;
1486         }
1487         return 0;
1488 }
1489