Fix: blender internal transparent shadows were not influenced by object color alpha.
[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_BUTS_PREVIEW)
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 cuts, 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         cuts= 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                         cuts= 2;
260                 }
261                 else if (disc > 0.0) {
262                         disc = sqrt(disc);
263                         t1 = (-b + disc) / a;
264                         t2 = (-b - disc) / a;
265                         cuts= 2;
266                 }
267         }
268         if (cuts==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         /* modulate by the object color */
929         if ((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
930                 float obcol[4];
931
932                 copy_v4_v4(obcol, shi->obr->ob->col);
933                 CLAMP(obcol[3], 0.0f, 1.0f);
934
935                 shr->diff[0] *= obcol[0];
936                 shr->diff[1] *= obcol[1];
937                 shr->diff[2] *= obcol[2];
938                 if (shi->mode & MA_TRANSP) shr->alpha *= obcol[3];
939         }
940 }
941
942 /* ramp for at end of shade */
943 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
944 {
945         Material *ma= shi->mat;
946         float col[4];
947
948         if (ma->ramp_col) {
949                 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
950                         float fac = rgb_to_grayscale(diff);
951                         do_colorband(ma->ramp_col, fac, col);
952                         
953                         /* blending method */
954                         fac= col[3]*ma->rampfac_col;
955                         
956                         ramp_blend(ma->rampblend_col, diff, fac, col);
957                 }
958         }
959 }
960
961 /* r,g,b denote energy, ramp is used with different values to make new material color */
962 static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, float g, float b)
963 {
964         Material *ma= shi->mat;
965
966         if (ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
967                 
968                 /* MA_RAMP_IN_RESULT is exceptional */
969                 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
970                         /* normal add */
971                         diff[0] += r * shi->r;
972                         diff[1] += g * shi->g;
973                         diff[2] += b * shi->b;
974                 }
975                 else {
976                         float colt[3], col[4];
977                         float fac;
978
979                         /* input */
980                         switch (ma->rampin_col) {
981                         case MA_RAMP_IN_ENERGY:
982                                 /* should use 'rgb_to_grayscale' but we only have a vector version */
983                                 fac= 0.3f*r + 0.58f*g + 0.12f*b;
984                                 break;
985                         case MA_RAMP_IN_SHADER:
986                                 fac= is;
987                                 break;
988                         case MA_RAMP_IN_NOR:
989                                 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
990                                 break;
991                         default:
992                                 fac= 0.0f;
993                                 break;
994                         }
995         
996                         do_colorband(ma->ramp_col, fac, col);
997                         
998                         /* blending method */
999                         fac= col[3]*ma->rampfac_col;
1000                         colt[0]= shi->r;
1001                         colt[1]= shi->g;
1002                         colt[2]= shi->b;
1003
1004                         ramp_blend(ma->rampblend_col, colt, fac, col);
1005
1006                         /* output to */
1007                         diff[0] += r * colt[0];
1008                         diff[1] += g * colt[1];
1009                         diff[2] += b * colt[2];
1010                 }
1011         }
1012         else {
1013                 diff[0] += r * shi->r;
1014                 diff[1] += g * shi->g;
1015                 diff[2] += b * shi->b;
1016         }
1017 }
1018
1019 static void ramp_spec_result(float spec_col[3], ShadeInput *shi)
1020 {
1021         Material *ma= shi->mat;
1022
1023         if (ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
1024                 float col[4];
1025                 float fac = rgb_to_grayscale(spec_col);
1026
1027                 do_colorband(ma->ramp_spec, fac, col);
1028                 
1029                 /* blending method */
1030                 fac= col[3]*ma->rampfac_spec;
1031                 
1032                 ramp_blend(ma->rampblend_spec, spec_col, fac, col);
1033                 
1034         }
1035 }
1036
1037 /* is = dot product shade, t = spec energy */
1038 static void do_specular_ramp(ShadeInput *shi, float is, float t, float spec[3])
1039 {
1040         Material *ma= shi->mat;
1041
1042         spec[0]= shi->specr;
1043         spec[1]= shi->specg;
1044         spec[2]= shi->specb;
1045
1046         /* MA_RAMP_IN_RESULT is exception */
1047         if (ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
1048                 float fac;
1049                 float col[4];
1050
1051                 /* input */
1052                 switch (ma->rampin_spec) {
1053                 case MA_RAMP_IN_ENERGY:
1054                         fac= t;
1055                         break;
1056                 case MA_RAMP_IN_SHADER:
1057                         fac= is;
1058                         break;
1059                 case MA_RAMP_IN_NOR:
1060                         fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1061                         break;
1062                 default:
1063                         fac= 0.0f;
1064                         break;
1065                 }
1066                 
1067                 do_colorband(ma->ramp_spec, fac, col);
1068                 
1069                 /* blending method */
1070                 fac= col[3]*ma->rampfac_spec;
1071                 
1072                 ramp_blend(ma->rampblend_spec, spec, fac, col);
1073         }
1074 }
1075
1076 /* pure AO, check for raytrace and world should have been done */
1077 /* preprocess, textures were not done, don't use shi->amb for that reason */
1078 void ambient_occlusion(ShadeInput *shi)
1079 {
1080         if ((R.wrld.ao_gather_method == WO_AOGATHER_APPROX) && shi->mat->amb!=0.0f) {
1081                 sample_occ(&R, shi);
1082         }
1083         else if ((R.r.mode & R_RAYTRACE) && shi->mat->amb!=0.0f) {
1084                 ray_ao(shi, shi->ao, shi->env);
1085         }
1086         else {
1087                 shi->ao[0]= shi->ao[1]= shi->ao[2]= 1.0f;
1088                 zero_v3(shi->env);
1089                 zero_v3(shi->indirect);
1090         }
1091 }
1092
1093
1094 /* wrld mode was checked for */
1095 static void ambient_occlusion_apply(ShadeInput *shi, ShadeResult *shr)
1096 {
1097         float f= R.wrld.aoenergy;
1098         float tmp[3], tmpspec[3];
1099
1100         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1101                 return;
1102         if (f == 0.0f)
1103                 return;
1104
1105         if (R.wrld.aomix==WO_AOADD) {
1106                 shr->combined[0] += shi->ao[0]*shi->r*shi->refl*f;
1107                 shr->combined[1] += shi->ao[1]*shi->g*shi->refl*f;
1108                 shr->combined[2] += shi->ao[2]*shi->b*shi->refl*f;
1109         }
1110         else if (R.wrld.aomix==WO_AOMUL) {
1111                 mul_v3_v3v3(tmp, shr->combined, shi->ao);
1112                 mul_v3_v3v3(tmpspec, shr->spec, shi->ao);
1113
1114                 if (f == 1.0f) {
1115                         copy_v3_v3(shr->combined, tmp);
1116                         copy_v3_v3(shr->spec, tmpspec);
1117                 }
1118                 else {
1119                         interp_v3_v3v3(shr->combined, shr->combined, tmp, f);
1120                         interp_v3_v3v3(shr->spec, shr->spec, tmpspec, f);
1121                 }
1122         }
1123 }
1124
1125 void environment_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1126 {
1127         float f= R.wrld.ao_env_energy*shi->amb;
1128
1129         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1130                 return;
1131         if (f == 0.0f)
1132                 return;
1133         
1134         shr->combined[0] += shi->env[0]*shi->r*shi->refl*f;
1135         shr->combined[1] += shi->env[1]*shi->g*shi->refl*f;
1136         shr->combined[2] += shi->env[2]*shi->b*shi->refl*f;
1137 }
1138
1139 static void indirect_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1140 {
1141         float f= R.wrld.ao_indirect_energy;
1142
1143         if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1144                 return;
1145         if (f == 0.0f)
1146                 return;
1147
1148         shr->combined[0] += shi->indirect[0]*shi->r*shi->refl*f;
1149         shr->combined[1] += shi->indirect[1]*shi->g*shi->refl*f;
1150         shr->combined[2] += shi->indirect[2]*shi->b*shi->refl*f;
1151 }
1152
1153 /* result written in shadfac */
1154 void lamp_get_shadow(LampRen *lar, ShadeInput *shi, float inp, float shadfac[4], int do_real)
1155 {
1156         LampShadowSubSample *lss= &(lar->shadsamp[shi->thread].s[shi->sample]);
1157         
1158         if (do_real || lss->samplenr!=shi->samplenr) {
1159                 
1160                 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1161                 
1162                 if (lar->shb) {
1163                         if (lar->buftype==LA_SHADBUF_IRREGULAR)
1164                                 shadfac[3]= ISB_getshadow(shi, lar->shb);
1165                         else
1166                                 shadfac[3] = testshadowbuf(&R, lar->shb, shi->co, shi->dxco, shi->dyco, inp, shi->mat->lbias);
1167                 }
1168                 else if (lar->mode & LA_SHAD_RAY) {
1169                         ray_shadow(shi, lar, shadfac);
1170                 }
1171                 
1172                 if (shi->depth==0) {
1173                         copy_v4_v4(lss->shadfac, shadfac);
1174                         lss->samplenr= shi->samplenr;
1175                 }
1176         }
1177         else {
1178                 copy_v4_v4(shadfac, lss->shadfac);
1179         }
1180 }
1181
1182 /* lampdistance and spot angle, writes in lv and dist */
1183 float lamp_get_visibility(LampRen *lar, const float co[3], float lv[3], float *dist)
1184 {
1185         if (lar->type==LA_SUN || lar->type==LA_HEMI) {
1186                 *dist= 1.0f;
1187                 copy_v3_v3(lv, lar->vec);
1188                 return 1.0f;
1189         }
1190         else {
1191                 float visifac= 1.0f, t;
1192                 
1193                 sub_v3_v3v3(lv, co, lar->co);
1194                 *dist= sqrtf(dot_v3v3(lv, lv));
1195                 t= 1.0f/dist[0];
1196                 mul_v3_fl(lv, t);
1197                 
1198                 /* area type has no quad or sphere option */
1199                 if (lar->type==LA_AREA) {
1200                         /* area is single sided */
1201                         //if (dot_v3v3(lv, lar->vec) > 0.0f)
1202                         //      visifac= 1.0f;
1203                         //else
1204                         //      visifac= 0.0f;
1205                 }
1206                 else {
1207                         switch (lar->falloff_type) {
1208                                 case LA_FALLOFF_CONSTANT:
1209                                         visifac = 1.0f;
1210                                         break;
1211                                 case LA_FALLOFF_INVLINEAR:
1212                                         visifac = lar->dist/(lar->dist + dist[0]);
1213                                         break;
1214                                 case LA_FALLOFF_INVSQUARE:
1215                                         /* NOTE: This seems to be a hack since commit r12045 says this
1216                                          * option is similar to old Quad, but with slight changes.
1217                                          * Correct inv square would be (which would be old Quad):
1218                                          * visifac = lar->distkw / (lar->distkw + dist[0]*dist[0]);
1219                                          */
1220                                         visifac = lar->dist / (lar->dist + dist[0]*dist[0]);
1221                                         break;
1222                                 case LA_FALLOFF_SLIDERS:
1223                                         if (lar->ld1>0.0f)
1224                                                 visifac= lar->dist/(lar->dist+lar->ld1*dist[0]);
1225                                         if (lar->ld2>0.0f)
1226                                                 visifac*= lar->distkw/(lar->distkw+lar->ld2*dist[0]*dist[0]);
1227                                         break;
1228                                 case LA_FALLOFF_CURVE:
1229                                         /* curvemapping_initialize is called from #add_render_lamp */
1230                                         visifac = curvemapping_evaluateF(lar->curfalloff, 0, dist[0]/lar->dist);
1231                                         break;
1232                         }
1233                         
1234                         if (lar->mode & LA_SPHERE) {
1235                                 float t= lar->dist - dist[0];
1236                                 if (t<=0.0f) 
1237                                         visifac= 0.0f;
1238                                 else
1239                                         visifac*= t/lar->dist;
1240                         }
1241                         
1242                         if (visifac > 0.0f) {
1243                                 if (lar->type==LA_SPOT) {
1244                                         float inpr;
1245                                         
1246                                         if (lar->mode & LA_SQUARE) {
1247                                                 if (dot_v3v3(lv, lar->vec) > 0.0f) {
1248                                                         float lvrot[3], x;
1249                                                         
1250                                                         /* rotate view to lampspace */
1251                                                         copy_v3_v3(lvrot, lv);
1252                                                         mul_m3_v3(lar->imat, lvrot);
1253                                                         
1254                                                         x = max_ff(fabsf(lvrot[0]/lvrot[2]), fabsf(lvrot[1]/lvrot[2]));
1255                                                         /* 1.0f/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1256                                                         
1257                                                         inpr= 1.0f/(sqrt(1.0f+x*x));
1258                                                 }
1259                                                 else inpr= 0.0f;
1260                                         }
1261                                         else {
1262                                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1263                                         }
1264                                         
1265                                         t= lar->spotsi;
1266                                         if (inpr<=t) 
1267                                                 visifac= 0.0f;
1268                                         else {
1269                                                 t= inpr-t;
1270                                                 if (t<lar->spotbl && lar->spotbl!=0.0f) {
1271                                                         /* soft area */
1272                                                         float i= t/lar->spotbl;
1273                                                         t= i*i;
1274                                                         inpr*= (3.0f*t-2.0f*t*i);
1275                                                 }
1276                                                 visifac*= inpr;
1277                                         }
1278                                 }
1279                         }
1280                 }
1281                 if (visifac <= 0.001f) visifac = 0.0f;
1282                 return visifac;
1283         }
1284 }
1285
1286 /* function returns raw diff, spec and full shadowed diff in the 'shad' pass */
1287 static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int passflag)
1288 {
1289         Material *ma= shi->mat;
1290         VlakRen *vlr= shi->vlr;
1291         float lv[3], lampdist, lacol[3], shadfac[4], lashdw[3];
1292         float i, is, i_noshad, inp, *vn, *view, vnor[3], phongcorr=1.0f;
1293         float visifac;
1294         
1295         vn= shi->vn;
1296         view= shi->view;
1297         
1298         
1299         if (lar->energy == 0.0f) return;
1300         /* only shadow lamps shouldn't affect shadow-less materials at all */
1301         if ((lar->mode & LA_ONLYSHADOW) && (!(ma->mode & MA_SHADOW) || !(R.r.mode & R_SHADOW)))
1302                 return;
1303         /* optimization, don't render fully black lamps */
1304         if (!(lar->mode & LA_TEXTURE) && (lar->r + lar->g + lar->b == 0.0f))
1305                 return;
1306         
1307         /* lampdist, spot angle, area side, ... */
1308         visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1309         if (visifac==0.0f)
1310                 return;
1311         
1312         if (lar->type==LA_SPOT) {
1313                 if (lar->mode & LA_OSATEX) {
1314                         shi->osatex= 1; /* signal for multitex() */
1315                         
1316                         shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/lampdist;
1317                         shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/lampdist;
1318                         shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/lampdist;
1319                         
1320                         shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/lampdist;
1321                         shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/lampdist;
1322                         shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/lampdist;
1323                 }
1324         }
1325         
1326         /* lamp color texture */
1327         lacol[0]= lar->r;
1328         lacol[1]= lar->g;
1329         lacol[2]= lar->b;
1330         
1331         lashdw[0]= lar->shdwr;
1332         lashdw[1]= lar->shdwg;
1333         lashdw[2]= lar->shdwb;
1334         
1335         if (lar->mode & LA_TEXTURE)     do_lamp_tex(lar, lv, shi, lacol, LA_TEXTURE);
1336         if (lar->mode & LA_SHAD_TEX)    do_lamp_tex(lar, lv, shi, lashdw, LA_SHAD_TEX);
1337
1338                 /* tangent case; calculate fake face normal, aligned with lampvector */
1339                 /* note, vnor==vn is used as tangent trigger for buffer shadow */
1340         if (vlr->flag & R_TANGENT) {
1341                 float cross[3], nstrand[3], blend;
1342
1343                 if (ma->mode & MA_STR_SURFDIFF) {
1344                         cross_v3_v3v3(cross, shi->surfnor, vn);
1345                         cross_v3_v3v3(nstrand, vn, cross);
1346
1347                         blend= dot_v3v3(nstrand, shi->surfnor);
1348                         blend= 1.0f - blend;
1349                         CLAMP(blend, 0.0f, 1.0f);
1350
1351                         interp_v3_v3v3(vnor, nstrand, shi->surfnor, blend);
1352                         normalize_v3(vnor);
1353                 }
1354                 else {
1355                         cross_v3_v3v3(cross, lv, vn);
1356                         cross_v3_v3v3(vnor, cross, vn);
1357                         normalize_v3(vnor);
1358                 }
1359
1360                 if (ma->strand_surfnor > 0.0f) {
1361                         if (ma->strand_surfnor > shi->surfdist) {
1362                                 blend= (ma->strand_surfnor - shi->surfdist)/ma->strand_surfnor;
1363                                 interp_v3_v3v3(vnor, vnor, shi->surfnor, blend);
1364                                 normalize_v3(vnor);
1365                         }
1366                 }
1367
1368                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1369                 vn= vnor;
1370         }
1371         else if (ma->mode & MA_TANGENT_V) {
1372                 float cross[3];
1373                 cross_v3_v3v3(cross, lv, shi->tang);
1374                 cross_v3_v3v3(vnor, cross, shi->tang);
1375                 normalize_v3(vnor);
1376                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1377                 vn= vnor;
1378         }
1379         
1380         /* dot product and reflectivity */
1381         /* inp = dotproduct, is = shader result, i = lamp energy (with shadow), i_noshad = i without shadow */
1382         inp= dot_v3v3(vn, lv);
1383
1384         /* phong threshold to prevent backfacing faces having artifacts on ray shadow (terminator problem) */
1385         /* this complex construction screams for a nicer implementation! (ton) */
1386         if (R.r.mode & R_SHADOW) {
1387                 if (ma->mode & MA_SHADOW) {
1388                         if (lar->type == LA_HEMI || lar->type == LA_AREA) {
1389                                 /* pass */
1390                         }
1391                         else if ((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1392                                 float thresh= shi->obr->ob->smoothresh;
1393                                 if (inp>thresh)
1394                                         phongcorr= (inp-thresh)/(inp*(1.0f-thresh));
1395                                 else
1396                                         phongcorr= 0.0f;
1397                         }
1398                         else if (ma->sbias!=0.0f && ((lar->mode & LA_SHAD_RAY) || lar->shb)) {
1399                                 if (inp>ma->sbias)
1400                                         phongcorr= (inp-ma->sbias)/(inp*(1.0f-ma->sbias));
1401                                 else
1402                                         phongcorr= 0.0f;
1403                         }
1404                 }
1405         }
1406         
1407         /* diffuse shaders */
1408         if (lar->mode & LA_NO_DIFF) {
1409                 is = 0.0f;  /* skip shaders */
1410         }
1411         else if (lar->type==LA_HEMI) {
1412                 is = 0.5f * inp + 0.5f;
1413         }
1414         else {
1415                 
1416                 if (lar->type==LA_AREA)
1417                         inp= area_lamp_energy_multisample(lar, shi->co, vn);
1418                 
1419                 /* diffuse shaders (oren nayer gets inp from area light) */
1420                 if (ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1421                 else if (ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1422                 else if (ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1423                 else if (ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1424                 else is= inp;  /* Lambert */
1425         }
1426
1427         /* 'is' is diffuse */
1428         if ((ma->shade_flag & MA_CUBIC) && is > 0.0f && is < 1.0f) {
1429                 is= 3.0f * is * is - 2.0f * is * is * is;  /* nicer termination of shades */
1430         }
1431
1432         i= is*phongcorr;
1433         
1434         if (i>0.0f) {
1435                 i*= visifac*shi->refl;
1436         }
1437         i_noshad= i;
1438         
1439         vn = shi->vn;  /* bring back original vector, we use special specular shaders for tangent */
1440         if (ma->mode & MA_TANGENT_V)
1441                 vn= shi->tang;
1442         
1443         /* init transp shadow */
1444         shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1445         
1446         /* shadow and spec, (visifac==0 outside spot) */
1447         if (visifac> 0.0f) {
1448                 
1449                 if ((R.r.mode & R_SHADOW)) {
1450                         if (ma->mode & MA_SHADOW) {
1451                                 if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1452                                         
1453                                         if (vn==vnor)   /* tangent trigger */
1454                                                 lamp_get_shadow(lar, shi, dot_v3v3(shi->vn, lv), shadfac, shi->depth);
1455                                         else
1456                                                 lamp_get_shadow(lar, shi, inp, shadfac, shi->depth);
1457                                                 
1458                                         /* warning, here it skips the loop */
1459                                         if ((lar->mode & LA_ONLYSHADOW) && i>0.0f) {
1460                                                 
1461                                                 shadfac[3]= i*lar->energy*(1.0f-shadfac[3]);
1462                                                 shr->shad[0] -= shadfac[3]*shi->r*(1.0f-lashdw[0]);
1463                                                 shr->shad[1] -= shadfac[3]*shi->g*(1.0f-lashdw[1]);
1464                                                 shr->shad[2] -= shadfac[3]*shi->b*(1.0f-lashdw[2]);
1465                                                 
1466                                                 shr->spec[0] -= shadfac[3]*shi->specr*(1.0f-lashdw[0]);
1467                                                 shr->spec[1] -= shadfac[3]*shi->specg*(1.0f-lashdw[1]);
1468                                                 shr->spec[2] -= shadfac[3]*shi->specb*(1.0f-lashdw[2]);
1469                                                 
1470                                                 return;
1471                                         }
1472                                         
1473                                         i*= shadfac[3];
1474                                         shr->shad[3] = shadfac[3]; /* store this for possible check in troublesome cases */
1475                                 }
1476                         }
1477                 }
1478                 
1479                 /* in case 'no diffuse' we still do most calculus, spec can be in shadow.*/
1480                 if (!(lar->mode & LA_NO_DIFF)) {
1481                         if (i>0.0f) {
1482                                 if (ma->mode & MA_SHADOW_TRA)
1483                                         add_to_diffuse(shr->shad, shi, is, i*shadfac[0]*lacol[0], i*shadfac[1]*lacol[1], i*shadfac[2]*lacol[2]);
1484                                 else
1485                                         add_to_diffuse(shr->shad, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
1486                         }
1487                         /* add light for colored shadow */
1488                         if (i_noshad>i && !(lashdw[0]==0 && lashdw[1]==0 && lashdw[2]==0)) {
1489                                 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]);
1490                         }
1491                         if (i_noshad>0.0f) {
1492                                 if (passflag & (SCE_PASS_DIFFUSE|SCE_PASS_SHADOW)) {
1493                                         add_to_diffuse(shr->diff, shi, is, i_noshad*lacol[0], i_noshad*lacol[1], i_noshad*lacol[2]);
1494                                 }
1495                                 else
1496                                         copy_v3_v3(shr->diff, shr->shad);
1497                         }
1498                 }
1499                 
1500                 /* specularity */
1501                 shadfac[3]*= phongcorr; /* note, shadfac not allowed to be stored nonlocal */
1502                 
1503                 if (shadfac[3]>0.0f && shi->spec!=0.0f && !(lar->mode & LA_NO_SPEC) && !(lar->mode & LA_ONLYSHADOW)) {
1504                         
1505                         if (!(passflag & (SCE_PASS_COMBINED | SCE_PASS_SPEC))) {
1506                                 /* pass */
1507                         }
1508                         else if (lar->type == LA_HEMI) {
1509                                 float t;
1510                                 /* hemi uses no spec shaders (yet) */
1511                                 
1512                                 lv[0]+= view[0];
1513                                 lv[1]+= view[1];
1514                                 lv[2]+= view[2];
1515                                 
1516                                 normalize_v3(lv);
1517                                 
1518                                 t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1519                                 
1520                                 if (lar->type==LA_HEMI) {
1521                                         t= 0.5f*t+0.5f;
1522                                 }
1523                                 
1524                                 t= shadfac[3]*shi->spec*spec(t, shi->har);
1525                                 
1526                                 shr->spec[0]+= t*(lacol[0] * shi->specr);
1527                                 shr->spec[1]+= t*(lacol[1] * shi->specg);
1528                                 shr->spec[2]+= t*(lacol[2] * shi->specb);
1529                         }
1530                         else {
1531                                 /* specular shaders */
1532                                 float specfac, t;
1533                                 
1534                                 if (ma->spec_shader==MA_SPEC_PHONG) 
1535                                         specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1536                                 else if (ma->spec_shader==MA_SPEC_COOKTORR) 
1537                                         specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1538                                 else if (ma->spec_shader==MA_SPEC_BLINN) 
1539                                         specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1540                                 else if (ma->spec_shader==MA_SPEC_WARDISO)
1541                                         specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1542                                 else 
1543                                         specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1544                                 
1545                                 /* area lamp correction */
1546                                 if (lar->type==LA_AREA) specfac*= inp;
1547                                 
1548                                 t= shadfac[3]*shi->spec*visifac*specfac;
1549                                 
1550                                 if (ma->mode & MA_RAMP_SPEC) {
1551                                         float spec[3];
1552                                         do_specular_ramp(shi, specfac, t, spec);
1553                                         shr->spec[0]+= t*(lacol[0] * spec[0]);
1554                                         shr->spec[1]+= t*(lacol[1] * spec[1]);
1555                                         shr->spec[2]+= t*(lacol[2] * spec[2]);
1556                                 }
1557                                 else {
1558                                         shr->spec[0]+= t*(lacol[0] * shi->specr);
1559                                         shr->spec[1]+= t*(lacol[1] * shi->specg);
1560                                         shr->spec[2]+= t*(lacol[2] * shi->specb);
1561                                 }
1562                         }
1563                 }
1564         }
1565 }
1566
1567 static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr)
1568 {
1569         
1570         if (R.r.mode & R_SHADOW) {
1571                 ListBase *lights;
1572                 LampRen *lar;
1573                 GroupObject *go;
1574                 float inpr, lv[3];
1575                 float /* *view, */ shadfac[4];
1576                 float ir, accum, visifac, lampdist;
1577                 float shaded = 0.0f, lightness = 0.0f;
1578                 
1579
1580                 /* view= shi->view; */ /* UNUSED */
1581                 accum= ir= 0.0f;
1582                 
1583                 lights= get_lights(shi);
1584                 for (go=lights->first; go; go= go->next) {
1585                         lar= go->lampren;
1586                         if (lar==NULL) continue;
1587                         
1588                         /* yafray: ignore shading by photonlights, not used in Blender */
1589                         if (lar->type==LA_YF_PHOTON) continue;
1590                         
1591                         if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1592                         if ((lar->lay & shi->lay)==0) continue;
1593                         
1594                         if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1595                                 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1596                                 ir+= 1.0f;
1597
1598                                 if (visifac <= 0.0f) {
1599                                         if (shi->mat->shadowonly_flag == MA_SO_OLD)
1600                                                 accum+= 1.0f;
1601
1602                                         continue;
1603                                 }
1604                                 inpr= dot_v3v3(shi->vn, lv);
1605                                 if (inpr <= 0.0f) {
1606                                         if (shi->mat->shadowonly_flag == MA_SO_OLD)
1607                                                 accum+= 1.0f;
1608
1609                                         continue;
1610                                 }
1611
1612                                 lamp_get_shadow(lar, shi, inpr, shadfac, shi->depth);
1613
1614                                 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1615                                         /* Old "Shadows Only" */
1616                                         accum+= (1.0f-visifac) + (visifac)*rgb_to_grayscale(shadfac)*shadfac[3];
1617                                 }
1618                                 else {
1619                                         shaded += rgb_to_grayscale(shadfac)*shadfac[3] * visifac * lar->energy;
1620
1621                                         if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1622                                                 lightness += visifac * lar->energy;
1623                                         }
1624                                 }
1625                         }
1626                 }
1627
1628                 /* Apply shadows as alpha */
1629                 if (ir>0.0f) {
1630                         if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1631                                 accum = 1.0f - accum/ir;
1632                         }
1633                         else {
1634                                 if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1635                                         if (lightness > 0.0f) {
1636                                                 /* Get shadow value from between 0.0f and non-shadowed lightness */
1637                                                 accum = (lightness - shaded) / (lightness);
1638                                         }
1639                                         else {
1640                                                 accum = 0.0f;
1641                                         }
1642                                 }
1643                                 else { /* shadowonly_flag == MA_SO_SHADED */
1644                                         /* Use shaded value */
1645                                         accum = 1.0f - shaded;
1646                         }}
1647
1648                         shr->alpha= (shi->alpha)*(accum);
1649                         if (shr->alpha<0.0f) shr->alpha=0.0f;
1650                 }
1651                 else {
1652                         /* If "fully shaded", use full alpha even on areas that have no lights */
1653                         if (shi->mat->shadowonly_flag == MA_SO_SHADED) shr->alpha=shi->alpha;
1654                         else shr->alpha= 0.f;
1655                 }
1656         }
1657         
1658         /* quite disputable this...  also note it doesn't mirror-raytrace */
1659         if ((R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT)) && shi->amb!=0.0f) {
1660                 float f;
1661                 
1662                 if (R.wrld.mode & WO_AMB_OCC) {
1663                         f= R.wrld.aoenergy*shi->amb;
1664                         
1665                         if (R.wrld.aomix==WO_AOADD) {
1666                                 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1667                                         f= f*(1.0f - rgb_to_grayscale(shi->ao));
1668                                         shr->alpha= (shr->alpha + f)*f;
1669                                 }
1670                                 else {
1671                                         shr->alpha -= f*rgb_to_grayscale(shi->ao);
1672                                         if (shr->alpha<0.0f) shr->alpha=0.0f;
1673                                 }
1674                         }
1675                         else /* AO Multiply */
1676                                 shr->alpha= (1.0f - f)*shr->alpha + f*(1.0f - (1.0f - shr->alpha)*rgb_to_grayscale(shi->ao));
1677                 }
1678
1679                 if (R.wrld.mode & WO_ENV_LIGHT) {
1680                         if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1681                                 f= R.wrld.ao_env_energy*shi->amb*(1.0f - rgb_to_grayscale(shi->env));
1682                                 shr->alpha= (shr->alpha + f)*f;
1683                         }
1684                         else {
1685                                 f= R.wrld.ao_env_energy*shi->amb;
1686                                 shr->alpha -= f*rgb_to_grayscale(shi->env);
1687                                 if (shr->alpha<0.0f) shr->alpha=0.0f;
1688                         }
1689                 }
1690         }
1691 }
1692
1693 /* let's map negative light as if it mirrors positive light, otherwise negative values disappear */
1694 static void wrld_exposure_correct(float diff[3])
1695 {
1696         
1697         diff[0]= R.wrld.linfac*(1.0f-expf( diff[0]*R.wrld.logfac) );
1698         diff[1]= R.wrld.linfac*(1.0f-expf( diff[1]*R.wrld.logfac) );
1699         diff[2]= R.wrld.linfac*(1.0f-expf( diff[2]*R.wrld.logfac) );
1700 }
1701
1702 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1703 {
1704         Material *ma= shi->mat;
1705         int passflag= shi->passflag;
1706         
1707         memset(shr, 0, sizeof(ShadeResult));
1708         
1709         if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1710         
1711         /* separate loop */
1712         if (ma->mode & MA_ONLYSHADOW) {
1713                 shade_lamp_loop_only_shadow(shi, shr);
1714                 return;
1715         }
1716         
1717         /* envmap hack, always reset */
1718         shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0f;
1719         
1720         /* material color itself */
1721         if (passflag & (SCE_PASS_COMBINED|SCE_PASS_RGBA)) {
1722                 if (ma->mode & (MA_FACETEXTURE)) {
1723                         shi->r= shi->vcol[0];
1724                         shi->g= shi->vcol[1];
1725                         shi->b= shi->vcol[2];
1726                         if (ma->mode & (MA_FACETEXTURE_ALPHA))
1727                                 shi->alpha= shi->vcol[3];
1728                 }
1729 #ifdef WITH_FREESTYLE
1730                 else if (ma->vcol_alpha) {
1731                         shi->r= shi->vcol[0];
1732                         shi->g= shi->vcol[1];
1733                         shi->b= shi->vcol[2];
1734                         shi->alpha= shi->vcol[3];
1735                 }
1736 #endif
1737                 else if (ma->mode & (MA_VERTEXCOLP)) {
1738                         float neg_alpha = 1.0f - shi->vcol[3];
1739                         shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
1740                         shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
1741                         shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
1742                 }
1743                 if (ma->texco) {
1744                         do_material_tex(shi, &R);
1745                         if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1746                 }
1747                 
1748                 shr->col[0]= shi->r*shi->alpha;
1749                 shr->col[1]= shi->g*shi->alpha;
1750                 shr->col[2]= shi->b*shi->alpha;
1751                 shr->col[3]= shi->alpha;
1752
1753                 if ((ma->sss_flag & MA_DIFF_SSS) && !sss_pass_done(&R, ma)) {
1754                         if (ma->sss_texfac == 0.0f) {
1755                                 shi->r= shi->g= shi->b= shi->alpha= 1.0f;
1756                                 shr->col[0]= shr->col[1]= shr->col[2]= shr->col[3]= 1.0f;
1757                         }
1758                         else {
1759                                 shi->r= pow(max_ff(shi->r, 0.0f), ma->sss_texfac);
1760                                 shi->g= pow(max_ff(shi->g, 0.0f), ma->sss_texfac);
1761                                 shi->b= pow(max_ff(shi->b, 0.0f), ma->sss_texfac);
1762                                 shi->alpha= pow(max_ff(shi->alpha, 0.0f), ma->sss_texfac);
1763                                 
1764                                 shr->col[0]= pow(max_ff(shr->col[0], 0.0f), ma->sss_texfac);
1765                                 shr->col[1]= pow(max_ff(shr->col[1], 0.0f), ma->sss_texfac);
1766                                 shr->col[2]= pow(max_ff(shr->col[2], 0.0f), ma->sss_texfac);
1767                                 shr->col[3]= pow(max_ff(shr->col[3], 0.0f), ma->sss_texfac);
1768                         }
1769                 }
1770         }
1771         
1772         if (ma->mode & MA_SHLESS) {
1773                 shr->combined[0]= shi->r;
1774                 shr->combined[1]= shi->g;
1775                 shr->combined[2]= shi->b;
1776                 shr->alpha= shi->alpha;
1777                 return;
1778         }
1779
1780         if ( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) {       /* vertexcolor light */
1781                 shr->emit[0]= shi->r*(shi->emit+shi->vcol[0]*shi->vcol[3]);
1782                 shr->emit[1]= shi->g*(shi->emit+shi->vcol[1]*shi->vcol[3]);
1783                 shr->emit[2]= shi->b*(shi->emit+shi->vcol[2]*shi->vcol[3]);
1784         }
1785         else {
1786                 shr->emit[0]= shi->r*shi->emit;
1787                 shr->emit[1]= shi->g*shi->emit;
1788                 shr->emit[2]= shi->b*shi->emit;
1789         }
1790         
1791         /* AO pass */
1792         if (R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) {
1793                 if (((passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT))) ||
1794                     (passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1795                 {
1796                         if (R.r.mode & R_SHADOW) {
1797                                 /* AO was calculated for scanline already */
1798                                 if (shi->depth || shi->volume_depth)
1799                                         ambient_occlusion(shi);
1800                                 copy_v3_v3(shr->ao, shi->ao);
1801                                 copy_v3_v3(shr->env, shi->env); /* XXX multiply */
1802                                 copy_v3_v3(shr->indirect, shi->indirect); /* XXX multiply */
1803                         }
1804                 }
1805         }
1806         
1807         /* lighting pass */
1808         if (passflag & (SCE_PASS_COMBINED|SCE_PASS_DIFFUSE|SCE_PASS_SPEC|SCE_PASS_SHADOW)) {
1809                 GroupObject *go;
1810                 ListBase *lights;
1811                 LampRen *lar;
1812                 
1813                 lights= get_lights(shi);
1814                 for (go=lights->first; go; go= go->next) {
1815                         lar= go->lampren;
1816                         if (lar==NULL) continue;
1817                         
1818                         /* yafray: ignore shading by photonlights, not used in Blender */
1819                         if (lar->type==LA_YF_PHOTON) continue;
1820                         
1821                         /* test for lamp layer */
1822                         if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1823                         if ((lar->lay & shi->lay)==0) continue;
1824                         
1825                         /* accumulates in shr->diff and shr->spec and shr->shad (diffuse with shadow!) */
1826                         shade_one_light(lar, shi, shr, passflag);
1827                 }
1828
1829                 /* this check is to prevent only shadow lamps from producing negative
1830                  * colors.*/
1831                 if (shr->spec[0] < 0) shr->spec[0] = 0;
1832                 if (shr->spec[1] < 0) shr->spec[1] = 0;
1833                 if (shr->spec[2] < 0) shr->spec[2] = 0;
1834
1835                 if (shr->shad[0] < 0) shr->shad[0] = 0;
1836                 if (shr->shad[1] < 0) shr->shad[1] = 0;
1837                 if (shr->shad[2] < 0) shr->shad[2] = 0;
1838                                                 
1839                 if (ma->sss_flag & MA_DIFF_SSS) {
1840                         float sss[3], col[3], invalpha, texfac= ma->sss_texfac;
1841
1842                         /* this will return false in the preprocess stage */
1843                         if (sample_sss(&R, ma, shi->co, sss)) {
1844                                 invalpha= (shr->col[3] > FLT_EPSILON)? 1.0f/shr->col[3]: 1.0f;
1845
1846                                 if (texfac==0.0f) {
1847                                         copy_v3_v3(col, shr->col);
1848                                         mul_v3_fl(col, invalpha);
1849                                 }
1850                                 else if (texfac==1.0f) {
1851                                         col[0]= col[1]= col[2]= 1.0f;
1852                                         mul_v3_fl(col, invalpha);
1853                                 }
1854                                 else {
1855                                         copy_v3_v3(col, shr->col);
1856                                         mul_v3_fl(col, invalpha);
1857                                         col[0]= pow(max_ff(col[0], 0.0f), 1.0f-texfac);
1858                                         col[1]= pow(max_ff(col[1], 0.0f), 1.0f-texfac);
1859                                         col[2]= pow(max_ff(col[2], 0.0f), 1.0f-texfac);
1860                                 }
1861
1862                                 shr->diff[0]= sss[0]*col[0];
1863                                 shr->diff[1]= sss[1]*col[1];
1864                                 shr->diff[2]= sss[2]*col[2];
1865
1866                                 if (shi->combinedflag & SCE_PASS_SHADOW) {
1867                                         shr->shad[0]= shr->diff[0];
1868                                         shr->shad[1]= shr->diff[1];
1869                                         shr->shad[2]= shr->diff[2];
1870                                 }
1871                         }
1872                 }
1873                 
1874                 if (shi->combinedflag & SCE_PASS_SHADOW)
1875                         copy_v3_v3(shr->combined, shr->shad);   /* note, no ';' ! */
1876                 else
1877                         copy_v3_v3(shr->combined, shr->diff);
1878                         
1879                 /* calculate shadow pass, we use a multiplication mask */
1880                 /* if diff = 0,0,0 it doesn't matter what the shadow pass is, so leave it as is */
1881                 if (passflag & SCE_PASS_SHADOW && !(shr->diff[0]==0.0f && shr->diff[1]==0.0f && shr->diff[2]==0.0f)) {
1882                         if (shr->diff[0]!=0.0f) shr->shad[0]= shr->shad[0]/shr->diff[0];
1883                         /* can't determine proper shadow from shad/diff (0/0), so use shadow intensity */
1884                         else if (shr->shad[0]==0.0f) shr->shad[0]= shr->shad[3];
1885
1886                         if (shr->diff[1]!=0.0f) shr->shad[1]= shr->shad[1]/shr->diff[1];
1887                         else if (shr->shad[1]==0.0f) shr->shad[1]= shr->shad[3];
1888
1889                         if (shr->diff[2]!=0.0f) shr->shad[2]= shr->shad[2]/shr->diff[2];
1890                         else if (shr->shad[2]==0.0f) shr->shad[2]= shr->shad[3];
1891                 }
1892                 
1893                 /* exposure correction */
1894                 if ((R.wrld.exp!=0.0f || R.wrld.range!=1.0f) && !R.sss_points) {
1895                         wrld_exposure_correct(shr->combined);   /* has no spec! */
1896                         wrld_exposure_correct(shr->spec);
1897                 }
1898         }
1899         
1900         /* alpha in end, spec can influence it */
1901         if (passflag & (SCE_PASS_COMBINED)) {
1902                 if ((ma->fresnel_tra!=0.0f) && (shi->mode & MA_TRANSP))
1903                         shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1904                         
1905                 /* note: shi->mode! */
1906                 if (shi->mode & MA_TRANSP && (shi->mode & (MA_ZTRANSP|MA_RAYTRANSP))) {
1907                         if (shi->spectra!=0.0f) {
1908                                 float t = max_fff(shr->spec[0], shr->spec[1], shr->spec[2]);
1909                                 t *= shi->spectra;
1910                                 if (t>1.0f) t= 1.0f;
1911                                 shi->alpha= (1.0f-t)*shi->alpha+t;
1912                         }
1913                 }
1914         }
1915         shr->alpha= shi->alpha;
1916         
1917         /* from now stuff everything in shr->combined: ambient, AO, ramps, exposure */
1918         if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1919                 if (R.r.mode & R_SHADOW) {
1920                         /* add AO in combined? */
1921                         if (R.wrld.mode & WO_AMB_OCC)
1922                                 if (shi->combinedflag & SCE_PASS_AO)
1923                                         ambient_occlusion_apply(shi, shr);
1924
1925                         if (R.wrld.mode & WO_ENV_LIGHT)
1926                                 if (shi->combinedflag & SCE_PASS_ENVIRONMENT)
1927                                         environment_lighting_apply(shi, shr);
1928
1929                         if (R.wrld.mode & WO_INDIRECT_LIGHT)
1930                                 if (shi->combinedflag & SCE_PASS_INDIRECT)
1931                                         indirect_lighting_apply(shi, shr);
1932                 }
1933                 
1934                 shr->combined[0]+= shi->ambr;
1935                 shr->combined[1]+= shi->ambg;
1936                 shr->combined[2]+= shi->ambb;
1937                 
1938                 if (ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->combined, shi);
1939         }
1940
1941         if (ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shi);
1942         
1943         /* refcol is for envmap only */
1944         if (shi->refcol[0]!=0.0f) {
1945                 float result[3];
1946                 
1947                 result[0]= shi->mirr*shi->refcol[1] + (1.0f - shi->mirr*shi->refcol[0])*shr->combined[0];
1948                 result[1]= shi->mirg*shi->refcol[2] + (1.0f - shi->mirg*shi->refcol[0])*shr->combined[1];
1949                 result[2]= shi->mirb*shi->refcol[3] + (1.0f - shi->mirb*shi->refcol[0])*shr->combined[2];
1950                 
1951                 if (passflag & SCE_PASS_REFLECT)
1952                         sub_v3_v3v3(shr->refl, result, shr->combined);
1953                 
1954                 if (shi->combinedflag & SCE_PASS_REFLECT)
1955                         copy_v3_v3(shr->combined, result);
1956                         
1957         }
1958         
1959         /* and add emit and spec */
1960         if (shi->combinedflag & SCE_PASS_EMIT)
1961                 add_v3_v3(shr->combined, shr->emit);
1962         if (shi->combinedflag & SCE_PASS_SPEC)
1963                 add_v3_v3(shr->combined, shr->spec);
1964         
1965         /* modulate by the object color */
1966         if ((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
1967                 if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1968                         float obcol[4];
1969
1970                         copy_v4_v4(obcol, shi->obr->ob->col);
1971                         CLAMP(obcol[3], 0.0f, 1.0f);
1972
1973                         shr->combined[0] *= obcol[0];
1974                         shr->combined[1] *= obcol[1];
1975                         shr->combined[2] *= obcol[2];
1976                         if (shi->mode & MA_TRANSP) shr->alpha *= obcol[3];
1977                 }
1978         }
1979
1980         shr->combined[3]= shr->alpha;
1981 }
1982