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