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