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