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