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