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