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