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