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