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