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