2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2006 Blender Foundation
19 * All rights reserved.
21 * Contributors: Hos, Robert Wenzlaff.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/render/intern/source/shadeoutput.c
37 #include "BLI_utildefines.h"
39 #include "BKE_colortools.h"
40 #include "BKE_material.h"
41 #include "BKE_texture.h"
44 #include "DNA_group_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
49 #include "occlusion.h"
50 #include "renderpipeline.h"
51 #include "render_types.h"
52 #include "pixelblending.h"
53 #include "rendercore.h"
58 #include "shading.h" /* own include */
60 /* could enable at some point but for now there are far too many conversions */
61 #pragma GCC diagnostic ignored "-Wdouble-promotion"
63 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
64 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
65 /* only to be used here in this file, it's for speed */
66 extern struct Render R;
67 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
69 ListBase *get_lights(ShadeInput *shi)
72 if (R.r.scemode & R_PREVIEWBUTS)
74 if (shi->light_override)
75 return &shi->light_override->gobject;
76 if (shi->mat && shi->mat->group)
77 return &shi->mat->group->gobject;
83 static void fogcolor(const float colf[3], float *rco, float *view)
85 float alpha, stepsize, startdist, dist, hor[4], zen[3], vec[3], dview[3];
86 float div=0.0f, distfac;
88 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
89 zen[0]= R.wrld.zenr; zen[1]= R.wrld.zeng; zen[2]= R.wrld.zenb;
93 /* we loop from cur coord to mist start in steps */
97 dview[0]= view[0]/(stepsize*div);
98 dview[1]= view[1]/(stepsize*div);
101 startdist= -rco[2] + BLI_frand();
102 for (dist= startdist; dist>R.wrld.miststa; dist-= stepsize) {
104 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
106 do_sky_tex(vec, vec, NULL, hor, zen, &alpha);
108 distfac= (dist-R.wrld.miststa)/R.wrld.mistdist;
110 hor[3]= hor[0]*distfac*distfac;
114 hor[0]= hor[0]*alpha;
115 hor[1]= hor[1]*alpha;
116 hor[2]= hor[2]*alpha;
117 addAlphaOverFloat(colf, hor);
119 sub_v3_v3(vec, dview);
124 /* zcor is distance, co the 3d coordinate in eye space, return alpha */
125 float mistfactor(float zcor, float const co[3])
129 fac = zcor - R.wrld.miststa; /* zcor is calculated per pixel */
131 /* fac= -co[2]-R.wrld.miststa; */
134 if (fac < R.wrld.mistdist) {
136 fac = (fac / R.wrld.mistdist);
138 if (R.wrld.mistype == 0) {
141 else if (R.wrld.mistype == 1) {
156 /* height switched off mist */
157 if (R.wrld.misthi!=0.0f && fac!=0.0f) {
158 /* at height misthi the mist is completely gone */
160 hi = R.viewinv[0][2] * co[0] +
161 R.viewinv[1][2] * co[1] +
162 R.viewinv[2][2] * co[2] +
165 if (hi > R.wrld.misthi) {
169 hi= (R.wrld.misthi-hi)/R.wrld.misthi;
174 return (1.0f-fac)* (1.0f-R.wrld.misi);
177 static void spothalo(struct LampRen *lar, ShadeInput *shi, float *intens)
179 double a, b, c, disc, nray[3], npos[3];
180 double t0, t1 = 0.0f, t2= 0.0f, t3;
181 float p1[3], p2[3], ladist, maxz = 0.0f, maxy = 0.0f, haint;
182 int snijp, do_clip = TRUE, use_yco = FALSE;
187 if (R.r.mode & R_ORTHO) {
188 /* camera pos (view vector) cannot be used... */
189 /* camera position (cox,coy,0) rotate around lamp */
190 p1[0]= shi->co[0]-lar->co[0];
191 p1[1]= shi->co[1]-lar->co[1];
193 mul_m3_v3(lar->imat, p1);
194 copy_v3db_v3fl(npos, p1); /* npos is double! */
197 npos[2] *= (double)lar->sh_zfac;
200 copy_v3db_v3fl(npos, lar->sh_invcampos); /* in initlamp calculated */
204 copy_v3db_v3fl(nray, shi->view);
205 mul_m3_v3_double(lar->imat, nray);
207 if (R.wrld.mode & WO_MIST) {
209 haint *= mistfactor(-lar->co[2], lar->co);
217 if (shi->co[2]==0.0f) {
218 do_clip = FALSE; /* for when halo at sky */
221 p1[0]= shi->co[0]-lar->co[0];
222 p1[1]= shi->co[1]-lar->co[1];
223 p1[2]= shi->co[2]-lar->co[2];
225 maxz= lar->imat[0][2]*p1[0]+lar->imat[1][2]*p1[1]+lar->imat[2][2]*p1[2];
227 maxy= lar->imat[0][1]*p1[0]+lar->imat[1][1]*p1[1]+lar->imat[2][1]*p1[2];
229 if (fabsf(nray[2]) < FLT_EPSILON) {
234 /* scale z to make sure volume is normalized */
235 nray[2] *= (double)lar->sh_zfac;
236 /* nray does not need normalization */
238 ladist= lar->sh_zfac*lar->dist;
241 a = nray[0] * nray[0] + nray[1] * nray[1] - nray[2]*nray[2];
242 b = nray[0] * npos[0] + nray[1] * npos[1] - nray[2]*npos[2];
243 c = npos[0] * npos[0] + npos[1] * npos[1] - npos[2]*npos[2];
246 if (fabs(a) < DBL_EPSILON) {
248 * Only one intersection point...
259 else if (disc > 0.0) {
261 t1 = (-b + disc) / a;
262 t2 = (-b - disc) / a;
271 a= t1; t1= t2; t2= a;
274 /* z of intersection points with diabolo */
275 p1[2]= npos[2] + t1*nray[2];
276 p2[2]= npos[2] + t2*nray[2];
278 /* evaluate both points */
279 if (p1[2]<=0.0f) ok1= 1;
280 if (p2[2]<=0.0f && t1!=t2) ok2= 1;
282 /* at least 1 point with negative z */
283 if (ok1==0 && ok2==0) return;
285 /* intersction point with -ladist, the bottom of the cone */
286 if (use_yco == FALSE) {
287 t3= ((double)(-ladist)-npos[2])/nray[2];
289 /* de we have to replace one of the intersection points? */
291 if (p1[2]<-ladist) t1= t3;
297 if (p2[2]<-ladist) t2= t3;
303 else if (ok1==0 || ok2==0) return;
305 /* at least 1 visible interesction point */
306 if (t1<0.0 && t2<0.0) return;
313 /* sort again to be sure */
315 a= t1; t1= t2; t2= a;
318 /* calculate t0: is the maximum visible z (when halo is intersected by face) */
320 if (use_yco == FALSE) t0 = ((double)maxz - npos[2]) / nray[2];
321 else t0 = ((double)maxy - npos[1]) / nray[1];
328 p1[0]= npos[0] + t1*nray[0];
329 p1[1]= npos[1] + t1*nray[1];
330 p1[2]= npos[2] + t1*nray[2];
331 p2[0]= npos[0] + t2*nray[0];
332 p2[1]= npos[1] + t2*nray[1];
333 p2[2]= npos[2] + t2*nray[2];
336 /* now we have 2 points, make three lengths with it */
338 a= sqrt(p1[0]*p1[0]+p1[1]*p1[1]+p1[2]*p1[2]);
339 b= sqrt(p2[0]*p2[0]+p2[1]*p2[1]+p2[2]*p2[2]);
348 *intens= c*( (1.0-a)+(1.0-b) );
350 /* WATCH IT: do not clip a,b en c at 1.0, this gives nasty little overflows
351 * at the edges (especially with narrow halos) */
352 if (*intens<=0.0f) return;
355 /* not needed because t0 has been used for p1/p2 as well */
356 /* if (doclip && t0<t2) { */
357 /* *intens *= (t0-t1)/(t2-t1); */
362 if (lar->shb && lar->shb->shadhalostep) {
363 *intens *= shadow_halo(lar, p1, p2);
369 void renderspothalo(ShadeInput *shi, float col[4], float alpha)
376 if (alpha==0.0f) return;
378 lights= get_lights(shi);
379 for (go=lights->first; go; go= go->next) {
381 if (lar==NULL) continue;
383 if (lar->type==LA_SPOT && (lar->mode & LA_HALO) && (lar->buftype != LA_SHADBUF_DEEP) && lar->haint>0) {
385 if (lar->mode & LA_LAYER)
386 if (shi->vlr && (lar->lay & shi->obi->lay)==0)
388 if ((lar->lay & shi->lay)==0)
391 spothalo(lar, shi, &i);
393 const float i_alpha = i * alpha;
394 col[0] += i_alpha * lar->r;
395 col[1] += i_alpha * lar->g;
396 col[2] += i_alpha * lar->b;
397 col[3] += i_alpha; /* all premul */
401 /* clip alpha, is needed for unified 'alpha threshold' (vanillaRenderPipe.c) */
402 if (col[3]>1.0f) col[3]= 1.0f;
407 /* ---------------- shaders ----------------------- */
409 static double Normalize_d(double *n)
413 d= n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
415 if (d>0.00000000000000001) {
429 /* mix of 'real' fresnel and allowing control. grad defines blending gradient */
430 float fresnel_fac(const float view[3], const float vn[3], float grad, float fac)
434 if (fac==0.0f) return 1.0f;
436 t1 = dot_v3v3(view, vn);
437 if (t1>0.0f) t2= 1.0f+t1;
440 t2= grad + (1.0f-grad)*powf(t2, fac);
442 if (t2<0.0f) return 0.0f;
443 else if (t2>1.0f) return 1.0f;
447 static double saacos_d(double fac)
449 if (fac<= -1.0) return M_PI;
450 else if (fac>=1.0) return 0.0;
451 else return acos(fac);
454 /* Stoke's form factor. Need doubles here for extreme small area sizes */
455 static float area_lamp_energy(float (*area)[3], const float co[3], const float vn[3])
458 double vec[4][3]; /* vectors of rendered co to vertices lamp */
459 double cross[4][3]; /* cross products of this */
460 double rad[4]; /* angles between vecs */
462 VECSUB(vec[0], co, area[0]);
463 VECSUB(vec[1], co, area[1]);
464 VECSUB(vec[2], co, area[2]);
465 VECSUB(vec[3], co, area[3]);
473 #define CROSS(dest, a, b) \
475 dest[0]= a[1] * b[2] - a[2] * b[1]; \
476 dest[1]= a[2] * b[0] - a[0] * b[2]; \
477 dest[2]= a[0] * b[1] - a[1] * b[0]; \
480 CROSS(cross[0], vec[0], vec[1]);
481 CROSS(cross[1], vec[1], vec[2]);
482 CROSS(cross[2], vec[2], vec[3]);
483 CROSS(cross[3], vec[3], vec[0]);
487 Normalize_d(cross[0]);
488 Normalize_d(cross[1]);
489 Normalize_d(cross[2]);
490 Normalize_d(cross[3]);
493 rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
494 rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
495 rad[2]= vec[2][0]*vec[3][0]+ vec[2][1]*vec[3][1]+ vec[2][2]*vec[3][2];
496 rad[3]= vec[3][0]*vec[0][0]+ vec[3][1]*vec[0][1]+ vec[3][2]*vec[0][2];
498 rad[0]= saacos_d(rad[0]);
499 rad[1]= saacos_d(rad[1]);
500 rad[2]= saacos_d(rad[2]);
501 rad[3]= saacos_d(rad[3]);
504 fac= rad[0]*(vn[0]*cross[0][0]+ vn[1]*cross[0][1]+ vn[2]*cross[0][2]);
505 fac+= rad[1]*(vn[0]*cross[1][0]+ vn[1]*cross[1][1]+ vn[2]*cross[1][2]);
506 fac+= rad[2]*(vn[0]*cross[2][0]+ vn[1]*cross[2][1]+ vn[2]*cross[2][2]);
507 fac+= rad[3]*(vn[0]*cross[3][0]+ vn[1]*cross[3][1]+ vn[2]*cross[3][2]);
509 if (fac<=0.0) return 0.0;
513 static float area_lamp_energy_multisample(LampRen *lar, const float co[3], float *vn)
515 /* corner vectors are moved around according lamp jitter */
516 float *jitlamp= lar->jitter, vec[3];
517 float area[4][3], intens= 0.0f;
518 int a= lar->ray_totsamp;
520 /* test if co is behind lamp */
521 sub_v3_v3v3(vec, co, lar->co);
522 if (dot_v3v3(vec, lar->vec) < 0.0f)
529 mul_m3_v3(lar->mat, vec);
531 add_v3_v3v3(area[0], lar->area[0], vec);
532 add_v3_v3v3(area[1], lar->area[1], vec);
533 add_v3_v3v3(area[2], lar->area[2], vec);
534 add_v3_v3v3(area[3], lar->area[3], vec);
536 intens+= area_lamp_energy(area, co, vn);
540 intens /= (float)lar->ray_totsamp;
542 return pow(intens * lar->areasize, lar->k); /* corrected for buttons size and lar->dist^2 */
545 static float spec(float inp, int hard)
549 if (inp>=1.0f) return 1.0f;
550 else if (inp<=0.0f) return 0.0f;
554 if (b1<0.01f) b1= 0.01f;
556 if ((hard & 1)==0) inp= 1.0f;
557 if (hard & 2) inp*= b1;
559 if (hard & 4) inp*= b1;
561 if (hard & 8) inp*= b1;
563 if (hard & 16) inp*= b1;
567 if (b1<0.001f) b1= 0.0f;
569 if (hard & 32) inp*= b1;
571 if (hard & 64) inp*=b1;
573 if (hard & 128) inp*=b1;
575 if (b1<0.001f) b1= 0.0f;
585 static float Phong_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent )
595 rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
596 if (tangent) rslt= sasqrt(1.0f - rslt*rslt);
598 if ( rslt > 0.0f ) rslt= spec(rslt, hard);
605 /* reduced cook torrance spec (for off-specular peak) */
606 static float CookTorr_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent)
608 float i, nh, nv, h[3];
615 nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2];
616 if (tangent) nh= sasqrt(1.0f - nh*nh);
617 else if (nh<0.0f) return 0.0f;
619 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
620 if (tangent) nv= sasqrt(1.0f - nv*nv);
621 else if (nv<0.0f) nv= 0.0f;
630 static float Blinn_Spec(const float n[3], const float l[3], const float v[3], float refrac, float spec_power, int tangent)
632 float i, nh, nv, nl, vh, h[3];
633 float a, b, c, g=0.0f, p, f, ang;
635 if (refrac < 1.0f) return 0.0f;
636 if (spec_power == 0.0f) return 0.0f;
638 /* conversion from 'hardness' (1-255) to 'spec_power' (50 maps at 0.1) */
639 if (spec_power<100.0f)
640 spec_power= sqrt(1.0f/spec_power);
641 else spec_power= 10.0f/spec_power;
648 nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
649 if (tangent) nh= sasqrt(1.0f - nh*nh);
650 else if (nh<0.0f) return 0.0f;
652 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
653 if (tangent) nv= sasqrt(1.0f - nv*nv);
654 if (nv<=0.01f) nv= 0.01f; /* hrms... */
656 nl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
657 if (tangent) nl= sasqrt(1.0f - nl*nl);
662 vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and half-way vector */
663 if (vh<=0.0f) vh= 0.01f;
669 if ( a < b && a < c ) g = a;
670 else if ( b < a && b < c ) g = b;
671 else if ( c < a && c < b ) g = c;
673 p = sqrt((double)((refrac * refrac)+(vh * vh) - 1.0f));
674 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))));
677 i= f * g * exp((double)(-(ang*ang) / (2.0f*spec_power*spec_power)));
683 /* cartoon render spec */
684 static float Toon_Spec(const float n[3], const float l[3], const float v[3], float size, float smooth, int tangent)
695 rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
696 if (tangent) rslt = sasqrt(1.0f - rslt*rslt);
698 ang = saacos( rslt );
700 if ( ang < size ) rslt = 1.0f;
701 else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
702 else rslt = 1.0f - ((ang - size) / smooth);
707 /* Ward isotropic gaussian spec */
708 static float WardIso_Spec(const float n[3], const float l[3], const float v[3], float rms, int tangent)
710 float i, nh, nv, nl, h[3], angle, alpha;
713 /* half-way vector */
719 nh = n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
720 if (tangent) nh = sasqrt(1.0f - nh*nh);
721 if (nh<=0.0f) nh = 0.001f;
723 nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
724 if (tangent) nv = sasqrt(1.0f - nv*nv);
725 if (nv<=0.0f) nv = 0.001f;
727 nl = n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
728 if (tangent) nl = sasqrt(1.0f - nl*nl);
729 if (nl<=0.0f) nl = 0.001f;
731 angle = tan(saacos(nh));
732 alpha = MAX2(rms, 0.001f);
734 i= nl * (1.0f/(4.0f*(float)M_PI*alpha*alpha)) * (expf( -(angle*angle)/(alpha*alpha))/(sqrtf(nv*nl)));
739 /* cartoon render diffuse */
740 static float Toon_Diff(const float n[3], const float l[3], const float UNUSED(v[3]), float size, float smooth)
744 rslt = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
746 ang = saacos( (double)(rslt) );
748 if ( ang < size ) rslt = 1.0f;
749 else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
750 else rslt = 1.0f - ((ang - size) / smooth);
755 /* Oren Nayar diffuse */
757 /* 'nl' is either dot product, or return value of area light */
758 /* in latter case, only last multiplication uses 'nl' */
759 static float OrenNayar_Diff(float nl, const float n[3], const float l[3], const float v[3], float rough )
761 float i/*, nh*/, nv /*, vh */, realnl, h[3];
763 float Lit_A, View_A, Lit_B[3], View_B[3];
770 /* nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; */ /* Dot product between surface normal and half-way vector */
771 /* if (nh<0.0f) nh = 0.0f; */
773 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
774 if (nv<=0.0f) nv= 0.0f;
776 realnl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
777 if (realnl<=0.0f) return 0.0f;
778 if (nl<0.0f) return 0.0f; /* value from area light */
780 /* vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; */ /* Dot product between view vector and halfway vector */
781 /* if (vh<=0.0f) vh= 0.0f; */
783 Lit_A = saacos(realnl);
784 View_A = saacos( nv );
786 Lit_B[0] = l[0] - (realnl * n[0]);
787 Lit_B[1] = l[1] - (realnl * n[1]);
788 Lit_B[2] = l[2] - (realnl * n[2]);
791 View_B[0] = v[0] - (nv * n[0]);
792 View_B[1] = v[1] - (nv * n[1]);
793 View_B[2] = v[2] - (nv * n[2]);
794 normalize_v3(View_B);
796 t = Lit_B[0]*View_B[0] + Lit_B[1]*View_B[1] + Lit_B[2]*View_B[2];
799 if ( Lit_A > View_A ) {
808 A = 1.0f - (0.5f * ((rough * rough) / ((rough * rough) + 0.33f)));
809 B = 0.45f * ((rough * rough) / ((rough * rough) + 0.09f));
811 b*= 0.95f; /* prevent tangens from shooting to inf, 'nl' can be not a dot product here. */
812 /* overflow only happens with extreme size area light, and higher roughness */
813 i = nl * ( A + ( B * t * sinf(a) * tanf(b) ) );
818 /* Minnaert diffuse */
819 static float Minnaert_Diff(float nl, const float n[3], const float v[3], float darkness)
823 /* nl = dot product between surface normal and light vector */
827 /* nv = dot product between surface normal and view vector */
832 if (darkness <= 1.0f)
833 i = nl * pow(max_ff(nv * nl, 0.1f), (darkness - 1.0f) ); /*The Real model*/
835 i = nl * pow( (1.001f - nv), (darkness - 1.0f) ); /*Nvidia model*/
840 static float Fresnel_Diff(float *vn, float *lv, float *UNUSED(view), float fac_i, float fac)
842 return fresnel_fac(lv, vn, fac_i, fac);
845 /* --------------------------------------------- */
846 /* also called from texture.c */
847 void calc_R_ref(ShadeInput *shi)
851 /* shi->vn dot shi->view */
852 i= -2*(shi->vn[0]*shi->view[0]+shi->vn[1]*shi->view[1]+shi->vn[2]*shi->view[2]);
854 shi->ref[0]= (shi->view[0]+i*shi->vn[0]);
855 shi->ref[1]= (shi->view[1]+i*shi->vn[1]);
856 shi->ref[2]= (shi->view[2]+i*shi->vn[2]);
858 if (shi->vlr->flag & R_SMOOTH) {
859 i= -2*( (shi->vn[0]+shi->dxno[0])*(shi->view[0]+shi->dxview) +
860 (shi->vn[1]+shi->dxno[1])*shi->view[1]+ (shi->vn[2]+shi->dxno[2])*shi->view[2] );
862 shi->dxref[0]= shi->ref[0]- ( shi->view[0]+shi->dxview+i*(shi->vn[0]+shi->dxno[0]));
863 shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*(shi->vn[1]+shi->dxno[1]));
864 shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dxno[2]));
866 i= -2*( (shi->vn[0]+shi->dyno[0])*shi->view[0]+
867 (shi->vn[1]+shi->dyno[1])*(shi->view[1]+shi->dyview)+ (shi->vn[2]+shi->dyno[2])*shi->view[2] );
869 shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*(shi->vn[0]+shi->dyno[0]));
870 shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*(shi->vn[1]+shi->dyno[1]));
871 shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dyno[2]));
876 i= -2*( shi->vn[0]*(shi->view[0]+shi->dxview) +
877 shi->vn[1]*shi->view[1]+ shi->vn[2]*shi->view[2] );
879 shi->dxref[0]= shi->ref[0]- (shi->view[0]+shi->dxview+i*shi->vn[0]);
880 shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*shi->vn[1]);
881 shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
883 i= -2*( shi->vn[0]*shi->view[0]+
884 shi->vn[1]*(shi->view[1]+shi->dyview)+ shi->vn[2]*shi->view[2] );
886 shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*shi->vn[0]);
887 shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*shi->vn[1]);
888 shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
894 /* called from ray.c */
895 void shade_color(ShadeInput *shi, ShadeResult *shr)
897 Material *ma= shi->mat;
899 if (ma->mode & (MA_FACETEXTURE)) {
900 shi->r= shi->vcol[0];
901 shi->g= shi->vcol[1];
902 shi->b= shi->vcol[2];
903 if (ma->mode & (MA_FACETEXTURE_ALPHA))
904 shi->alpha= shi->vcol[3];
906 else if (ma->mode & (MA_VERTEXCOLP)) {
907 float neg_alpha = 1.0f - shi->vcol[3];
908 shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
909 shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
910 shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
914 do_material_tex(shi, &R);
916 if (ma->fresnel_tra!=0.0f)
917 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
919 if (!(shi->mode & MA_TRANSP)) shi->alpha= 1.0f;
921 shr->diff[0]= shi->r;
922 shr->diff[1]= shi->g;
923 shr->diff[2]= shi->b;
924 shr->alpha= shi->alpha;
927 /* ramp for at end of shade */
928 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
930 Material *ma= shi->mat;
934 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
935 float fac = rgb_to_grayscale(diff);
936 do_colorband(ma->ramp_col, fac, col);
938 /* blending method */
939 fac= col[3]*ma->rampfac_col;
941 ramp_blend(ma->rampblend_col, diff, fac, col);
946 /* r,g,b denote energy, ramp is used with different values to make new material color */
947 static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, float g, float b)
949 Material *ma= shi->mat;
951 if (ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
953 /* MA_RAMP_IN_RESULT is exceptional */
954 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
956 diff[0] += r * shi->r;
957 diff[1] += g * shi->g;
958 diff[2] += b * shi->b;
961 float colt[3], col[4];
965 switch (ma->rampin_col) {
966 case MA_RAMP_IN_ENERGY:
967 /* should use 'rgb_to_grayscale' but we only have a vector version */
968 fac= 0.3f*r + 0.58f*g + 0.12f*b;
970 case MA_RAMP_IN_SHADER:
974 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
981 do_colorband(ma->ramp_col, fac, col);
983 /* blending method */
984 fac= col[3]*ma->rampfac_col;
989 ramp_blend(ma->rampblend_col, colt, fac, col);
992 diff[0] += r * colt[0];
993 diff[1] += g * colt[1];
994 diff[2] += b * colt[2];
998 diff[0] += r * shi->r;
999 diff[1] += g * shi->g;
1000 diff[2] += b * shi->b;
1004 static void ramp_spec_result(float spec_col[3], ShadeInput *shi)
1006 Material *ma= shi->mat;
1008 if (ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
1010 float fac = rgb_to_grayscale(spec_col);
1012 do_colorband(ma->ramp_spec, fac, col);
1014 /* blending method */
1015 fac= col[3]*ma->rampfac_spec;
1017 ramp_blend(ma->rampblend_spec, spec_col, fac, col);
1022 /* is = dot product shade, t = spec energy */
1023 static void do_specular_ramp(ShadeInput *shi, float is, float t, float spec[3])
1025 Material *ma= shi->mat;
1027 spec[0]= shi->specr;
1028 spec[1]= shi->specg;
1029 spec[2]= shi->specb;
1031 /* MA_RAMP_IN_RESULT is exception */
1032 if (ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
1037 switch (ma->rampin_spec) {
1038 case MA_RAMP_IN_ENERGY:
1041 case MA_RAMP_IN_SHADER:
1044 case MA_RAMP_IN_NOR:
1045 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1052 do_colorband(ma->ramp_spec, fac, col);
1054 /* blending method */
1055 fac= col[3]*ma->rampfac_spec;
1057 ramp_blend(ma->rampblend_spec, spec, fac, col);
1061 /* pure AO, check for raytrace and world should have been done */
1062 /* preprocess, textures were not done, don't use shi->amb for that reason */
1063 void ambient_occlusion(ShadeInput *shi)
1065 if ((R.wrld.ao_gather_method == WO_AOGATHER_APPROX) && shi->mat->amb!=0.0f) {
1066 sample_occ(&R, shi);
1068 else if ((R.r.mode & R_RAYTRACE) && shi->mat->amb!=0.0f) {
1069 ray_ao(shi, shi->ao, shi->env);
1072 shi->ao[0]= shi->ao[1]= shi->ao[2]= 1.0f;
1074 zero_v3(shi->indirect);
1079 /* wrld mode was checked for */
1080 static void ambient_occlusion_apply(ShadeInput *shi, ShadeResult *shr)
1082 float f= R.wrld.aoenergy;
1083 float tmp[3], tmpspec[3];
1085 if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1090 if (R.wrld.aomix==WO_AOADD) {
1091 shr->combined[0] += shi->ao[0]*shi->r*shi->refl*f;
1092 shr->combined[1] += shi->ao[1]*shi->g*shi->refl*f;
1093 shr->combined[2] += shi->ao[2]*shi->b*shi->refl*f;
1095 else if (R.wrld.aomix==WO_AOMUL) {
1096 mul_v3_v3v3(tmp, shr->combined, shi->ao);
1097 mul_v3_v3v3(tmpspec, shr->spec, shi->ao);
1100 copy_v3_v3(shr->combined, tmp);
1101 copy_v3_v3(shr->spec, tmpspec);
1104 interp_v3_v3v3(shr->combined, shr->combined, tmp, f);
1105 interp_v3_v3v3(shr->spec, shr->spec, tmpspec, f);
1110 void environment_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1112 float f= R.wrld.ao_env_energy*shi->amb;
1114 if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1119 shr->combined[0] += shi->env[0]*shi->r*shi->refl*f;
1120 shr->combined[1] += shi->env[1]*shi->g*shi->refl*f;
1121 shr->combined[2] += shi->env[2]*shi->b*shi->refl*f;
1124 static void indirect_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1126 float f= R.wrld.ao_indirect_energy;
1128 if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1133 shr->combined[0] += shi->indirect[0]*shi->r*shi->refl*f;
1134 shr->combined[1] += shi->indirect[1]*shi->g*shi->refl*f;
1135 shr->combined[2] += shi->indirect[2]*shi->b*shi->refl*f;
1138 /* result written in shadfac */
1139 void lamp_get_shadow(LampRen *lar, ShadeInput *shi, float inp, float shadfac[4], int do_real)
1141 LampShadowSubSample *lss= &(lar->shadsamp[shi->thread].s[shi->sample]);
1143 if (do_real || lss->samplenr!=shi->samplenr) {
1145 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1148 if (lar->buftype==LA_SHADBUF_IRREGULAR)
1149 shadfac[3]= ISB_getshadow(shi, lar->shb);
1151 shadfac[3] = testshadowbuf(&R, lar->shb, shi->co, shi->dxco, shi->dyco, inp, shi->mat->lbias);
1153 else if (lar->mode & LA_SHAD_RAY) {
1154 ray_shadow(shi, lar, shadfac);
1157 if (shi->depth==0) {
1158 copy_v4_v4(lss->shadfac, shadfac);
1159 lss->samplenr= shi->samplenr;
1163 copy_v4_v4(shadfac, lss->shadfac);
1167 /* lampdistance and spot angle, writes in lv and dist */
1168 float lamp_get_visibility(LampRen *lar, const float co[3], float lv[3], float *dist)
1170 if (lar->type==LA_SUN || lar->type==LA_HEMI) {
1172 copy_v3_v3(lv, lar->vec);
1176 float visifac= 1.0f, t;
1178 sub_v3_v3v3(lv, co, lar->co);
1179 *dist= sqrtf(dot_v3v3(lv, lv));
1183 /* area type has no quad or sphere option */
1184 if (lar->type==LA_AREA) {
1185 /* area is single sided */
1186 //if (dot_v3v3(lv, lar->vec) > 0.0f)
1192 switch (lar->falloff_type) {
1193 case LA_FALLOFF_CONSTANT:
1196 case LA_FALLOFF_INVLINEAR:
1197 visifac = lar->dist/(lar->dist + dist[0]);
1199 case LA_FALLOFF_INVSQUARE:
1200 /* NOTE: This seems to be a hack since commit r12045 says this
1201 * option is similar to old Quad, but with slight changes.
1202 * Correct inv square would be (which would be old Quad):
1203 * visifac = lar->distkw / (lar->distkw + dist[0]*dist[0]);
1205 visifac = lar->dist / (lar->dist + dist[0]*dist[0]);
1207 case LA_FALLOFF_SLIDERS:
1209 visifac= lar->dist/(lar->dist+lar->ld1*dist[0]);
1211 visifac*= lar->distkw/(lar->distkw+lar->ld2*dist[0]*dist[0]);
1213 case LA_FALLOFF_CURVE:
1214 /* curvemapping_initialize is called from #add_render_lamp */
1215 visifac = curvemapping_evaluateF(lar->curfalloff, 0, dist[0]/lar->dist);
1219 if (lar->mode & LA_SPHERE) {
1220 float t= lar->dist - dist[0];
1224 visifac*= t/lar->dist;
1227 if (visifac > 0.0f) {
1228 if (lar->type==LA_SPOT) {
1231 if (lar->mode & LA_SQUARE) {
1232 if (dot_v3v3(lv, lar->vec) > 0.0f) {
1235 /* rotate view to lampspace */
1236 copy_v3_v3(lvrot, lv);
1237 mul_m3_v3(lar->imat, lvrot);
1239 x = max_ff(fabsf(lvrot[0]/lvrot[2]), fabsf(lvrot[1]/lvrot[2]));
1240 /* 1.0f/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1242 inpr= 1.0f/(sqrt(1.0f+x*x));
1247 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1255 if (t<lar->spotbl && lar->spotbl!=0.0f) {
1257 float i= t/lar->spotbl;
1259 inpr*= (3.0f*t-2.0f*t*i);
1266 if (visifac <= 0.001f) visifac = 0.0f;
1271 /* function returns raw diff, spec and full shadowed diff in the 'shad' pass */
1272 static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int passflag)
1274 Material *ma= shi->mat;
1275 VlakRen *vlr= shi->vlr;
1276 float lv[3], lampdist, lacol[3], shadfac[4], lashdw[3];
1277 float i, is, i_noshad, inp, *vn, *view, vnor[3], phongcorr=1.0f;
1284 if (lar->energy == 0.0f) return;
1285 /* only shadow lamps shouldn't affect shadow-less materials at all */
1286 if ((lar->mode & LA_ONLYSHADOW) && (!(ma->mode & MA_SHADOW) || !(R.r.mode & R_SHADOW)))
1288 /* optimization, don't render fully black lamps */
1289 if (!(lar->mode & LA_TEXTURE) && (lar->r + lar->g + lar->b == 0.0f))
1292 /* lampdist, spot angle, area side, ... */
1293 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1297 if (lar->type==LA_SPOT) {
1298 if (lar->mode & LA_OSATEX) {
1299 shi->osatex= 1; /* signal for multitex() */
1301 shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/lampdist;
1302 shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/lampdist;
1303 shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/lampdist;
1305 shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/lampdist;
1306 shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/lampdist;
1307 shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/lampdist;
1311 /* lamp color texture */
1316 lashdw[0]= lar->shdwr;
1317 lashdw[1]= lar->shdwg;
1318 lashdw[2]= lar->shdwb;
1320 if (lar->mode & LA_TEXTURE) do_lamp_tex(lar, lv, shi, lacol, LA_TEXTURE);
1321 if (lar->mode & LA_SHAD_TEX) do_lamp_tex(lar, lv, shi, lashdw, LA_SHAD_TEX);
1323 /* tangent case; calculate fake face normal, aligned with lampvector */
1324 /* note, vnor==vn is used as tangent trigger for buffer shadow */
1325 if (vlr->flag & R_TANGENT) {
1326 float cross[3], nstrand[3], blend;
1328 if (ma->mode & MA_STR_SURFDIFF) {
1329 cross_v3_v3v3(cross, shi->surfnor, vn);
1330 cross_v3_v3v3(nstrand, vn, cross);
1332 blend= dot_v3v3(nstrand, shi->surfnor);
1333 blend= 1.0f - blend;
1334 CLAMP(blend, 0.0f, 1.0f);
1336 interp_v3_v3v3(vnor, nstrand, shi->surfnor, blend);
1340 cross_v3_v3v3(cross, lv, vn);
1341 cross_v3_v3v3(vnor, cross, vn);
1345 if (ma->strand_surfnor > 0.0f) {
1346 if (ma->strand_surfnor > shi->surfdist) {
1347 blend= (ma->strand_surfnor - shi->surfdist)/ma->strand_surfnor;
1348 interp_v3_v3v3(vnor, vnor, shi->surfnor, blend);
1353 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1356 else if (ma->mode & MA_TANGENT_V) {
1358 cross_v3_v3v3(cross, lv, shi->tang);
1359 cross_v3_v3v3(vnor, cross, shi->tang);
1361 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1365 /* dot product and reflectivity */
1366 /* inp = dotproduct, is = shader result, i = lamp energy (with shadow), i_noshad = i without shadow */
1367 inp= dot_v3v3(vn, lv);
1369 /* phong threshold to prevent backfacing faces having artifacts on ray shadow (terminator problem) */
1370 /* this complex construction screams for a nicer implementation! (ton) */
1371 if (R.r.mode & R_SHADOW) {
1372 if (ma->mode & MA_SHADOW) {
1373 if (lar->type == LA_HEMI || lar->type == LA_AREA) {
1376 else if ((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1377 float thresh= shi->obr->ob->smoothresh;
1379 phongcorr= (inp-thresh)/(inp*(1.0f-thresh));
1383 else if (ma->sbias!=0.0f && ((lar->mode & LA_SHAD_RAY) || lar->shb)) {
1385 phongcorr= (inp-ma->sbias)/(inp*(1.0f-ma->sbias));
1392 /* diffuse shaders */
1393 if (lar->mode & LA_NO_DIFF) {
1394 is = 0.0f; /* skip shaders */
1396 else if (lar->type==LA_HEMI) {
1397 is = 0.5f * inp + 0.5f;
1401 if (lar->type==LA_AREA)
1402 inp= area_lamp_energy_multisample(lar, shi->co, vn);
1404 /* diffuse shaders (oren nayer gets inp from area light) */
1405 if (ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1406 else if (ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1407 else if (ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1408 else if (ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1409 else is= inp; /* Lambert */
1412 /* 'is' is diffuse */
1413 if ((ma->shade_flag & MA_CUBIC) && is > 0.0f && is < 1.0f) {
1414 is= 3.0f * is * is - 2.0f * is * is * is; /* nicer termination of shades */
1420 i*= visifac*shi->refl;
1424 vn = shi->vn; /* bring back original vector, we use special specular shaders for tangent */
1425 if (ma->mode & MA_TANGENT_V)
1428 /* init transp shadow */
1429 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1431 /* shadow and spec, (visifac==0 outside spot) */
1432 if (visifac> 0.0f) {
1434 if ((R.r.mode & R_SHADOW)) {
1435 if (ma->mode & MA_SHADOW) {
1436 if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1438 if (vn==vnor) /* tangent trigger */
1439 lamp_get_shadow(lar, shi, dot_v3v3(shi->vn, lv), shadfac, shi->depth);
1441 lamp_get_shadow(lar, shi, inp, shadfac, shi->depth);
1443 /* warning, here it skips the loop */
1444 if ((lar->mode & LA_ONLYSHADOW) && i>0.0f) {
1446 shadfac[3]= i*lar->energy*(1.0f-shadfac[3]);
1447 shr->shad[0] -= shadfac[3]*shi->r*(1.0f-lashdw[0]);
1448 shr->shad[1] -= shadfac[3]*shi->g*(1.0f-lashdw[1]);
1449 shr->shad[2] -= shadfac[3]*shi->b*(1.0f-lashdw[2]);
1451 shr->spec[0] -= shadfac[3]*shi->specr*(1.0f-lashdw[0]);
1452 shr->spec[1] -= shadfac[3]*shi->specg*(1.0f-lashdw[1]);
1453 shr->spec[2] -= shadfac[3]*shi->specb*(1.0f-lashdw[2]);
1459 shr->shad[3] = shadfac[3]; /* store this for possible check in troublesome cases */
1464 /* in case 'no diffuse' we still do most calculus, spec can be in shadow.*/
1465 if (!(lar->mode & LA_NO_DIFF)) {
1467 if (ma->mode & MA_SHADOW_TRA)
1468 add_to_diffuse(shr->shad, shi, is, i*shadfac[0]*lacol[0], i*shadfac[1]*lacol[1], i*shadfac[2]*lacol[2]);
1470 add_to_diffuse(shr->shad, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
1472 /* add light for colored shadow */
1473 if (i_noshad>i && !(lashdw[0]==0 && lashdw[1]==0 && lashdw[2]==0)) {
1474 add_to_diffuse(shr->shad, shi, is, lashdw[0]*(i_noshad-i)*lacol[0], lashdw[1]*(i_noshad-i)*lacol[1], lashdw[2]*(i_noshad-i)*lacol[2]);
1476 if (i_noshad>0.0f) {
1477 if (passflag & (SCE_PASS_DIFFUSE|SCE_PASS_SHADOW)) {
1478 add_to_diffuse(shr->diff, shi, is, i_noshad*lacol[0], i_noshad*lacol[1], i_noshad*lacol[2]);
1481 copy_v3_v3(shr->diff, shr->shad);
1486 shadfac[3]*= phongcorr; /* note, shadfac not allowed to be stored nonlocal */
1488 if (shadfac[3]>0.0f && shi->spec!=0.0f && !(lar->mode & LA_NO_SPEC) && !(lar->mode & LA_ONLYSHADOW)) {
1490 if (!(passflag & (SCE_PASS_COMBINED | SCE_PASS_SPEC))) {
1493 else if (lar->type == LA_HEMI) {
1495 /* hemi uses no spec shaders (yet) */
1503 t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1505 if (lar->type==LA_HEMI) {
1509 t= shadfac[3]*shi->spec*spec(t, shi->har);
1511 shr->spec[0]+= t*(lacol[0] * shi->specr);
1512 shr->spec[1]+= t*(lacol[1] * shi->specg);
1513 shr->spec[2]+= t*(lacol[2] * shi->specb);
1516 /* specular shaders */
1519 if (ma->spec_shader==MA_SPEC_PHONG)
1520 specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1521 else if (ma->spec_shader==MA_SPEC_COOKTORR)
1522 specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1523 else if (ma->spec_shader==MA_SPEC_BLINN)
1524 specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1525 else if (ma->spec_shader==MA_SPEC_WARDISO)
1526 specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1528 specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1530 /* area lamp correction */
1531 if (lar->type==LA_AREA) specfac*= inp;
1533 t= shadfac[3]*shi->spec*visifac*specfac;
1535 if (ma->mode & MA_RAMP_SPEC) {
1537 do_specular_ramp(shi, specfac, t, spec);
1538 shr->spec[0]+= t*(lacol[0] * spec[0]);
1539 shr->spec[1]+= t*(lacol[1] * spec[1]);
1540 shr->spec[2]+= t*(lacol[2] * spec[2]);
1543 shr->spec[0]+= t*(lacol[0] * shi->specr);
1544 shr->spec[1]+= t*(lacol[1] * shi->specg);
1545 shr->spec[2]+= t*(lacol[2] * shi->specb);
1552 static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr)
1555 if (R.r.mode & R_SHADOW) {
1560 float /* *view, */ shadfac[4];
1561 float ir, accum, visifac, lampdist;
1562 float shaded = 0.0f, lightness = 0.0f;
1565 /* view= shi->view; */ /* UNUSED */
1568 lights= get_lights(shi);
1569 for (go=lights->first; go; go= go->next) {
1571 if (lar==NULL) continue;
1573 /* yafray: ignore shading by photonlights, not used in Blender */
1574 if (lar->type==LA_YF_PHOTON) continue;
1576 if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1577 if ((lar->lay & shi->lay)==0) continue;
1579 if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1580 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1583 if (visifac <= 0.0f) {
1584 if (shi->mat->shadowonly_flag == MA_SO_OLD)
1589 inpr= dot_v3v3(shi->vn, lv);
1591 if (shi->mat->shadowonly_flag == MA_SO_OLD)
1597 lamp_get_shadow(lar, shi, inpr, shadfac, shi->depth);
1599 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1600 /* Old "Shadows Only" */
1601 accum+= (1.0f-visifac) + (visifac)*rgb_to_grayscale(shadfac)*shadfac[3];
1604 shaded += rgb_to_grayscale(shadfac)*shadfac[3] * visifac * lar->energy;
1606 if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1607 lightness += visifac * lar->energy;
1613 /* Apply shadows as alpha */
1615 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1616 accum = 1.0f - accum/ir;
1619 if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1620 if (lightness > 0.0f) {
1621 /* Get shadow value from between 0.0f and non-shadowed lightness */
1622 accum = (lightness - shaded) / (lightness);
1628 else { /* shadowonly_flag == MA_SO_SHADED */
1629 /* Use shaded value */
1630 accum = 1.0f - shaded;
1633 shr->alpha= (shi->alpha)*(accum);
1634 if (shr->alpha<0.0f) shr->alpha=0.0f;
1637 /* If "fully shaded", use full alpha even on areas that have no lights */
1638 if (shi->mat->shadowonly_flag == MA_SO_SHADED) shr->alpha=shi->alpha;
1639 else shr->alpha= 0.f;
1643 /* quite disputable this... also note it doesn't mirror-raytrace */
1644 if ((R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT)) && shi->amb!=0.0f) {
1647 if (R.wrld.mode & WO_AMB_OCC) {
1648 f= R.wrld.aoenergy*shi->amb;
1650 if (R.wrld.aomix==WO_AOADD) {
1651 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1652 f= f*(1.0f - rgb_to_grayscale(shi->ao));
1653 shr->alpha= (shr->alpha + f)*f;
1656 shr->alpha -= f*rgb_to_grayscale(shi->ao);
1657 if (shr->alpha<0.0f) shr->alpha=0.0f;
1660 else /* AO Multiply */
1661 shr->alpha= (1.0f - f)*shr->alpha + f*(1.0f - (1.0f - shr->alpha)*rgb_to_grayscale(shi->ao));
1664 if (R.wrld.mode & WO_ENV_LIGHT) {
1665 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1666 f= R.wrld.ao_env_energy*shi->amb*(1.0f - rgb_to_grayscale(shi->env));
1667 shr->alpha= (shr->alpha + f)*f;
1670 f= R.wrld.ao_env_energy*shi->amb;
1671 shr->alpha -= f*rgb_to_grayscale(shi->env);
1672 if (shr->alpha<0.0f) shr->alpha=0.0f;
1678 /* let's map negative light as if it mirrors positive light, otherwise negative values disappear */
1679 static void wrld_exposure_correct(float diff[3])
1682 diff[0]= R.wrld.linfac*(1.0f-expf( diff[0]*R.wrld.logfac) );
1683 diff[1]= R.wrld.linfac*(1.0f-expf( diff[1]*R.wrld.logfac) );
1684 diff[2]= R.wrld.linfac*(1.0f-expf( diff[2]*R.wrld.logfac) );
1687 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1689 Material *ma= shi->mat;
1690 int passflag= shi->passflag;
1692 memset(shr, 0, sizeof(ShadeResult));
1694 if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1697 if (ma->mode & MA_ONLYSHADOW) {
1698 shade_lamp_loop_only_shadow(shi, shr);
1702 /* envmap hack, always reset */
1703 shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0f;
1705 /* material color itself */
1706 if (passflag & (SCE_PASS_COMBINED|SCE_PASS_RGBA)) {
1707 if (ma->mode & (MA_FACETEXTURE)) {
1708 shi->r= shi->vcol[0];
1709 shi->g= shi->vcol[1];
1710 shi->b= shi->vcol[2];
1711 if (ma->mode & (MA_FACETEXTURE_ALPHA))
1712 shi->alpha= shi->vcol[3];
1714 else if (ma->mode & (MA_VERTEXCOLP)) {
1715 float neg_alpha = 1.0f - shi->vcol[3];
1716 shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
1717 shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
1718 shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
1721 do_material_tex(shi, &R);
1722 if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1725 shr->col[0]= shi->r*shi->alpha;
1726 shr->col[1]= shi->g*shi->alpha;
1727 shr->col[2]= shi->b*shi->alpha;
1728 shr->col[3]= shi->alpha;
1730 if ((ma->sss_flag & MA_DIFF_SSS) && !sss_pass_done(&R, ma)) {
1731 if (ma->sss_texfac == 0.0f) {
1732 shi->r= shi->g= shi->b= shi->alpha= 1.0f;
1733 shr->col[0]= shr->col[1]= shr->col[2]= shr->col[3]= 1.0f;
1736 shi->r= pow(shi->r, ma->sss_texfac);
1737 shi->g= pow(shi->g, ma->sss_texfac);
1738 shi->b= pow(shi->b, ma->sss_texfac);
1739 shi->alpha= pow(shi->alpha, ma->sss_texfac);
1741 shr->col[0]= pow(shr->col[0], ma->sss_texfac);
1742 shr->col[1]= pow(shr->col[1], ma->sss_texfac);
1743 shr->col[2]= pow(shr->col[2], ma->sss_texfac);
1744 shr->col[3]= pow(shr->col[3], ma->sss_texfac);
1749 if (ma->mode & MA_SHLESS) {
1750 shr->combined[0]= shi->r;
1751 shr->combined[1]= shi->g;
1752 shr->combined[2]= shi->b;
1753 shr->alpha= shi->alpha;
1757 if ( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) { /* vertexcolor light */
1758 shr->emit[0]= shi->r*(shi->emit+shi->vcol[0]*shi->vcol[3]);
1759 shr->emit[1]= shi->g*(shi->emit+shi->vcol[1]*shi->vcol[3]);
1760 shr->emit[2]= shi->b*(shi->emit+shi->vcol[2]*shi->vcol[3]);
1763 shr->emit[0]= shi->r*shi->emit;
1764 shr->emit[1]= shi->g*shi->emit;
1765 shr->emit[2]= shi->b*shi->emit;
1769 if (R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) {
1770 if (((passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT))) ||
1771 (passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1773 if (R.r.mode & R_SHADOW) {
1774 /* AO was calculated for scanline already */
1775 if (shi->depth || shi->volume_depth)
1776 ambient_occlusion(shi);
1777 copy_v3_v3(shr->ao, shi->ao);
1778 copy_v3_v3(shr->env, shi->env); /* XXX multiply */
1779 copy_v3_v3(shr->indirect, shi->indirect); /* XXX multiply */
1785 if (passflag & (SCE_PASS_COMBINED|SCE_PASS_DIFFUSE|SCE_PASS_SPEC|SCE_PASS_SHADOW)) {
1790 lights= get_lights(shi);
1791 for (go=lights->first; go; go= go->next) {
1793 if (lar==NULL) continue;
1795 /* yafray: ignore shading by photonlights, not used in Blender */
1796 if (lar->type==LA_YF_PHOTON) continue;
1798 /* test for lamp layer */
1799 if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1800 if ((lar->lay & shi->lay)==0) continue;
1802 /* accumulates in shr->diff and shr->spec and shr->shad (diffuse with shadow!) */
1803 shade_one_light(lar, shi, shr, passflag);
1806 /* this check is to prevent only shadow lamps from producing negative
1808 if (shr->spec[0] < 0) shr->spec[0] = 0;
1809 if (shr->spec[1] < 0) shr->spec[1] = 0;
1810 if (shr->spec[2] < 0) shr->spec[2] = 0;
1812 if (shr->shad[0] < 0) shr->shad[0] = 0;
1813 if (shr->shad[1] < 0) shr->shad[1] = 0;
1814 if (shr->shad[2] < 0) shr->shad[2] = 0;
1816 if (ma->sss_flag & MA_DIFF_SSS) {
1817 float sss[3], col[3], invalpha, texfac= ma->sss_texfac;
1819 /* this will return false in the preprocess stage */
1820 if (sample_sss(&R, ma, shi->co, sss)) {
1821 invalpha= (shr->col[3] > FLT_EPSILON)? 1.0f/shr->col[3]: 1.0f;
1824 copy_v3_v3(col, shr->col);
1825 mul_v3_fl(col, invalpha);
1827 else if (texfac==1.0f) {
1828 col[0]= col[1]= col[2]= 1.0f;
1829 mul_v3_fl(col, invalpha);
1832 copy_v3_v3(col, shr->col);
1833 mul_v3_fl(col, invalpha);
1834 col[0]= pow(col[0], 1.0f-texfac);
1835 col[1]= pow(col[1], 1.0f-texfac);
1836 col[2]= pow(col[2], 1.0f-texfac);
1839 shr->diff[0]= sss[0]*col[0];
1840 shr->diff[1]= sss[1]*col[1];
1841 shr->diff[2]= sss[2]*col[2];
1843 if (shi->combinedflag & SCE_PASS_SHADOW) {
1844 shr->shad[0]= shr->diff[0];
1845 shr->shad[1]= shr->diff[1];
1846 shr->shad[2]= shr->diff[2];
1851 if (shi->combinedflag & SCE_PASS_SHADOW)
1852 copy_v3_v3(shr->combined, shr->shad); /* note, no ';' ! */
1854 copy_v3_v3(shr->combined, shr->diff);
1856 /* calculate shadow pass, we use a multiplication mask */
1857 /* if diff = 0,0,0 it doesn't matter what the shadow pass is, so leave it as is */
1858 if (passflag & SCE_PASS_SHADOW && !(shr->diff[0]==0.0f && shr->diff[1]==0.0f && shr->diff[2]==0.0f)) {
1859 if (shr->diff[0]!=0.0f) shr->shad[0]= shr->shad[0]/shr->diff[0];
1860 /* can't determine proper shadow from shad/diff (0/0), so use shadow intensity */
1861 else if (shr->shad[0]==0.0f) shr->shad[0]= shr->shad[3];
1863 if (shr->diff[1]!=0.0f) shr->shad[1]= shr->shad[1]/shr->diff[1];
1864 else if (shr->shad[1]==0.0f) shr->shad[1]= shr->shad[3];
1866 if (shr->diff[2]!=0.0f) shr->shad[2]= shr->shad[2]/shr->diff[2];
1867 else if (shr->shad[2]==0.0f) shr->shad[2]= shr->shad[3];
1870 /* exposure correction */
1871 if ((R.wrld.exp!=0.0f || R.wrld.range!=1.0f) && !R.sss_points) {
1872 wrld_exposure_correct(shr->combined); /* has no spec! */
1873 wrld_exposure_correct(shr->spec);
1877 /* alpha in end, spec can influence it */
1878 if (passflag & (SCE_PASS_COMBINED)) {
1879 if ((ma->fresnel_tra!=0.0f) && (shi->mode & MA_TRANSP))
1880 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1882 /* note: shi->mode! */
1883 if (shi->mode & MA_TRANSP && (shi->mode & (MA_ZTRANSP|MA_RAYTRANSP))) {
1884 if (shi->spectra!=0.0f) {
1885 float t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
1887 if (t>1.0f) t= 1.0f;
1888 shi->alpha= (1.0f-t)*shi->alpha+t;
1892 shr->alpha= shi->alpha;
1894 /* from now stuff everything in shr->combined: ambient, AO, ramps, exposure */
1895 if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1896 if (R.r.mode & R_SHADOW) {
1897 /* add AO in combined? */
1898 if (R.wrld.mode & WO_AMB_OCC)
1899 if (shi->combinedflag & SCE_PASS_AO)
1900 ambient_occlusion_apply(shi, shr);
1902 if (R.wrld.mode & WO_ENV_LIGHT)
1903 if (shi->combinedflag & SCE_PASS_ENVIRONMENT)
1904 environment_lighting_apply(shi, shr);
1906 if (R.wrld.mode & WO_INDIRECT_LIGHT)
1907 if (shi->combinedflag & SCE_PASS_INDIRECT)
1908 indirect_lighting_apply(shi, shr);
1911 shr->combined[0]+= shi->ambr;
1912 shr->combined[1]+= shi->ambg;
1913 shr->combined[2]+= shi->ambb;
1915 if (ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->combined, shi);
1918 if (ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shi);
1920 /* refcol is for envmap only */
1921 if (shi->refcol[0]!=0.0f) {
1924 result[0]= shi->mirr*shi->refcol[1] + (1.0f - shi->mirr*shi->refcol[0])*shr->combined[0];
1925 result[1]= shi->mirg*shi->refcol[2] + (1.0f - shi->mirg*shi->refcol[0])*shr->combined[1];
1926 result[2]= shi->mirb*shi->refcol[3] + (1.0f - shi->mirb*shi->refcol[0])*shr->combined[2];
1928 if (passflag & SCE_PASS_REFLECT)
1929 sub_v3_v3v3(shr->refl, result, shr->combined);
1931 if (shi->combinedflag & SCE_PASS_REFLECT)
1932 copy_v3_v3(shr->combined, result);
1936 /* and add emit and spec */
1937 if (shi->combinedflag & SCE_PASS_EMIT)
1938 add_v3_v3(shr->combined, shr->emit);
1939 if (shi->combinedflag & SCE_PASS_SPEC)
1940 add_v3_v3(shr->combined, shr->spec);
1942 /* modulate by the object color */
1943 if ((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
1944 if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1947 copy_v4_v4(obcol, shi->obr->ob->col);
1948 CLAMP(obcol[3], 0.0f, 1.0f);
1950 shr->combined[0] *= obcol[0];
1951 shr->combined[1] *= obcol[1];
1952 shr->combined[2] *= obcol[2];
1953 if (shi->mode & MA_TRANSP) shr->alpha *= obcol[3];
1957 shr->combined[3]= shr->alpha;