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