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