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