* Volume rendering
[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         else
356                 VECCOPY(shi->vn, spoint->nor)
357
358         VECCOPY(shi->vno, shi->vn);
359 }
360
361 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
362 {
363         StrandBuffer *strandbuf= strand->buffer;
364         ObjectRen *obr= strandbuf->obr;
365         StrandVert *sv;
366         int mode= shi->mode;            /* or-ed result for all nodes */
367         short texco= shi->mat->texco;
368
369         if((shi->mat->texco & TEXCO_REFL)) {
370                 /* shi->dxview, shi->dyview, not supported */
371         }
372
373         if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL))) {
374                 /* not supported */
375         }
376
377         if(mode & (MA_TANGENT_V|MA_NORMAP_TANG)) {
378                 VECCOPY(shi->tang, spoint->tan);
379                 VECCOPY(shi->nmaptang, spoint->tan);
380         }
381
382         if(mode & MA_STR_SURFDIFF) {
383                 float *surfnor= RE_strandren_get_surfnor(obr, strand, 0);
384
385                 if(surfnor)
386                         VECCOPY(shi->surfnor, surfnor)
387                 else
388                         VECCOPY(shi->surfnor, shi->vn)
389
390                 if(shi->mat->strand_surfnor > 0.0f) {
391                         shi->surfdist= 0.0f;
392                         for(sv=strand->vert; sv!=svert; sv++)
393                                 shi->surfdist+=VecLenf(sv->co, (sv+1)->co);
394                         shi->surfdist += spoint->t*VecLenf(sv->co, (sv+1)->co);
395                 }
396         }
397
398         if(R.r.mode & R_SPEED) {
399                 float *speed;
400                 
401                 speed= RE_strandren_get_winspeed(shi->obi, strand, 0);
402                 if(speed)
403                         QUATCOPY(shi->winspeed, speed)
404                 else
405                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
406         }
407
408         /* shade_input_set_shade_texco equivalent */
409         if(texco & NEED_UV) {
410                 if(texco & TEXCO_ORCO) {
411                         VECCOPY(shi->lo, strand->orco);
412                         /* no shi->osatex, orco derivatives are zero */
413                 }
414
415                 if(texco & TEXCO_GLOB) {
416                         VECCOPY(shi->gl, shi->co);
417                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
418                         
419                         if(shi->osatex) {
420                                 VECCOPY(shi->dxgl, shi->dxco);
421                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
422                                 VECCOPY(shi->dygl, shi->dyco);
423                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
424                         }
425                 }
426
427                 if(texco & TEXCO_STRAND) {
428                         shi->strandco= spoint->strandco;
429
430                         if(shi->osatex) {
431                                 shi->dxstrand= spoint->dtstrandco;
432                                 shi->dystrand= 0.0f;
433                         }
434                 }
435
436                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
437                         MCol *mcol;
438                         float *uv;
439                         char *name;
440                         int i;
441
442                         shi->totuv= 0;
443                         shi->totcol= 0;
444                         shi->actuv= obr->actmtface;
445                         shi->actcol= obr->actmcol;
446
447                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
448                                 for (i=0; (mcol=RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
449                                         ShadeInputCol *scol= &shi->col[i];
450                                         char *cp= (char*)mcol;
451                                         
452                                         shi->totcol++;
453                                         scol->name= name;
454
455                                         scol->col[0]= cp[3]/255.0f;
456                                         scol->col[1]= cp[2]/255.0f;
457                                         scol->col[2]= cp[1]/255.0f;
458                                 }
459
460                                 if(shi->totcol) {
461                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
462                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
463                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
464                                 }
465                                 else {
466                                         shi->vcol[0]= 0.0f;
467                                         shi->vcol[1]= 0.0f;
468                                         shi->vcol[2]= 0.0f;
469                                 }
470                         }
471
472                         for (i=0; (uv=RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
473                                 ShadeInputUV *suv= &shi->uv[i];
474
475                                 shi->totuv++;
476                                 suv->name= name;
477
478                                 if(strandbuf->overrideuv == i) {
479                                         suv->uv[0]= -1.0f;
480                                         suv->uv[1]= spoint->strandco;
481                                         suv->uv[2]= 0.0f;
482                                 }
483                                 else {
484                                         suv->uv[0]= -1.0f + 2.0f*uv[0];
485                                         suv->uv[1]= -1.0f + 2.0f*uv[1];
486                                         suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
487                                 }
488
489                                 if(shi->osatex) {
490                                         suv->dxuv[0]= 0.0f;
491                                         suv->dxuv[1]= 0.0f;
492                                         suv->dyuv[0]= 0.0f;
493                                         suv->dyuv[1]= 0.0f;
494                                 }
495
496                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
497                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
498                                                 shi->vcol[0]= 1.0f;
499                                                 shi->vcol[1]= 1.0f;
500                                                 shi->vcol[2]= 1.0f;
501                                         }
502                                 }
503                         }
504
505                         if(shi->totuv == 0) {
506                                 ShadeInputUV *suv= &shi->uv[0];
507
508                                 suv->uv[0]= 0.0f;
509                                 suv->uv[1]= spoint->strandco;
510                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
511                                 
512                                 if(mode & MA_FACETEXTURE) {
513                                         /* no tface? set at 1.0f */
514                                         shi->vcol[0]= 1.0f;
515                                         shi->vcol[1]= 1.0f;
516                                         shi->vcol[2]= 1.0f;
517                                 }
518                         }
519
520                 }
521
522                 if(texco & TEXCO_NORM) {
523                         shi->orn[0]= -shi->vn[0];
524                         shi->orn[1]= -shi->vn[1];
525                         shi->orn[2]= -shi->vn[2];
526                 }
527
528                 if(mode & MA_RADIO) {
529                         /* not supported */
530                 }
531
532                 if(texco & TEXCO_REFL) {
533                         /* mirror reflection color textures (and envmap) */
534                         calc_R_ref(shi);    /* wrong location for normal maps! XXXXXXXXXXXXXX */
535                 }
536
537                 if(texco & TEXCO_STRESS) {
538                         /* not supported */
539                 }
540
541                 if(texco & TEXCO_TANGENT) {
542                         if((mode & MA_TANGENT_V)==0) {
543                                 /* just prevent surprises */
544                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
545                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
546                         }
547                 }
548         }
549
550         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
551
552         /* this only avalailable for scanline renders */
553         if(shi->depth==0) {
554                 if(texco & TEXCO_WINDOW) {
555                         shi->winco[0]= -1.0f + 2.0f*spoint->x/(float)R.winx;
556                         shi->winco[1]= -1.0f + 2.0f*spoint->y/(float)R.winy;
557                         shi->winco[2]= 0.0f;
558
559                         /* not supported */
560                         if(shi->osatex) {
561                                 shi->dxwin[0]= 0.0f;
562                                 shi->dywin[1]= 0.0f;
563                                 shi->dxwin[0]= 0.0f;
564                                 shi->dywin[1]= 0.0f;
565                         }
566                 }
567
568                 if(texco & TEXCO_STICKY) {
569                         /* not supported */
570                 }
571         }
572 }
573
574 /* scanline pixel coordinates */
575 /* requires set_triangle */
576 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float z)
577 {
578         float fac;
579         
580         /* currently in use for dithering (soft shadow), node preview, irregular shad */
581         shi->xs= (int)(x);
582         shi->ys= (int)(y);
583         
584         calc_view_vector(shi->view, x, y);      /* returns not normalized, so is in viewplane coords */
585         
586         /* wire cannot use normal for calculating shi->co */
587         if(shi->mat->mode & MA_WIRE) {
588                 
589                 if(R.r.mode & R_ORTHO)
590                         calc_renderco_ortho(shi->co, x, y, z);
591                 else
592                         calc_renderco_zbuf(shi->co, shi->view, z);
593         }
594         else {
595                 float dface, v1[3];
596                 
597                 VECCOPY(v1, shi->v1->co);
598
599                 if(shi->obi->flag & R_TRANSFORMED)
600                         Mat4MulVecfl(shi->obi->mat, v1);
601                 
602                 dface= v1[0]*shi->facenor[0]+v1[1]*shi->facenor[1]+v1[2]*shi->facenor[2];
603                 
604                 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
605                 if(R.r.mode & R_ORTHO) {
606                         /* x and y 3d coordinate can be derived from pixel coord and winmat */
607                         float fx= 2.0f/(R.winx*R.winmat[0][0]);
608                         float fy= 2.0f/(R.winy*R.winmat[1][1]);
609                         
610                         shi->co[0]= (x - 0.5f*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
611                         shi->co[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
612                         
613                         /* using a*x + b*y + c*z = d equation, (a b c) is normal */
614                         if(shi->facenor[2]!=0.0f)
615                                 shi->co[2]= (dface - shi->facenor[0]*shi->co[0] - shi->facenor[1]*shi->co[1])/shi->facenor[2];
616                         else
617                                 shi->co[2]= 0.0f;
618                         
619                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
620                                 shi->dxco[0]= fx;
621                                 shi->dxco[1]= 0.0f;
622                                 if(shi->facenor[2]!=0.0f)
623                                         shi->dxco[2]= (shi->facenor[0]*fx)/shi->facenor[2];
624                                 else 
625                                         shi->dxco[2]= 0.0f;
626                                 
627                                 shi->dyco[0]= 0.0f;
628                                 shi->dyco[1]= fy;
629                                 if(shi->facenor[2]!=0.0f)
630                                         shi->dyco[2]= (shi->facenor[1]*fy)/shi->facenor[2];
631                                 else 
632                                         shi->dyco[2]= 0.0f;
633                                 
634                                 if( (shi->mat->texco & TEXCO_REFL) ) {
635                                         if(shi->co[2]!=0.0f) fac= 1.0f/shi->co[2]; else fac= 0.0f;
636                                         shi->dxview= -R.viewdx*fac;
637                                         shi->dyview= -R.viewdy*fac;
638                                 }
639                         }
640                 }
641                 else {
642                         float div;
643                         
644                         div= shi->facenor[0]*shi->view[0] + shi->facenor[1]*shi->view[1] + shi->facenor[2]*shi->view[2];
645                         if (div!=0.0f) fac= dface/div;
646                         else fac= 0.0f;
647                         
648                         shi->co[0]= fac*shi->view[0];
649                         shi->co[1]= fac*shi->view[1];
650                         shi->co[2]= fac*shi->view[2];
651                         
652                         /* pixel dx/dy for render coord */
653                         if(shi->osatex || (R.r.mode & R_SHADOW) ) {
654                                 float u= dface/(div - R.viewdx*shi->facenor[0]);
655                                 float v= dface/(div - R.viewdy*shi->facenor[1]);
656                                 
657                                 shi->dxco[0]= shi->co[0]- (shi->view[0]-R.viewdx)*u;
658                                 shi->dxco[1]= shi->co[1]- (shi->view[1])*u;
659                                 shi->dxco[2]= shi->co[2]- (shi->view[2])*u;
660                                 
661                                 shi->dyco[0]= shi->co[0]- (shi->view[0])*v;
662                                 shi->dyco[1]= shi->co[1]- (shi->view[1]-R.viewdy)*v;
663                                 shi->dyco[2]= shi->co[2]- (shi->view[2])*v;
664                                 
665                                 if( (shi->mat->texco & TEXCO_REFL) ) {
666                                         if(fac!=0.0f) fac= 1.0f/fac;
667                                         shi->dxview= -R.viewdx*fac;
668                                         shi->dyview= -R.viewdy*fac;
669                                 }
670                         }
671                 }
672         }
673         
674         /* cannot normalize earlier, code above needs it at viewplane level */
675         Normalize(shi->view);
676 }
677
678 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
679 void shade_input_set_uv(ShadeInput *shi)
680 {
681         VlakRen *vlr= shi->vlr;
682         
683         if((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
684                 float v1[3], v2[3], v3[3];
685
686                 VECCOPY(v1, shi->v1->co);
687                 VECCOPY(v2, shi->v2->co);
688                 VECCOPY(v3, shi->v3->co);
689
690                 if(shi->obi->flag & R_TRANSFORMED) {
691                         Mat4MulVecfl(shi->obi->mat, v1);
692                         Mat4MulVecfl(shi->obi->mat, v2);
693                         Mat4MulVecfl(shi->obi->mat, v3);
694                 }
695
696                 /* exception case for wire render of edge */
697                 if(vlr->v2==vlr->v3) {
698                         float lend, lenc;
699                         
700                         lend= VecLenf(v2, v1);
701                         lenc= VecLenf(shi->co, v1);
702                         
703                         if(lend==0.0f) {
704                                 shi->u=shi->v= 0.0f;
705                         }
706                         else {
707                                 shi->u= - (1.0f - lenc/lend);
708                                 shi->v= 0.0f;
709                         }
710                         
711                         if(shi->osatex) {
712                                 shi->dx_u=  0.0f;
713                                 shi->dx_v=  0.0f;
714                                 shi->dy_u=  0.0f;
715                                 shi->dy_v=  0.0f;
716                         }
717                 }
718                 else {
719                         /* most of this could become re-used for faces */
720                         float detsh, t00, t10, t01, t11, xn, yn, zn;
721                         int axis1, axis2;
722
723                         /* find most stable axis to project */
724                         xn= fabs(shi->facenor[0]);
725                         yn= fabs(shi->facenor[1]);
726                         zn= fabs(shi->facenor[2]);
727
728                         if(zn>=xn && zn>=yn) { axis1= 0; axis2= 1; }
729                         else if(yn>=xn && yn>=zn) { axis1= 0; axis2= 2; }
730                         else { axis1= 1; axis2= 2; }
731
732                         /* compute u,v and derivatives */
733                         t00= v3[axis1]-v1[axis1]; t01= v3[axis2]-v1[axis2];
734                         t10= v3[axis1]-v2[axis1]; t11= v3[axis2]-v2[axis2];
735
736                         detsh= 1.0f/(t00*t11-t10*t01);
737                         t00*= detsh; t01*=detsh; 
738                         t10*=detsh; t11*=detsh;
739
740                         shi->u= (shi->co[axis1]-v3[axis1])*t11-(shi->co[axis2]-v3[axis2])*t10;
741                         shi->v= (shi->co[axis2]-v3[axis2])*t00-(shi->co[axis1]-v3[axis1])*t01;
742                         if(shi->osatex) {
743                                 shi->dx_u=  shi->dxco[axis1]*t11- shi->dxco[axis2]*t10;
744                                 shi->dx_v=  shi->dxco[axis2]*t00- shi->dxco[axis1]*t01;
745                                 shi->dy_u=  shi->dyco[axis1]*t11- shi->dyco[axis2]*t10;
746                                 shi->dy_v=  shi->dyco[axis2]*t00- shi->dyco[axis1]*t01;
747                         }
748
749                         /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
750                         CLAMP(shi->u, -2.0f, 1.0f);
751                         CLAMP(shi->v, -2.0f, 1.0f);
752                 }
753         }       
754 }
755
756 void shade_input_set_normals(ShadeInput *shi)
757 {
758         float u= shi->u, v= shi->v;
759         float l= 1.0f+u+v;
760         
761         /* calculate vertexnormals */
762         if(shi->vlr->flag & R_SMOOTH) {
763                 float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
764                 
765                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
766                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
767                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
768                 
769                 Normalize(shi->vn);
770         }
771         else
772                 VECCOPY(shi->vn, shi->facenor);
773         
774         /* used in nodes */
775         VECCOPY(shi->vno, shi->vn);
776
777 }
778
779 /* use by raytrace, sss, bake to flip into the right direction */
780 void shade_input_flip_normals(ShadeInput *shi)
781 {
782         shi->facenor[0]= -shi->facenor[0];
783         shi->facenor[1]= -shi->facenor[1];
784         shi->facenor[2]= -shi->facenor[2];
785
786         shi->vn[0]= -shi->vn[0];
787         shi->vn[1]= -shi->vn[1];
788         shi->vn[2]= -shi->vn[2];
789
790         shi->vno[0]= -shi->vno[0];
791         shi->vno[1]= -shi->vno[1];
792         shi->vno[2]= -shi->vno[2];
793
794         shi->flippednor= !shi->flippednor;
795 }
796
797 void shade_input_set_shade_texco(ShadeInput *shi)
798 {
799         ObjectInstanceRen *obi= shi->obi;
800         ObjectRen *obr= shi->obr;
801         VertRen *v1= shi->v1, *v2= shi->v2, *v3= shi->v3;
802         float u= shi->u, v= shi->v;
803         float l= 1.0f+u+v, dl;
804         int mode= shi->mode;            /* or-ed result for all nodes */
805         short texco= shi->mat->texco;
806
807         /* calculate dxno */
808         if(shi->vlr->flag & R_SMOOTH) {
809                 
810                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
811                         float *n1= shi->n1, *n2= shi->n2, *n3= shi->n3;
812                         
813                         dl= shi->dx_u+shi->dx_v;
814                         shi->dxno[0]= dl*n3[0]-shi->dx_u*n1[0]-shi->dx_v*n2[0];
815                         shi->dxno[1]= dl*n3[1]-shi->dx_u*n1[1]-shi->dx_v*n2[1];
816                         shi->dxno[2]= dl*n3[2]-shi->dx_u*n1[2]-shi->dx_v*n2[2];
817                         dl= shi->dy_u+shi->dy_v;
818                         shi->dyno[0]= dl*n3[0]-shi->dy_u*n1[0]-shi->dy_v*n2[0];
819                         shi->dyno[1]= dl*n3[1]-shi->dy_u*n1[1]-shi->dy_v*n2[1];
820                         shi->dyno[2]= dl*n3[2]-shi->dy_u*n1[2]-shi->dy_v*n2[2];
821                         
822                 }
823         }
824
825         /* calc tangents */
826         if (mode & (MA_TANGENT_V|MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
827                 float *tangent, *s1, *s2, *s3;
828                 float tl, tu, tv;
829
830                 if(shi->vlr->flag & R_SMOOTH) {
831                         tl= l;
832                         tu= u;
833                         tv= v;
834                 }
835                 else {
836                         /* qdn: flat faces have tangents too,
837                            could pick either one, using average here */
838                         tl= 1.0f/3.0f;
839                         tu= -1.0f/3.0f;
840                         tv= -1.0f/3.0f;
841                 }
842
843                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
844                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
845
846                 if(mode & MA_TANGENT_V) {
847                         s1 = RE_vertren_get_tangent(obr, v1, 0);
848                         s2 = RE_vertren_get_tangent(obr, v2, 0);
849                         s3 = RE_vertren_get_tangent(obr, v3, 0);
850
851                         if(s1 && s2 && s3) {
852                                 shi->tang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
853                                 shi->tang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
854                                 shi->tang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
855
856                                 if(obi->flag & R_TRANSFORMED)
857                                         Mat3MulVecfl(obi->nmat, shi->tang);
858
859                                 Normalize(shi->tang);
860                                 VECCOPY(shi->nmaptang, shi->tang);
861                         }
862                 }
863
864                 if(mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
865                         tangent= RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
866
867                         if(tangent) {
868                                 int j1= shi->i1, j2= shi->i2, j3= shi->i3;
869
870                                 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
871
872                                 s1= &tangent[j1*3];
873                                 s2= &tangent[j2*3];
874                                 s3= &tangent[j3*3];
875
876                                 shi->nmaptang[0]= (tl*s3[0] - tu*s1[0] - tv*s2[0]);
877                                 shi->nmaptang[1]= (tl*s3[1] - tu*s1[1] - tv*s2[1]);
878                                 shi->nmaptang[2]= (tl*s3[2] - tu*s1[2] - tv*s2[2]);
879
880                                 if(obi->flag & R_TRANSFORMED)
881                                         Mat3MulVecfl(obi->nmat, shi->nmaptang);
882
883                                 Normalize(shi->nmaptang);
884                         }
885                 }
886         }
887
888         if(mode & MA_STR_SURFDIFF) {
889                 float *surfnor= RE_vlakren_get_surfnor(obr, shi->vlr, 0);
890
891                 if(surfnor) {
892                         VECCOPY(shi->surfnor, surfnor)
893                         if(obi->flag & R_TRANSFORMED)
894                                 Mat3MulVecfl(obi->nmat, shi->surfnor);
895                 }
896                 else
897                         VECCOPY(shi->surfnor, shi->vn)
898
899                 shi->surfdist= 0.0f;
900         }
901         
902         if(R.r.mode & R_SPEED) {
903                 float *s1, *s2, *s3;
904                 
905                 s1= RE_vertren_get_winspeed(obi, v1, 0);
906                 s2= RE_vertren_get_winspeed(obi, v2, 0);
907                 s3= RE_vertren_get_winspeed(obi, v3, 0);
908                 if(s1 && s2 && s3) {
909                         shi->winspeed[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
910                         shi->winspeed[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
911                         shi->winspeed[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
912                         shi->winspeed[3]= (l*s3[3] - u*s1[3] - v*s2[3]);
913                 }
914                 else {
915                         shi->winspeed[0]= shi->winspeed[1]= shi->winspeed[2]= shi->winspeed[3]= 0.0f;
916                 }
917         }
918
919         /* pass option forces UV calc */
920         if(shi->passflag & SCE_PASS_UV)
921                 texco |= (NEED_UV|TEXCO_UV);
922         
923         /* texture coordinates. shi->dxuv shi->dyuv have been set */
924         if(texco & NEED_UV) {
925                 
926                 if(texco & TEXCO_ORCO) {
927                         if(v1->orco) {
928                                 float *o1, *o2, *o3;
929                                 
930                                 o1= v1->orco;
931                                 o2= v2->orco;
932                                 o3= v3->orco;
933                                 
934                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
935                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
936                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
937                                 
938                                 if(shi->osatex) {
939                                         dl= shi->dx_u+shi->dx_v;
940                                         shi->dxlo[0]= dl*o3[0]-shi->dx_u*o1[0]-shi->dx_v*o2[0];
941                                         shi->dxlo[1]= dl*o3[1]-shi->dx_u*o1[1]-shi->dx_v*o2[1];
942                                         shi->dxlo[2]= dl*o3[2]-shi->dx_u*o1[2]-shi->dx_v*o2[2];
943                                         dl= shi->dy_u+shi->dy_v;
944                                         shi->dylo[0]= dl*o3[0]-shi->dy_u*o1[0]-shi->dy_v*o2[0];
945                                         shi->dylo[1]= dl*o3[1]-shi->dy_u*o1[1]-shi->dy_v*o2[1];
946                                         shi->dylo[2]= dl*o3[2]-shi->dy_u*o1[2]-shi->dy_v*o2[2];
947                                 }
948                         }
949
950                         VECCOPY(shi->duplilo, obi->dupliorco);
951                 }
952                 
953                 if(texco & TEXCO_GLOB) {
954                         VECCOPY(shi->gl, shi->co);
955                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
956                         if(shi->osatex) {
957                                 VECCOPY(shi->dxgl, shi->dxco);
958                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
959                                 VECCOPY(shi->dygl, shi->dyco);
960                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
961                         }
962                 }
963                 
964                 if(texco & TEXCO_STRAND) {
965                         shi->strandco= (l*v3->accum - u*v1->accum - v*v2->accum);
966                         if(shi->osatex) {
967                                 dl= shi->dx_u+shi->dx_v;
968                                 shi->dxstrand= dl*v3->accum-shi->dx_u*v1->accum-shi->dx_v*v2->accum;
969                                 dl= shi->dy_u+shi->dy_v;
970                                 shi->dystrand= dl*v3->accum-shi->dy_u*v1->accum-shi->dy_v*v2->accum;
971                         }
972                 }
973                                 
974                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
975                         VlakRen *vlr= shi->vlr;
976                         MTFace *tface;
977                         MCol *mcol;
978                         char *name;
979                         int i, j1=shi->i1, j2=shi->i2, j3=shi->i3;
980
981                         /* uv and vcols are not copied on split, so set them according vlr divide flag */
982                         vlr_set_uv_indices(vlr, &j1, &j2, &j3);
983
984                         shi->totuv= 0;
985                         shi->totcol= 0;
986                         shi->actuv= obr->actmtface;
987                         shi->actcol= obr->actmcol;
988
989                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
990                                 for (i=0; (mcol=RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
991                                         ShadeInputCol *scol= &shi->col[i];
992                                         char *cp1, *cp2, *cp3;
993                                         
994                                         shi->totcol++;
995                                         scol->name= name;
996
997                                         cp1= (char *)(mcol+j1);
998                                         cp2= (char *)(mcol+j2);
999                                         cp3= (char *)(mcol+j3);
1000                                         
1001                                         scol->col[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0f;
1002                                         scol->col[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0f;
1003                                         scol->col[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0f;
1004                                 }
1005
1006                                 if(shi->totcol) {
1007                                         shi->vcol[0]= shi->col[shi->actcol].col[0];
1008                                         shi->vcol[1]= shi->col[shi->actcol].col[1];
1009                                         shi->vcol[2]= shi->col[shi->actcol].col[2];
1010                                         shi->vcol[3]= 1.0f;
1011                                 }
1012                                 else {
1013                                         shi->vcol[0]= 0.0f;
1014                                         shi->vcol[1]= 0.0f;
1015                                         shi->vcol[2]= 0.0f;
1016                                         shi->vcol[3]= 1.0f;
1017                                 }
1018                         }
1019
1020                         for (i=0; (tface=RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1021                                 ShadeInputUV *suv= &shi->uv[i];
1022                                 float *uv1, *uv2, *uv3;
1023
1024                                 shi->totuv++;
1025                                 suv->name= name;
1026                                 
1027                                 uv1= tface->uv[j1];
1028                                 uv2= tface->uv[j2];
1029                                 uv3= tface->uv[j3];
1030                                 
1031                                 suv->uv[0]= -1.0f + 2.0f*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1032                                 suv->uv[1]= -1.0f + 2.0f*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1033                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1034
1035                                 if(shi->osatex) {
1036                                         float duv[2];
1037                                         
1038                                         dl= shi->dx_u+shi->dx_v;
1039                                         duv[0]= shi->dx_u; 
1040                                         duv[1]= shi->dx_v;
1041                                         
1042                                         suv->dxuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1043                                         suv->dxuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1044                                         
1045                                         dl= shi->dy_u+shi->dy_v;
1046                                         duv[0]= shi->dy_u; 
1047                                         duv[1]= shi->dy_v;
1048                                         
1049                                         suv->dyuv[0]= 2.0f*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1050                                         suv->dyuv[1]= 2.0f*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1051                                 }
1052
1053                                 if((mode & MA_FACETEXTURE) && i==obr->actmtface) {
1054                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1055                                                 shi->vcol[0]= 1.0f;
1056                                                 shi->vcol[1]= 1.0f;
1057                                                 shi->vcol[2]= 1.0f;
1058                                                 shi->vcol[3]= 1.0f;
1059                                         }
1060                                         if(tface && tface->tpage)
1061                                                 render_realtime_texture(shi, tface->tpage);
1062                                 }
1063                         }
1064
1065                         shi->dupliuv[0]= -1.0f + 2.0f*obi->dupliuv[0];
1066                         shi->dupliuv[1]= -1.0f + 2.0f*obi->dupliuv[1];
1067                         shi->dupliuv[2]= 0.0f;
1068
1069                         if(shi->totuv == 0) {
1070                                 ShadeInputUV *suv= &shi->uv[0];
1071
1072                                 suv->uv[0]= 2.0f*(u+.5f);
1073                                 suv->uv[1]= 2.0f*(v+.5f);
1074                                 suv->uv[2]= 0.0f;       /* texture.c assumes there are 3 coords */
1075                                 
1076                                 if(mode & MA_FACETEXTURE) {
1077                                         /* no tface? set at 1.0f */
1078                                         shi->vcol[0]= 1.0f;
1079                                         shi->vcol[1]= 1.0f;
1080                                         shi->vcol[2]= 1.0f;
1081                                         shi->vcol[3]= 1.0f;
1082                                 }
1083                         }
1084                 }
1085                 
1086                 if(texco & TEXCO_NORM) {
1087                         shi->orn[0]= -shi->vn[0];
1088                         shi->orn[1]= -shi->vn[1];
1089                         shi->orn[2]= -shi->vn[2];
1090                 }
1091                 
1092                 if(mode & MA_RADIO) {
1093                         float *r1, *r2, *r3;
1094                         
1095                         r1= RE_vertren_get_rad(obr, v1, 0);
1096                         r2= RE_vertren_get_rad(obr, v2, 0);
1097                         r3= RE_vertren_get_rad(obr, v3, 0);
1098                         
1099                         if(r1 && r2 && r3) {
1100                                 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
1101                                 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
1102                                 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
1103                         }
1104                         else
1105                                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1106                 }
1107                 else
1108                         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1109                 
1110                 if(texco & TEXCO_REFL) {
1111                         /* mirror reflection color textures (and envmap) */
1112                         calc_R_ref(shi);        /* wrong location for normal maps! XXXXXXXXXXXXXX */
1113                 }
1114                 
1115                 if(texco & TEXCO_STRESS) {
1116                         float *s1, *s2, *s3;
1117                         
1118                         s1= RE_vertren_get_stress(obr, v1, 0);
1119                         s2= RE_vertren_get_stress(obr, v2, 0);
1120                         s3= RE_vertren_get_stress(obr, v3, 0);
1121                         if(s1 && s2 && s3) {
1122                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
1123                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
1124                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
1125                         }
1126                         else shi->stress= 0.0f;
1127                 }
1128                 
1129                 if(texco & TEXCO_TANGENT) {
1130                         if((mode & MA_TANGENT_V)==0) {
1131                                 /* just prevent surprises */
1132                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1133                                 shi->nmaptang[0]= shi->nmaptang[1]= shi->nmaptang[2]= 0.0f;
1134                         }
1135                 }
1136         }
1137         else
1138                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0f;
1139         
1140         /* this only avalailable for scanline renders */
1141         if(shi->depth==0) {
1142                 float x= shi->xs;
1143                 float y= shi->ys;
1144                 
1145                 if(texco & TEXCO_WINDOW) {
1146                         shi->winco[0]= -1.0f + 2.0f*x/(float)R.winx;
1147                         shi->winco[1]= -1.0f + 2.0f*y/(float)R.winy;
1148                         shi->winco[2]= 0.0f;
1149                         if(shi->osatex) {
1150                                 shi->dxwin[0]= 2.0f/(float)R.winx;
1151                                 shi->dywin[1]= 2.0f/(float)R.winy;
1152                                 shi->dxwin[1]= shi->dxwin[2]= 0.0f;
1153                                 shi->dywin[0]= shi->dywin[2]= 0.0f;
1154                         }
1155                 }
1156
1157                 if(texco & TEXCO_STICKY) {
1158                         float *s1, *s2, *s3;
1159                         
1160                         s1= RE_vertren_get_sticky(obr, v1, 0);
1161                         s2= RE_vertren_get_sticky(obr, v2, 0);
1162                         s3= RE_vertren_get_sticky(obr, v3, 0);
1163                         
1164                         if(s1 && s2 && s3) {
1165                                 float winmat[4][4], ho1[4], ho2[4], ho3[4];
1166                                 float Zmulx, Zmuly;
1167                                 float hox, hoy, l, dl, u, v;
1168                                 float s00, s01, s10, s11, detsh;
1169                                 
1170                                 /* old globals, localized now */
1171                                 Zmulx=  ((float)R.winx)/2.0f; Zmuly=  ((float)R.winy)/2.0f;
1172
1173                                 if(shi->obi->flag & R_TRANSFORMED)
1174                                         zbuf_make_winmat(&R, shi->obi->mat, winmat);
1175                                 else
1176                                         zbuf_make_winmat(&R, NULL, winmat);
1177
1178                                 zbuf_render_project(winmat, v1->co, ho1);
1179                                 zbuf_render_project(winmat, v2->co, ho2);
1180                                 zbuf_render_project(winmat, v3->co, ho3);
1181                                 
1182                                 s00= ho3[0]/ho3[3] - ho1[0]/ho1[3];
1183                                 s01= ho3[1]/ho3[3] - ho1[1]/ho1[3];
1184                                 s10= ho3[0]/ho3[3] - ho2[0]/ho2[3];
1185                                 s11= ho3[1]/ho3[3] - ho2[1]/ho2[3];
1186                                 
1187                                 detsh= s00*s11-s10*s01;
1188                                 s00/= detsh; s01/=detsh; 
1189                                 s10/=detsh; s11/=detsh;
1190                                 
1191                                 /* recalc u and v again */
1192                                 hox= x/Zmulx -1.0f;
1193                                 hoy= y/Zmuly -1.0f;
1194                                 u= (hox - ho3[0]/ho3[3])*s11 - (hoy - ho3[1]/ho3[3])*s10;
1195                                 v= (hoy - ho3[1]/ho3[3])*s00 - (hox - ho3[0]/ho3[3])*s01;
1196                                 l= 1.0f+u+v;
1197                                 
1198                                 shi->sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
1199                                 shi->sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
1200                                 shi->sticky[2]= 0.0f;
1201                                 
1202                                 if(shi->osatex) {
1203                                         float dxuv[2], dyuv[2];
1204                                         dxuv[0]=  s11/Zmulx;
1205                                         dxuv[1]=  - s01/Zmulx;
1206                                         dyuv[0]=  - s10/Zmuly;
1207                                         dyuv[1]=  s00/Zmuly;
1208                                         
1209                                         dl= dxuv[0] + dxuv[1];
1210                                         shi->dxsticky[0]= dl*s3[0] - dxuv[0]*s1[0] - dxuv[1]*s2[0];
1211                                         shi->dxsticky[1]= dl*s3[1] - dxuv[0]*s1[1] - dxuv[1]*s2[1];
1212                                         dl= dyuv[0] + dyuv[1];
1213                                         shi->dysticky[0]= dl*s3[0] - dyuv[0]*s1[0] - dyuv[1]*s2[0];
1214                                         shi->dysticky[1]= dl*s3[1] - dyuv[0]*s1[1] - dyuv[1]*s2[1];
1215                                 }
1216                         }
1217                 }
1218         } /* else {
1219          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
1220         */
1221 }
1222
1223 /* ****************** ShadeSample ************************************** */
1224
1225 /* initialize per part, not per pixel! */
1226 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1227 {
1228         
1229         memset(shi, 0, sizeof(ShadeInput));
1230         
1231         shi->sample= sample;
1232         shi->thread= pa->thread;
1233         shi->do_preview= R.r.scemode & R_NODE_PREVIEW;
1234         shi->lay= rl->lay;
1235         shi->layflag= rl->layflag;
1236         shi->passflag= rl->passflag;
1237         shi->combinedflag= ~rl->pass_xor;
1238         shi->mat_override= rl->mat_override;
1239         shi->light_override= rl->light_override;
1240 //      shi->rl= rl;
1241         /* note shi.depth==0  means first hit, not raytracing */
1242         
1243 }
1244
1245 /* initialize per part, not per pixel! */
1246 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1247 {
1248         int a, tot;
1249         
1250         tot= R.osa==0?1:R.osa;
1251         
1252         for(a=0; a<tot; a++) {
1253                 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1254                 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1255         }
1256         
1257         get_sample_layers(pa, rl, ssamp->rlpp);
1258 }
1259
1260 /* Do AO or (future) GI */
1261 void shade_samples_do_AO(ShadeSample *ssamp)
1262 {
1263         ShadeInput *shi;
1264         int sample;
1265         
1266         if(!(R.r.mode & R_SHADOW))
1267                 return;
1268         if(!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1269                 return;
1270         
1271         if(R.wrld.mode & WO_AMB_OCC) {
1272                 shi= &ssamp->shi[0];
1273
1274                 if(((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & SCE_PASS_AO))
1275                         || (shi->passflag & SCE_PASS_AO))
1276                         for(sample=0, shi= ssamp->shi; sample<ssamp->tot; shi++, sample++)
1277                                 if(!(shi->mode & MA_SHLESS))
1278                                         ambient_occlusion(shi);         /* stores in shi->ao[] */
1279         }
1280 }
1281
1282
1283 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1284 {
1285         ShadeInput *shi;
1286         float xs, ys;
1287         
1288         ssamp->tot= 0;
1289         
1290         for(shi= ssamp->shi; ps; ps= ps->next) {
1291                 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1292                 
1293                 if(shi->vlr) {  /* NULL happens for env material or for 'all z' */
1294                         unsigned short curmask= ps->mask;
1295                         
1296                         /* full osa is only set for OSA renders */
1297                         if(shi->vlr->flag & R_FULL_OSA) {
1298                                 short shi_cp= 0, samp;
1299                                 
1300                                 for(samp=0; samp<R.osa; samp++) {
1301                                         if(curmask & (1<<samp)) {
1302                                                 xs= (float)x + R.jit[samp][0] + 0.5f;   /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1303                                                 ys= (float)y + R.jit[samp][1] + 0.5f;
1304                                                 
1305                                                 if(shi_cp)
1306                                                         shade_input_copy_triangle(shi, shi-1);
1307                                                 
1308                                                 shi->mask= (1<<samp);
1309 //                                              shi->rl= ssamp->rlpp[samp];
1310                                                 shi->samplenr= R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1311                                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
1312                                                 shade_input_set_uv(shi);
1313                                                 shade_input_set_normals(shi);
1314                                                 
1315                                                 shi_cp= 1;
1316                                                 shi++;
1317                                         }
1318                                 }
1319                         }
1320                         else {
1321                                 if(R.osa) {
1322                                         short b= R.samples->centmask[curmask];
1323                                         xs= (float)x + R.samples->centLut[b & 15] + 0.5f;
1324                                         ys= (float)y + R.samples->centLut[b>>4] + 0.5f;
1325                                 }
1326                                 else {
1327                                         xs= (float)x + 0.5f;
1328                                         ys= (float)y + 0.5f;
1329                                 }
1330                                 shi->mask= curmask;
1331                                 shi->samplenr= R.shadowsamplenr[shi->thread]++;
1332                                 shade_input_set_viewco(shi, xs, ys, (float)ps->z);
1333                                 shade_input_set_uv(shi);
1334                                 shade_input_set_normals(shi);
1335                                 shi++;
1336                         }
1337                         
1338                         /* total sample amount, shi->sample is static set in initialize */
1339                         if(shi!=ssamp->shi)
1340                                 ssamp->tot= (shi-1)->sample + 1;
1341                 }
1342         }
1343 }
1344
1345 /* shades samples, returns true if anything happened */
1346 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1347 {
1348         shade_samples_fill_with_ps(ssamp, ps, x, y);
1349         
1350         if(ssamp->tot) {
1351                 ShadeInput *shi= ssamp->shi;
1352                 ShadeResult *shr= ssamp->shr;
1353                 int samp;
1354                 
1355                 /* if shadow or AO? */
1356                 shade_samples_do_AO(ssamp);
1357                 
1358                 /* if shade (all shadepinputs have same passflag) */
1359                 if(ssamp->shi[0].passflag & ~(SCE_PASS_Z|SCE_PASS_INDEXOB)) {
1360
1361                         for(samp=0; samp<ssamp->tot; samp++, shi++, shr++) {
1362                                 shade_input_set_shade_texco(shi);
1363                                 shade_input_do_shade(shi, shr);
1364                         }
1365                 }
1366                 
1367                 return 1;
1368         }
1369         return 0;
1370 }
1371