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