rename api functions...
[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         { \
472                 dest[0]= a[1] * b[2] - a[2] * b[1]; \
473                 dest[1]= a[2] * b[0] - a[0] * b[2]; \
474                 dest[2]= a[0] * b[1] - a[1] * b[0]; \
475         } (void)0
476
477         CROSS(cross[0], vec[0], vec[1]);
478         CROSS(cross[1], vec[1], vec[2]);
479         CROSS(cross[2], vec[2], vec[3]);
480         CROSS(cross[3], vec[3], vec[0]);
481
482 #undef CROSS
483
484         Normalize_d(cross[0]);
485         Normalize_d(cross[1]);
486         Normalize_d(cross[2]);
487         Normalize_d(cross[3]);
488
489         /* angles */
490         rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
491         rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
492         rad[2]= vec[2][0]*vec[3][0]+ vec[2][1]*vec[3][1]+ vec[2][2]*vec[3][2];
493         rad[3]= vec[3][0]*vec[0][0]+ vec[3][1]*vec[0][1]+ vec[3][2]*vec[0][2];
494
495         rad[0]= saacos_d(rad[0]);
496         rad[1]= saacos_d(rad[1]);
497         rad[2]= saacos_d(rad[2]);
498         rad[3]= saacos_d(rad[3]);
499
500         /* Stoke formula */
501         fac=  rad[0]*(vn[0]*cross[0][0]+ vn[1]*cross[0][1]+ vn[2]*cross[0][2]);
502         fac+= rad[1]*(vn[0]*cross[1][0]+ vn[1]*cross[1][1]+ vn[2]*cross[1][2]);
503         fac+= rad[2]*(vn[0]*cross[2][0]+ vn[1]*cross[2][1]+ vn[2]*cross[2][2]);
504         fac+= rad[3]*(vn[0]*cross[3][0]+ vn[1]*cross[3][1]+ vn[2]*cross[3][2]);
505
506         if (fac<=0.0) return 0.0;
507         return fac;
508 }
509
510 static float area_lamp_energy_multisample(LampRen *lar, const float co[3], float *vn)
511 {
512         /* corner vectors are moved around according lamp jitter */
513         float *jitlamp= lar->jitter, vec[3];
514         float area[4][3], intens= 0.0f;
515         int a= lar->ray_totsamp;
516
517         /* test if co is behind lamp */
518         sub_v3_v3v3(vec, co, lar->co);
519         if (dot_v3v3(vec, lar->vec) < 0.0f)
520                 return 0.0f;
521
522         while (a--) {
523                 vec[0]= jitlamp[0];
524                 vec[1]= jitlamp[1];
525                 vec[2]= 0.0f;
526                 mul_m3_v3(lar->mat, vec);
527                 
528                 add_v3_v3v3(area[0], lar->area[0], vec);
529                 add_v3_v3v3(area[1], lar->area[1], vec);
530                 add_v3_v3v3(area[2], lar->area[2], vec);
531                 add_v3_v3v3(area[3], lar->area[3], vec);
532                 
533                 intens+= area_lamp_energy(area, co, vn);
534                 
535                 jitlamp+= 2;
536         }
537         intens /= (float)lar->ray_totsamp;
538         
539         return pow(intens * lar->areasize, lar->k);     /* corrected for buttons size and lar->dist^2 */
540 }
541
542 static float spec(float inp, int hard)  
543 {
544         float b1;
545         
546         if (inp>=1.0f) return 1.0f;
547         else if (inp<=0.0f) return 0.0f;
548         
549         b1= inp*inp;
550         /* avoid FPE */
551         if (b1<0.01f) b1= 0.01f;
552         
553         if ((hard & 1)==0)  inp= 1.0f;
554         if (hard & 2)  inp*= b1;
555         b1*= b1;
556         if (hard & 4)  inp*= b1;
557         b1*= b1;
558         if (hard & 8)  inp*= b1;
559         b1*= b1;
560         if (hard & 16) inp*= b1;
561         b1*= b1;
562
563         /* avoid FPE */
564         if (b1<0.001f) b1= 0.0f;
565
566         if (hard & 32) inp*= b1;
567         b1*= b1;
568         if (hard & 64) inp*=b1;
569         b1*= b1;
570         if (hard & 128) inp*=b1;
571
572         if (b1<0.001f) b1= 0.0f;
573
574         if (hard & 256) {
575                 b1*= b1;
576                 inp*=b1;
577         }
578
579         return inp;
580 }
581
582 static float Phong_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent )
583 {
584         float h[3];
585         float rslt;
586         
587         h[0] = l[0] + v[0];
588         h[1] = l[1] + v[1];
589         h[2] = l[2] + v[2];
590         normalize_v3(h);
591         
592         rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
593         if (tangent) rslt= sasqrt(1.0f - rslt*rslt);
594                 
595         if ( rslt > 0.0f ) rslt= spec(rslt, hard);
596         else rslt = 0.0f;
597         
598         return rslt;
599 }
600
601
602 /* reduced cook torrance spec (for off-specular peak) */
603 static float CookTorr_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent)
604 {
605         float i, nh, nv, h[3];
606
607         h[0]= v[0]+l[0];
608         h[1]= v[1]+l[1];
609         h[2]= v[2]+l[2];
610         normalize_v3(h);
611
612         nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2];
613         if (tangent) nh= sasqrt(1.0f - nh*nh);
614         else if (nh<0.0f) return 0.0f;
615         
616         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
617         if (tangent) nv= sasqrt(1.0f - nv*nv);
618         else if (nv<0.0f) nv= 0.0f;
619
620         i= spec(nh, hard);
621
622         i= i/(0.1f+nv);
623         return i;
624 }
625
626 /* Blinn spec */
627 static float Blinn_Spec(const float n[3], const float l[3], const float v[3], float refrac, float spec_power, int tangent)
628 {
629         float i, nh, nv, nl, vh, h[3];
630         float a, b, c, g=0.0f, p, f, ang;
631
632         if (refrac < 1.0f) return 0.0f;
633         if (spec_power == 0.0f) return 0.0f;
634         
635         /* conversion from 'hardness' (1-255) to 'spec_power' (50 maps at 0.1) */
636         if (spec_power<100.0f)
637                 spec_power= sqrt(1.0f/spec_power);
638         else spec_power= 10.0f/spec_power;
639         
640         h[0]= v[0]+l[0];
641         h[1]= v[1]+l[1];
642         h[2]= v[2]+l[2];
643         normalize_v3(h);
644
645         nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
646         if (tangent) nh= sasqrt(1.0f - nh*nh);
647         else if (nh<0.0f) return 0.0f;
648
649         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
650         if (tangent) nv= sasqrt(1.0f - nv*nv);
651         if (nv<=0.01f) nv= 0.01f;                               /* hrms... */
652
653         nl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
654         if (tangent) nl= sasqrt(1.0f - nl*nl);
655         if (nl<=0.01f) {
656                 return 0.0f;
657         }
658
659         vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and half-way vector */
660         if (vh<=0.0f) vh= 0.01f;
661
662         a = 1.0f;
663         b = (2.0f*nh*nv)/vh;
664         c = (2.0f*nh*nl)/vh;
665
666         if ( a < b && a < c ) g = a;
667         else if ( b < a && b < c ) g = b;
668         else if ( c < a && c < b ) g = c;
669
670         p = sqrt((double)((refrac * refrac)+(vh * vh) - 1.0f));
671         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))));
672         ang = saacos(nh);
673
674         i= f * g * exp((double)(-(ang*ang) / (2.0f*spec_power*spec_power)));
675         if (i<0.0f) i= 0.0f;
676         
677         return i;
678 }
679
680 /* cartoon render spec */
681 static float Toon_Spec(const float n[3], const float l[3], const float v[3], float size, float smooth, int tangent)
682 {
683         float h[3];
684         float ang;
685         float rslt;
686         
687         h[0] = l[0] + v[0];
688         h[1] = l[1] + v[1];
689         h[2] = l[2] + v[2];
690         normalize_v3(h);
691         
692         rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
693         if (tangent) rslt = sasqrt(1.0f - rslt*rslt);
694         
695         ang = saacos( rslt ); 
696         
697         if ( ang < size ) rslt = 1.0f;
698         else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
699         else rslt = 1.0f - ((ang - size) / smooth);
700         
701         return rslt;
702 }
703
704 /* Ward isotropic gaussian spec */
705 static float WardIso_Spec(const float n[3], const float l[3], const float v[3], float rms, int tangent)
706 {
707         float i, nh, nv, nl, h[3], angle, alpha;
708
709
710         /* half-way vector */
711         h[0] = l[0] + v[0];
712         h[1] = l[1] + v[1];
713         h[2] = l[2] + v[2];
714         normalize_v3(h);
715
716         nh = n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
717         if (tangent) nh = sasqrt(1.0f - nh*nh);
718         if (nh<=0.0f) nh = 0.001f;
719         
720         nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
721         if (tangent) nv = sasqrt(1.0f - nv*nv);
722         if (nv<=0.0f) nv = 0.001f;
723
724         nl = n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
725         if (tangent) nl = sasqrt(1.0f - nl*nl);
726         if (nl<=0.0f) nl = 0.001f;
727
728         angle = tan(saacos(nh));
729         alpha = MAX2(rms, 0.001f);
730
731         i= nl * (1.0f/(4.0f*(float)M_PI*alpha*alpha)) * (expf( -(angle*angle)/(alpha*alpha))/(sqrtf(nv*nl)));
732
733         return i;
734 }
735
736 /* cartoon render diffuse */
737 static float Toon_Diff(const float n[3], const float l[3], const float UNUSED(v[3]), float size, float smooth)
738 {
739         float rslt, ang;
740
741         rslt = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
742
743         ang = saacos( (double)(rslt) );
744
745         if ( ang < size ) rslt = 1.0f;
746         else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
747         else rslt = 1.0f - ((ang - size) / smooth);
748
749         return rslt;
750 }
751
752 /* Oren Nayar diffuse */
753
754 /* 'nl' is either dot product, or return value of area light */
755 /* in latter case, only last multiplication uses 'nl' */
756 static float OrenNayar_Diff(float nl, const float n[3], const float l[3], const float v[3], float rough )
757 {
758         float i/*, nh*/, nv /*, vh */, realnl, h[3];
759         float a, b, t, A, B;
760         float Lit_A, View_A, Lit_B[3], View_B[3];
761         
762         h[0]= v[0]+l[0];
763         h[1]= v[1]+l[1];
764         h[2]= v[2]+l[2];
765         normalize_v3(h);
766         
767         /* nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; */ /* Dot product between surface normal and half-way vector */
768         /* if (nh<0.0f) nh = 0.0f; */
769         
770         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
771         if (nv<=0.0f) nv= 0.0f;
772         
773         realnl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
774         if (realnl<=0.0f) return 0.0f;
775         if (nl<0.0f) return 0.0f;               /* value from area light */
776         
777         /* vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; */ /* Dot product between view vector and halfway vector */
778         /* if (vh<=0.0f) vh= 0.0f; */
779         
780         Lit_A = saacos(realnl);
781         View_A = saacos( nv );
782         
783         Lit_B[0] = l[0] - (realnl * n[0]);
784         Lit_B[1] = l[1] - (realnl * n[1]);
785         Lit_B[2] = l[2] - (realnl * n[2]);
786         normalize_v3(Lit_B);
787         
788         View_B[0] = v[0] - (nv * n[0]);
789         View_B[1] = v[1] - (nv * n[1]);
790         View_B[2] = v[2] - (nv * n[2]);
791         normalize_v3(View_B);
792         
793         t = Lit_B[0]*View_B[0] + Lit_B[1]*View_B[1] + Lit_B[2]*View_B[2];
794         if ( t < 0 ) t = 0;
795         
796         if ( Lit_A > View_A ) {
797                 a = Lit_A;
798                 b = View_A;
799         }
800         else {
801                 a = View_A;
802                 b = Lit_A;
803         }
804         
805         A = 1.0f - (0.5f * ((rough * rough) / ((rough * rough) + 0.33f)));
806         B = 0.45f * ((rough * rough) / ((rough * rough) + 0.09f));
807         
808         b*= 0.95f;      /* prevent tangens from shooting to inf, 'nl' can be not a dot product here. */
809                                 /* overflow only happens with extreme size area light, and higher roughness */
810         i = nl * ( A + ( B * t * sinf(a) * tanf(b) ) );
811         
812         return i;
813 }
814
815 /* Minnaert diffuse */
816 static float Minnaert_Diff(float nl, const float n[3], const float v[3], float darkness)
817 {
818         float i, nv;
819
820         /* nl = dot product between surface normal and light vector */
821         if (nl <= 0.0f)
822                 return 0.0f;
823
824         /* nv = dot product between surface normal and view vector */
825         nv = dot_v3v3(n, v);
826         if (nv < 0.0f)
827                 nv = 0.0f;
828
829         if (darkness <= 1.0f)
830                 i = nl * pow(max_ff(nv * nl, 0.1f), (darkness - 1.0f) ); /*The Real model*/
831         else
832                 i = nl * pow( (1.001f - nv), (darkness  - 1.0f) ); /*Nvidia model*/
833
834         return i;
835 }
836
837 static float Fresnel_Diff(float *vn, float *lv, float *UNUSED(view), float fac_i, float fac)
838 {
839         return fresnel_fac(lv, vn, fac_i, fac);
840 }
841
842 /* --------------------------------------------- */
843 /* also called from texture.c */
844 void calc_R_ref(ShadeInput *shi)
845 {
846         float i;
847
848         /* shi->vn dot shi->view */
849         i= -2*(shi->vn[0]*shi->view[0]+shi->vn[1]*shi->view[1]+shi->vn[2]*shi->view[2]);
850
851         shi->ref[0]= (shi->view[0]+i*shi->vn[0]);
852         shi->ref[1]= (shi->view[1]+i*shi->vn[1]);
853         shi->ref[2]= (shi->view[2]+i*shi->vn[2]);
854         if (shi->osatex) {
855                 if (shi->vlr->flag & R_SMOOTH) {
856                         i= -2*( (shi->vn[0]+shi->dxno[0])*(shi->view[0]+shi->dxview) +
857                                 (shi->vn[1]+shi->dxno[1])*shi->view[1]+ (shi->vn[2]+shi->dxno[2])*shi->view[2] );
858
859                         shi->dxref[0]= shi->ref[0]- ( shi->view[0]+shi->dxview+i*(shi->vn[0]+shi->dxno[0]));
860                         shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*(shi->vn[1]+shi->dxno[1]));
861                         shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dxno[2]));
862
863                         i= -2*( (shi->vn[0]+shi->dyno[0])*shi->view[0]+
864                                 (shi->vn[1]+shi->dyno[1])*(shi->view[1]+shi->dyview)+ (shi->vn[2]+shi->dyno[2])*shi->view[2] );
865
866                         shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*(shi->vn[0]+shi->dyno[0]));
867                         shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*(shi->vn[1]+shi->dyno[1]));
868                         shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dyno[2]));
869
870                 }
871                 else {
872
873                         i= -2*( shi->vn[0]*(shi->view[0]+shi->dxview) +
874                                 shi->vn[1]*shi->view[1]+ shi->vn[2]*shi->view[2] );
875
876                         shi->dxref[0]= shi->ref[0]- (shi->view[0]+shi->dxview+i*shi->vn[0]);
877                         shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*shi->vn[1]);
878                         shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
879
880                         i= -2*( shi->vn[0]*shi->view[0]+
881                                 shi->vn[1]*(shi->view[1]+shi->dyview)+ shi->vn[2]*shi->view[2] );
882
883                         shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*shi->vn[0]);
884                         shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*shi->vn[1]);
885                         shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
886                 }
887         }
888
889 }
890
891 /* called from ray.c */
892 void shade_color(ShadeInput *shi, ShadeResult *shr)
893 {
894         Material *ma= shi->mat;
895
896         if (ma->mode & (MA_FACETEXTURE)) {
897                 shi->r= shi->vcol[0];
898                 shi->g= shi->vcol[1];
899                 shi->b= shi->vcol[2];
900                 if (ma->mode & (MA_FACETEXTURE_ALPHA))
901                         shi->alpha= shi->vcol[3];
902         }
903         else if (ma->mode & (MA_VERTEXCOLP)) {
904                 float neg_alpha = 1.0f - shi->vcol[3];
905                 shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
906                 shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
907                 shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
908         }
909         
910         if (ma->texco)
911                 do_material_tex(shi, &R);
912
913         if (ma->fresnel_tra!=0.0f) 
914                 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
915         
916         if (!(shi->mode & MA_TRANSP)) shi->alpha= 1.0f;
917         
918         shr->diff[0]= shi->r;
919         shr->diff[1]= shi->g;
920         shr->diff[2]= shi->b;
921         shr->alpha= shi->alpha;
922 }
923
924 /* ramp for at end of shade */
925 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
926 {
927         Material *ma= shi->mat;
928         float col[4];
929
930         if (ma->ramp_col) {
931                 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
932                         float fac = rgb_to_grayscale(diff);
933                         do_colorband(ma->ramp_col, fac, col);
934                         
935                         /* blending method */
936                         fac= col[3]*ma->rampfac_col;
937                         
938                         ramp_blend(ma->rampblend_col, diff, fac, col);
939                 }
940         }
941 }
942
943 /* r,g,b denote energy, ramp is used with different values to make new material color */
944 static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, float g, float b)
945 {
946         Material *ma= shi->mat;
947
948         if (ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
949                 
950                 /* MA_RAMP_IN_RESULT is exceptional */
951                 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
952                         /* normal add */
953                         diff[0] += r * shi->r;
954                         diff[1] += g * shi->g;
955                         diff[2] += b * shi->b;
956                 }
957                 else {
958                         float colt[3], col[4];
959                         float fac;
960
961                         /* input */
962                         switch (ma->rampin_col) {
963                         case MA_RAMP_IN_ENERGY:
964                                 /* should use 'rgb_to_grayscale' but we only have a vector version */
965                                 fac= 0.3f*r + 0.58f*g + 0.12f*b;
966                                 break;
967                         case MA_RAMP_IN_SHADER:
968                                 fac= is;
969                                 break;
970                         case MA_RAMP_IN_NOR:
971                                 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
972                                 break;
973                         default:
974                                 fac= 0.0f;
975                                 break;
976                         }
977         
978                         do_colorband(ma->ramp_col, fac, col);
979                         
980                         /* blending method */
981                         fac= col[3]*ma->rampfac_col;
982                         colt[0]= shi->r;
983                         colt[1]= shi->g;
984                         colt[2]= shi->b;
985
986                         ramp_blend(ma->rampblend_col, colt, fac, col);
987
988                         /* output to */
989                         diff[0] += r * colt[0];
990                         diff[1] += g * colt[1];
991                         diff[2] += b * colt[2];
992                 }
993         }
994         else {
995                 diff[0] += r * shi->r;
996                 diff[1] += g * shi->g;
997                 diff[2] += b * shi->b;
998         }
999 }
1000
1001 static void ramp_spec_result(float spec_col[3], ShadeInput *shi)
1002 {
1003         Material *ma= shi->mat;
1004
1005         if (ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
1006                 float col[4];
1007                 float fac = rgb_to_grayscale(spec_col);
1008
1009                 do_colorband(ma->ramp_spec, fac, col);
1010                 
1011                 /* blending method */
1012                 fac= col[3]*ma->rampfac_spec;
1013                 
1014                 ramp_blend(ma->rampblend_spec, spec_col, fac, col);
1015                 
1016         }
1017 }
1018
1019 /* is = dot product shade, t = spec energy */
1020 static void do_specular_ramp(ShadeInput *shi, float is, float t, float spec[3])
1021 {
1022         Material *ma= shi->mat;
1023
1024         spec[0]= shi->specr;
1025         spec[1]= shi->specg;
1026         spec[2]= shi->specb;
1027
1028         /* MA_RAMP_IN_RESULT is exception */
1029         if (ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
1030                 float fac;
1031                 float col[4];
1032
1033                 /* input */
1034                 switch (ma->rampin_spec) {
1035                 case MA_RAMP_IN_ENERGY:
1036                         fac= t;
1037                         break;
1038                 case MA_RAMP_IN_SHADER:
1039                         fac= is;
1040                         break;
1041                 case MA_RAMP_IN_NOR:
1042                         fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1043                         break;
1044                 default:
1045                         fac= 0.0f;
1046                         break;
1047                 }
1048                 
1049                 do_colorband(ma->ramp_spec, fac, col);
1050                 
1051                 /* blending method */
1052                 fac= col[3]*ma->rampfac_spec;
1053                 
1054                 ramp_blend(ma->rampblend_spec, spec, fac, col);
1055         }
1056 }
1057
1058 /* pure AO, check for raytrace and world should have been done */
1059 /* preprocess, textures were not done, don't use shi->amb for that reason */
1060 void ambient_occlusion(ShadeInput *shi)
1061 {
1062         if ((R.wrld.ao_gather_method == WO_AOGATHER_APPROX) && shi->mat->amb!=0.0f) {
1063                 sample_occ(&R, shi);
1064         }
1065         else if ((R.r.mode & R_RAYTRACE) && shi->mat->amb!=0.0f) {
1066                 ray_ao(shi, shi->ao, shi->env);
1067         }
1068         else {
1069                 shi->ao[0]= shi->ao[1]= shi->ao[2]= 1.0f;
1070                 zero_v3(shi->env);
1071                 zero_v3(shi->indirect);
1072         }
1073 }
1074
1075
1076 /* wrld mode was checked for */
1077 static void ambient_occlusion_apply(ShadeInput *shi, ShadeResult *shr)
1078 {
1079         float f= R.wrld.aoenergy;
1080         float tmp[3], tmpspec[3];
1081
1082         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1083                 return;
1084         if (f == 0.0f)
1085                 return;
1086
1087         if (R.wrld.aomix==WO_AOADD) {
1088                 shr->combined[0] += shi->ao[0]*shi->r*shi->refl*f;
1089                 shr->combined[1] += shi->ao[1]*shi->g*shi->refl*f;
1090                 shr->combined[2] += shi->ao[2]*shi->b*shi->refl*f;
1091         }
1092         else if (R.wrld.aomix==WO_AOMUL) {
1093                 mul_v3_v3v3(tmp, shr->combined, shi->ao);
1094                 mul_v3_v3v3(tmpspec, shr->spec, shi->ao);
1095
1096                 if (f == 1.0f) {
1097                         copy_v3_v3(shr->combined, tmp);
1098                         copy_v3_v3(shr->spec, tmpspec);
1099                 }
1100                 else {
1101                         interp_v3_v3v3(shr->combined, shr->combined, tmp, f);
1102                         interp_v3_v3v3(shr->spec, shr->spec, tmpspec, f);
1103                 }
1104         }
1105 }
1106
1107 void environment_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1108 {
1109         float f= R.wrld.ao_env_energy*shi->amb;
1110
1111         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1112                 return;
1113         if (f == 0.0f)
1114                 return;
1115         
1116         shr->combined[0] += shi->env[0]*shi->r*shi->refl*f;
1117         shr->combined[1] += shi->env[1]*shi->g*shi->refl*f;
1118         shr->combined[2] += shi->env[2]*shi->b*shi->refl*f;
1119 }
1120
1121 static void indirect_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1122 {
1123         float f= R.wrld.ao_indirect_energy;
1124
1125         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1126                 return;
1127         if (f == 0.0f)
1128                 return;
1129
1130         shr->combined[0] += shi->indirect[0]*shi->r*shi->refl*f;
1131         shr->combined[1] += shi->indirect[1]*shi->g*shi->refl*f;
1132         shr->combined[2] += shi->indirect[2]*shi->b*shi->refl*f;
1133 }
1134
1135 /* result written in shadfac */
1136 void lamp_get_shadow(LampRen *lar, ShadeInput *shi, float inp, float shadfac[4], int do_real)
1137 {
1138         LampShadowSubSample *lss= &(lar->shadsamp[shi->thread].s[shi->sample]);
1139         
1140         if (do_real || lss->samplenr!=shi->samplenr) {
1141                 
1142                 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1143                 
1144                 if (lar->shb) {
1145                         if (lar->buftype==LA_SHADBUF_IRREGULAR)
1146                                 shadfac[3]= ISB_getshadow(shi, lar->shb);
1147                         else
1148                                 shadfac[3] = testshadowbuf(&R, lar->shb, shi->co, shi->dxco, shi->dyco, inp, shi->mat->lbias);
1149                 }
1150                 else if (lar->mode & LA_SHAD_RAY) {
1151                         ray_shadow(shi, lar, shadfac);
1152                 }
1153                 
1154                 if (shi->depth==0) {
1155                         copy_v4_v4(lss->shadfac, shadfac);
1156                         lss->samplenr= shi->samplenr;
1157                 }
1158         }
1159         else {
1160                 copy_v4_v4(shadfac, lss->shadfac);
1161         }
1162 }
1163
1164 /* lampdistance and spot angle, writes in lv and dist */
1165 float lamp_get_visibility(LampRen *lar, const float co[3], float lv[3], float *dist)
1166 {
1167         if (lar->type==LA_SUN || lar->type==LA_HEMI) {
1168                 *dist= 1.0f;
1169                 copy_v3_v3(lv, lar->vec);
1170                 return 1.0f;
1171         }
1172         else {
1173                 float visifac= 1.0f, t;
1174                 
1175                 sub_v3_v3v3(lv, co, lar->co);
1176                 *dist= sqrtf(dot_v3v3(lv, lv));
1177                 t= 1.0f/dist[0];
1178                 mul_v3_fl(lv, t);
1179                 
1180                 /* area type has no quad or sphere option */
1181                 if (lar->type==LA_AREA) {
1182                         /* area is single sided */
1183                         //if (dot_v3v3(lv, lar->vec) > 0.0f)
1184                         //      visifac= 1.0f;
1185                         //else
1186                         //      visifac= 0.0f;
1187                 }
1188                 else {
1189                         switch (lar->falloff_type) {
1190                                 case LA_FALLOFF_CONSTANT:
1191                                         visifac = 1.0f;
1192                                         break;
1193                                 case LA_FALLOFF_INVLINEAR:
1194                                         visifac = lar->dist/(lar->dist + dist[0]);
1195                                         break;
1196                                 case LA_FALLOFF_INVSQUARE:
1197                                         /* NOTE: This seems to be a hack since commit r12045 says this
1198                                          * option is similar to old Quad, but with slight changes.
1199                                          * Correct inv square would be (which would be old Quad):
1200                                          * visifac = lar->distkw / (lar->distkw + dist[0]*dist[0]);
1201                                          */
1202                                         visifac = lar->dist / (lar->dist + dist[0]*dist[0]);
1203                                         break;
1204                                 case LA_FALLOFF_SLIDERS:
1205                                         if (lar->ld1>0.0f)
1206                                                 visifac= lar->dist/(lar->dist+lar->ld1*dist[0]);
1207                                         if (lar->ld2>0.0f)
1208                                                 visifac*= lar->distkw/(lar->distkw+lar->ld2*dist[0]*dist[0]);
1209                                         break;
1210                                 case LA_FALLOFF_CURVE:
1211                                         /* curvemapping_initialize is called from #add_render_lamp */
1212                                         visifac = curvemapping_evaluateF(lar->curfalloff, 0, dist[0]/lar->dist);
1213                                         break;
1214                         }
1215                         
1216                         if (lar->mode & LA_SPHERE) {
1217                                 float t= lar->dist - dist[0];
1218                                 if (t<=0.0f) 
1219                                         visifac= 0.0f;
1220                                 else
1221                                         visifac*= t/lar->dist;
1222                         }
1223                         
1224                         if (visifac > 0.0f) {
1225                                 if (lar->type==LA_SPOT) {
1226                                         float inpr;
1227                                         
1228                                         if (lar->mode & LA_SQUARE) {
1229                                                 if (dot_v3v3(lv, lar->vec) > 0.0f) {
1230                                                         float lvrot[3], x;
1231                                                         
1232                                                         /* rotate view to lampspace */
1233                                                         copy_v3_v3(lvrot, lv);
1234                                                         mul_m3_v3(lar->imat, lvrot);
1235                                                         
1236                                                         x = max_ff(fabsf(lvrot[0]/lvrot[2]), fabsf(lvrot[1]/lvrot[2]));
1237                                                         /* 1.0f/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1238                                                         
1239                                                         inpr= 1.0f/(sqrt(1.0f+x*x));
1240                                                 }
1241                                                 else inpr= 0.0f;
1242                                         }
1243                                         else {
1244                                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1245                                         }
1246                                         
1247                                         t= lar->spotsi;
1248                                         if (inpr<=t) 
1249                                                 visifac= 0.0f;
1250                                         else {
1251                                                 t= inpr-t;
1252                                                 if (t<lar->spotbl && lar->spotbl!=0.0f) {
1253                                                         /* soft area */
1254                                                         float i= t/lar->spotbl;
1255                                                         t= i*i;
1256                                                         inpr*= (3.0f*t-2.0f*t*i);
1257                                                 }
1258                                                 visifac*= inpr;
1259                                         }
1260                                 }
1261                         }
1262                 }
1263                 if (visifac <= 0.001f) visifac = 0.0f;
1264                 return visifac;
1265         }
1266 }
1267
1268 /* function returns raw diff, spec and full shadowed diff in the 'shad' pass */
1269 static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int passflag)
1270 {
1271         Material *ma= shi->mat;
1272         VlakRen *vlr= shi->vlr;
1273         float lv[3], lampdist, lacol[3], shadfac[4], lashdw[3];
1274         float i, is, i_noshad, inp, *vn, *view, vnor[3], phongcorr=1.0f;
1275         float visifac;
1276         
1277         vn= shi->vn;
1278         view= shi->view;
1279         
1280         
1281         if (lar->energy == 0.0f) return;
1282         /* only shadow lamps shouldn't affect shadow-less materials at all */
1283         if ((lar->mode & LA_ONLYSHADOW) && (!(ma->mode & MA_SHADOW) || !(R.r.mode & R_SHADOW)))
1284                 return;
1285         /* optimization, don't render fully black lamps */
1286         if (!(lar->mode & LA_TEXTURE) && (lar->r + lar->g + lar->b == 0.0f))
1287                 return;
1288         
1289         /* lampdist, spot angle, area side, ... */
1290         visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1291         if (visifac==0.0f)
1292                 return;
1293         
1294         if (lar->type==LA_SPOT) {
1295                 if (lar->mode & LA_OSATEX) {
1296                         shi->osatex= 1; /* signal for multitex() */
1297                         
1298                         shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/lampdist;
1299                         shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/lampdist;
1300                         shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/lampdist;
1301                         
1302                         shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/lampdist;
1303                         shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/lampdist;
1304                         shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/lampdist;
1305                 }
1306         }
1307         
1308         /* lamp color texture */
1309         lacol[0]= lar->r;
1310         lacol[1]= lar->g;
1311         lacol[2]= lar->b;
1312         
1313         lashdw[0]= lar->shdwr;
1314         lashdw[1]= lar->shdwg;
1315         lashdw[2]= lar->shdwb;
1316         
1317         if (lar->mode & LA_TEXTURE)     do_lamp_tex(lar, lv, shi, lacol, LA_TEXTURE);
1318         if (lar->mode & LA_SHAD_TEX)    do_lamp_tex(lar, lv, shi, lashdw, LA_SHAD_TEX);
1319
1320                 /* tangent case; calculate fake face normal, aligned with lampvector */
1321                 /* note, vnor==vn is used as tangent trigger for buffer shadow */
1322         if (vlr->flag & R_TANGENT) {
1323                 float cross[3], nstrand[3], blend;
1324
1325                 if (ma->mode & MA_STR_SURFDIFF) {
1326                         cross_v3_v3v3(cross, shi->surfnor, vn);
1327                         cross_v3_v3v3(nstrand, vn, cross);
1328
1329                         blend= dot_v3v3(nstrand, shi->surfnor);
1330                         blend= 1.0f - blend;
1331                         CLAMP(blend, 0.0f, 1.0f);
1332
1333                         interp_v3_v3v3(vnor, nstrand, shi->surfnor, blend);
1334                         normalize_v3(vnor);
1335                 }
1336                 else {
1337                         cross_v3_v3v3(cross, lv, vn);
1338                         cross_v3_v3v3(vnor, cross, vn);
1339                         normalize_v3(vnor);
1340                 }
1341
1342                 if (ma->strand_surfnor > 0.0f) {
1343                         if (ma->strand_surfnor > shi->surfdist) {
1344                                 blend= (ma->strand_surfnor - shi->surfdist)/ma->strand_surfnor;
1345                                 interp_v3_v3v3(vnor, vnor, shi->surfnor, blend);
1346                                 normalize_v3(vnor);
1347                         }
1348                 }
1349
1350                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1351                 vn= vnor;
1352         }
1353         else if (ma->mode & MA_TANGENT_V) {
1354                 float cross[3];
1355                 cross_v3_v3v3(cross, lv, shi->tang);
1356                 cross_v3_v3v3(vnor, cross, shi->tang);
1357                 normalize_v3(vnor);
1358                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1359                 vn= vnor;
1360         }
1361         
1362         /* dot product and reflectivity */
1363         /* inp = dotproduct, is = shader result, i = lamp energy (with shadow), i_noshad = i without shadow */
1364         inp= dot_v3v3(vn, lv);
1365
1366         /* phong threshold to prevent backfacing faces having artifacts on ray shadow (terminator problem) */
1367         /* this complex construction screams for a nicer implementation! (ton) */
1368         if (R.r.mode & R_SHADOW) {
1369                 if (ma->mode & MA_SHADOW) {
1370                         if (lar->type == LA_HEMI || lar->type == LA_AREA) {
1371                                 /* pass */
1372                         }
1373                         else if ((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1374                                 float thresh= shi->obr->ob->smoothresh;
1375                                 if (inp>thresh)
1376                                         phongcorr= (inp-thresh)/(inp*(1.0f-thresh));
1377                                 else
1378                                         phongcorr= 0.0f;
1379                         }
1380                         else if (ma->sbias!=0.0f && ((lar->mode & LA_SHAD_RAY) || lar->shb)) {
1381                                 if (inp>ma->sbias)
1382                                         phongcorr= (inp-ma->sbias)/(inp*(1.0f-ma->sbias));
1383                                 else
1384                                         phongcorr= 0.0f;
1385                         }
1386                 }
1387         }
1388         
1389         /* diffuse shaders */
1390         if (lar->mode & LA_NO_DIFF) {
1391                 is = 0.0f;  /* skip shaders */
1392         }
1393         else if (lar->type==LA_HEMI) {
1394                 is = 0.5f * inp + 0.5f;
1395         }
1396         else {
1397                 
1398                 if (lar->type==LA_AREA)
1399                         inp= area_lamp_energy_multisample(lar, shi->co, vn);
1400                 
1401                 /* diffuse shaders (oren nayer gets inp from area light) */
1402                 if (ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1403                 else if (ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1404                 else if (ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1405                 else if (ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1406                 else is= inp;  /* Lambert */
1407         }
1408
1409         /* 'is' is diffuse */
1410         if ((ma->shade_flag & MA_CUBIC) && is > 0.0f && is < 1.0f) {
1411                 is= 3.0f * is * is - 2.0f * is * is * is;  /* nicer termination of shades */
1412         }
1413
1414         i= is*phongcorr;
1415         
1416         if (i>0.0f) {
1417                 i*= visifac*shi->refl;
1418         }
1419         i_noshad= i;
1420         
1421         vn = shi->vn;  /* bring back original vector, we use special specular shaders for tangent */
1422         if (ma->mode & MA_TANGENT_V)
1423                 vn= shi->tang;
1424         
1425         /* init transp shadow */
1426         shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1427         
1428         /* shadow and spec, (visifac==0 outside spot) */
1429         if (visifac> 0.0f) {
1430                 
1431                 if ((R.r.mode & R_SHADOW)) {
1432                         if (ma->mode & MA_SHADOW) {
1433                                 if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1434                                         
1435                                         if (vn==vnor)   /* tangent trigger */
1436                                                 lamp_get_shadow(lar, shi, dot_v3v3(shi->vn, lv), shadfac, shi->depth);
1437                                         else
1438                                                 lamp_get_shadow(lar, shi, inp, shadfac, shi->depth);
1439                                                 
1440                                         /* warning, here it skips the loop */
1441                                         if ((lar->mode & LA_ONLYSHADOW) && i>0.0f) {
1442                                                 
1443                                                 shadfac[3]= i*lar->energy*(1.0f-shadfac[3]);
1444                                                 shr->shad[0] -= shadfac[3]*shi->r*(1.0f-lashdw[0]);
1445                                                 shr->shad[1] -= shadfac[3]*shi->g*(1.0f-lashdw[1]);
1446                                                 shr->shad[2] -= shadfac[3]*shi->b*(1.0f-lashdw[2]);
1447                                                 
1448                                                 shr->spec[0] -= shadfac[3]*shi->specr*(1.0f-lashdw[0]);
1449                                                 shr->spec[1] -= shadfac[3]*shi->specg*(1.0f-lashdw[1]);
1450                                                 shr->spec[2] -= shadfac[3]*shi->specb*(1.0f-lashdw[2]);
1451                                                 
1452                                                 return;
1453                                         }
1454                                         
1455                                         i*= shadfac[3];
1456                                         shr->shad[3] = shadfac[3]; /* store this for possible check in troublesome cases */
1457                                 }
1458                         }
1459                 }
1460                 
1461                 /* in case 'no diffuse' we still do most calculus, spec can be in shadow.*/
1462                 if (!(lar->mode & LA_NO_DIFF)) {
1463                         if (i>0.0f) {
1464                                 if (ma->mode & MA_SHADOW_TRA)
1465                                         add_to_diffuse(shr->shad, shi, is, i*shadfac[0]*lacol[0], i*shadfac[1]*lacol[1], i*shadfac[2]*lacol[2]);
1466                                 else
1467                                         add_to_diffuse(shr->shad, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
1468                         }
1469                         /* add light for colored shadow */
1470                         if (i_noshad>i && !(lashdw[0]==0 && lashdw[1]==0 && lashdw[2]==0)) {
1471                                 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]);
1472                         }
1473                         if (i_noshad>0.0f) {
1474                                 if (passflag & (SCE_PASS_DIFFUSE|SCE_PASS_SHADOW)) {
1475                                         add_to_diffuse(shr->diff, shi, is, i_noshad*lacol[0], i_noshad*lacol[1], i_noshad*lacol[2]);
1476                                 }
1477                                 else
1478                                         copy_v3_v3(shr->diff, shr->shad);
1479                         }
1480                 }
1481                 
1482                 /* specularity */
1483                 shadfac[3]*= phongcorr; /* note, shadfac not allowed to be stored nonlocal */
1484                 
1485                 if (shadfac[3]>0.0f && shi->spec!=0.0f && !(lar->mode & LA_NO_SPEC) && !(lar->mode & LA_ONLYSHADOW)) {
1486                         
1487                         if (!(passflag & (SCE_PASS_COMBINED | SCE_PASS_SPEC))) {
1488                                 /* pass */
1489                         }
1490                         else if (lar->type == LA_HEMI) {
1491                                 float t;
1492                                 /* hemi uses no spec shaders (yet) */
1493                                 
1494                                 lv[0]+= view[0];
1495                                 lv[1]+= view[1];
1496                                 lv[2]+= view[2];
1497                                 
1498                                 normalize_v3(lv);
1499                                 
1500                                 t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1501                                 
1502                                 if (lar->type==LA_HEMI) {
1503                                         t= 0.5f*t+0.5f;
1504                                 }
1505                                 
1506                                 t= shadfac[3]*shi->spec*spec(t, shi->har);
1507                                 
1508                                 shr->spec[0]+= t*(lacol[0] * shi->specr);
1509                                 shr->spec[1]+= t*(lacol[1] * shi->specg);
1510                                 shr->spec[2]+= t*(lacol[2] * shi->specb);
1511                         }
1512                         else {
1513                                 /* specular shaders */
1514                                 float specfac, t;
1515                                 
1516                                 if (ma->spec_shader==MA_SPEC_PHONG) 
1517                                         specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1518                                 else if (ma->spec_shader==MA_SPEC_COOKTORR) 
1519                                         specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1520                                 else if (ma->spec_shader==MA_SPEC_BLINN) 
1521                                         specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1522                                 else if (ma->spec_shader==MA_SPEC_WARDISO)
1523                                         specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1524                                 else 
1525                                         specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1526                                 
1527                                 /* area lamp correction */
1528                                 if (lar->type==LA_AREA) specfac*= inp;
1529                                 
1530                                 t= shadfac[3]*shi->spec*visifac*specfac;
1531                                 
1532                                 if (ma->mode & MA_RAMP_SPEC) {
1533                                         float spec[3];
1534                                         do_specular_ramp(shi, specfac, t, spec);
1535                                         shr->spec[0]+= t*(lacol[0] * spec[0]);
1536                                         shr->spec[1]+= t*(lacol[1] * spec[1]);
1537                                         shr->spec[2]+= t*(lacol[2] * spec[2]);
1538                                 }
1539                                 else {
1540                                         shr->spec[0]+= t*(lacol[0] * shi->specr);
1541                                         shr->spec[1]+= t*(lacol[1] * shi->specg);
1542                                         shr->spec[2]+= t*(lacol[2] * shi->specb);
1543                                 }
1544                         }
1545                 }
1546         }
1547 }
1548
1549 static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr)
1550 {
1551         
1552         if (R.r.mode & R_SHADOW) {
1553                 ListBase *lights;
1554                 LampRen *lar;
1555                 GroupObject *go;
1556                 float inpr, lv[3];
1557                 float /* *view, */ shadfac[4];
1558                 float ir, accum, visifac, lampdist;
1559                 float shaded = 0.0f, lightness = 0.0f;
1560                 
1561
1562                 /* view= shi->view; */ /* UNUSED */
1563                 accum= ir= 0.0f;
1564                 
1565                 lights= get_lights(shi);
1566                 for (go=lights->first; go; go= go->next) {
1567                         lar= go->lampren;
1568                         if (lar==NULL) continue;
1569                         
1570                         /* yafray: ignore shading by photonlights, not used in Blender */
1571                         if (lar->type==LA_YF_PHOTON) continue;
1572                         
1573                         if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1574                         if ((lar->lay & shi->lay)==0) continue;
1575                         
1576                         if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1577                                 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1578                                 ir+= 1.0f;
1579
1580                                 if (visifac <= 0.0f) {
1581                                         if (shi->mat->shadowonly_flag == MA_SO_OLD)
1582                                                 accum+= 1.0f;
1583
1584                                         continue;
1585                                 }
1586                                 inpr= dot_v3v3(shi->vn, lv);
1587                                 if (inpr <= 0.0f) {
1588                                         if (shi->mat->shadowonly_flag == MA_SO_OLD)
1589                                                 accum+= 1.0f;
1590
1591                                         continue;
1592                                 }
1593
1594                                 lamp_get_shadow(lar, shi, inpr, shadfac, shi->depth);
1595
1596                                 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1597                                         /* Old "Shadows Only" */
1598                                         accum+= (1.0f-visifac) + (visifac)*rgb_to_grayscale(shadfac)*shadfac[3];
1599                                 }
1600                                 else {
1601                                         shaded += rgb_to_grayscale(shadfac)*shadfac[3] * visifac * lar->energy;
1602
1603                                         if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1604                                                 lightness += visifac * lar->energy;
1605                                         }
1606                                 }
1607                         }
1608                 }
1609
1610                 /* Apply shadows as alpha */
1611                 if (ir>0.0f) {
1612                         if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1613                                 accum = 1.0f - accum/ir;
1614                         }
1615                         else {
1616                                 if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1617                                         if (lightness > 0.0f) {
1618                                                 /* Get shadow value from between 0.0f and non-shadowed lightness */
1619                                                 accum = (lightness - shaded) / (lightness);
1620                                         }
1621                                         else {
1622                                                 accum = 0.0f;
1623                                         }
1624                                 }
1625                                 else { /* shadowonly_flag == MA_SO_SHADED */
1626                                         /* Use shaded value */
1627                                         accum = 1.0f - shaded;
1628                         }}
1629
1630                         shr->alpha= (shi->alpha)*(accum);
1631                         if (shr->alpha<0.0f) shr->alpha=0.0f;
1632                 }
1633                 else {
1634                         /* If "fully shaded", use full alpha even on areas that have no lights */
1635                         if (shi->mat->shadowonly_flag == MA_SO_SHADED) shr->alpha=shi->alpha;
1636                         else shr->alpha= 0.f;
1637                 }
1638         }
1639         
1640         /* quite disputable this...  also note it doesn't mirror-raytrace */
1641         if ((R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT)) && shi->amb!=0.0f) {
1642                 float f;
1643                 
1644                 if (R.wrld.mode & WO_AMB_OCC) {
1645                         f= R.wrld.aoenergy*shi->amb;
1646                         
1647                         if (R.wrld.aomix==WO_AOADD) {
1648                                 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1649                                         f= f*(1.0f - rgb_to_grayscale(shi->ao));
1650                                         shr->alpha= (shr->alpha + f)*f;
1651                                 }
1652                                 else {
1653                                         shr->alpha -= f*rgb_to_grayscale(shi->ao);
1654                                         if (shr->alpha<0.0f) shr->alpha=0.0f;
1655                                 }
1656                         }
1657                         else /* AO Multiply */
1658                                 shr->alpha= (1.0f - f)*shr->alpha + f*(1.0f - (1.0f - shr->alpha)*rgb_to_grayscale(shi->ao));
1659                 }
1660
1661                 if (R.wrld.mode & WO_ENV_LIGHT) {
1662                         if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1663                                 f= R.wrld.ao_env_energy*shi->amb*(1.0f - rgb_to_grayscale(shi->env));
1664                                 shr->alpha= (shr->alpha + f)*f;
1665                         }
1666                         else {
1667                                 f= R.wrld.ao_env_energy*shi->amb;
1668                                 shr->alpha -= f*rgb_to_grayscale(shi->env);
1669                                 if (shr->alpha<0.0f) shr->alpha=0.0f;
1670                         }
1671                 }
1672         }
1673 }
1674
1675 /* let's map negative light as if it mirrors positive light, otherwise negative values disappear */
1676 static void wrld_exposure_correct(float diff[3])
1677 {
1678         
1679         diff[0]= R.wrld.linfac*(1.0f-expf( diff[0]*R.wrld.logfac) );
1680         diff[1]= R.wrld.linfac*(1.0f-expf( diff[1]*R.wrld.logfac) );
1681         diff[2]= R.wrld.linfac*(1.0f-expf( diff[2]*R.wrld.logfac) );
1682 }
1683
1684 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1685 {
1686         Material *ma= shi->mat;
1687         int passflag= shi->passflag;
1688         
1689         memset(shr, 0, sizeof(ShadeResult));
1690         
1691         if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1692         
1693         /* separate loop */
1694         if (ma->mode & MA_ONLYSHADOW) {
1695                 shade_lamp_loop_only_shadow(shi, shr);
1696                 return;
1697         }
1698         
1699         /* envmap hack, always reset */
1700         shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0f;
1701         
1702         /* material color itself */
1703         if (passflag & (SCE_PASS_COMBINED|SCE_PASS_RGBA)) {
1704                 if (ma->mode & (MA_FACETEXTURE)) {
1705                         shi->r= shi->vcol[0];
1706                         shi->g= shi->vcol[1];
1707                         shi->b= shi->vcol[2];
1708                         if (ma->mode & (MA_FACETEXTURE_ALPHA))
1709                                 shi->alpha= shi->vcol[3];
1710                 }
1711                 else if (ma->mode & (MA_VERTEXCOLP)) {
1712                         float neg_alpha = 1.0f - shi->vcol[3];
1713                         shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
1714                         shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
1715                         shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
1716                 }
1717                 if (ma->texco) {
1718                         do_material_tex(shi, &R);
1719                         if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1720                 }
1721                 
1722                 shr->col[0]= shi->r*shi->alpha;
1723                 shr->col[1]= shi->g*shi->alpha;
1724                 shr->col[2]= shi->b*shi->alpha;
1725                 shr->col[3]= shi->alpha;
1726
1727                 if ((ma->sss_flag & MA_DIFF_SSS) && !sss_pass_done(&R, ma)) {
1728                         if (ma->sss_texfac == 0.0f) {
1729                                 shi->r= shi->g= shi->b= shi->alpha= 1.0f;
1730                                 shr->col[0]= shr->col[1]= shr->col[2]= shr->col[3]= 1.0f;
1731                         }
1732                         else {
1733                                 shi->r= pow(shi->r, ma->sss_texfac);
1734                                 shi->g= pow(shi->g, ma->sss_texfac);
1735                                 shi->b= pow(shi->b, ma->sss_texfac);
1736                                 shi->alpha= pow(shi->alpha, ma->sss_texfac);
1737                                 
1738                                 shr->col[0]= pow(shr->col[0], ma->sss_texfac);
1739                                 shr->col[1]= pow(shr->col[1], ma->sss_texfac);
1740                                 shr->col[2]= pow(shr->col[2], ma->sss_texfac);
1741                                 shr->col[3]= pow(shr->col[3], ma->sss_texfac);
1742                         }
1743                 }
1744         }
1745         
1746         if (ma->mode & MA_SHLESS) {
1747                 shr->combined[0]= shi->r;
1748                 shr->combined[1]= shi->g;
1749                 shr->combined[2]= shi->b;
1750                 shr->alpha= shi->alpha;
1751                 return;
1752         }
1753
1754         if ( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) {       /* vertexcolor light */
1755                 shr->emit[0]= shi->r*(shi->emit+shi->vcol[0]*shi->vcol[3]);
1756                 shr->emit[1]= shi->g*(shi->emit+shi->vcol[1]*shi->vcol[3]);
1757                 shr->emit[2]= shi->b*(shi->emit+shi->vcol[2]*shi->vcol[3]);
1758         }
1759         else {
1760                 shr->emit[0]= shi->r*shi->emit;
1761                 shr->emit[1]= shi->g*shi->emit;
1762                 shr->emit[2]= shi->b*shi->emit;
1763         }
1764         
1765         /* AO pass */
1766         if (R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) {
1767                 if (((passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT))) ||
1768                     (passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1769                 {
1770                         if (R.r.mode & R_SHADOW) {
1771                                 /* AO was calculated for scanline already */
1772                                 if (shi->depth || shi->volume_depth)
1773                                         ambient_occlusion(shi);
1774                                 copy_v3_v3(shr->ao, shi->ao);
1775                                 copy_v3_v3(shr->env, shi->env); /* XXX multiply */
1776                                 copy_v3_v3(shr->indirect, shi->indirect); /* XXX multiply */
1777                         }
1778                 }
1779         }
1780         
1781         /* lighting pass */
1782         if (passflag & (SCE_PASS_COMBINED|SCE_PASS_DIFFUSE|SCE_PASS_SPEC|SCE_PASS_SHADOW)) {
1783                 GroupObject *go;
1784                 ListBase *lights;
1785                 LampRen *lar;
1786                 
1787                 lights= get_lights(shi);
1788                 for (go=lights->first; go; go= go->next) {
1789                         lar= go->lampren;
1790                         if (lar==NULL) continue;
1791                         
1792                         /* yafray: ignore shading by photonlights, not used in Blender */
1793                         if (lar->type==LA_YF_PHOTON) continue;
1794                         
1795                         /* test for lamp layer */
1796                         if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1797                         if ((lar->lay & shi->lay)==0) continue;
1798                         
1799                         /* accumulates in shr->diff and shr->spec and shr->shad (diffuse with shadow!) */
1800                         shade_one_light(lar, shi, shr, passflag);
1801                 }
1802
1803                 /* this check is to prevent only shadow lamps from producing negative
1804                  * colors.*/
1805                 if (shr->spec[0] < 0) shr->spec[0] = 0;
1806                 if (shr->spec[1] < 0) shr->spec[1] = 0;
1807                 if (shr->spec[2] < 0) shr->spec[2] = 0;
1808
1809                 if (shr->shad[0] < 0) shr->shad[0] = 0;
1810                 if (shr->shad[1] < 0) shr->shad[1] = 0;
1811                 if (shr->shad[2] < 0) shr->shad[2] = 0;
1812                                                 
1813                 if (ma->sss_flag & MA_DIFF_SSS) {
1814                         float sss[3], col[3], invalpha, texfac= ma->sss_texfac;
1815
1816                         /* this will return false in the preprocess stage */
1817                         if (sample_sss(&R, ma, shi->co, sss)) {
1818                                 invalpha= (shr->col[3] > FLT_EPSILON)? 1.0f/shr->col[3]: 1.0f;
1819
1820                                 if (texfac==0.0f) {
1821                                         copy_v3_v3(col, shr->col);
1822                                         mul_v3_fl(col, invalpha);
1823                                 }
1824                                 else if (texfac==1.0f) {
1825                                         col[0]= col[1]= col[2]= 1.0f;
1826                                         mul_v3_fl(col, invalpha);
1827                                 }
1828                                 else {
1829                                         copy_v3_v3(col, shr->col);
1830                                         mul_v3_fl(col, invalpha);
1831                                         col[0]= pow(col[0], 1.0f-texfac);
1832                                         col[1]= pow(col[1], 1.0f-texfac);
1833                                         col[2]= pow(col[2], 1.0f-texfac);
1834                                 }
1835
1836                                 shr->diff[0]= sss[0]*col[0];
1837                                 shr->diff[1]= sss[1]*col[1];
1838                                 shr->diff[2]= sss[2]*col[2];
1839
1840                                 if (shi->combinedflag & SCE_PASS_SHADOW) {
1841                                         shr->shad[0]= shr->diff[0];
1842                                         shr->shad[1]= shr->diff[1];
1843                                         shr->shad[2]= shr->diff[2];
1844                                 }
1845                         }
1846                 }
1847                 
1848                 if (shi->combinedflag & SCE_PASS_SHADOW)
1849                         copy_v3_v3(shr->combined, shr->shad);   /* note, no ';' ! */
1850                 else
1851                         copy_v3_v3(shr->combined, shr->diff);
1852                         
1853                 /* calculate shadow pass, we use a multiplication mask */
1854                 /* if diff = 0,0,0 it doesn't matter what the shadow pass is, so leave it as is */
1855                 if (passflag & SCE_PASS_SHADOW && !(shr->diff[0]==0.0f && shr->diff[1]==0.0f && shr->diff[2]==0.0f)) {
1856                         if (shr->diff[0]!=0.0f) shr->shad[0]= shr->shad[0]/shr->diff[0];
1857                         /* can't determine proper shadow from shad/diff (0/0), so use shadow intensity */
1858                         else if (shr->shad[0]==0.0f) shr->shad[0]= shr->shad[3];
1859
1860                         if (shr->diff[1]!=0.0f) shr->shad[1]= shr->shad[1]/shr->diff[1];
1861                         else if (shr->shad[1]==0.0f) shr->shad[1]= shr->shad[3];
1862
1863                         if (shr->diff[2]!=0.0f) shr->shad[2]= shr->shad[2]/shr->diff[2];
1864                         else if (shr->shad[2]==0.0f) shr->shad[2]= shr->shad[3];
1865                 }
1866                 
1867                 /* exposure correction */
1868                 if ((R.wrld.exp!=0.0f || R.wrld.range!=1.0f) && !R.sss_points) {
1869                         wrld_exposure_correct(shr->combined);   /* has no spec! */
1870                         wrld_exposure_correct(shr->spec);
1871                 }
1872         }
1873         
1874         /* alpha in end, spec can influence it */
1875         if (passflag & (SCE_PASS_COMBINED)) {
1876                 if ((ma->fresnel_tra!=0.0f) && (shi->mode & MA_TRANSP))
1877                         shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1878                         
1879                 /* note: shi->mode! */
1880                 if (shi->mode & MA_TRANSP && (shi->mode & (MA_ZTRANSP|MA_RAYTRANSP))) {
1881                         if (shi->spectra!=0.0f) {
1882                                 float t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
1883                                 t *= shi->spectra;
1884                                 if (t>1.0f) t= 1.0f;
1885                                 shi->alpha= (1.0f-t)*shi->alpha+t;
1886                         }
1887                 }
1888         }
1889         shr->alpha= shi->alpha;
1890         
1891         /* from now stuff everything in shr->combined: ambient, AO, ramps, exposure */
1892         if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1893                 if (R.r.mode & R_SHADOW) {
1894                         /* add AO in combined? */
1895                         if (R.wrld.mode & WO_AMB_OCC)
1896                                 if (shi->combinedflag & SCE_PASS_AO)
1897                                         ambient_occlusion_apply(shi, shr);
1898
1899                         if (R.wrld.mode & WO_ENV_LIGHT)
1900                                 if (shi->combinedflag & SCE_PASS_ENVIRONMENT)
1901                                         environment_lighting_apply(shi, shr);
1902
1903                         if (R.wrld.mode & WO_INDIRECT_LIGHT)
1904                                 if (shi->combinedflag & SCE_PASS_INDIRECT)
1905                                         indirect_lighting_apply(shi, shr);
1906                 }
1907                 
1908                 shr->combined[0]+= shi->ambr;
1909                 shr->combined[1]+= shi->ambg;
1910                 shr->combined[2]+= shi->ambb;
1911                 
1912                 if (ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->combined, shi);
1913         }
1914
1915         if (ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shi);
1916         
1917         /* refcol is for envmap only */
1918         if (shi->refcol[0]!=0.0f) {
1919                 float result[3];
1920                 
1921                 result[0]= shi->mirr*shi->refcol[1] + (1.0f - shi->mirr*shi->refcol[0])*shr->combined[0];
1922                 result[1]= shi->mirg*shi->refcol[2] + (1.0f - shi->mirg*shi->refcol[0])*shr->combined[1];
1923                 result[2]= shi->mirb*shi->refcol[3] + (1.0f - shi->mirb*shi->refcol[0])*shr->combined[2];
1924                 
1925                 if (passflag & SCE_PASS_REFLECT)
1926                         sub_v3_v3v3(shr->refl, result, shr->combined);
1927                 
1928                 if (shi->combinedflag & SCE_PASS_REFLECT)
1929                         copy_v3_v3(shr->combined, result);
1930                         
1931         }
1932         
1933         /* and add emit and spec */
1934         if (shi->combinedflag & SCE_PASS_EMIT)
1935                 add_v3_v3(shr->combined, shr->emit);
1936         if (shi->combinedflag & SCE_PASS_SPEC)
1937                 add_v3_v3(shr->combined, shr->spec);
1938         
1939         /* modulate by the object color */
1940         if ((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
1941                 if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1942                         float obcol[4];
1943
1944                         copy_v4_v4(obcol, shi->obr->ob->col);
1945                         CLAMP(obcol[3], 0.0f, 1.0f);
1946
1947                         shr->combined[0] *= obcol[0];
1948                         shr->combined[1] *= obcol[1];
1949                         shr->combined[2] *= obcol[2];
1950                         if (shi->mode & MA_TRANSP) shr->alpha *= obcol[3];
1951                 }
1952         }
1953
1954         shr->combined[3]= shr->alpha;
1955 }
1956