Vector Transform node support for GLSL mode and the internal renderer
[blender.git] / source / blender / render / intern / source / shadeoutput.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/shadeoutput.c
27  *  \ingroup render
28  */
29
30
31 #include <stdio.h>
32 #include <float.h>
33 #include <math.h>
34 #include <string.h>
35
36 #include "BLI_math.h"
37 #include "BLI_utildefines.h"
38
39 #include "BKE_colortools.h"
40 #include "BKE_material.h"
41 #include "BKE_texture.h"
42
43
44 #include "DNA_group_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
47
48 /* local include */
49 #include "occlusion.h"
50 #include "render_types.h"
51 #include "rendercore.h"
52 #include "shadbuf.h"
53 #include "sss.h"
54 #include "texture.h"
55
56 #include "shading.h" /* own include */
57
58 #include "IMB_colormanagement.h"
59
60 /* could enable at some point but for now there are far too many conversions */
61 #ifdef __GNUC__
62 #  pragma GCC diagnostic ignored "-Wdouble-promotion"
63 #endif
64
65 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
66 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
67 /* only to be used here in this file, it's for speed */
68 extern struct Render R;
69 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
70
71 ListBase *get_lights(ShadeInput *shi)
72 {
73         
74         if (R.r.scemode & R_BUTS_PREVIEW)
75                 return &R.lights;
76         if (shi->light_override)
77                 return &shi->light_override->gobject;
78         if (shi->mat && shi->mat->group)
79                 return &shi->mat->group->gobject;
80         
81         return &R.lights;
82 }
83
84 #if 0
85 static void fogcolor(const float colf[3], float *rco, float *view)
86 {
87         float alpha, stepsize, startdist, dist, hor[4], zen[3], vec[3], dview[3];
88         float div=0.0f, distfac;
89         
90         hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
91         zen[0]= R.wrld.zenr; zen[1]= R.wrld.zeng; zen[2]= R.wrld.zenb;
92         
93         copy_v3_v3(vec, rco);
94         
95         /* we loop from cur coord to mist start in steps */
96         stepsize= 1.0f;
97         
98         div= ABS(view[2]);
99         dview[0]= view[0]/(stepsize*div);
100         dview[1]= view[1]/(stepsize*div);
101         dview[2]= -stepsize;
102
103         startdist= -rco[2] + BLI_frand();
104         for (dist= startdist; dist>R.wrld.miststa; dist-= stepsize) {
105                 
106                 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
107                 alpha= 1.0f;
108                 do_sky_tex(vec, vec, NULL, hor, zen, &alpha);
109                 
110                 distfac= (dist-R.wrld.miststa)/R.wrld.mistdist;
111                 
112                 hor[3]= hor[0]*distfac*distfac;
113                 
114                 /* premul! */
115                 alpha= hor[3];
116                 hor[0]= hor[0]*alpha;
117                 hor[1]= hor[1]*alpha;
118                 hor[2]= hor[2]*alpha;
119                 addAlphaOverFloat(colf, hor);
120                 
121                 sub_v3_v3(vec, dview);
122         }
123 }
124 #endif
125
126 /* zcor is distance, co the 3d coordinate in eye space, return alpha */
127 float mistfactor(float zcor, float const co[3])
128 {
129         float fac, hi;
130         
131         fac = zcor - R.wrld.miststa;    /* zcor is calculated per pixel */
132
133         /* fac= -co[2]-R.wrld.miststa; */
134
135         if (fac > 0.0f) {
136                 if (fac < R.wrld.mistdist) {
137                         
138                         fac = (fac / R.wrld.mistdist);
139                         
140                         if (R.wrld.mistype == 0) {
141                                 fac *= fac;
142                         }
143                         else if (R.wrld.mistype == 1) {
144                                 /* pass */
145                         }
146                         else {
147                                 fac = sqrtf(fac);
148                         }
149                 }
150                 else {
151                         fac = 1.0f;
152                 }
153         }
154         else {
155                 fac = 0.0f;
156         }
157         
158         /* height switched off mist */
159         if (R.wrld.misthi!=0.0f && fac!=0.0f) {
160                 /* at height misthi the mist is completely gone */
161
162                 hi = R.viewinv[0][2] * co[0] +
163                      R.viewinv[1][2] * co[1] +
164                      R.viewinv[2][2] * co[2] +
165                      R.viewinv[3][2];
166                 
167                 if (hi > R.wrld.misthi) {
168                         fac = 0.0f;
169                 }
170                 else if (hi>0.0f) {
171                         hi= (R.wrld.misthi-hi)/R.wrld.misthi;
172                         fac*= hi*hi;
173                 }
174         }
175
176         return (1.0f-fac)* (1.0f-R.wrld.misi);
177 }
178
179 static void spothalo(struct LampRen *lar, ShadeInput *shi, float *intens)
180 {
181         double a, b, c, disc, nray[3], npos[3];
182         double t0, t1 = 0.0f, t2= 0.0f, t3;
183         float p1[3], p2[3], ladist, maxz = 0.0f, maxy = 0.0f, haint;
184         int cuts;
185         bool do_clip = true, use_yco = false;
186
187         *intens= 0.0f;
188         haint= lar->haint;
189         
190         if (R.r.mode & R_ORTHO) {
191                 /* camera pos (view vector) cannot be used... */
192                 /* camera position (cox,coy,0) rotate around lamp */
193                 p1[0]= shi->co[0]-lar->co[0];
194                 p1[1]= shi->co[1]-lar->co[1];
195                 p1[2]= -lar->co[2];
196                 mul_m3_v3(lar->imat, p1);
197                 copy_v3db_v3fl(npos, p1);  /* npos is double! */
198                 
199                 /* pre-scale */
200                 npos[2] *= (double)lar->sh_zfac;
201         }
202         else {
203                 copy_v3db_v3fl(npos, lar->sh_invcampos);        /* in initlamp calculated */
204         }
205         
206         /* rotate view */
207         copy_v3db_v3fl(nray, shi->view);
208         mul_m3_v3_double(lar->imat, nray);
209         
210         if (R.wrld.mode & WO_MIST) {
211                 /* patchy... */
212                 haint *= mistfactor(-lar->co[2], lar->co);
213                 if (haint==0.0f) {
214                         return;
215                 }
216         }
217
218
219         /* rotate maxz */
220         if (shi->co[2]==0.0f) {
221                 do_clip = false;  /* for when halo at sky */
222         }
223         else {
224                 p1[0]= shi->co[0]-lar->co[0];
225                 p1[1]= shi->co[1]-lar->co[1];
226                 p1[2]= shi->co[2]-lar->co[2];
227         
228                 maxz= lar->imat[0][2]*p1[0]+lar->imat[1][2]*p1[1]+lar->imat[2][2]*p1[2];
229                 maxz*= lar->sh_zfac;
230                 maxy= lar->imat[0][1]*p1[0]+lar->imat[1][1]*p1[1]+lar->imat[2][1]*p1[2];
231
232                 if (fabs(nray[2]) < FLT_EPSILON) {
233                         use_yco = true;
234                 }
235         }
236         
237         /* scale z to make sure volume is normalized */
238         nray[2] *= (double)lar->sh_zfac;
239         /* nray does not need normalization */
240         
241         ladist= lar->sh_zfac*lar->dist;
242         
243         /* solve */
244         a = nray[0] * nray[0] + nray[1] * nray[1] - nray[2]*nray[2];
245         b = nray[0] * npos[0] + nray[1] * npos[1] - nray[2]*npos[2];
246         c = npos[0] * npos[0] + npos[1] * npos[1] - npos[2]*npos[2];
247
248         cuts= 0;
249         if (fabs(a) < DBL_EPSILON) {
250                 /*
251                  * Only one intersection point...
252                  */
253                 return;
254         }
255         else {
256                 disc = b*b - a*c;
257                 
258                 if (disc==0.0) {
259                         t1=t2= (-b)/ a;
260                         cuts= 2;
261                 }
262                 else if (disc > 0.0) {
263                         disc = sqrt(disc);
264                         t1 = (-b + disc) / a;
265                         t2 = (-b - disc) / a;
266                         cuts= 2;
267                 }
268         }
269         if (cuts==2) {
270                 int ok1=0, ok2=0;
271
272                 /* sort */
273                 if (t1>t2) {
274                         a= t1; t1= t2; t2= a;
275                 }
276
277                 /* z of intersection points with diabolo */
278                 p1[2]= npos[2] + t1*nray[2];
279                 p2[2]= npos[2] + t2*nray[2];
280
281                 /* evaluate both points */
282                 if (p1[2]<=0.0f) ok1= 1;
283                 if (p2[2]<=0.0f && t1!=t2) ok2= 1;
284                 
285                 /* at least 1 point with negative z */
286                 if (ok1==0 && ok2==0) return;
287                 
288                 /* intersction point with -ladist, the bottom of the cone */
289                 if (use_yco == false) {
290                         t3= ((double)(-ladist)-npos[2])/nray[2];
291                                 
292                         /* de we have to replace one of the intersection points? */
293                         if (ok1) {
294                                 if (p1[2]<-ladist) t1= t3;
295                         }
296                         else {
297                                 t1= t3;
298                         }
299                         if (ok2) {
300                                 if (p2[2]<-ladist) t2= t3;
301                         }
302                         else {
303                                 t2= t3;
304                         }
305                 }
306                 else if (ok1==0 || ok2==0) return;
307                 
308                 /* at least 1 visible interesction point */
309                 if (t1<0.0 && t2<0.0) return;
310                 
311                 if (t1<0.0) t1= 0.0;
312                 if (t2<0.0) t2= 0.0;
313                 
314                 if (t1==t2) return;
315                 
316                 /* sort again to be sure */
317                 if (t1>t2) {
318                         a= t1; t1= t2; t2= a;
319                 }
320                 
321                 /* calculate t0: is the maximum visible z (when halo is intersected by face) */ 
322                 if (do_clip) {
323                         if (use_yco == false) t0 = ((double)maxz - npos[2]) / nray[2];
324                         else                  t0 = ((double)maxy - npos[1]) / nray[1];
325
326                         if (t0 < t1) return;
327                         if (t0 < t2) t2= t0;
328                 }
329
330                 /* calc points */
331                 p1[0]= npos[0] + t1*nray[0];
332                 p1[1]= npos[1] + t1*nray[1];
333                 p1[2]= npos[2] + t1*nray[2];
334                 p2[0]= npos[0] + t2*nray[0];
335                 p2[1]= npos[1] + t2*nray[1];
336                 p2[2]= npos[2] + t2*nray[2];
337                 
338                         
339                 /* now we have 2 points, make three lengths with it */
340                 
341                 a = len_v3(p1);
342                 b = len_v3(p2);
343                 c = len_v3v3(p1, p2);
344                 
345                 a/= ladist;
346                 a= sqrt(a);
347                 b/= ladist; 
348                 b= sqrt(b);
349                 c/= ladist;
350                 
351                 *intens= c*( (1.0-a)+(1.0-b) );
352
353                 /* WATCH IT: do not clip a,b en c at 1.0, this gives nasty little overflows
354                  * at the edges (especially with narrow halos) */
355                 if (*intens<=0.0f) return;
356
357                 /* soft area */
358                 /* not needed because t0 has been used for p1/p2 as well */
359                 /* if (doclip && t0<t2) { */
360                 /*      *intens *= (t0-t1)/(t2-t1); */
361                 /* } */
362                 
363                 *intens *= haint;
364                 
365                 if (lar->shb && lar->shb->shadhalostep) {
366                         *intens *= shadow_halo(lar, p1, p2);
367                 }
368                 
369         }
370 }
371
372 void renderspothalo(ShadeInput *shi, float col[4], float alpha)
373 {
374         ListBase *lights;
375         GroupObject *go;
376         LampRen *lar;
377         float i;
378         
379         if (alpha==0.0f) return;
380         
381         lights= get_lights(shi);
382         for (go=lights->first; go; go= go->next) {
383                 lar= go->lampren;
384                 if (lar==NULL) continue;
385                 
386                 if (lar->type==LA_SPOT && (lar->mode & LA_HALO) && (lar->buftype != LA_SHADBUF_DEEP) && lar->haint>0) {
387                         
388                         if (lar->mode & LA_LAYER) 
389                                 if (shi->vlr && (lar->lay & shi->obi->lay)==0) 
390                                         continue;
391                         if ((lar->lay & shi->lay)==0) 
392                                 continue;
393                         
394                         spothalo(lar, shi, &i);
395                         if (i > 0.0f) {
396                                 const float i_alpha = i * alpha;
397                                 col[0] += i_alpha * lar->r;
398                                 col[1] += i_alpha * lar->g;
399                                 col[2] += i_alpha * lar->b;
400                                 col[3] += i_alpha;  /* all premul */
401                         }
402                 }
403         }
404         /* clip alpha, is needed for unified 'alpha threshold' (vanillaRenderPipe.c) */
405         if (col[3]>1.0f) col[3]= 1.0f;
406 }
407
408
409
410 /* ---------------- shaders ----------------------- */
411
412 static double Normalize_d(double *n)
413 {
414         double d;
415         
416         d= n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
417
418         if (d>0.00000000000000001) {
419                 d= sqrt(d);
420
421                 n[0]/=d; 
422                 n[1]/=d; 
423                 n[2]/=d;
424         }
425         else {
426                 n[0]=n[1]=n[2]= 0.0;
427                 d= 0.0;
428         }
429         return d;
430 }
431
432 /* mix of 'real' fresnel and allowing control. grad defines blending gradient */
433 float fresnel_fac(const float view[3], const float vn[3], float grad, float fac)
434 {
435         float t1, t2;
436         
437         if (fac==0.0f) return 1.0f;
438         
439         t1 = dot_v3v3(view, vn);
440         if (t1>0.0f)  t2= 1.0f+t1;
441         else t2= 1.0f-t1;
442         
443         t2= grad + (1.0f-grad)*powf(t2, fac);
444         
445         if (t2<0.0f) return 0.0f;
446         else if (t2>1.0f) return 1.0f;
447         return t2;
448 }
449
450 static double saacos_d(double fac)
451 {
452         if (fac<= -1.0) return M_PI;
453         else if (fac>=1.0) return 0.0;
454         else return acos(fac);
455 }
456
457 /* Stoke's form factor. Need doubles here for extreme small area sizes */
458 static float area_lamp_energy(float (*area)[3], const float co[3], const float vn[3])
459 {
460         double fac;
461         double vec[4][3];       /* vectors of rendered co to vertices lamp */
462         double cross[4][3];     /* cross products of this */
463         double rad[4];          /* angles between vecs */
464
465         VECSUB(vec[0], co, area[0]);
466         VECSUB(vec[1], co, area[1]);
467         VECSUB(vec[2], co, area[2]);
468         VECSUB(vec[3], co, area[3]);
469         
470         Normalize_d(vec[0]);
471         Normalize_d(vec[1]);
472         Normalize_d(vec[2]);
473         Normalize_d(vec[3]);
474
475         /* cross product */
476 #define CROSS(dest, a, b) \
477         { \
478                 dest[0]= a[1] * b[2] - a[2] * b[1]; \
479                 dest[1]= a[2] * b[0] - a[0] * b[2]; \
480                 dest[2]= a[0] * b[1] - a[1] * b[0]; \
481         } (void)0
482
483         CROSS(cross[0], vec[0], vec[1]);
484         CROSS(cross[1], vec[1], vec[2]);
485         CROSS(cross[2], vec[2], vec[3]);
486         CROSS(cross[3], vec[3], vec[0]);
487
488 #undef CROSS
489
490         Normalize_d(cross[0]);
491         Normalize_d(cross[1]);
492         Normalize_d(cross[2]);
493         Normalize_d(cross[3]);
494
495         /* angles */
496         rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
497         rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
498         rad[2]= vec[2][0]*vec[3][0]+ vec[2][1]*vec[3][1]+ vec[2][2]*vec[3][2];
499         rad[3]= vec[3][0]*vec[0][0]+ vec[3][1]*vec[0][1]+ vec[3][2]*vec[0][2];
500
501         rad[0]= saacos_d(rad[0]);
502         rad[1]= saacos_d(rad[1]);
503         rad[2]= saacos_d(rad[2]);
504         rad[3]= saacos_d(rad[3]);
505
506         /* Stoke formula */
507         fac=  rad[0]*(vn[0]*cross[0][0]+ vn[1]*cross[0][1]+ vn[2]*cross[0][2]);
508         fac+= rad[1]*(vn[0]*cross[1][0]+ vn[1]*cross[1][1]+ vn[2]*cross[1][2]);
509         fac+= rad[2]*(vn[0]*cross[2][0]+ vn[1]*cross[2][1]+ vn[2]*cross[2][2]);
510         fac+= rad[3]*(vn[0]*cross[3][0]+ vn[1]*cross[3][1]+ vn[2]*cross[3][2]);
511
512         if (fac<=0.0) return 0.0;
513         return fac;
514 }
515
516 static float area_lamp_energy_multisample(LampRen *lar, const float co[3], float *vn)
517 {
518         /* corner vectors are moved around according lamp jitter */
519         float *jitlamp= lar->jitter, vec[3];
520         float area[4][3], intens= 0.0f;
521         int a= lar->ray_totsamp;
522
523         /* test if co is behind lamp */
524         sub_v3_v3v3(vec, co, lar->co);
525         if (dot_v3v3(vec, lar->vec) < 0.0f)
526                 return 0.0f;
527
528         while (a--) {
529                 vec[0]= jitlamp[0];
530                 vec[1]= jitlamp[1];
531                 vec[2]= 0.0f;
532                 mul_m3_v3(lar->mat, vec);
533                 
534                 add_v3_v3v3(area[0], lar->area[0], vec);
535                 add_v3_v3v3(area[1], lar->area[1], vec);
536                 add_v3_v3v3(area[2], lar->area[2], vec);
537                 add_v3_v3v3(area[3], lar->area[3], vec);
538                 
539                 intens+= area_lamp_energy(area, co, vn);
540                 
541                 jitlamp+= 2;
542         }
543         intens /= (float)lar->ray_totsamp;
544         
545         return pow(intens * lar->areasize, lar->k);     /* corrected for buttons size and lar->dist^2 */
546 }
547
548 static float spec(float inp, int hard)  
549 {
550         float b1;
551         
552         if (inp>=1.0f) return 1.0f;
553         else if (inp<=0.0f) return 0.0f;
554         
555         b1= inp*inp;
556         /* avoid FPE */
557         if (b1<0.01f) b1= 0.01f;
558         
559         if ((hard & 1)==0)  inp= 1.0f;
560         if (hard & 2)  inp*= b1;
561         b1*= b1;
562         if (hard & 4)  inp*= b1;
563         b1*= b1;
564         if (hard & 8)  inp*= b1;
565         b1*= b1;
566         if (hard & 16) inp*= b1;
567         b1*= b1;
568
569         /* avoid FPE */
570         if (b1<0.001f) b1= 0.0f;
571
572         if (hard & 32) inp*= b1;
573         b1*= b1;
574         if (hard & 64) inp*=b1;
575         b1*= b1;
576         if (hard & 128) inp*=b1;
577
578         if (b1<0.001f) b1= 0.0f;
579
580         if (hard & 256) {
581                 b1*= b1;
582                 inp*=b1;
583         }
584
585         return inp;
586 }
587
588 static float Phong_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent )
589 {
590         float h[3];
591         float rslt;
592         
593         h[0] = l[0] + v[0];
594         h[1] = l[1] + v[1];
595         h[2] = l[2] + v[2];
596         normalize_v3(h);
597         
598         rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
599         if (tangent) rslt= sasqrt(1.0f - rslt*rslt);
600                 
601         if ( rslt > 0.0f ) rslt= spec(rslt, hard);
602         else rslt = 0.0f;
603         
604         return rslt;
605 }
606
607
608 /* reduced cook torrance spec (for off-specular peak) */
609 static float CookTorr_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent)
610 {
611         float i, nh, nv, h[3];
612
613         h[0]= v[0]+l[0];
614         h[1]= v[1]+l[1];
615         h[2]= v[2]+l[2];
616         normalize_v3(h);
617
618         nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2];
619         if (tangent) nh= sasqrt(1.0f - nh*nh);
620         else if (nh<0.0f) return 0.0f;
621         
622         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
623         if (tangent) nv= sasqrt(1.0f - nv*nv);
624         else if (nv<0.0f) nv= 0.0f;
625
626         i= spec(nh, hard);
627
628         i= i/(0.1f+nv);
629         return i;
630 }
631
632 /* Blinn spec */
633 static float Blinn_Spec(const float n[3], const float l[3], const float v[3], float refrac, float spec_power, int tangent)
634 {
635         float i, nh, nv, nl, vh, h[3];
636         float a, b, c, g=0.0f, p, f, ang;
637
638         if (refrac < 1.0f) return 0.0f;
639         if (spec_power == 0.0f) return 0.0f;
640         
641         /* conversion from 'hardness' (1-255) to 'spec_power' (50 maps at 0.1) */
642         if (spec_power<100.0f)
643                 spec_power = sqrtf(1.0f / spec_power);
644         else spec_power= 10.0f/spec_power;
645         
646         h[0]= v[0]+l[0];
647         h[1]= v[1]+l[1];
648         h[2]= v[2]+l[2];
649         normalize_v3(h);
650
651         nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
652         if (tangent) nh= sasqrt(1.0f - nh*nh);
653         else if (nh<0.0f) return 0.0f;
654
655         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
656         if (tangent) nv= sasqrt(1.0f - nv*nv);
657         if (nv<=0.01f) nv= 0.01f;                               /* hrms... */
658
659         nl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
660         if (tangent) nl= sasqrt(1.0f - nl*nl);
661         if (nl<=0.01f) {
662                 return 0.0f;
663         }
664
665         vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and half-way vector */
666         if (vh<=0.0f) vh= 0.01f;
667
668         a = 1.0f;
669         b = (2.0f*nh*nv)/vh;
670         c = (2.0f*nh*nl)/vh;
671
672         if ( a < b && a < c ) g = a;
673         else if ( b < a && b < c ) g = b;
674         else if ( c < a && c < b ) g = c;
675
676         p = sqrt((double)((refrac * refrac)+(vh * vh) - 1.0f));
677         f = (((p-vh)*(p-vh))/((p+vh)*(p+vh)))*(1+((((vh*(p+vh))-1.0f)*((vh*(p+vh))-1.0f))/(((vh*(p-vh))+1.0f)*((vh*(p-vh))+1.0f))));
678         ang = saacos(nh);
679
680         i= f * g * exp((double)(-(ang*ang) / (2.0f*spec_power*spec_power)));
681         if (i<0.0f) i= 0.0f;
682         
683         return i;
684 }
685
686 /* cartoon render spec */
687 static float Toon_Spec(const float n[3], const float l[3], const float v[3], float size, float smooth, int tangent)
688 {
689         float h[3];
690         float ang;
691         float rslt;
692         
693         h[0] = l[0] + v[0];
694         h[1] = l[1] + v[1];
695         h[2] = l[2] + v[2];
696         normalize_v3(h);
697         
698         rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
699         if (tangent) rslt = sasqrt(1.0f - rslt*rslt);
700         
701         ang = saacos( rslt ); 
702         
703         if ( ang < size ) rslt = 1.0f;
704         else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
705         else rslt = 1.0f - ((ang - size) / smooth);
706         
707         return rslt;
708 }
709
710 /* Ward isotropic gaussian spec */
711 static float WardIso_Spec(const float n[3], const float l[3], const float v[3], float rms, int tangent)
712 {
713         float i, nh, nv, nl, h[3], angle, alpha;
714
715
716         /* half-way vector */
717         h[0] = l[0] + v[0];
718         h[1] = l[1] + v[1];
719         h[2] = l[2] + v[2];
720         normalize_v3(h);
721
722         nh = n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
723         if (tangent) nh = sasqrt(1.0f - nh*nh);
724         if (nh<=0.0f) nh = 0.001f;
725         
726         nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
727         if (tangent) nv = sasqrt(1.0f - nv*nv);
728         if (nv<=0.0f) nv = 0.001f;
729
730         nl = n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
731         if (tangent) nl = sasqrt(1.0f - nl*nl);
732         if (nl<=0.0f) nl = 0.001f;
733
734         angle = tanf(saacos(nh));
735         alpha = MAX2(rms, 0.001f);
736
737         i= nl * (1.0f/(4.0f*(float)M_PI*alpha*alpha)) * (expf( -(angle*angle)/(alpha*alpha))/(sqrtf(nv*nl)));
738
739         return i;
740 }
741
742 /* cartoon render diffuse */
743 static float Toon_Diff(const float n[3], const float l[3], const float UNUSED(v[3]), float size, float smooth)
744 {
745         float rslt, ang;
746
747         rslt = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
748
749         ang = saacos(rslt);
750
751         if ( ang < size ) rslt = 1.0f;
752         else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
753         else rslt = 1.0f - ((ang - size) / smooth);
754
755         return rslt;
756 }
757
758 /* Oren Nayar diffuse */
759
760 /* 'nl' is either dot product, or return value of area light */
761 /* in latter case, only last multiplication uses 'nl' */
762 static float OrenNayar_Diff(float nl, const float n[3], const float l[3], const float v[3], float rough )
763 {
764         float i/*, nh*/, nv /*, vh */, realnl, h[3];
765         float a, b, t, A, B;
766         float Lit_A, View_A, Lit_B[3], View_B[3];
767         
768         h[0]= v[0]+l[0];
769         h[1]= v[1]+l[1];
770         h[2]= v[2]+l[2];
771         normalize_v3(h);
772         
773         /* nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; */ /* Dot product between surface normal and half-way vector */
774         /* if (nh<0.0f) nh = 0.0f; */
775         
776         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
777         if (nv<=0.0f) nv= 0.0f;
778         
779         realnl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
780         if (realnl<=0.0f) return 0.0f;
781         if (nl<0.0f) return 0.0f;               /* value from area light */
782         
783         /* vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; */ /* Dot product between view vector and halfway vector */
784         /* if (vh<=0.0f) vh= 0.0f; */
785         
786         Lit_A = saacos(realnl);
787         View_A = saacos( nv );
788         
789         Lit_B[0] = l[0] - (realnl * n[0]);
790         Lit_B[1] = l[1] - (realnl * n[1]);
791         Lit_B[2] = l[2] - (realnl * n[2]);
792         normalize_v3(Lit_B);
793         
794         View_B[0] = v[0] - (nv * n[0]);
795         View_B[1] = v[1] - (nv * n[1]);
796         View_B[2] = v[2] - (nv * n[2]);
797         normalize_v3(View_B);
798         
799         t = Lit_B[0]*View_B[0] + Lit_B[1]*View_B[1] + Lit_B[2]*View_B[2];
800         if ( t < 0 ) t = 0;
801         
802         if ( Lit_A > View_A ) {
803                 a = Lit_A;
804                 b = View_A;
805         }
806         else {
807                 a = View_A;
808                 b = Lit_A;
809         }
810         
811         A = 1.0f - (0.5f * ((rough * rough) / ((rough * rough) + 0.33f)));
812         B = 0.45f * ((rough * rough) / ((rough * rough) + 0.09f));
813         
814         b*= 0.95f;      /* prevent tangens from shooting to inf, 'nl' can be not a dot product here. */
815                                 /* overflow only happens with extreme size area light, and higher roughness */
816         i = nl * ( A + ( B * t * sinf(a) * tanf(b) ) );
817         
818         return i;
819 }
820
821 /* Minnaert diffuse */
822 static float Minnaert_Diff(float nl, const float n[3], const float v[3], float darkness)
823 {
824         float i, nv;
825
826         /* nl = dot product between surface normal and light vector */
827         if (nl <= 0.0f)
828                 return 0.0f;
829
830         /* nv = dot product between surface normal and view vector */
831         nv = dot_v3v3(n, v);
832         if (nv < 0.0f)
833                 nv = 0.0f;
834
835         if (darkness <= 1.0f)
836                 i = nl * pow(max_ff(nv * nl, 0.1f), (darkness - 1.0f) ); /*The Real model*/
837         else
838                 i = nl * pow( (1.001f - nv), (darkness  - 1.0f) ); /*Nvidia model*/
839
840         return i;
841 }
842
843 static float Fresnel_Diff(float *vn, float *lv, float *UNUSED(view), float fac_i, float fac)
844 {
845         return fresnel_fac(lv, vn, fac_i, fac);
846 }
847
848 /* --------------------------------------------- */
849 /* also called from texture.c */
850 void calc_R_ref(ShadeInput *shi)
851 {
852         float i;
853
854         /* shi->vn dot shi->view */
855         i= -2*(shi->vn[0]*shi->view[0]+shi->vn[1]*shi->view[1]+shi->vn[2]*shi->view[2]);
856
857         shi->ref[0]= (shi->view[0]+i*shi->vn[0]);
858         shi->ref[1]= (shi->view[1]+i*shi->vn[1]);
859         shi->ref[2]= (shi->view[2]+i*shi->vn[2]);
860         if (shi->osatex) {
861                 if (shi->vlr->flag & R_SMOOTH) {
862                         i= -2*( (shi->vn[0]+shi->dxno[0])*(shi->view[0]+shi->dxview) +
863                                 (shi->vn[1]+shi->dxno[1])*shi->view[1]+ (shi->vn[2]+shi->dxno[2])*shi->view[2] );
864
865                         shi->dxref[0]= shi->ref[0]- ( shi->view[0]+shi->dxview+i*(shi->vn[0]+shi->dxno[0]));
866                         shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*(shi->vn[1]+shi->dxno[1]));
867                         shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dxno[2]));
868
869                         i= -2*( (shi->vn[0]+shi->dyno[0])*shi->view[0]+
870                                 (shi->vn[1]+shi->dyno[1])*(shi->view[1]+shi->dyview)+ (shi->vn[2]+shi->dyno[2])*shi->view[2] );
871
872                         shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*(shi->vn[0]+shi->dyno[0]));
873                         shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*(shi->vn[1]+shi->dyno[1]));
874                         shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dyno[2]));
875
876                 }
877                 else {
878
879                         i= -2*( shi->vn[0]*(shi->view[0]+shi->dxview) +
880                                 shi->vn[1]*shi->view[1]+ shi->vn[2]*shi->view[2] );
881
882                         shi->dxref[0]= shi->ref[0]- (shi->view[0]+shi->dxview+i*shi->vn[0]);
883                         shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*shi->vn[1]);
884                         shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
885
886                         i= -2*( shi->vn[0]*shi->view[0]+
887                                 shi->vn[1]*(shi->view[1]+shi->dyview)+ shi->vn[2]*shi->view[2] );
888
889                         shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*shi->vn[0]);
890                         shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*shi->vn[1]);
891                         shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
892                 }
893         }
894
895 }
896
897 /* called from rayshade.c */
898 void shade_color(ShadeInput *shi, ShadeResult *shr)
899 {
900         Material *ma= shi->mat;
901
902         if (ma->mode & (MA_FACETEXTURE)) {
903                 shi->r= shi->vcol[0];
904                 shi->g= shi->vcol[1];
905                 shi->b= shi->vcol[2];
906                 if (ma->mode & (MA_FACETEXTURE_ALPHA))
907                         shi->alpha= shi->vcol[3];
908         }
909         else if (ma->mode & (MA_VERTEXCOLP)) {
910                 float neg_alpha = 1.0f - shi->vcol[3];
911                 shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
912                 shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
913                 shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
914         }
915         
916         if (ma->texco)
917                 do_material_tex(shi, &R);
918
919         if (ma->fresnel_tra!=0.0f) 
920                 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
921         
922         if (!(shi->mode & MA_TRANSP)) shi->alpha= 1.0f;
923         
924         shr->diff[0]= shi->r;
925         shr->diff[1]= shi->g;
926         shr->diff[2]= shi->b;
927         shr->alpha= shi->alpha;
928
929         /* modulate by the object color */
930         if ((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
931                 float obcol[4];
932
933                 copy_v4_v4(obcol, shi->obr->ob->col);
934                 CLAMP(obcol[3], 0.0f, 1.0f);
935
936                 shr->diff[0] *= obcol[0];
937                 shr->diff[1] *= obcol[1];
938                 shr->diff[2] *= obcol[2];
939                 if (shi->mode & MA_TRANSP) shr->alpha *= obcol[3];
940         }
941
942         copy_v3_v3(shr->diffshad, shr->diff);
943 }
944
945 /* ramp for at end of shade */
946 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
947 {
948         Material *ma= shi->mat;
949         float col[4];
950
951         if (ma->ramp_col) {
952                 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
953                         float fac = IMB_colormanagement_get_luminance(diff);
954                         do_colorband(ma->ramp_col, fac, col);
955                         
956                         /* blending method */
957                         fac= col[3]*ma->rampfac_col;
958                         
959                         ramp_blend(ma->rampblend_col, diff, fac, col);
960                 }
961         }
962 }
963
964 /* r,g,b denote energy, ramp is used with different values to make new material color */
965 static void add_to_diffuse(float diff[3], const ShadeInput *shi, const float is, const float rgb[3])
966 {
967         Material *ma= shi->mat;
968
969         if (ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
970                 
971                 /* MA_RAMP_IN_RESULT is exceptional */
972                 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
973                         /* normal add */
974                         diff[0] += rgb[0] * shi->r;
975                         diff[1] += rgb[1] * shi->g;
976                         diff[2] += rgb[2] * shi->b;
977                 }
978                 else {
979                         float colt[3], col[4];
980                         float fac;
981
982                         /* input */
983                         switch (ma->rampin_col) {
984                         case MA_RAMP_IN_ENERGY:
985                                 fac = IMB_colormanagement_get_luminance(rgb);
986                                 break;
987                         case MA_RAMP_IN_SHADER:
988                                 fac = is;
989                                 break;
990                         case MA_RAMP_IN_NOR:
991                                 fac = dot_v3v3(shi->view, shi->vn);
992                                 break;
993                         default:
994                                 fac = 0.0f;
995                                 break;
996                         }
997         
998                         do_colorband(ma->ramp_col, fac, col);
999                         
1000                         /* blending method */
1001                         fac = col[3] * ma->rampfac_col;
1002                         copy_v3_v3(colt, &shi->r);
1003
1004                         ramp_blend(ma->rampblend_col, colt, fac, col);
1005
1006                         /* output to */
1007                         diff[0] += rgb[0] * colt[0];
1008                         diff[1] += rgb[1] * colt[1];
1009                         diff[2] += rgb[2] * colt[2];
1010                 }
1011         }
1012         else {
1013                 diff[0] += rgb[0] * shi->r;
1014                 diff[1] += rgb[1] * shi->g;
1015                 diff[2] += rgb[2] * shi->b;
1016         }
1017 }
1018
1019 static void ramp_spec_result(float spec_col[3], ShadeInput *shi)
1020 {
1021         Material *ma= shi->mat;
1022
1023         if (ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
1024                 float col[4];
1025                 float fac = IMB_colormanagement_get_luminance(spec_col);
1026
1027                 do_colorband(ma->ramp_spec, fac, col);
1028                 
1029                 /* blending method */
1030                 fac= col[3]*ma->rampfac_spec;
1031                 
1032                 ramp_blend(ma->rampblend_spec, spec_col, fac, col);
1033                 
1034         }
1035 }
1036
1037 /* is = dot product shade, t = spec energy */
1038 static void do_specular_ramp(ShadeInput *shi, float is, float t, float spec[3])
1039 {
1040         Material *ma= shi->mat;
1041
1042         spec[0]= shi->specr;
1043         spec[1]= shi->specg;
1044         spec[2]= shi->specb;
1045
1046         /* MA_RAMP_IN_RESULT is exception */
1047         if (ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
1048                 float fac;
1049                 float col[4];
1050
1051                 /* input */
1052                 switch (ma->rampin_spec) {
1053                 case MA_RAMP_IN_ENERGY:
1054                         fac= t;
1055                         break;
1056                 case MA_RAMP_IN_SHADER:
1057                         fac= is;
1058                         break;
1059                 case MA_RAMP_IN_NOR:
1060                         fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1061                         break;
1062                 default:
1063                         fac= 0.0f;
1064                         break;
1065                 }
1066                 
1067                 do_colorband(ma->ramp_spec, fac, col);
1068                 
1069                 /* blending method */
1070                 fac= col[3]*ma->rampfac_spec;
1071                 
1072                 ramp_blend(ma->rampblend_spec, spec, fac, col);
1073         }
1074 }
1075
1076 /* pure AO, check for raytrace and world should have been done */
1077 /* preprocess, textures were not done, don't use shi->amb for that reason */
1078 void ambient_occlusion(ShadeInput *shi)
1079 {
1080         if ((R.wrld.ao_gather_method == WO_AOGATHER_APPROX) && shi->mat->amb!=0.0f) {
1081                 sample_occ(&R, shi);
1082         }
1083         else if ((R.r.mode & R_RAYTRACE) && shi->mat->amb!=0.0f) {
1084                 ray_ao(shi, shi->ao, shi->env);
1085         }
1086         else {
1087                 shi->ao[0]= shi->ao[1]= shi->ao[2]= 1.0f;
1088                 zero_v3(shi->env);
1089                 zero_v3(shi->indirect);
1090         }
1091 }
1092
1093
1094 /* wrld mode was checked for */
1095 static void ambient_occlusion_apply(ShadeInput *shi, ShadeResult *shr)
1096 {
1097         float f= R.wrld.aoenergy;
1098         float tmp[3], tmpspec[3];
1099
1100         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1101                 return;
1102         if (f == 0.0f)
1103                 return;
1104
1105         if (R.wrld.aomix==WO_AOADD) {
1106                 shr->combined[0] += shi->ao[0]*shi->r*shi->refl*f;
1107                 shr->combined[1] += shi->ao[1]*shi->g*shi->refl*f;
1108                 shr->combined[2] += shi->ao[2]*shi->b*shi->refl*f;
1109         }
1110         else if (R.wrld.aomix==WO_AOMUL) {
1111                 mul_v3_v3v3(tmp, shr->combined, shi->ao);
1112                 mul_v3_v3v3(tmpspec, shr->spec, shi->ao);
1113
1114                 if (f == 1.0f) {
1115                         copy_v3_v3(shr->combined, tmp);
1116                         copy_v3_v3(shr->spec, tmpspec);
1117                 }
1118                 else {
1119                         interp_v3_v3v3(shr->combined, shr->combined, tmp, f);
1120                         interp_v3_v3v3(shr->spec, shr->spec, tmpspec, f);
1121                 }
1122         }
1123 }
1124
1125 void environment_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1126 {
1127         float f= R.wrld.ao_env_energy*shi->amb;
1128
1129         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1130                 return;
1131         if (f == 0.0f)
1132                 return;
1133         
1134         shr->combined[0] += shi->env[0]*shi->r*shi->refl*f;
1135         shr->combined[1] += shi->env[1]*shi->g*shi->refl*f;
1136         shr->combined[2] += shi->env[2]*shi->b*shi->refl*f;
1137 }
1138
1139 static void indirect_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1140 {
1141         float f= R.wrld.ao_indirect_energy;
1142
1143         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1144                 return;
1145         if (f == 0.0f)
1146                 return;
1147
1148         shr->combined[0] += shi->indirect[0]*shi->r*shi->refl*f;
1149         shr->combined[1] += shi->indirect[1]*shi->g*shi->refl*f;
1150         shr->combined[2] += shi->indirect[2]*shi->b*shi->refl*f;
1151 }
1152
1153 /* result written in shadfac */
1154 void lamp_get_shadow(LampRen *lar, ShadeInput *shi, float inp, float shadfac[4], int do_real)
1155 {
1156         LampShadowSubSample *lss= &(lar->shadsamp[shi->thread].s[shi->sample]);
1157         
1158         if (do_real || lss->samplenr!=shi->samplenr) {
1159                 
1160                 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1161                 
1162                 if (lar->shb) {
1163                         if (lar->buftype==LA_SHADBUF_IRREGULAR)
1164                                 shadfac[3]= ISB_getshadow(shi, lar->shb);
1165                         else
1166                                 shadfac[3] = testshadowbuf(&R, lar->shb, shi->co, shi->dxco, shi->dyco, inp, shi->mat->lbias);
1167                 }
1168                 else if (lar->mode & LA_SHAD_RAY) {
1169                         ray_shadow(shi, lar, shadfac);
1170                 }
1171                 
1172                 if (shi->depth==0) {
1173                         copy_v4_v4(lss->shadfac, shadfac);
1174                         lss->samplenr= shi->samplenr;
1175                 }
1176         }
1177         else {
1178                 copy_v4_v4(shadfac, lss->shadfac);
1179         }
1180 }
1181
1182 /* lampdistance and spot angle, writes in lv and dist */
1183 float lamp_get_visibility(LampRen *lar, const float co[3], float lv[3], float *dist)
1184 {
1185         if (lar->type==LA_SUN || lar->type==LA_HEMI) {
1186                 *dist= 1.0f;
1187                 copy_v3_v3(lv, lar->vec);
1188                 return 1.0f;
1189         }
1190         else {
1191                 float visifac= 1.0f;
1192                 
1193                 sub_v3_v3v3(lv, co, lar->co);
1194                 mul_v3_fl(lv, 1.0f / (*dist = len_v3(lv)));
1195                 
1196                 /* area type has no quad or sphere option */
1197                 if (lar->type==LA_AREA) {
1198                         /* area is single sided */
1199                         //if (dot_v3v3(lv, lar->vec) > 0.0f)
1200                         //      visifac= 1.0f;
1201                         //else
1202                         //      visifac= 0.0f;
1203                 }
1204                 else {
1205                         switch (lar->falloff_type) {
1206                                 case LA_FALLOFF_CONSTANT:
1207                                         visifac = 1.0f;
1208                                         break;
1209                                 case LA_FALLOFF_INVLINEAR:
1210                                         visifac = lar->dist/(lar->dist + dist[0]);
1211                                         break;
1212                                 case LA_FALLOFF_INVSQUARE:
1213                                         /* NOTE: This seems to be a hack since commit r12045 says this
1214                                          * option is similar to old Quad, but with slight changes.
1215                                          * Correct inv square would be (which would be old Quad):
1216                                          * visifac = lar->distkw / (lar->distkw + dist[0]*dist[0]);
1217                                          */
1218                                         visifac = lar->dist / (lar->dist + dist[0]*dist[0]);
1219                                         break;
1220                                 case LA_FALLOFF_SLIDERS:
1221                                         if (lar->ld1>0.0f)
1222                                                 visifac= lar->dist/(lar->dist+lar->ld1*dist[0]);
1223                                         if (lar->ld2>0.0f)
1224                                                 visifac*= lar->distkw/(lar->distkw+lar->ld2*dist[0]*dist[0]);
1225                                         break;
1226                                 case LA_FALLOFF_CURVE:
1227                                         /* curvemapping_initialize is called from #add_render_lamp */
1228                                         visifac = curvemapping_evaluateF(lar->curfalloff, 0, dist[0]/lar->dist);
1229                                         break;
1230                         }
1231                         
1232                         if (lar->mode & LA_SPHERE) {
1233                                 float t= lar->dist - dist[0];
1234                                 if (t<=0.0f) 
1235                                         visifac= 0.0f;
1236                                 else
1237                                         visifac*= t/lar->dist;
1238                         }
1239                         
1240                         if (visifac > 0.0f) {
1241                                 if (lar->type==LA_SPOT) {
1242                                         float inpr, t;
1243                                         
1244                                         if (lar->mode & LA_SQUARE) {
1245                                                 if (dot_v3v3(lv, lar->vec) > 0.0f) {
1246                                                         float lvrot[3], x;
1247                                                         
1248                                                         /* rotate view to lampspace */
1249                                                         copy_v3_v3(lvrot, lv);
1250                                                         mul_m3_v3(lar->imat, lvrot);
1251                                                         
1252                                                         x = max_ff(fabsf(lvrot[0]/lvrot[2]), fabsf(lvrot[1]/lvrot[2]));
1253                                                         /* 1.0f/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1254                                                         
1255                                                         inpr = 1.0f / (sqrtf(1.0f + x * x));
1256                                                 }
1257                                                 else inpr= 0.0f;
1258                                         }
1259                                         else {
1260                                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1261                                         }
1262                                         
1263                                         t= lar->spotsi;
1264                                         if (inpr<=t) 
1265                                                 visifac= 0.0f;
1266                                         else {
1267                                                 t= inpr-t;
1268                                                 if (t<lar->spotbl && lar->spotbl!=0.0f) {
1269                                                         /* soft area */
1270                                                         float i= t/lar->spotbl;
1271                                                         t= i*i;
1272                                                         inpr*= (3.0f*t-2.0f*t*i);
1273                                                 }
1274                                                 visifac*= inpr;
1275                                         }
1276                                 }
1277                         }
1278                 }
1279                 if (visifac <= 0.001f) visifac = 0.0f;
1280                 return visifac;
1281         }
1282 }
1283
1284 /* function returns raw diff, spec and full shadowed diff in the 'shad' pass */
1285 static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int passflag)
1286 {
1287         Material *ma= shi->mat;
1288         VlakRen *vlr= shi->vlr;
1289         float lv[3], lampdist, lacol[3], shadfac[4], lashdw[3];
1290         float i, is, i_noshad, inp, *vn, *view, vnor[3], phongcorr=1.0f;
1291         float visifac;
1292         
1293         vn= shi->vn;
1294         view= shi->view;
1295         
1296         
1297         if (lar->energy == 0.0f) return;
1298         /* only shadow lamps shouldn't affect shadow-less materials at all */
1299         if ((lar->mode & LA_ONLYSHADOW) && (!(ma->mode & MA_SHADOW) || !(R.r.mode & R_SHADOW)))
1300                 return;
1301         /* optimization, don't render fully black lamps */
1302         if (!(lar->mode & LA_TEXTURE) && (lar->r + lar->g + lar->b == 0.0f))
1303                 return;
1304         
1305         /* lampdist, spot angle, area side, ... */
1306         visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1307         if (visifac==0.0f)
1308                 return;
1309         
1310         if (lar->type==LA_SPOT) {
1311                 if (lar->mode & LA_OSATEX) {
1312                         shi->osatex= 1; /* signal for multitex() */
1313                         
1314                         shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/lampdist;
1315                         shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/lampdist;
1316                         shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/lampdist;
1317                         
1318                         shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/lampdist;
1319                         shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/lampdist;
1320                         shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/lampdist;
1321                 }
1322         }
1323         
1324         /* lamp color texture */
1325         lacol[0]= lar->r;
1326         lacol[1]= lar->g;
1327         lacol[2]= lar->b;
1328         
1329         lashdw[0]= lar->shdwr;
1330         lashdw[1]= lar->shdwg;
1331         lashdw[2]= lar->shdwb;
1332         
1333         if (lar->mode & LA_TEXTURE)     do_lamp_tex(lar, lv, shi, lacol, LA_TEXTURE);
1334         if (lar->mode & LA_SHAD_TEX)    do_lamp_tex(lar, lv, shi, lashdw, LA_SHAD_TEX);
1335
1336                 /* tangent case; calculate fake face normal, aligned with lampvector */
1337                 /* note, vnor==vn is used as tangent trigger for buffer shadow */
1338         if (vlr->flag & R_TANGENT) {
1339                 float cross[3], nstrand[3], blend;
1340
1341                 if (ma->mode & MA_STR_SURFDIFF) {
1342                         cross_v3_v3v3(cross, shi->surfnor, vn);
1343                         cross_v3_v3v3(nstrand, vn, cross);
1344
1345                         blend= dot_v3v3(nstrand, shi->surfnor);
1346                         blend= 1.0f - blend;
1347                         CLAMP(blend, 0.0f, 1.0f);
1348
1349                         interp_v3_v3v3(vnor, nstrand, shi->surfnor, blend);
1350                         normalize_v3(vnor);
1351                 }
1352                 else {
1353                         cross_v3_v3v3(cross, lv, vn);
1354                         cross_v3_v3v3(vnor, cross, vn);
1355                         normalize_v3(vnor);
1356                 }
1357
1358                 if (ma->strand_surfnor > 0.0f) {
1359                         if (ma->strand_surfnor > shi->surfdist) {
1360                                 blend= (ma->strand_surfnor - shi->surfdist)/ma->strand_surfnor;
1361                                 interp_v3_v3v3(vnor, vnor, shi->surfnor, blend);
1362                                 normalize_v3(vnor);
1363                         }
1364                 }
1365
1366                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1367                 vn= vnor;
1368         }
1369         else if (ma->mode & MA_TANGENT_V) {
1370                 float cross[3];
1371                 cross_v3_v3v3(cross, lv, shi->tang);
1372                 cross_v3_v3v3(vnor, cross, shi->tang);
1373                 normalize_v3(vnor);
1374                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1375                 vn= vnor;
1376         }
1377         
1378         /* dot product and reflectivity */
1379         /* inp = dotproduct, is = shader result, i = lamp energy (with shadow), i_noshad = i without shadow */
1380         inp= dot_v3v3(vn, lv);
1381
1382         /* phong threshold to prevent backfacing faces having artifacts on ray shadow (terminator problem) */
1383         /* this complex construction screams for a nicer implementation! (ton) */
1384         if (R.r.mode & R_SHADOW) {
1385                 if (ma->mode & MA_SHADOW) {
1386                         if (lar->type == LA_HEMI || lar->type == LA_AREA) {
1387                                 /* pass */
1388                         }
1389                         else if ((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1390                                 float thresh= shi->obr->ob->smoothresh;
1391                                 if (inp>thresh)
1392                                         phongcorr= (inp-thresh)/(inp*(1.0f-thresh));
1393                                 else
1394                                         phongcorr= 0.0f;
1395                         }
1396                         else if (ma->sbias!=0.0f && ((lar->mode & LA_SHAD_RAY) || lar->shb)) {
1397                                 if (inp>ma->sbias)
1398                                         phongcorr= (inp-ma->sbias)/(inp*(1.0f-ma->sbias));
1399                                 else
1400                                         phongcorr= 0.0f;
1401                         }
1402                 }
1403         }
1404         
1405         /* diffuse shaders */
1406         if (lar->mode & LA_NO_DIFF) {
1407                 is = 0.0f;  /* skip shaders */
1408         }
1409         else if (lar->type==LA_HEMI) {
1410                 is = 0.5f * inp + 0.5f;
1411         }
1412         else {
1413                 
1414                 if (lar->type==LA_AREA)
1415                         inp= area_lamp_energy_multisample(lar, shi->co, vn);
1416                 
1417                 /* diffuse shaders (oren nayer gets inp from area light) */
1418                 if (ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1419                 else if (ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1420                 else if (ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1421                 else if (ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1422                 else is= inp;  /* Lambert */
1423         }
1424
1425         /* 'is' is diffuse */
1426         if ((ma->shade_flag & MA_CUBIC) && is > 0.0f && is < 1.0f) {
1427                 is= 3.0f * is * is - 2.0f * is * is * is;  /* nicer termination of shades */
1428         }
1429
1430         i= is*phongcorr;
1431         
1432         if (i>0.0f) {
1433                 i*= visifac*shi->refl;
1434         }
1435         i_noshad= i;
1436         
1437         vn = shi->vn;  /* bring back original vector, we use special specular shaders for tangent */
1438         if (ma->mode & MA_TANGENT_V)
1439                 vn= shi->tang;
1440         
1441         /* init transp shadow */
1442         shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1443         
1444         /* shadow and spec, (visifac==0 outside spot) */
1445         if (visifac> 0.0f) {
1446                 
1447                 if ((R.r.mode & R_SHADOW)) {
1448                         if (ma->mode & MA_SHADOW) {
1449                                 if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1450                                         
1451                                         if (vn==vnor)   /* tangent trigger */
1452                                                 lamp_get_shadow(lar, shi, dot_v3v3(shi->vn, lv), shadfac, shi->depth);
1453                                         else
1454                                                 lamp_get_shadow(lar, shi, inp, shadfac, shi->depth);
1455                                                 
1456                                         /* warning, here it skips the loop */
1457                                         if ((lar->mode & LA_ONLYSHADOW) && i>0.0f) {
1458                                                 
1459                                                 shadfac[3]= i*lar->energy*(1.0f-shadfac[3]);
1460                                                 shr->shad[0] -= shadfac[3]*shi->r*(1.0f-lashdw[0]);
1461                                                 shr->shad[1] -= shadfac[3]*shi->g*(1.0f-lashdw[1]);
1462                                                 shr->shad[2] -= shadfac[3]*shi->b*(1.0f-lashdw[2]);
1463                                                 
1464                                                 if (!(lar->mode & LA_NO_SPEC)) {
1465                                                         shr->spec[0] -= shadfac[3]*shi->specr*(1.0f-lashdw[0]);
1466                                                         shr->spec[1] -= shadfac[3]*shi->specg*(1.0f-lashdw[1]);
1467                                                         shr->spec[2] -= shadfac[3]*shi->specb*(1.0f-lashdw[2]);
1468                                                 }
1469                                                 
1470                                                 return;
1471                                         }
1472                                         
1473                                         i*= shadfac[3];
1474                                         shr->shad[3] = shadfac[3]; /* store this for possible check in troublesome cases */
1475                                 }
1476                                 else {
1477                                         shr->shad[3] = 1.0f;  /* No shadow at all! */
1478                                 }
1479                         }
1480                 }
1481                 
1482                 /* in case 'no diffuse' we still do most calculus, spec can be in shadow.*/
1483                 if (!(lar->mode & LA_NO_DIFF)) {
1484                         if (i>0.0f) {
1485                                 if (ma->mode & MA_SHADOW_TRA) {
1486                                         const float tcol[3] = {
1487                                             i * shadfac[0] * lacol[0],
1488                                             i * shadfac[1] * lacol[1],
1489                                             i * shadfac[2] * lacol[2],
1490                                         };
1491                                         add_to_diffuse(shr->shad, shi, is, tcol);
1492                                 }
1493                                 else {
1494                                         const float tcol[3] = {
1495                                             i * lacol[0],
1496                                             i * lacol[1],
1497                                             i * lacol[2],
1498                                         };
1499                                         add_to_diffuse(shr->shad, shi, is, tcol);
1500                                 }
1501                         }
1502                         /* add light for colored shadow */
1503                         if (i_noshad>i && !(lashdw[0]==0 && lashdw[1]==0 && lashdw[2]==0)) {
1504                                 const float tcol[3] = {
1505                                     lashdw[0] * (i_noshad - i) * lacol[0],
1506                                     lashdw[1] * (i_noshad - i) * lacol[1],
1507                                     lashdw[2] * (i_noshad - i) * lacol[2],
1508                                 };
1509                                 add_to_diffuse(shr->shad, shi, is, tcol);
1510                         }
1511                         if (i_noshad>0.0f) {
1512                                 if (passflag & (SCE_PASS_DIFFUSE|SCE_PASS_SHADOW) ||
1513                                     ((passflag & SCE_PASS_COMBINED) && !(shi->combinedflag & SCE_PASS_SHADOW)))
1514                                 {
1515                                         const float tcol[3] = {
1516                                             i_noshad * lacol[0],
1517                                             i_noshad * lacol[1],
1518                                             i_noshad * lacol[2]
1519                                         };
1520                                         add_to_diffuse(shr->diff, shi, is, tcol);
1521                                 }
1522                                 else {
1523                                         copy_v3_v3(shr->diff, shr->shad);
1524                                 }
1525                         }
1526                 }
1527                 
1528                 /* specularity */
1529                 shadfac[3]*= phongcorr; /* note, shadfac not allowed to be stored nonlocal */
1530                 
1531                 if (shadfac[3]>0.0f && shi->spec!=0.0f && !(lar->mode & LA_NO_SPEC) && !(lar->mode & LA_ONLYSHADOW)) {
1532                         
1533                         if (!(passflag & (SCE_PASS_COMBINED | SCE_PASS_SPEC))) {
1534                                 /* pass */
1535                         }
1536                         else if (lar->type == LA_HEMI) {
1537                                 float t;
1538                                 /* hemi uses no spec shaders (yet) */
1539                                 
1540                                 lv[0]+= view[0];
1541                                 lv[1]+= view[1];
1542                                 lv[2]+= view[2];
1543                                 
1544                                 normalize_v3(lv);
1545                                 
1546                                 t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1547                                 
1548                                 if (lar->type==LA_HEMI) {
1549                                         t= 0.5f*t+0.5f;
1550                                 }
1551                                 
1552                                 t= shadfac[3]*shi->spec*spec(t, shi->har);
1553                                 
1554                                 shr->spec[0]+= t*(lacol[0] * shi->specr);
1555                                 shr->spec[1]+= t*(lacol[1] * shi->specg);
1556                                 shr->spec[2]+= t*(lacol[2] * shi->specb);
1557                         }
1558                         else {
1559                                 /* specular shaders */
1560                                 float specfac, t;
1561                                 
1562                                 if (ma->spec_shader==MA_SPEC_PHONG) 
1563                                         specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1564                                 else if (ma->spec_shader==MA_SPEC_COOKTORR) 
1565                                         specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1566                                 else if (ma->spec_shader==MA_SPEC_BLINN) 
1567                                         specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1568                                 else if (ma->spec_shader==MA_SPEC_WARDISO)
1569                                         specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1570                                 else 
1571                                         specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1572                                 
1573                                 /* area lamp correction */
1574                                 if (lar->type==LA_AREA) specfac*= inp;
1575                                 
1576                                 t= shadfac[3]*shi->spec*visifac*specfac;
1577                                 
1578                                 if (ma->mode & MA_RAMP_SPEC) {
1579                                         float spec[3];
1580                                         do_specular_ramp(shi, specfac, t, spec);
1581                                         shr->spec[0]+= t*(lacol[0] * spec[0]);
1582                                         shr->spec[1]+= t*(lacol[1] * spec[1]);
1583                                         shr->spec[2]+= t*(lacol[2] * spec[2]);
1584                                 }
1585                                 else {
1586                                         shr->spec[0]+= t*(lacol[0] * shi->specr);
1587                                         shr->spec[1]+= t*(lacol[1] * shi->specg);
1588                                         shr->spec[2]+= t*(lacol[2] * shi->specb);
1589                                 }
1590                         }
1591                 }
1592         }
1593 }
1594
1595 static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr)
1596 {
1597         
1598         if (R.r.mode & R_SHADOW) {
1599                 ListBase *lights;
1600                 LampRen *lar;
1601                 GroupObject *go;
1602                 float inpr, lv[3];
1603                 float /* *view, */ shadfac[4];
1604                 float ir, accum, visifac, lampdist;
1605                 float shaded = 0.0f, lightness = 0.0f;
1606                 
1607
1608                 /* view= shi->view; */ /* UNUSED */
1609                 accum= ir= 0.0f;
1610                 
1611                 lights= get_lights(shi);
1612                 for (go=lights->first; go; go= go->next) {
1613                         lar= go->lampren;
1614                         if (lar==NULL) continue;
1615                         
1616                         if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1617                         if ((lar->lay & shi->lay)==0) continue;
1618                         
1619                         if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1620                                 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1621                                 ir+= 1.0f;
1622
1623                                 if (visifac <= 0.0f) {
1624                                         if (shi->mat->shadowonly_flag == MA_SO_OLD)
1625                                                 accum+= 1.0f;
1626
1627                                         continue;
1628                                 }
1629                                 inpr= dot_v3v3(shi->vn, lv);
1630                                 if (inpr <= 0.0f) {
1631                                         if (shi->mat->shadowonly_flag == MA_SO_OLD)
1632                                                 accum+= 1.0f;
1633
1634                                         continue;
1635                                 }
1636
1637                                 lamp_get_shadow(lar, shi, inpr, shadfac, shi->depth);
1638
1639                                 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1640                                         /* Old "Shadows Only" */
1641                                         accum+= (1.0f-visifac) + (visifac)*IMB_colormanagement_get_luminance(shadfac)*shadfac[3];
1642                                 }
1643                                 else {
1644                                         shaded += IMB_colormanagement_get_luminance(shadfac)*shadfac[3] * visifac * lar->energy;
1645
1646                                         if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1647                                                 lightness += visifac * lar->energy;
1648                                         }
1649                                 }
1650                         }
1651                 }
1652
1653                 /* Apply shadows as alpha */
1654                 if (ir>0.0f) {
1655                         if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1656                                 accum = 1.0f - accum/ir;
1657                         }
1658                         else {
1659                                 if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1660                                         if (lightness > 0.0f) {
1661                                                 /* Get shadow value from between 0.0f and non-shadowed lightness */
1662                                                 accum = (lightness - shaded) / (lightness);
1663                                         }
1664                                         else {
1665                                                 accum = 0.0f;
1666                                         }
1667                                 }
1668                                 else { /* shadowonly_flag == MA_SO_SHADED */
1669                                         /* Use shaded value */
1670                                         accum = 1.0f - shaded;
1671                                 }
1672                         }
1673
1674                         shr->alpha= (shi->alpha)*(accum);
1675                         if (shr->alpha<0.0f) shr->alpha=0.0f;
1676                 }
1677                 else {
1678                         /* If "fully shaded", use full alpha even on areas that have no lights */
1679                         if (shi->mat->shadowonly_flag == MA_SO_SHADED) shr->alpha=shi->alpha;
1680                         else shr->alpha= 0.f;
1681                 }
1682         }
1683         
1684         /* quite disputable this...  also note it doesn't mirror-raytrace */
1685         if ((R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT)) && shi->amb!=0.0f) {
1686                 float f;
1687                 
1688                 if (R.wrld.mode & WO_AMB_OCC) {
1689                         f= R.wrld.aoenergy*shi->amb;
1690                         
1691                         if (R.wrld.aomix==WO_AOADD) {
1692                                 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1693                                         f= f*(1.0f - IMB_colormanagement_get_luminance(shi->ao));
1694                                         shr->alpha= (shr->alpha + f)*f;
1695                                 }
1696                                 else {
1697                                         shr->alpha -= f*IMB_colormanagement_get_luminance(shi->ao);
1698                                         if (shr->alpha<0.0f) shr->alpha=0.0f;
1699                                 }
1700                         }
1701                         else /* AO Multiply */
1702                                 shr->alpha= (1.0f - f)*shr->alpha + f*(1.0f - (1.0f - shr->alpha)*IMB_colormanagement_get_luminance(shi->ao));
1703                 }
1704
1705                 if (R.wrld.mode & WO_ENV_LIGHT) {
1706                         if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1707                                 f= R.wrld.ao_env_energy*shi->amb*(1.0f - IMB_colormanagement_get_luminance(shi->env));
1708                                 shr->alpha= (shr->alpha + f)*f;
1709                         }
1710                         else {
1711                                 f= R.wrld.ao_env_energy*shi->amb;
1712                                 shr->alpha -= f*IMB_colormanagement_get_luminance(shi->env);
1713                                 if (shr->alpha<0.0f) shr->alpha=0.0f;
1714                         }
1715                 }
1716         }
1717 }
1718
1719 /* let's map negative light as if it mirrors positive light, otherwise negative values disappear */
1720 static void wrld_exposure_correct(float diff[3])
1721 {
1722         
1723         diff[0]= R.wrld.linfac*(1.0f-expf( diff[0]*R.wrld.logfac) );
1724         diff[1]= R.wrld.linfac*(1.0f-expf( diff[1]*R.wrld.logfac) );
1725         diff[2]= R.wrld.linfac*(1.0f-expf( diff[2]*R.wrld.logfac) );
1726 }
1727
1728 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1729 {
1730         /* Passes which might need to know material color.
1731          *
1732          * It seems to be faster to just calculate material color
1733          * even if the pass doesn't really need it than trying to
1734          * figure out whether color is really needed or not.
1735          */
1736         const int color_passes =
1737                 SCE_PASS_COMBINED | SCE_PASS_RGBA | SCE_PASS_DIFFUSE | SCE_PASS_SPEC |
1738                 SCE_PASS_REFLECT | SCE_PASS_NORMAL | SCE_PASS_REFRACT | SCE_PASS_EMIT | SCE_PASS_SHADOW;
1739
1740         Material *ma= shi->mat;
1741         int passflag= shi->passflag;
1742
1743         memset(shr, 0, sizeof(ShadeResult));
1744         
1745         if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1746         
1747         /* separate loop */
1748         if (ma->mode & MA_ONLYSHADOW) {
1749                 shade_lamp_loop_only_shadow(shi, shr);
1750                 return;
1751         }
1752         
1753         /* envmap hack, always reset */
1754         shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0f;
1755         
1756         /* material color itself */
1757         if (passflag & color_passes) {
1758                 if (ma->mode & (MA_FACETEXTURE)) {
1759                         shi->r= shi->vcol[0];
1760                         shi->g= shi->vcol[1];
1761                         shi->b= shi->vcol[2];
1762                         if (ma->mode & (MA_FACETEXTURE_ALPHA))
1763                                 shi->alpha= shi->vcol[3];
1764                 }
1765 #ifdef WITH_FREESTYLE
1766                 else if (ma->vcol_alpha) {
1767                         shi->r= shi->vcol[0];
1768                         shi->g= shi->vcol[1];
1769                         shi->b= shi->vcol[2];
1770                         shi->alpha= shi->vcol[3];
1771                 }
1772 #endif
1773                 else if (ma->mode & (MA_VERTEXCOLP)) {
1774                         float neg_alpha = 1.0f - shi->vcol[3];
1775                         shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
1776                         shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
1777                         shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
1778                 }
1779                 if (ma->texco) {
1780                         do_material_tex(shi, &R);
1781                         if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1782                 }
1783                 
1784                 shr->col[0]= shi->r*shi->alpha;
1785                 shr->col[1]= shi->g*shi->alpha;
1786                 shr->col[2]= shi->b*shi->alpha;
1787                 shr->col[3]= shi->alpha;
1788
1789                 if ((ma->sss_flag & MA_DIFF_SSS) && !sss_pass_done(&R, ma)) {
1790                         if (ma->sss_texfac == 0.0f) {
1791                                 shi->r= shi->g= shi->b= shi->alpha= 1.0f;
1792                                 shr->col[0]= shr->col[1]= shr->col[2]= shr->col[3]= 1.0f;
1793                         }
1794                         else {
1795                                 shi->r= pow(max_ff(shi->r, 0.0f), ma->sss_texfac);
1796                                 shi->g= pow(max_ff(shi->g, 0.0f), ma->sss_texfac);
1797                                 shi->b= pow(max_ff(shi->b, 0.0f), ma->sss_texfac);
1798                                 shi->alpha= pow(max_ff(shi->alpha, 0.0f), ma->sss_texfac);
1799                                 
1800                                 shr->col[0]= pow(max_ff(shr->col[0], 0.0f), ma->sss_texfac);
1801                                 shr->col[1]= pow(max_ff(shr->col[1], 0.0f), ma->sss_texfac);
1802                                 shr->col[2]= pow(max_ff(shr->col[2], 0.0f), ma->sss_texfac);
1803                                 shr->col[3]= pow(max_ff(shr->col[3], 0.0f), ma->sss_texfac);
1804                         }
1805                 }
1806         }
1807         
1808         if (ma->mode & MA_SHLESS) {
1809                 shr->combined[0]= shi->r;
1810                 shr->combined[1]= shi->g;
1811                 shr->combined[2]= shi->b;
1812                 shr->alpha= shi->alpha;
1813                 goto finally_shadeless;
1814         }
1815
1816         if ( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) {       /* vertexcolor light */
1817                 shr->emit[0]= shi->r*(shi->emit+shi->vcol[0]*shi->vcol[3]);
1818                 shr->emit[1]= shi->g*(shi->emit+shi->vcol[1]*shi->vcol[3]);
1819                 shr->emit[2]= shi->b*(shi->emit+shi->vcol[2]*shi->vcol[3]);
1820         }
1821         else {
1822                 shr->emit[0]= shi->r*shi->emit;
1823                 shr->emit[1]= shi->g*shi->emit;
1824                 shr->emit[2]= shi->b*shi->emit;
1825         }
1826         
1827         /* AO pass */
1828         if (((passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT))) ||
1829             (passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT))) {
1830                 if ((R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && (R.r.mode & R_SHADOW)) {
1831                         /* AO was calculated for scanline already */
1832                         if (shi->depth || shi->volume_depth)
1833                                 ambient_occlusion(shi);
1834                         copy_v3_v3(shr->ao, shi->ao);
1835                         copy_v3_v3(shr->env, shi->env); /* XXX multiply */
1836                         copy_v3_v3(shr->indirect, shi->indirect); /* XXX multiply */
1837                 }
1838                 else {
1839                         shr->ao[0]= shr->ao[1]= shr->ao[2]= 1.0f;
1840                         zero_v3(shr->env);
1841                         zero_v3(shr->indirect);
1842                 }
1843         }
1844         
1845         /* lighting pass */
1846         if (passflag & (SCE_PASS_COMBINED|SCE_PASS_DIFFUSE|SCE_PASS_SPEC|SCE_PASS_SHADOW)) {
1847                 GroupObject *go;
1848                 ListBase *lights;
1849                 LampRen *lar;
1850                 
1851                 lights= get_lights(shi);
1852                 for (go=lights->first; go; go= go->next) {
1853                         lar= go->lampren;
1854                         if (lar==NULL) continue;
1855                         
1856                         /* test for lamp layer */
1857                         if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1858                         if ((lar->lay & shi->lay)==0) continue;
1859                         
1860                         /* accumulates in shr->diff and shr->spec and shr->shad (diffuse with shadow!) */
1861                         shade_one_light(lar, shi, shr, passflag);
1862                 }
1863
1864                 /* this check is to prevent only shadow lamps from producing negative
1865                  * colors.*/
1866                 if (shr->spec[0] < 0) shr->spec[0] = 0;
1867                 if (shr->spec[1] < 0) shr->spec[1] = 0;
1868                 if (shr->spec[2] < 0) shr->spec[2] = 0;
1869
1870                 if (shr->shad[0] < 0) shr->shad[0] = 0;
1871                 if (shr->shad[1] < 0) shr->shad[1] = 0;
1872                 if (shr->shad[2] < 0) shr->shad[2] = 0;
1873                                                 
1874                 if (ma->sss_flag & MA_DIFF_SSS) {
1875                         float sss[3], col[3], invalpha, texfac= ma->sss_texfac;
1876
1877                         /* this will return false in the preprocess stage */
1878                         if (sample_sss(&R, ma, shi->co, sss)) {
1879                                 invalpha= (shr->col[3] > FLT_EPSILON)? 1.0f/shr->col[3]: 1.0f;
1880
1881                                 if (texfac==0.0f) {
1882                                         copy_v3_v3(col, shr->col);
1883                                         mul_v3_fl(col, invalpha);
1884                                 }
1885                                 else if (texfac==1.0f) {
1886                                         col[0]= col[1]= col[2]= 1.0f;
1887                                         mul_v3_fl(col, invalpha);
1888                                 }
1889                                 else {
1890                                         copy_v3_v3(col, shr->col);
1891                                         mul_v3_fl(col, invalpha);
1892                                         col[0]= pow(max_ff(col[0], 0.0f), 1.0f-texfac);
1893                                         col[1]= pow(max_ff(col[1], 0.0f), 1.0f-texfac);
1894                                         col[2]= pow(max_ff(col[2], 0.0f), 1.0f-texfac);
1895                                 }
1896
1897                                 shr->diff[0]= sss[0]*col[0];
1898                                 shr->diff[1]= sss[1]*col[1];
1899                                 shr->diff[2]= sss[2]*col[2];
1900
1901                                 if (shi->combinedflag & SCE_PASS_SHADOW) {
1902                                         shr->shad[0]= shr->diff[0];
1903                                         shr->shad[1]= shr->diff[1];
1904                                         shr->shad[2]= shr->diff[2];
1905                                 }
1906                         }
1907                 }
1908                 
1909                 if (shi->combinedflag & SCE_PASS_SHADOW)
1910                         copy_v3_v3(shr->diffshad, shr->shad);
1911                 else
1912                         copy_v3_v3(shr->diffshad, shr->diff);
1913
1914                 copy_v3_v3(shr->combined, shr->diffshad);
1915                         
1916                 /* calculate shadow pass, we use a multiplication mask */
1917                 /* Even if diff = 0,0,0, it does matter what the shadow pass is, since we may want it 'for itself'! */
1918                 if (passflag & SCE_PASS_SHADOW) {
1919                         if (shr->diff[0]!=0.0f) shr->shad[0]= shr->shad[0]/shr->diff[0];
1920                         /* can't determine proper shadow from shad/diff (0/0), so use shadow intensity */
1921                         else if (shr->shad[0]==0.0f) shr->shad[0]= shr->shad[3];
1922
1923                         if (shr->diff[1]!=0.0f) shr->shad[1]= shr->shad[1]/shr->diff[1];
1924                         else if (shr->shad[1]==0.0f) shr->shad[1]= shr->shad[3];
1925
1926                         if (shr->diff[2]!=0.0f) shr->shad[2]= shr->shad[2]/shr->diff[2];
1927                         else if (shr->shad[2]==0.0f) shr->shad[2]= shr->shad[3];
1928                 }
1929                 
1930                 /* exposure correction */
1931                 if ((R.wrld.exp!=0.0f || R.wrld.range!=1.0f) && !R.sss_points) {
1932                         wrld_exposure_correct(shr->combined);   /* has no spec! */
1933                         wrld_exposure_correct(shr->spec);
1934                 }
1935         }
1936         
1937         /* alpha in end, spec can influence it */
1938         if (passflag & (SCE_PASS_COMBINED)) {
1939                 if ((ma->fresnel_tra!=0.0f) && (shi->mode & MA_TRANSP))
1940                         shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1941                         
1942                 /* note: shi->mode! */
1943                 if (shi->mode & MA_TRANSP && (shi->mode & (MA_ZTRANSP|MA_RAYTRANSP))) {
1944                         if (shi->spectra!=0.0f) {
1945                                 float t = max_fff(shr->spec[0], shr->spec[1], shr->spec[2]);
1946                                 t *= shi->spectra;
1947                                 if (t>1.0f) t= 1.0f;
1948                                 shi->alpha= (1.0f-t)*shi->alpha+t;
1949                         }
1950                 }
1951         }
1952         shr->alpha= shi->alpha;
1953         
1954         /* from now stuff everything in shr->combined: ambient, AO, ramps, exposure */
1955         if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1956                 if (R.r.mode & R_SHADOW) {
1957                         /* add AO in combined? */
1958                         if (R.wrld.mode & WO_AMB_OCC)
1959                                 if (shi->combinedflag & SCE_PASS_AO)
1960                                         ambient_occlusion_apply(shi, shr);
1961
1962                         if (R.wrld.mode & WO_ENV_LIGHT)
1963                                 if (shi->combinedflag & SCE_PASS_ENVIRONMENT)
1964                                         environment_lighting_apply(shi, shr);
1965
1966                         if (R.wrld.mode & WO_INDIRECT_LIGHT)
1967                                 if (shi->combinedflag & SCE_PASS_INDIRECT)
1968                                         indirect_lighting_apply(shi, shr);
1969                 }
1970                 
1971                 shr->combined[0]+= shi->ambr;
1972                 shr->combined[1]+= shi->ambg;
1973                 shr->combined[2]+= shi->ambb;
1974                 
1975                 if (ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->combined, shi);
1976         }
1977
1978         if (ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shi);
1979         
1980         /* refcol is for envmap only */
1981         if (shi->refcol[0]!=0.0f) {
1982                 float result[3];
1983                 
1984                 result[0]= shi->mirr*shi->refcol[1] + (1.0f - shi->mirr*shi->refcol[0])*shr->combined[0];
1985                 result[1]= shi->mirg*shi->refcol[2] + (1.0f - shi->mirg*shi->refcol[0])*shr->combined[1];
1986                 result[2]= shi->mirb*shi->refcol[3] + (1.0f - shi->mirb*shi->refcol[0])*shr->combined[2];
1987                 
1988                 if (passflag & SCE_PASS_REFLECT)
1989                         sub_v3_v3v3(shr->refl, result, shr->combined);
1990                 
1991                 if (shi->combinedflag & SCE_PASS_REFLECT)
1992                         copy_v3_v3(shr->combined, result);
1993                         
1994         }
1995         
1996         /* and add emit and spec */
1997         if (shi->combinedflag & SCE_PASS_EMIT)
1998                 add_v3_v3(shr->combined, shr->emit);
1999         if (shi->combinedflag & SCE_PASS_SPEC)
2000                 add_v3_v3(shr->combined, shr->spec);
2001
2002
2003         /* Last section of this function applies to shadeless colors too */
2004 finally_shadeless:
2005
2006         /* modulate by the object color */
2007         if ((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
2008                 if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
2009                         float obcol[4];
2010
2011                         copy_v4_v4(obcol, shi->obr->ob->col);
2012                         CLAMP(obcol[3], 0.0f, 1.0f);
2013
2014                         shr->combined[0] *= obcol[0];
2015                         shr->combined[1] *= obcol[1];
2016                         shr->combined[2] *= obcol[2];
2017                         if (shi->mode & MA_TRANSP) shr->alpha *= obcol[3];
2018                 }
2019         }
2020
2021         shr->combined[3]= shr->alpha;
2022 }
2023
2024 /* used for "Lamp Data" shader node */
2025 static float lamp_get_data_internal(ShadeInput *shi, GroupObject *go, float col[4], float lv[3], float *dist, float shadow[4])
2026 {
2027         LampRen *lar = go->lampren;
2028         float visifac, inp;
2029
2030         if (!lar
2031             || ((lar->mode & LA_LAYER) && (lar->lay & shi->obi->lay) == 0)
2032             || (lar->lay & shi->lay) == 0)
2033                 return 0.0f;
2034
2035         if (lar->mode & LA_TEXTURE)
2036                 do_lamp_tex(lar, lv, shi, col, LA_TEXTURE);
2037
2038         visifac = lamp_get_visibility(lar, shi->co, lv, dist);
2039
2040         if (visifac == 0.0f
2041             || lar->type == LA_HEMI
2042             || (lar->type != LA_SPOT && !(lar->mode & LA_SHAD_RAY))
2043             || (R.r.scemode & R_BUTS_PREVIEW))
2044                 return visifac;
2045
2046         inp = dot_v3v3(shi->vn, lv);
2047
2048         if (inp > 0.0f) {
2049                 float shadfac[4];
2050
2051                 shadow[0] = lar->shdwr;
2052                 shadow[1] = lar->shdwg;
2053                 shadow[2] = lar->shdwb;
2054
2055                 if (lar->mode & LA_SHAD_TEX)
2056                         do_lamp_tex(lar, lv, shi, shadow, LA_SHAD_TEX);
2057
2058                 lamp_get_shadow(lar, shi, inp, shadfac, shi->depth);
2059
2060                 shadow[0] = 1.0f - ((1.0f - shadfac[0] * shadfac[3]) * (1.0f - shadow[0]));
2061                 shadow[1] = 1.0f - ((1.0f - shadfac[1] * shadfac[3]) * (1.0f - shadow[1]));
2062                 shadow[2] = 1.0f - ((1.0f - shadfac[2] * shadfac[3]) * (1.0f - shadow[2]));
2063         }
2064
2065         return visifac;
2066 }
2067
2068 float RE_lamp_get_data(ShadeInput *shi, Object *lamp_obj, float col[4], float lv[3], float *dist, float shadow[4])
2069 {
2070         col[0] = col[1] = col[2] = 0.0f;
2071         col[3] = 1.0f;
2072         copy_v3_v3(lv, shi->vn);
2073         *dist = 1.0f;
2074         shadow[0] = shadow[1] = shadow[2] = shadow[3] = 1.0f;
2075
2076         if (lamp_obj->type == OB_LAMP) {
2077                 GroupObject *go;
2078                 Lamp *lamp = (Lamp *)lamp_obj->data;
2079
2080                 col[0] = lamp->r * lamp->energy;
2081                 col[1] = lamp->g * lamp->energy;
2082                 col[2] = lamp->b * lamp->energy;
2083
2084                 if (R.r.scemode & R_BUTS_PREVIEW) {
2085                         for (go = R.lights.first; go; go = go->next) {
2086                                 /* "Lamp.002" is main key light of material preview */
2087                                 if (STREQ(go->ob->id.name + 2, "Lamp.002"))
2088                                         return lamp_get_data_internal(shi, go, col, lv, dist, shadow);
2089                         }
2090                         return 0.0f;
2091                 }
2092
2093                 if (shi->light_override) {
2094                         for (go = shi->light_override->gobject.first; go; go = go->next) {
2095                                 if (go->ob == lamp_obj)
2096                                         return lamp_get_data_internal(shi, go, col, lv, dist, shadow);
2097                         }
2098                 }
2099
2100                 if (shi->mat && shi->mat->group) {
2101                         for (go = shi->mat->group->gobject.first; go; go = go->next) {
2102                                 if (go->ob == lamp_obj)
2103                                         return lamp_get_data_internal(shi, go, col, lv, dist, shadow);
2104                         }
2105                 }
2106
2107                 for (go = R.lights.first; go; go = go->next) {
2108                         if (go->ob == lamp_obj)
2109                                 return lamp_get_data_internal(shi, go, col, lv, dist, shadow);
2110                 }
2111         }
2112
2113         return 0.0f;
2114 }
2115
2116 const float (*RE_object_instance_get_matrix(struct ObjectInstanceRen *obi, int matrix_id))[4]
2117 {
2118         if (obi) {
2119                 switch (matrix_id) {
2120                         case RE_OBJECT_INSTANCE_MATRIX_OB:
2121                                 return (const float(*)[4])obi->obmat;
2122                         case RE_OBJECT_INSTANCE_MATRIX_OBINV:
2123                                 return (const float(*)[4])obi->obinvmat;
2124                         case RE_OBJECT_INSTANCE_MATRIX_LOCALTOVIEW:
2125                                 return (const float(*)[4])obi->localtoviewmat;
2126                         case RE_OBJECT_INSTANCE_MATRIX_LOCALTOVIEWINV:
2127                                 return (const float(*)[4])obi->localtoviewinvmat;
2128                 }
2129         }
2130         return NULL;
2131 }
2132
2133 const float (*RE_render_current_get_matrix(int matrix_id))[4]
2134 {
2135         switch(matrix_id) {
2136                 case RE_VIEW_MATRIX:
2137                         return (const float(*)[4])R.viewmat;
2138                 case RE_VIEWINV_MATRIX:
2139                         return (const float(*)[4])R.viewinv;
2140         }
2141         return NULL;
2142 }