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