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