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