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