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