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