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