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