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