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