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