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