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