24eabffdf6ff3b7250531554b79ff62051f65643
[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         }
833         
834         if(ma->texco)
835                 do_material_tex(shi);
836
837         if(ma->fresnel_tra!=0.0f) 
838                 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
839
840         shr->diff[0]= shi->r;
841         shr->diff[1]= shi->g;
842         shr->diff[2]= shi->b;
843         shr->alpha= shi->alpha;
844 }
845
846 /* ramp for at end of shade */
847 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
848 {
849         Material *ma= shi->mat;
850         float col[4], fac=0;
851
852         if(ma->ramp_col) {
853                 if(ma->rampin_col==MA_RAMP_IN_RESULT) {
854                         
855                         fac= 0.3*diff[0] + 0.58*diff[1] + 0.12*diff[2];
856                         do_colorband(ma->ramp_col, fac, col);
857                         
858                         /* blending method */
859                         fac= col[3]*ma->rampfac_col;
860                         
861                         ramp_blend(ma->rampblend_col, diff, diff+1, diff+2, fac, col);
862                 }
863         }
864 }
865
866 /* r,g,b denote energy, ramp is used with different values to make new material color */
867 static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, float g, float b)
868 {
869         Material *ma= shi->mat;
870         float col[4], colt[3], fac=0;
871         
872         if(ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
873                 
874                 /* MA_RAMP_IN_RESULT is exceptional */
875                 if(ma->rampin_col==MA_RAMP_IN_RESULT) {
876                         // normal add
877                         diff[0] += r * shi->r;
878                         diff[1] += g * shi->g;
879                         diff[2] += b * shi->b;
880                 }
881                 else {
882                         /* input */
883                         switch(ma->rampin_col) {
884                         case MA_RAMP_IN_ENERGY:
885                                 fac= 0.3*r + 0.58*g + 0.12*b;
886                                 break;
887                         case MA_RAMP_IN_SHADER:
888                                 fac= is;
889                                 break;
890                         case MA_RAMP_IN_NOR:
891                                 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
892                                 break;
893                         }
894         
895                         do_colorband(ma->ramp_col, fac, col);
896                         
897                         /* blending method */
898                         fac= col[3]*ma->rampfac_col;
899                         colt[0]= shi->r;
900                         colt[1]= shi->g;
901                         colt[2]= shi->b;
902
903                         ramp_blend(ma->rampblend_col, colt, colt+1, colt+2, fac, col);
904
905                         /* output to */
906                         diff[0] += r * colt[0];
907                         diff[1] += g * colt[1];
908                         diff[2] += b * colt[2];
909                 }
910         }
911         else {
912                 diff[0] += r * shi->r;
913                 diff[1] += g * shi->g;
914                 diff[2] += b * shi->b;
915         }
916 }
917
918 static void ramp_spec_result(float *specr, float *specg, float *specb, ShadeInput *shi)
919 {
920         Material *ma= shi->mat;
921         float col[4];
922         float fac;
923         
924         if(ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
925                 fac= 0.3*(*specr) + 0.58*(*specg) + 0.12*(*specb);
926                 do_colorband(ma->ramp_spec, fac, col);
927                 
928                 /* blending method */
929                 fac= col[3]*ma->rampfac_spec;
930                 
931                 ramp_blend(ma->rampblend_spec, specr, specg, specb, fac, col);
932                 
933         }
934 }
935
936 /* is = dot product shade, t = spec energy */
937 static void do_specular_ramp(ShadeInput *shi, float is, float t, float *spec)
938 {
939         Material *ma= shi->mat;
940         float col[4];
941         float fac=0.0f;
942         
943         spec[0]= shi->specr;
944         spec[1]= shi->specg;
945         spec[2]= shi->specb;
946
947         /* MA_RAMP_IN_RESULT is exception */
948         if(ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
949                 
950                 /* input */
951                 switch(ma->rampin_spec) {
952                 case MA_RAMP_IN_ENERGY:
953                         fac= t;
954                         break;
955                 case MA_RAMP_IN_SHADER:
956                         fac= is;
957                         break;
958                 case MA_RAMP_IN_NOR:
959                         fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
960                         break;
961                 }
962                 
963                 do_colorband(ma->ramp_spec, fac, col);
964                 
965                 /* blending method */
966                 fac= col[3]*ma->rampfac_spec;
967                 
968                 ramp_blend(ma->rampblend_spec, spec, spec+1, spec+2, fac, col);
969         }
970 }
971
972 /* pure AO, check for raytrace and world should have been done */
973 void ambient_occlusion(ShadeInput *shi)
974 {
975         
976         if((R.r.mode & R_RAYTRACE) && shi->mat->amb!=0.0f)
977                 ray_ao(shi, shi->ao);
978         else
979                 shi->ao[0]= shi->ao[1]= shi->ao[2]= 1.0f;
980 }
981
982
983 /* wrld mode was checked for */
984 void ambient_occlusion_to_diffuse(ShadeInput *shi, float *diff)
985 {
986         
987         if((R.r.mode & R_RAYTRACE) && shi->mat->amb!=0.0f) {
988                 float f= R.wrld.aoenergy*shi->mat->amb;
989
990                 if (R.wrld.aomix==WO_AOADDSUB) {
991                         diff[0] = 2.0f*shi->ao[0]-1.0f;
992                         diff[1] = 2.0f*shi->ao[1]-1.0f;
993                         diff[2] = 2.0f*shi->ao[2]-1.0f;
994                 }
995                 else if (R.wrld.aomix==WO_AOSUB) {
996                         diff[0] = shi->ao[0]-1.0f;
997                         diff[1] = shi->ao[1]-1.0f;
998                         diff[2] = shi->ao[2]-1.0f;
999                 }
1000                 else {
1001                         VECCOPY(diff, shi->ao);
1002                 }
1003                 
1004                 VECMUL(diff, f);
1005         }
1006         else
1007                 diff[0]= diff[1]= diff[2]= 0.0f;
1008 }
1009
1010 /* result written in shadfac */
1011 void lamp_get_shadow(LampRen *lar, ShadeInput *shi, float inp, float *shadfac, int do_real)
1012 {
1013         LampShadowSubSample *lss= &(lar->shadsamp[shi->thread].s[shi->sample]);
1014         
1015         if(do_real || lss->samplenr!=shi->samplenr) {
1016                 
1017                 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1018                 
1019                 if(lar->shb) {
1020                         if(lar->buftype==LA_SHADBUF_IRREGULAR)
1021                                 shadfac[3]= ISB_getshadow(shi, lar->shb);
1022                         else
1023                                 shadfac[3] = testshadowbuf(lar->shb, shi->co, shi->dxco, shi->dyco, inp);
1024                 }
1025                 else if(lar->mode & LA_SHAD_RAY) {
1026                         ray_shadow(shi, lar, shadfac);
1027                 }
1028                 
1029                 if(shi->depth==0) {
1030                         QUATCOPY(lss->shadfac, shadfac);
1031                         lss->samplenr= shi->samplenr;
1032                 }
1033         }
1034         else {
1035                 QUATCOPY(shadfac, lss->shadfac);
1036         }
1037 }
1038
1039 /* lampdistance and spot angle, writes in lv and dist */
1040 float lamp_get_visibility(LampRen *lar, float *co, float *lv, float *dist)
1041 {
1042         if(lar->type==LA_SUN || lar->type==LA_HEMI) {
1043                 *dist= 1.0f;
1044                 VECCOPY(lv, lar->vec);
1045                 return 1.0f;
1046         }
1047         else {
1048                 float visifac= 1.0f, t;
1049                 
1050                 VECSUB(lv, co, lar->co);
1051                 *dist= sqrt( INPR(lv, lv));
1052                 t= 1.0f/dist[0];
1053                 VECMUL(lv, t);
1054                 
1055                 /* area type has no quad or sphere option */
1056                 if(lar->type==LA_AREA) {
1057                         /* area is single sided */
1058                         if(INPR(lv, lar->vec) > 0.0f)
1059                                 visifac= 1.0f;
1060                         else
1061                                 visifac= 0.0f;
1062                 }
1063                 else {
1064                         switch(lar->falloff_type)
1065                         {
1066                                 case LA_FALLOFF_CONSTANT:
1067                                         visifac = 1.0f;
1068                                         break;
1069                                 case LA_FALLOFF_INVLINEAR:
1070                                         visifac = lar->dist/(lar->dist + dist[0]);
1071                                         break;
1072                                 case LA_FALLOFF_INVSQUARE:
1073                                         visifac = lar->dist / (lar->dist + dist[0]*dist[0]);
1074                                         break;
1075                                 case LA_FALLOFF_SLIDERS:
1076                                         if(lar->ld1>0.0f)
1077                                                 visifac= lar->dist/(lar->dist+lar->ld1*dist[0]);
1078                                         if(lar->ld2>0.0f)
1079                                                 visifac*= lar->distkw/(lar->distkw+lar->ld2*dist[0]*dist[0]);
1080                                         break;
1081                                 case LA_FALLOFF_CURVE:
1082                                         visifac = curvemapping_evaluateF(lar->curfalloff, 0, dist[0]/lar->dist);
1083                                         break;
1084                         }
1085                         
1086                         if(lar->mode & LA_SPHERE) {
1087                                 float t= lar->dist - dist[0];
1088                                 if(t<=0.0f) 
1089                                         visifac= 0.0f;
1090                                 else
1091                                         visifac*= t/lar->dist;
1092                         }
1093                         
1094                         if(visifac > 0.0f) {
1095                                 if(lar->type==LA_SPOT) {
1096                                         float inpr;
1097                                         
1098                                         if(lar->mode & LA_SQUARE) {
1099                                                 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0f) {
1100                                                         float lvrot[3], x;
1101                                                         
1102                                                         /* rotate view to lampspace */
1103                                                         VECCOPY(lvrot, lv);
1104                                                         MTC_Mat3MulVecfl(lar->imat, lvrot);
1105                                                         
1106                                                         x= MAX2(fabs(lvrot[0]/lvrot[2]) , fabs(lvrot[1]/lvrot[2]));
1107                                                         /* 1.0f/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1108                                                         
1109                                                         inpr= 1.0f/(sqrt(1.0f+x*x));
1110                                                 }
1111                                                 else inpr= 0.0f;
1112                                         }
1113                                         else {
1114                                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1115                                         }
1116                                         
1117                                         t= lar->spotsi;
1118                                         if(inpr<=t) 
1119                                                 visifac= 0.0f;
1120                                         else {
1121                                                 t= inpr-t;
1122                                                 if(t<lar->spotbl && lar->spotbl!=0.0f) {
1123                                                         /* soft area */
1124                                                         float i= t/lar->spotbl;
1125                                                         t= i*i;
1126                                                         inpr*= (3.0f*t-2.0f*t*i);
1127                                                 }
1128                                                 visifac*= inpr;
1129                                         }
1130                                 }
1131                         }
1132                 }
1133                 if (visifac <= 0.001) visifac = 0.0f;
1134                 return visifac;
1135         }
1136 }
1137
1138 /* function returns raw diff, spec and full shadowed diff in the 'shad' pass */
1139 static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int passflag)
1140 {
1141         Material *ma= shi->mat;
1142         VlakRen *vlr= shi->vlr;
1143         float lv[3], lampdist, lacol[3], shadfac[4];
1144         float i, is, i_noshad, inp, *vn, *view, vnor[3], phongcorr=1.0f;
1145         float visifac;
1146         
1147         vn= shi->vn;
1148         view= shi->view;
1149         
1150         if (lar->energy == 0.0) return;
1151         
1152         /* lampdist, spot angle, area side, ... */
1153         visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1154         if(visifac==0.0f)
1155                 return;
1156         
1157         if(lar->type==LA_SPOT) {
1158                 if(lar->mode & LA_OSATEX) {
1159                         shi->osatex= 1; /* signal for multitex() */
1160                         
1161                         shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/lampdist;
1162                         shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/lampdist;
1163                         shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/lampdist;
1164                         
1165                         shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/lampdist;
1166                         shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/lampdist;
1167                         shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/lampdist;
1168                 }
1169         }
1170         
1171         /* lamp color texture */
1172         lacol[0]= lar->r;
1173         lacol[1]= lar->g;
1174         lacol[2]= lar->b;
1175         
1176         if(lar->mode & LA_TEXTURE)  do_lamp_tex(lar, lv, shi, lacol);
1177         
1178         /* tangent case; calculate fake face normal, aligned with lampvector */ 
1179         /* note, vnor==vn is used as tangent trigger for buffer shadow */
1180         if(vlr->flag & R_TANGENT) {
1181                 float cross[3];
1182                 Crossf(cross, lv, vn);
1183                 Crossf(vnor, cross, vn);
1184                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1185                 vn= vnor;
1186         }
1187         else if (ma->mode & MA_TANGENT_V) {
1188                 float cross[3];
1189                 Crossf(cross, lv, shi->tang);
1190                 Crossf(vnor, cross, shi->tang);
1191                 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1192                 vn= vnor;
1193         }
1194         
1195         /* dot product and reflectivity */
1196         /* inp = dotproduct, is = shader result, i = lamp energy (with shadow), i_noshad = i without shadow */
1197         inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
1198         
1199         /* phong threshold to prevent backfacing faces having artefacts on ray shadow (terminator problem) */
1200         /* this complex construction screams for a nicer implementation! (ton) */
1201         if(R.r.mode & R_SHADOW) {
1202                 if(ma->mode & MA_SHADOW) {
1203                         if(lar->type==LA_HEMI);
1204                         else if((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1205                                 float thresh= vlr->ob->smoothresh;
1206                                 if(inp>thresh)
1207                                         phongcorr= (inp-thresh)/(inp*(1.0f-thresh));
1208                                 else
1209                                         phongcorr= 0.0f;
1210                         }
1211                         else if(ma->sbias!=0.0f && ((lar->mode & LA_SHAD_RAY) || lar->shb)) {
1212                                 if(inp>ma->sbias)
1213                                         phongcorr= (inp-ma->sbias)/(inp*(1.0f-ma->sbias));
1214                                 else
1215                                         phongcorr= 0.0f;
1216                         }
1217                 }
1218         }
1219         
1220         /* diffuse shaders */
1221         if(lar->mode & LA_NO_DIFF) {
1222                 is= 0.0f;       // skip shaders
1223         }
1224         else if(lar->type==LA_HEMI) {
1225                 is= 0.5f*inp + 0.5f;
1226         }
1227         else {
1228                 
1229                 if(lar->type==LA_AREA)
1230                         inp= area_lamp_energy(shi->co, vn, lar);
1231                 
1232                 /* diffuse shaders (oren nayer gets inp from area light) */
1233                 if(ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1234                 else if(ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1235                 else if(ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1236                 else if(ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1237                 else is= inp;   // Lambert
1238         }
1239         
1240         /* i is diffuse */
1241         i= is*phongcorr;
1242         
1243         if(i>0.0f) {
1244                 i*= visifac*shi->refl;
1245         }
1246         i_noshad= i;
1247         
1248         vn= shi->vn;    // bring back original vector, we use special specular shaders for tangent
1249         if(ma->mode & MA_TANGENT_V)
1250                 vn= shi->tang;
1251         
1252         /* init transp shadow */
1253         shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1254         
1255         /* shadow and spec, (visifac==0 outside spot) */
1256         if(visifac> 0.0f) {
1257                 
1258                 if((R.r.mode & R_SHADOW)) {
1259                         if(ma->mode & MA_SHADOW) {
1260                                 if(lar->shb || (lar->mode & LA_SHAD_RAY)) {
1261                                         
1262                                         if(vn==vnor)    /* tangent trigger */
1263                                                 lamp_get_shadow(lar, shi, INPR(shi->vn, lv), shadfac, shi->depth);
1264                                         else
1265                                                 lamp_get_shadow(lar, shi, inp, shadfac, shi->depth);
1266                                                 
1267                                         /* warning, here it skips the loop */
1268                                         if((lar->mode & LA_ONLYSHADOW) && i>0.0) {
1269                                                 
1270                                                 shadfac[3]= i*lar->energy*(1.0f-shadfac[3]);
1271                                                 shr->shad[0] -= shadfac[3]*shi->r;
1272                                                 shr->shad[1] -= shadfac[3]*shi->g;
1273                                                 shr->shad[2] -= shadfac[3]*shi->b;
1274                                                 
1275                                                 shr->spec[0] -= shadfac[3]*shi->specr;
1276                                                 shr->spec[1] -= shadfac[3]*shi->specg;
1277                                                 shr->spec[2] -= shadfac[3]*shi->specb;
1278                                                 
1279                                                 return;
1280                                         }
1281                                         
1282                                         i*= shadfac[3];
1283                                 }
1284                         }
1285                 }
1286                 
1287                 /* in case 'no diffuse' we still do most calculus, spec can be in shadow.*/
1288                 if(!(lar->mode & LA_NO_DIFF)) {
1289                         if(i>0.0f) {
1290                                 if(ma->mode & MA_SHADOW_TRA)
1291                                         add_to_diffuse(shr->shad, shi, is, i*shadfac[0]*lacol[0], i*shadfac[1]*lacol[1], i*shadfac[2]*lacol[2]);
1292                                 else
1293                                         add_to_diffuse(shr->shad, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
1294                         }
1295                         if(i_noshad>0.0f) {
1296                                 if(passflag & (SCE_PASS_DIFFUSE|SCE_PASS_SHADOW)) {
1297                                         if(ma->mode & MA_SHADOW_TRA)
1298                                                 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]);
1299                                         else
1300                                                 add_to_diffuse(shr->diff, shi, is, i_noshad*lacol[0], i_noshad*lacol[1], i_noshad*lacol[2]);
1301                                 }
1302                                 else
1303                                         VECCOPY(shr->diff, shr->shad);
1304                         }
1305                 }
1306                 
1307                 /* specularity */
1308                 if(shadfac[3]>0.0f && shi->spec!=0.0f && !(lar->mode & LA_NO_SPEC) && !(lar->mode & LA_ONLYSHADOW)) {
1309                         
1310                         if(!(passflag & (SCE_PASS_COMBINED|SCE_PASS_SPEC)));
1311                         else if(lar->type==LA_HEMI) {
1312                                 float t;
1313                                 /* hemi uses no spec shaders (yet) */
1314                                 
1315                                 lv[0]+= view[0];
1316                                 lv[1]+= view[1];
1317                                 lv[2]+= view[2];
1318                                 
1319                                 Normalize(lv);
1320                                 
1321                                 t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1322                                 
1323                                 if(lar->type==LA_HEMI) {
1324                                         t= 0.5*t+0.5;
1325                                 }
1326                                 
1327                                 t= shadfac[3]*shi->spec*spec(t, shi->har);
1328                                 
1329                                 shr->spec[0]+= t*(lacol[0] * shi->specr);
1330                                 shr->spec[1]+= t*(lacol[1] * shi->specg);
1331                                 shr->spec[2]+= t*(lacol[2] * shi->specb);
1332                         }
1333                         else {
1334                                 /* specular shaders */
1335                                 float specfac, t;
1336                                 
1337                                 if(ma->spec_shader==MA_SPEC_PHONG) 
1338                                         specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1339                                 else if(ma->spec_shader==MA_SPEC_COOKTORR) 
1340                                         specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1341                                 else if(ma->spec_shader==MA_SPEC_BLINN) 
1342                                         specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1343                                 else if(ma->spec_shader==MA_SPEC_WARDISO)
1344                                         specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1345                                 else 
1346                                         specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1347                                 
1348                                 /* area lamp correction */
1349                                 if(lar->type==LA_AREA) specfac*= inp;
1350                                 
1351                                 t= shadfac[3]*shi->spec*visifac*specfac;
1352                                 
1353                                 if(ma->mode & MA_RAMP_SPEC) {
1354                                         float spec[3];
1355                                         do_specular_ramp(shi, specfac, t, spec);
1356                                         shr->spec[0]+= t*(lacol[0] * spec[0]);
1357                                         shr->spec[1]+= t*(lacol[1] * spec[1]);
1358                                         shr->spec[2]+= t*(lacol[2] * spec[2]);
1359                                 }
1360                                 else {
1361                                         shr->spec[0]+= t*(lacol[0] * shi->specr);
1362                                         shr->spec[1]+= t*(lacol[1] * shi->specg);
1363                                         shr->spec[2]+= t*(lacol[2] * shi->specb);
1364                                 }
1365                         }
1366                 }
1367         }
1368 }
1369
1370 static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr)
1371 {
1372         
1373         if(R.r.mode & R_SHADOW) {
1374                 ListBase *lights;
1375                 LampRen *lar;
1376                 GroupObject *go;
1377                 float inpr, lv[3];
1378                 float *vn, *view, shadfac[4];
1379                 float ir, accum, visifac, lampdist;
1380                 
1381                 vn= shi->vn;
1382                 view= shi->view;
1383
1384                 accum= ir= 0.0f;
1385                 
1386                 lights= get_lights(shi);
1387                 for(go=lights->first; go; go= go->next) {
1388                         lar= go->lampren;
1389                         if(lar==NULL) continue;
1390                         
1391                         /* yafray: ignore shading by photonlights, not used in Blender */
1392                         if (lar->type==LA_YF_PHOTON) continue;
1393                         
1394                         if(lar->mode & LA_LAYER) if((lar->lay & shi->vlr->lay)==0) continue;
1395                         if((lar->lay & shi->lay)==0) continue;
1396                         
1397                         if(lar->shb || (lar->mode & LA_SHAD_RAY)) {
1398                                 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1399                                 if(visifac <= 0.0f) {
1400                                         ir+= 1.0f;
1401                                         accum+= 1.0f;
1402                                         continue;
1403                                 }
1404                                 inpr= INPR(shi->vn, lv);
1405                                 if(inpr <= 0.0f) {
1406                                         ir+= 1.0f;
1407                                         accum+= 1.0f;
1408                                         continue;
1409                                 }                               
1410                                 lamp_get_shadow(lar, shi, inpr, shadfac, shi->depth);
1411
1412                                 ir+= 1.0f;
1413                                 accum+= (1.0f-visifac) + (visifac)*shadfac[3];
1414                         }
1415                 }
1416                 if(ir>0.0f) {
1417                         accum/= ir;
1418                         shr->alpha= (shi->mat->alpha)*(1.0f-accum);
1419                 }
1420                 else shr->alpha= shi->mat->alpha;
1421         }
1422         
1423         /* quite disputable this...  also note it doesn't mirror-raytrace */    
1424         if((R.wrld.mode & WO_AMB_OCC) && shi->amb!=0.0f) {
1425                 float f;
1426                 
1427                 f= 1.0f - shi->ao[0];
1428                 f= R.wrld.aoenergy*f*shi->amb;
1429                 
1430                 if(R.wrld.aomix==WO_AOADD) {
1431                         shr->alpha += f;
1432                         shr->alpha *= f;
1433                 }
1434                 else if(R.wrld.aomix==WO_AOSUB) {
1435                         shr->alpha += f;
1436                 }
1437                 else {
1438                         shr->alpha *= f;
1439                         shr->alpha += f;
1440                 }
1441         }
1442 }
1443
1444 static void wrld_exposure_correct(float *diff)
1445 {
1446         diff[0]= R.wrld.linfac*(1.0f-exp( diff[0]*R.wrld.logfac) );
1447         diff[1]= R.wrld.linfac*(1.0f-exp( diff[1]*R.wrld.logfac) );
1448         diff[2]= R.wrld.linfac*(1.0f-exp( diff[2]*R.wrld.logfac) );
1449 }
1450
1451 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1452 {
1453         Material *ma= shi->mat;
1454         VlakRen *vlr= shi->vlr;
1455         int passflag= shi->passflag;
1456         
1457         memset(shr, 0, sizeof(ShadeResult));
1458         
1459         /* separate loop */
1460         if(ma->mode & MA_ONLYSHADOW) {
1461                 shade_lamp_loop_only_shadow(shi, shr);
1462                 return;
1463         }
1464         
1465         /* envmap hack, always reset */
1466         shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0f;
1467         
1468         /* material color itself */
1469         if(passflag & (SCE_PASS_COMBINED|SCE_PASS_RGBA)) {
1470                 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1471                         shi->r= shi->vcol[0];
1472                         shi->g= shi->vcol[1];
1473                         shi->b= shi->vcol[2];
1474                 }
1475                 if(ma->texco)
1476                         do_material_tex(shi);
1477                 
1478                 shr->col[0]= shi->r*shi->alpha;
1479                 shr->col[1]= shi->g*shi->alpha;
1480                 shr->col[2]= shi->b*shi->alpha;
1481                 shr->col[3]= shi->alpha;
1482         }
1483         
1484         if(ma->mode & MA_SHLESS) {
1485                 shr->combined[0]= shi->r;
1486                 shr->combined[1]= shi->g;
1487                 shr->combined[2]= shi->b;
1488                 shr->alpha= shi->alpha;
1489                 return;
1490         }
1491
1492         if( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) {        // vertexcolor light
1493                 shr->diff[0]= shi->r*(shi->emit+shi->vcol[0]);
1494                 shr->diff[1]= shi->g*(shi->emit+shi->vcol[1]);
1495                 shr->diff[2]= shi->b*(shi->emit+shi->vcol[2]);
1496         }
1497         else {
1498                 shr->diff[0]= shi->r*shi->emit;
1499                 shr->diff[1]= shi->g*shi->emit;
1500                 shr->diff[2]= shi->b*shi->emit;
1501         }
1502         VECCOPY(shr->shad, shr->diff);
1503         
1504         /* AO pass */
1505         if(R.wrld.mode & WO_AMB_OCC) {
1506                 if(passflag & (SCE_PASS_COMBINED|SCE_PASS_AO)) {
1507                         /* AO was calculated for scanline already */
1508                         if(shi->depth)
1509                                 ambient_occlusion(shi);
1510                         VECCOPY(shr->ao, shi->ao);
1511                 }
1512         }
1513         
1514         /* lighting pass */
1515         if(passflag & (SCE_PASS_COMBINED|SCE_PASS_DIFFUSE|SCE_PASS_SPEC|SCE_PASS_SHADOW)) {
1516                 GroupObject *go;
1517                 ListBase *lights;
1518                 LampRen *lar;
1519                 
1520                 lights= get_lights(shi);
1521                 for(go=lights->first; go; go= go->next) {
1522                         lar= go->lampren;
1523                         if(lar==NULL) continue;
1524                         
1525                         /* yafray: ignore shading by photonlights, not used in Blender */
1526                         if (lar->type==LA_YF_PHOTON) continue;
1527                         
1528                         /* test for lamp layer */
1529                         if(lar->mode & LA_LAYER) if((lar->lay & vlr->lay)==0) continue;
1530                         if((lar->lay & shi->lay)==0) continue;
1531                         
1532                         /* accumulates in shr->diff and shr->spec and shr->shad (diffuse with shadow!) */
1533                         shade_one_light(lar, shi, shr, passflag);
1534                 }
1535
1536                 /*this check is to prevent only shadow lamps from producing negative
1537                   colors.*/
1538                 if (shr->spec[0] < 0) shr->spec[0] = 0;
1539                 if (shr->spec[1] < 0) shr->spec[1] = 0;
1540                 if (shr->spec[2] < 0) shr->spec[2] = 0;
1541
1542                 if (shr->shad[0] < 0) shr->shad[0] = 0;
1543                 if (shr->shad[1] < 0) shr->shad[1] = 0;
1544                 if (shr->shad[2] < 0) shr->shad[2] = 0;
1545                                                 
1546                 if(ma->sss_flag & MA_DIFF_SSS) {
1547                         float sss[3], col[3], texfac= ma->sss_texfac;
1548
1549                         /* this will return false in the preprocess stage */
1550                         if(sample_sss(&R, ma, shi->co, sss)) {
1551                                 if(texfac==0.0f) {
1552                                         VECCOPY(col, shr->col);
1553                                 }
1554                                 else if(texfac==1.0f) {
1555                                         col[0]= col[1]= col[2]= 1.0f;
1556                                 }
1557                                 else {
1558                                         col[0]= pow(shr->col[0], 1.0f-texfac);
1559                                         col[1]= pow(shr->col[1], 1.0f-texfac);
1560                                         col[2]= pow(shr->col[2], 1.0f-texfac);
1561                                 }
1562
1563                                 shr->diff[0]= sss[0]*col[0];
1564                                 shr->diff[1]= sss[1]*col[1];
1565                                 shr->diff[2]= sss[2]*col[2];
1566
1567                                 if(shi->combinedflag & SCE_PASS_SHADOW) {
1568                                         shr->shad[0]= sss[0]*col[0];
1569                                         shr->shad[1]= sss[1]*col[1];
1570                                         shr->shad[2]= sss[2]*col[2];
1571                                 }
1572                         }
1573                 }
1574                 
1575                 if(shi->combinedflag & SCE_PASS_SHADOW) 
1576                         VECCOPY(shr->combined, shr->shad)       /* note, no ';' ! */
1577                 else
1578                         VECCOPY(shr->combined, shr->diff);
1579                         
1580                 /* calculate shadow pass, we use a multiplication mask */
1581                 if(passflag & SCE_PASS_SHADOW) {
1582                         if(shr->diff[0]!=0.0f) shr->shad[0]= shr->shad[0]/shr->diff[0];
1583                         if(shr->diff[1]!=0.0f) shr->shad[1]= shr->shad[1]/shr->diff[1];
1584                         if(shr->diff[2]!=0.0f) shr->shad[2]= shr->shad[2]/shr->diff[2];
1585                 }
1586                 
1587                 /* exposure correction */
1588                 if(R.wrld.exp!=0.0f || R.wrld.range!=1.0f) {
1589                         wrld_exposure_correct(shr->combined);   /* has no spec! */
1590                         wrld_exposure_correct(shr->spec);
1591                 }
1592         }
1593         
1594         /* alpha in end, spec can influence it */
1595         if(passflag & (SCE_PASS_COMBINED)) {
1596                 if(ma->fresnel_tra!=0.0f) 
1597                         shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1598                         
1599                 /* note: shi->mode! */
1600                 if(shi->mode & (MA_ZTRA|MA_RAYTRANSP)) {
1601                         if(shi->spectra!=0.0f) {
1602                                 float t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
1603                                 t *= shi->spectra;
1604                                 if(t>1.0f) t= 1.0f;
1605                                 shi->alpha= (1.0f-t)*shi->alpha+t;
1606                         }
1607                 }
1608         }
1609         shr->alpha= shi->alpha;
1610         
1611         /* from now stuff everything in shr->combined: ambient, AO, radio, ramps, exposure */
1612         if(!(ma->sss_flag & MA_DIFF_SSS) || !has_sss_tree(&R, ma)) {
1613                 shr->combined[0]+= shi->ambr + shi->r*shi->amb*shi->rad[0];
1614                 shr->combined[1]+= shi->ambg + shi->g*shi->amb*shi->rad[1];
1615                 shr->combined[2]+= shi->ambb + shi->b*shi->amb*shi->rad[2];
1616                 
1617                 /* add AO in combined? */
1618                 if(R.wrld.mode & WO_AMB_OCC) {
1619                         if(shi->combinedflag & SCE_PASS_AO) {
1620                                 float aodiff[3];
1621                                 ambient_occlusion_to_diffuse(shi, aodiff);
1622                                 
1623                                 shr->combined[0] += shi->r*aodiff[0];
1624                                 shr->combined[1] += shi->g*aodiff[1];
1625                                 shr->combined[2] += shi->b*aodiff[2];
1626                         }
1627                 }
1628                 
1629                 if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->combined, shi);
1630         }
1631
1632         if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shr->spec+1, shr->spec+2, shi);
1633         
1634         /* refcol is for envmap only */
1635         if(shi->refcol[0]!=0.0f) {
1636                 float result[3];
1637                 
1638                 result[0]= shi->mirr*shi->refcol[1] + (1.0f - shi->mirr*shi->refcol[0])*shr->combined[0];
1639                 result[1]= shi->mirg*shi->refcol[2] + (1.0f - shi->mirg*shi->refcol[0])*shr->combined[1];
1640                 result[2]= shi->mirb*shi->refcol[3] + (1.0f - shi->mirb*shi->refcol[0])*shr->combined[2];
1641                 
1642                 if(passflag & SCE_PASS_REFLECT)
1643                         VECSUB(shr->refl, result, shr->combined);
1644                 
1645                 if(shi->combinedflag & SCE_PASS_REFLECT)
1646                         VECCOPY(shr->combined, result);
1647                         
1648         }
1649         
1650         /* and add spec */
1651         if(shi->combinedflag & SCE_PASS_SPEC)
1652                 VECADD(shr->combined, shr->combined, shr->spec);
1653
1654         shr->combined[3]= shr->alpha;
1655 }
1656
1657