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 */
62 # pragma GCC diagnostic ignored "-Wdouble-promotion"
65 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
66 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
67 /* only to be used here in this file, it's for speed */
68 extern struct Render R;
69 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
71 ListBase *get_lights(ShadeInput *shi)
74 if (R.r.scemode & R_PREVIEWBUTS)
76 if (shi->light_override)
77 return &shi->light_override->gobject;
78 if (shi->mat && shi->mat->group)
79 return &shi->mat->group->gobject;
85 static void fogcolor(const float colf[3], float *rco, float *view)
87 float alpha, stepsize, startdist, dist, hor[4], zen[3], vec[3], dview[3];
88 float div=0.0f, distfac;
90 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
91 zen[0]= R.wrld.zenr; zen[1]= R.wrld.zeng; zen[2]= R.wrld.zenb;
95 /* we loop from cur coord to mist start in steps */
99 dview[0]= view[0]/(stepsize*div);
100 dview[1]= view[1]/(stepsize*div);
103 startdist= -rco[2] + BLI_frand();
104 for (dist= startdist; dist>R.wrld.miststa; dist-= stepsize) {
106 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
108 do_sky_tex(vec, vec, NULL, hor, zen, &alpha);
110 distfac= (dist-R.wrld.miststa)/R.wrld.mistdist;
112 hor[3]= hor[0]*distfac*distfac;
116 hor[0]= hor[0]*alpha;
117 hor[1]= hor[1]*alpha;
118 hor[2]= hor[2]*alpha;
119 addAlphaOverFloat(colf, hor);
121 sub_v3_v3(vec, dview);
126 /* zcor is distance, co the 3d coordinate in eye space, return alpha */
127 float mistfactor(float zcor, float const co[3])
131 fac = zcor - R.wrld.miststa; /* zcor is calculated per pixel */
133 /* fac= -co[2]-R.wrld.miststa; */
136 if (fac < R.wrld.mistdist) {
138 fac = (fac / R.wrld.mistdist);
140 if (R.wrld.mistype == 0) {
143 else if (R.wrld.mistype == 1) {
158 /* height switched off mist */
159 if (R.wrld.misthi!=0.0f && fac!=0.0f) {
160 /* at height misthi the mist is completely gone */
162 hi = R.viewinv[0][2] * co[0] +
163 R.viewinv[1][2] * co[1] +
164 R.viewinv[2][2] * co[2] +
167 if (hi > R.wrld.misthi) {
171 hi= (R.wrld.misthi-hi)/R.wrld.misthi;
176 return (1.0f-fac)* (1.0f-R.wrld.misi);
179 static void spothalo(struct LampRen *lar, ShadeInput *shi, float *intens)
181 double a, b, c, disc, nray[3], npos[3];
182 double t0, t1 = 0.0f, t2= 0.0f, t3;
183 float p1[3], p2[3], ladist, maxz = 0.0f, maxy = 0.0f, haint;
184 int snijp, do_clip = TRUE, use_yco = FALSE;
189 if (R.r.mode & R_ORTHO) {
190 /* camera pos (view vector) cannot be used... */
191 /* camera position (cox,coy,0) rotate around lamp */
192 p1[0]= shi->co[0]-lar->co[0];
193 p1[1]= shi->co[1]-lar->co[1];
195 mul_m3_v3(lar->imat, p1);
196 copy_v3db_v3fl(npos, p1); /* npos is double! */
199 npos[2] *= (double)lar->sh_zfac;
202 copy_v3db_v3fl(npos, lar->sh_invcampos); /* in initlamp calculated */
206 copy_v3db_v3fl(nray, shi->view);
207 mul_m3_v3_double(lar->imat, nray);
209 if (R.wrld.mode & WO_MIST) {
211 haint *= mistfactor(-lar->co[2], lar->co);
219 if (shi->co[2]==0.0f) {
220 do_clip = FALSE; /* for when halo at sky */
223 p1[0]= shi->co[0]-lar->co[0];
224 p1[1]= shi->co[1]-lar->co[1];
225 p1[2]= shi->co[2]-lar->co[2];
227 maxz= lar->imat[0][2]*p1[0]+lar->imat[1][2]*p1[1]+lar->imat[2][2]*p1[2];
229 maxy= lar->imat[0][1]*p1[0]+lar->imat[1][1]*p1[1]+lar->imat[2][1]*p1[2];
231 if (fabsf(nray[2]) < FLT_EPSILON) {
236 /* scale z to make sure volume is normalized */
237 nray[2] *= (double)lar->sh_zfac;
238 /* nray does not need normalization */
240 ladist= lar->sh_zfac*lar->dist;
243 a = nray[0] * nray[0] + nray[1] * nray[1] - nray[2]*nray[2];
244 b = nray[0] * npos[0] + nray[1] * npos[1] - nray[2]*npos[2];
245 c = npos[0] * npos[0] + npos[1] * npos[1] - npos[2]*npos[2];
248 if (fabs(a) < DBL_EPSILON) {
250 * Only one intersection point...
261 else if (disc > 0.0) {
263 t1 = (-b + disc) / a;
264 t2 = (-b - disc) / a;
273 a= t1; t1= t2; t2= a;
276 /* z of intersection points with diabolo */
277 p1[2]= npos[2] + t1*nray[2];
278 p2[2]= npos[2] + t2*nray[2];
280 /* evaluate both points */
281 if (p1[2]<=0.0f) ok1= 1;
282 if (p2[2]<=0.0f && t1!=t2) ok2= 1;
284 /* at least 1 point with negative z */
285 if (ok1==0 && ok2==0) return;
287 /* intersction point with -ladist, the bottom of the cone */
288 if (use_yco == FALSE) {
289 t3= ((double)(-ladist)-npos[2])/nray[2];
291 /* de we have to replace one of the intersection points? */
293 if (p1[2]<-ladist) t1= t3;
299 if (p2[2]<-ladist) t2= t3;
305 else if (ok1==0 || ok2==0) return;
307 /* at least 1 visible interesction point */
308 if (t1<0.0 && t2<0.0) return;
315 /* sort again to be sure */
317 a= t1; t1= t2; t2= a;
320 /* calculate t0: is the maximum visible z (when halo is intersected by face) */
322 if (use_yco == FALSE) t0 = ((double)maxz - npos[2]) / nray[2];
323 else t0 = ((double)maxy - npos[1]) / nray[1];
330 p1[0]= npos[0] + t1*nray[0];
331 p1[1]= npos[1] + t1*nray[1];
332 p1[2]= npos[2] + t1*nray[2];
333 p2[0]= npos[0] + t2*nray[0];
334 p2[1]= npos[1] + t2*nray[1];
335 p2[2]= npos[2] + t2*nray[2];
338 /* now we have 2 points, make three lengths with it */
340 a= sqrt(p1[0]*p1[0]+p1[1]*p1[1]+p1[2]*p1[2]);
341 b= sqrt(p2[0]*p2[0]+p2[1]*p2[1]+p2[2]*p2[2]);
350 *intens= c*( (1.0-a)+(1.0-b) );
352 /* WATCH IT: do not clip a,b en c at 1.0, this gives nasty little overflows
353 * at the edges (especially with narrow halos) */
354 if (*intens<=0.0f) return;
357 /* not needed because t0 has been used for p1/p2 as well */
358 /* if (doclip && t0<t2) { */
359 /* *intens *= (t0-t1)/(t2-t1); */
364 if (lar->shb && lar->shb->shadhalostep) {
365 *intens *= shadow_halo(lar, p1, p2);
371 void renderspothalo(ShadeInput *shi, float col[4], float alpha)
378 if (alpha==0.0f) return;
380 lights= get_lights(shi);
381 for (go=lights->first; go; go= go->next) {
383 if (lar==NULL) continue;
385 if (lar->type==LA_SPOT && (lar->mode & LA_HALO) && (lar->buftype != LA_SHADBUF_DEEP) && lar->haint>0) {
387 if (lar->mode & LA_LAYER)
388 if (shi->vlr && (lar->lay & shi->obi->lay)==0)
390 if ((lar->lay & shi->lay)==0)
393 spothalo(lar, shi, &i);
395 const float i_alpha = i * alpha;
396 col[0] += i_alpha * lar->r;
397 col[1] += i_alpha * lar->g;
398 col[2] += i_alpha * lar->b;
399 col[3] += i_alpha; /* all premul */
403 /* clip alpha, is needed for unified 'alpha threshold' (vanillaRenderPipe.c) */
404 if (col[3]>1.0f) col[3]= 1.0f;
409 /* ---------------- shaders ----------------------- */
411 static double Normalize_d(double *n)
415 d= n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
417 if (d>0.00000000000000001) {
431 /* mix of 'real' fresnel and allowing control. grad defines blending gradient */
432 float fresnel_fac(const float view[3], const float vn[3], float grad, float fac)
436 if (fac==0.0f) return 1.0f;
438 t1 = dot_v3v3(view, vn);
439 if (t1>0.0f) t2= 1.0f+t1;
442 t2= grad + (1.0f-grad)*powf(t2, fac);
444 if (t2<0.0f) return 0.0f;
445 else if (t2>1.0f) return 1.0f;
449 static double saacos_d(double fac)
451 if (fac<= -1.0) return M_PI;
452 else if (fac>=1.0) return 0.0;
453 else return acos(fac);
456 /* Stoke's form factor. Need doubles here for extreme small area sizes */
457 static float area_lamp_energy(float (*area)[3], const float co[3], const float vn[3])
460 double vec[4][3]; /* vectors of rendered co to vertices lamp */
461 double cross[4][3]; /* cross products of this */
462 double rad[4]; /* angles between vecs */
464 VECSUB(vec[0], co, area[0]);
465 VECSUB(vec[1], co, area[1]);
466 VECSUB(vec[2], co, area[2]);
467 VECSUB(vec[3], co, area[3]);
475 #define CROSS(dest, a, b) \
477 dest[0]= a[1] * b[2] - a[2] * b[1]; \
478 dest[1]= a[2] * b[0] - a[0] * b[2]; \
479 dest[2]= a[0] * b[1] - a[1] * b[0]; \
482 CROSS(cross[0], vec[0], vec[1]);
483 CROSS(cross[1], vec[1], vec[2]);
484 CROSS(cross[2], vec[2], vec[3]);
485 CROSS(cross[3], vec[3], vec[0]);
489 Normalize_d(cross[0]);
490 Normalize_d(cross[1]);
491 Normalize_d(cross[2]);
492 Normalize_d(cross[3]);
495 rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
496 rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
497 rad[2]= vec[2][0]*vec[3][0]+ vec[2][1]*vec[3][1]+ vec[2][2]*vec[3][2];
498 rad[3]= vec[3][0]*vec[0][0]+ vec[3][1]*vec[0][1]+ vec[3][2]*vec[0][2];
500 rad[0]= saacos_d(rad[0]);
501 rad[1]= saacos_d(rad[1]);
502 rad[2]= saacos_d(rad[2]);
503 rad[3]= saacos_d(rad[3]);
506 fac= rad[0]*(vn[0]*cross[0][0]+ vn[1]*cross[0][1]+ vn[2]*cross[0][2]);
507 fac+= rad[1]*(vn[0]*cross[1][0]+ vn[1]*cross[1][1]+ vn[2]*cross[1][2]);
508 fac+= rad[2]*(vn[0]*cross[2][0]+ vn[1]*cross[2][1]+ vn[2]*cross[2][2]);
509 fac+= rad[3]*(vn[0]*cross[3][0]+ vn[1]*cross[3][1]+ vn[2]*cross[3][2]);
511 if (fac<=0.0) return 0.0;
515 static float area_lamp_energy_multisample(LampRen *lar, const float co[3], float *vn)
517 /* corner vectors are moved around according lamp jitter */
518 float *jitlamp= lar->jitter, vec[3];
519 float area[4][3], intens= 0.0f;
520 int a= lar->ray_totsamp;
522 /* test if co is behind lamp */
523 sub_v3_v3v3(vec, co, lar->co);
524 if (dot_v3v3(vec, lar->vec) < 0.0f)
531 mul_m3_v3(lar->mat, vec);
533 add_v3_v3v3(area[0], lar->area[0], vec);
534 add_v3_v3v3(area[1], lar->area[1], vec);
535 add_v3_v3v3(area[2], lar->area[2], vec);
536 add_v3_v3v3(area[3], lar->area[3], vec);
538 intens+= area_lamp_energy(area, co, vn);
542 intens /= (float)lar->ray_totsamp;
544 return pow(intens * lar->areasize, lar->k); /* corrected for buttons size and lar->dist^2 */
547 static float spec(float inp, int hard)
551 if (inp>=1.0f) return 1.0f;
552 else if (inp<=0.0f) return 0.0f;
556 if (b1<0.01f) b1= 0.01f;
558 if ((hard & 1)==0) inp= 1.0f;
559 if (hard & 2) inp*= b1;
561 if (hard & 4) inp*= b1;
563 if (hard & 8) inp*= b1;
565 if (hard & 16) inp*= b1;
569 if (b1<0.001f) b1= 0.0f;
571 if (hard & 32) inp*= b1;
573 if (hard & 64) inp*=b1;
575 if (hard & 128) inp*=b1;
577 if (b1<0.001f) b1= 0.0f;
587 static float Phong_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent )
597 rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
598 if (tangent) rslt= sasqrt(1.0f - rslt*rslt);
600 if ( rslt > 0.0f ) rslt= spec(rslt, hard);
607 /* reduced cook torrance spec (for off-specular peak) */
608 static float CookTorr_Spec(const float n[3], const float l[3], const float v[3], int hard, int tangent)
610 float i, nh, nv, h[3];
617 nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2];
618 if (tangent) nh= sasqrt(1.0f - nh*nh);
619 else if (nh<0.0f) return 0.0f;
621 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
622 if (tangent) nv= sasqrt(1.0f - nv*nv);
623 else if (nv<0.0f) nv= 0.0f;
632 static float Blinn_Spec(const float n[3], const float l[3], const float v[3], float refrac, float spec_power, int tangent)
634 float i, nh, nv, nl, vh, h[3];
635 float a, b, c, g=0.0f, p, f, ang;
637 if (refrac < 1.0f) return 0.0f;
638 if (spec_power == 0.0f) return 0.0f;
640 /* conversion from 'hardness' (1-255) to 'spec_power' (50 maps at 0.1) */
641 if (spec_power<100.0f)
642 spec_power= sqrt(1.0f/spec_power);
643 else spec_power= 10.0f/spec_power;
650 nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
651 if (tangent) nh= sasqrt(1.0f - nh*nh);
652 else if (nh<0.0f) return 0.0f;
654 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
655 if (tangent) nv= sasqrt(1.0f - nv*nv);
656 if (nv<=0.01f) nv= 0.01f; /* hrms... */
658 nl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
659 if (tangent) nl= sasqrt(1.0f - nl*nl);
664 vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and half-way vector */
665 if (vh<=0.0f) vh= 0.01f;
671 if ( a < b && a < c ) g = a;
672 else if ( b < a && b < c ) g = b;
673 else if ( c < a && c < b ) g = c;
675 p = sqrt((double)((refrac * refrac)+(vh * vh) - 1.0f));
676 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))));
679 i= f * g * exp((double)(-(ang*ang) / (2.0f*spec_power*spec_power)));
685 /* cartoon render spec */
686 static float Toon_Spec(const float n[3], const float l[3], const float v[3], float size, float smooth, int tangent)
697 rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
698 if (tangent) rslt = sasqrt(1.0f - rslt*rslt);
700 ang = saacos( rslt );
702 if ( ang < size ) rslt = 1.0f;
703 else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
704 else rslt = 1.0f - ((ang - size) / smooth);
709 /* Ward isotropic gaussian spec */
710 static float WardIso_Spec(const float n[3], const float l[3], const float v[3], float rms, int tangent)
712 float i, nh, nv, nl, h[3], angle, alpha;
715 /* half-way vector */
721 nh = n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
722 if (tangent) nh = sasqrt(1.0f - nh*nh);
723 if (nh<=0.0f) nh = 0.001f;
725 nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
726 if (tangent) nv = sasqrt(1.0f - nv*nv);
727 if (nv<=0.0f) nv = 0.001f;
729 nl = n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
730 if (tangent) nl = sasqrt(1.0f - nl*nl);
731 if (nl<=0.0f) nl = 0.001f;
733 angle = tan(saacos(nh));
734 alpha = MAX2(rms, 0.001f);
736 i= nl * (1.0f/(4.0f*(float)M_PI*alpha*alpha)) * (expf( -(angle*angle)/(alpha*alpha))/(sqrtf(nv*nl)));
741 /* cartoon render diffuse */
742 static float Toon_Diff(const float n[3], const float l[3], const float UNUSED(v[3]), float size, float smooth)
746 rslt = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
748 ang = saacos( (double)(rslt) );
750 if ( ang < size ) rslt = 1.0f;
751 else if ( ang >= (size + smooth) || smooth == 0.0f ) rslt = 0.0f;
752 else rslt = 1.0f - ((ang - size) / smooth);
757 /* Oren Nayar diffuse */
759 /* 'nl' is either dot product, or return value of area light */
760 /* in latter case, only last multiplication uses 'nl' */
761 static float OrenNayar_Diff(float nl, const float n[3], const float l[3], const float v[3], float rough )
763 float i/*, nh*/, nv /*, vh */, realnl, h[3];
765 float Lit_A, View_A, Lit_B[3], View_B[3];
772 /* nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; */ /* Dot product between surface normal and half-way vector */
773 /* if (nh<0.0f) nh = 0.0f; */
775 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
776 if (nv<=0.0f) nv= 0.0f;
778 realnl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
779 if (realnl<=0.0f) return 0.0f;
780 if (nl<0.0f) return 0.0f; /* value from area light */
782 /* vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; */ /* Dot product between view vector and halfway vector */
783 /* if (vh<=0.0f) vh= 0.0f; */
785 Lit_A = saacos(realnl);
786 View_A = saacos( nv );
788 Lit_B[0] = l[0] - (realnl * n[0]);
789 Lit_B[1] = l[1] - (realnl * n[1]);
790 Lit_B[2] = l[2] - (realnl * n[2]);
793 View_B[0] = v[0] - (nv * n[0]);
794 View_B[1] = v[1] - (nv * n[1]);
795 View_B[2] = v[2] - (nv * n[2]);
796 normalize_v3(View_B);
798 t = Lit_B[0]*View_B[0] + Lit_B[1]*View_B[1] + Lit_B[2]*View_B[2];
801 if ( Lit_A > View_A ) {
810 A = 1.0f - (0.5f * ((rough * rough) / ((rough * rough) + 0.33f)));
811 B = 0.45f * ((rough * rough) / ((rough * rough) + 0.09f));
813 b*= 0.95f; /* prevent tangens from shooting to inf, 'nl' can be not a dot product here. */
814 /* overflow only happens with extreme size area light, and higher roughness */
815 i = nl * ( A + ( B * t * sinf(a) * tanf(b) ) );
820 /* Minnaert diffuse */
821 static float Minnaert_Diff(float nl, const float n[3], const float v[3], float darkness)
825 /* nl = dot product between surface normal and light vector */
829 /* nv = dot product between surface normal and view vector */
834 if (darkness <= 1.0f)
835 i = nl * pow(max_ff(nv * nl, 0.1f), (darkness - 1.0f) ); /*The Real model*/
837 i = nl * pow( (1.001f - nv), (darkness - 1.0f) ); /*Nvidia model*/
842 static float Fresnel_Diff(float *vn, float *lv, float *UNUSED(view), float fac_i, float fac)
844 return fresnel_fac(lv, vn, fac_i, fac);
847 /* --------------------------------------------- */
848 /* also called from texture.c */
849 void calc_R_ref(ShadeInput *shi)
853 /* shi->vn dot shi->view */
854 i= -2*(shi->vn[0]*shi->view[0]+shi->vn[1]*shi->view[1]+shi->vn[2]*shi->view[2]);
856 shi->ref[0]= (shi->view[0]+i*shi->vn[0]);
857 shi->ref[1]= (shi->view[1]+i*shi->vn[1]);
858 shi->ref[2]= (shi->view[2]+i*shi->vn[2]);
860 if (shi->vlr->flag & R_SMOOTH) {
861 i= -2*( (shi->vn[0]+shi->dxno[0])*(shi->view[0]+shi->dxview) +
862 (shi->vn[1]+shi->dxno[1])*shi->view[1]+ (shi->vn[2]+shi->dxno[2])*shi->view[2] );
864 shi->dxref[0]= shi->ref[0]- ( shi->view[0]+shi->dxview+i*(shi->vn[0]+shi->dxno[0]));
865 shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*(shi->vn[1]+shi->dxno[1]));
866 shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dxno[2]));
868 i= -2*( (shi->vn[0]+shi->dyno[0])*shi->view[0]+
869 (shi->vn[1]+shi->dyno[1])*(shi->view[1]+shi->dyview)+ (shi->vn[2]+shi->dyno[2])*shi->view[2] );
871 shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*(shi->vn[0]+shi->dyno[0]));
872 shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*(shi->vn[1]+shi->dyno[1]));
873 shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dyno[2]));
878 i= -2*( shi->vn[0]*(shi->view[0]+shi->dxview) +
879 shi->vn[1]*shi->view[1]+ shi->vn[2]*shi->view[2] );
881 shi->dxref[0]= shi->ref[0]- (shi->view[0]+shi->dxview+i*shi->vn[0]);
882 shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*shi->vn[1]);
883 shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
885 i= -2*( shi->vn[0]*shi->view[0]+
886 shi->vn[1]*(shi->view[1]+shi->dyview)+ shi->vn[2]*shi->view[2] );
888 shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*shi->vn[0]);
889 shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*shi->vn[1]);
890 shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
896 /* called from ray.c */
897 void shade_color(ShadeInput *shi, ShadeResult *shr)
899 Material *ma= shi->mat;
901 if (ma->mode & (MA_FACETEXTURE)) {
902 shi->r= shi->vcol[0];
903 shi->g= shi->vcol[1];
904 shi->b= shi->vcol[2];
905 if (ma->mode & (MA_FACETEXTURE_ALPHA))
906 shi->alpha= shi->vcol[3];
908 else if (ma->mode & (MA_VERTEXCOLP)) {
909 float neg_alpha = 1.0f - shi->vcol[3];
910 shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
911 shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
912 shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
916 do_material_tex(shi, &R);
918 if (ma->fresnel_tra!=0.0f)
919 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
921 if (!(shi->mode & MA_TRANSP)) shi->alpha= 1.0f;
923 shr->diff[0]= shi->r;
924 shr->diff[1]= shi->g;
925 shr->diff[2]= shi->b;
926 shr->alpha= shi->alpha;
929 /* ramp for at end of shade */
930 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
932 Material *ma= shi->mat;
936 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
937 float fac = rgb_to_grayscale(diff);
938 do_colorband(ma->ramp_col, fac, col);
940 /* blending method */
941 fac= col[3]*ma->rampfac_col;
943 ramp_blend(ma->rampblend_col, diff, fac, col);
948 /* r,g,b denote energy, ramp is used with different values to make new material color */
949 static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, float g, float b)
951 Material *ma= shi->mat;
953 if (ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
955 /* MA_RAMP_IN_RESULT is exceptional */
956 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
958 diff[0] += r * shi->r;
959 diff[1] += g * shi->g;
960 diff[2] += b * shi->b;
963 float colt[3], col[4];
967 switch (ma->rampin_col) {
968 case MA_RAMP_IN_ENERGY:
969 /* should use 'rgb_to_grayscale' but we only have a vector version */
970 fac= 0.3f*r + 0.58f*g + 0.12f*b;
972 case MA_RAMP_IN_SHADER:
976 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
983 do_colorband(ma->ramp_col, fac, col);
985 /* blending method */
986 fac= col[3]*ma->rampfac_col;
991 ramp_blend(ma->rampblend_col, colt, fac, col);
994 diff[0] += r * colt[0];
995 diff[1] += g * colt[1];
996 diff[2] += b * colt[2];
1000 diff[0] += r * shi->r;
1001 diff[1] += g * shi->g;
1002 diff[2] += b * shi->b;
1006 static void ramp_spec_result(float spec_col[3], ShadeInput *shi)
1008 Material *ma= shi->mat;
1010 if (ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
1012 float fac = rgb_to_grayscale(spec_col);
1014 do_colorband(ma->ramp_spec, fac, col);
1016 /* blending method */
1017 fac= col[3]*ma->rampfac_spec;
1019 ramp_blend(ma->rampblend_spec, spec_col, fac, col);
1024 /* is = dot product shade, t = spec energy */
1025 static void do_specular_ramp(ShadeInput *shi, float is, float t, float spec[3])
1027 Material *ma= shi->mat;
1029 spec[0]= shi->specr;
1030 spec[1]= shi->specg;
1031 spec[2]= shi->specb;
1033 /* MA_RAMP_IN_RESULT is exception */
1034 if (ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
1039 switch (ma->rampin_spec) {
1040 case MA_RAMP_IN_ENERGY:
1043 case MA_RAMP_IN_SHADER:
1046 case MA_RAMP_IN_NOR:
1047 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1054 do_colorband(ma->ramp_spec, fac, col);
1056 /* blending method */
1057 fac= col[3]*ma->rampfac_spec;
1059 ramp_blend(ma->rampblend_spec, spec, fac, col);
1063 /* pure AO, check for raytrace and world should have been done */
1064 /* preprocess, textures were not done, don't use shi->amb for that reason */
1065 void ambient_occlusion(ShadeInput *shi)
1067 if ((R.wrld.ao_gather_method == WO_AOGATHER_APPROX) && shi->mat->amb!=0.0f) {
1068 sample_occ(&R, shi);
1070 else if ((R.r.mode & R_RAYTRACE) && shi->mat->amb!=0.0f) {
1071 ray_ao(shi, shi->ao, shi->env);
1074 shi->ao[0]= shi->ao[1]= shi->ao[2]= 1.0f;
1076 zero_v3(shi->indirect);
1081 /* wrld mode was checked for */
1082 static void ambient_occlusion_apply(ShadeInput *shi, ShadeResult *shr)
1084 float f= R.wrld.aoenergy;
1085 float tmp[3], tmpspec[3];
1087 if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1092 if (R.wrld.aomix==WO_AOADD) {
1093 shr->combined[0] += shi->ao[0]*shi->r*shi->refl*f;
1094 shr->combined[1] += shi->ao[1]*shi->g*shi->refl*f;
1095 shr->combined[2] += shi->ao[2]*shi->b*shi->refl*f;
1097 else if (R.wrld.aomix==WO_AOMUL) {
1098 mul_v3_v3v3(tmp, shr->combined, shi->ao);
1099 mul_v3_v3v3(tmpspec, shr->spec, shi->ao);
1102 copy_v3_v3(shr->combined, tmp);
1103 copy_v3_v3(shr->spec, tmpspec);
1106 interp_v3_v3v3(shr->combined, shr->combined, tmp, f);
1107 interp_v3_v3v3(shr->spec, shr->spec, tmpspec, f);
1112 void environment_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1114 float f= R.wrld.ao_env_energy*shi->amb;
1116 if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1121 shr->combined[0] += shi->env[0]*shi->r*shi->refl*f;
1122 shr->combined[1] += shi->env[1]*shi->g*shi->refl*f;
1123 shr->combined[2] += shi->env[2]*shi->b*shi->refl*f;
1126 static void indirect_lighting_apply(ShadeInput *shi, ShadeResult *shr)
1128 float f= R.wrld.ao_indirect_energy;
1130 if (!((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1135 shr->combined[0] += shi->indirect[0]*shi->r*shi->refl*f;
1136 shr->combined[1] += shi->indirect[1]*shi->g*shi->refl*f;
1137 shr->combined[2] += shi->indirect[2]*shi->b*shi->refl*f;
1140 /* result written in shadfac */
1141 void lamp_get_shadow(LampRen *lar, ShadeInput *shi, float inp, float shadfac[4], int do_real)
1143 LampShadowSubSample *lss= &(lar->shadsamp[shi->thread].s[shi->sample]);
1145 if (do_real || lss->samplenr!=shi->samplenr) {
1147 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1150 if (lar->buftype==LA_SHADBUF_IRREGULAR)
1151 shadfac[3]= ISB_getshadow(shi, lar->shb);
1153 shadfac[3] = testshadowbuf(&R, lar->shb, shi->co, shi->dxco, shi->dyco, inp, shi->mat->lbias);
1155 else if (lar->mode & LA_SHAD_RAY) {
1156 ray_shadow(shi, lar, shadfac);
1159 if (shi->depth==0) {
1160 copy_v4_v4(lss->shadfac, shadfac);
1161 lss->samplenr= shi->samplenr;
1165 copy_v4_v4(shadfac, lss->shadfac);
1169 /* lampdistance and spot angle, writes in lv and dist */
1170 float lamp_get_visibility(LampRen *lar, const float co[3], float lv[3], float *dist)
1172 if (lar->type==LA_SUN || lar->type==LA_HEMI) {
1174 copy_v3_v3(lv, lar->vec);
1178 float visifac= 1.0f, t;
1180 sub_v3_v3v3(lv, co, lar->co);
1181 *dist= sqrtf(dot_v3v3(lv, lv));
1185 /* area type has no quad or sphere option */
1186 if (lar->type==LA_AREA) {
1187 /* area is single sided */
1188 //if (dot_v3v3(lv, lar->vec) > 0.0f)
1194 switch (lar->falloff_type) {
1195 case LA_FALLOFF_CONSTANT:
1198 case LA_FALLOFF_INVLINEAR:
1199 visifac = lar->dist/(lar->dist + dist[0]);
1201 case LA_FALLOFF_INVSQUARE:
1202 /* NOTE: This seems to be a hack since commit r12045 says this
1203 * option is similar to old Quad, but with slight changes.
1204 * Correct inv square would be (which would be old Quad):
1205 * visifac = lar->distkw / (lar->distkw + dist[0]*dist[0]);
1207 visifac = lar->dist / (lar->dist + dist[0]*dist[0]);
1209 case LA_FALLOFF_SLIDERS:
1211 visifac= lar->dist/(lar->dist+lar->ld1*dist[0]);
1213 visifac*= lar->distkw/(lar->distkw+lar->ld2*dist[0]*dist[0]);
1215 case LA_FALLOFF_CURVE:
1216 /* curvemapping_initialize is called from #add_render_lamp */
1217 visifac = curvemapping_evaluateF(lar->curfalloff, 0, dist[0]/lar->dist);
1221 if (lar->mode & LA_SPHERE) {
1222 float t= lar->dist - dist[0];
1226 visifac*= t/lar->dist;
1229 if (visifac > 0.0f) {
1230 if (lar->type==LA_SPOT) {
1233 if (lar->mode & LA_SQUARE) {
1234 if (dot_v3v3(lv, lar->vec) > 0.0f) {
1237 /* rotate view to lampspace */
1238 copy_v3_v3(lvrot, lv);
1239 mul_m3_v3(lar->imat, lvrot);
1241 x = max_ff(fabsf(lvrot[0]/lvrot[2]), fabsf(lvrot[1]/lvrot[2]));
1242 /* 1.0f/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1244 inpr= 1.0f/(sqrt(1.0f+x*x));
1249 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1257 if (t<lar->spotbl && lar->spotbl!=0.0f) {
1259 float i= t/lar->spotbl;
1261 inpr*= (3.0f*t-2.0f*t*i);
1268 if (visifac <= 0.001f) visifac = 0.0f;
1273 /* function returns raw diff, spec and full shadowed diff in the 'shad' pass */
1274 static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int passflag)
1276 Material *ma= shi->mat;
1277 VlakRen *vlr= shi->vlr;
1278 float lv[3], lampdist, lacol[3], shadfac[4], lashdw[3];
1279 float i, is, i_noshad, inp, *vn, *view, vnor[3], phongcorr=1.0f;
1286 if (lar->energy == 0.0f) return;
1287 /* only shadow lamps shouldn't affect shadow-less materials at all */
1288 if ((lar->mode & LA_ONLYSHADOW) && (!(ma->mode & MA_SHADOW) || !(R.r.mode & R_SHADOW)))
1290 /* optimization, don't render fully black lamps */
1291 if (!(lar->mode & LA_TEXTURE) && (lar->r + lar->g + lar->b == 0.0f))
1294 /* lampdist, spot angle, area side, ... */
1295 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1299 if (lar->type==LA_SPOT) {
1300 if (lar->mode & LA_OSATEX) {
1301 shi->osatex= 1; /* signal for multitex() */
1303 shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/lampdist;
1304 shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/lampdist;
1305 shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/lampdist;
1307 shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/lampdist;
1308 shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/lampdist;
1309 shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/lampdist;
1313 /* lamp color texture */
1318 lashdw[0]= lar->shdwr;
1319 lashdw[1]= lar->shdwg;
1320 lashdw[2]= lar->shdwb;
1322 if (lar->mode & LA_TEXTURE) do_lamp_tex(lar, lv, shi, lacol, LA_TEXTURE);
1323 if (lar->mode & LA_SHAD_TEX) do_lamp_tex(lar, lv, shi, lashdw, LA_SHAD_TEX);
1325 /* tangent case; calculate fake face normal, aligned with lampvector */
1326 /* note, vnor==vn is used as tangent trigger for buffer shadow */
1327 if (vlr->flag & R_TANGENT) {
1328 float cross[3], nstrand[3], blend;
1330 if (ma->mode & MA_STR_SURFDIFF) {
1331 cross_v3_v3v3(cross, shi->surfnor, vn);
1332 cross_v3_v3v3(nstrand, vn, cross);
1334 blend= dot_v3v3(nstrand, shi->surfnor);
1335 blend= 1.0f - blend;
1336 CLAMP(blend, 0.0f, 1.0f);
1338 interp_v3_v3v3(vnor, nstrand, shi->surfnor, blend);
1342 cross_v3_v3v3(cross, lv, vn);
1343 cross_v3_v3v3(vnor, cross, vn);
1347 if (ma->strand_surfnor > 0.0f) {
1348 if (ma->strand_surfnor > shi->surfdist) {
1349 blend= (ma->strand_surfnor - shi->surfdist)/ma->strand_surfnor;
1350 interp_v3_v3v3(vnor, vnor, shi->surfnor, blend);
1355 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1358 else if (ma->mode & MA_TANGENT_V) {
1360 cross_v3_v3v3(cross, lv, shi->tang);
1361 cross_v3_v3v3(vnor, cross, shi->tang);
1363 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1367 /* dot product and reflectivity */
1368 /* inp = dotproduct, is = shader result, i = lamp energy (with shadow), i_noshad = i without shadow */
1369 inp= dot_v3v3(vn, lv);
1371 /* phong threshold to prevent backfacing faces having artifacts on ray shadow (terminator problem) */
1372 /* this complex construction screams for a nicer implementation! (ton) */
1373 if (R.r.mode & R_SHADOW) {
1374 if (ma->mode & MA_SHADOW) {
1375 if (lar->type == LA_HEMI || lar->type == LA_AREA) {
1378 else if ((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1379 float thresh= shi->obr->ob->smoothresh;
1381 phongcorr= (inp-thresh)/(inp*(1.0f-thresh));
1385 else if (ma->sbias!=0.0f && ((lar->mode & LA_SHAD_RAY) || lar->shb)) {
1387 phongcorr= (inp-ma->sbias)/(inp*(1.0f-ma->sbias));
1394 /* diffuse shaders */
1395 if (lar->mode & LA_NO_DIFF) {
1396 is = 0.0f; /* skip shaders */
1398 else if (lar->type==LA_HEMI) {
1399 is = 0.5f * inp + 0.5f;
1403 if (lar->type==LA_AREA)
1404 inp= area_lamp_energy_multisample(lar, shi->co, vn);
1406 /* diffuse shaders (oren nayer gets inp from area light) */
1407 if (ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1408 else if (ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1409 else if (ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1410 else if (ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1411 else is= inp; /* Lambert */
1414 /* 'is' is diffuse */
1415 if ((ma->shade_flag & MA_CUBIC) && is > 0.0f && is < 1.0f) {
1416 is= 3.0f * is * is - 2.0f * is * is * is; /* nicer termination of shades */
1422 i*= visifac*shi->refl;
1426 vn = shi->vn; /* bring back original vector, we use special specular shaders for tangent */
1427 if (ma->mode & MA_TANGENT_V)
1430 /* init transp shadow */
1431 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 1.0f;
1433 /* shadow and spec, (visifac==0 outside spot) */
1434 if (visifac> 0.0f) {
1436 if ((R.r.mode & R_SHADOW)) {
1437 if (ma->mode & MA_SHADOW) {
1438 if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1440 if (vn==vnor) /* tangent trigger */
1441 lamp_get_shadow(lar, shi, dot_v3v3(shi->vn, lv), shadfac, shi->depth);
1443 lamp_get_shadow(lar, shi, inp, shadfac, shi->depth);
1445 /* warning, here it skips the loop */
1446 if ((lar->mode & LA_ONLYSHADOW) && i>0.0f) {
1448 shadfac[3]= i*lar->energy*(1.0f-shadfac[3]);
1449 shr->shad[0] -= shadfac[3]*shi->r*(1.0f-lashdw[0]);
1450 shr->shad[1] -= shadfac[3]*shi->g*(1.0f-lashdw[1]);
1451 shr->shad[2] -= shadfac[3]*shi->b*(1.0f-lashdw[2]);
1453 shr->spec[0] -= shadfac[3]*shi->specr*(1.0f-lashdw[0]);
1454 shr->spec[1] -= shadfac[3]*shi->specg*(1.0f-lashdw[1]);
1455 shr->spec[2] -= shadfac[3]*shi->specb*(1.0f-lashdw[2]);
1461 shr->shad[3] = shadfac[3]; /* store this for possible check in troublesome cases */
1466 /* in case 'no diffuse' we still do most calculus, spec can be in shadow.*/
1467 if (!(lar->mode & LA_NO_DIFF)) {
1469 if (ma->mode & MA_SHADOW_TRA)
1470 add_to_diffuse(shr->shad, shi, is, i*shadfac[0]*lacol[0], i*shadfac[1]*lacol[1], i*shadfac[2]*lacol[2]);
1472 add_to_diffuse(shr->shad, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
1474 /* add light for colored shadow */
1475 if (i_noshad>i && !(lashdw[0]==0 && lashdw[1]==0 && lashdw[2]==0)) {
1476 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]);
1478 if (i_noshad>0.0f) {
1479 if (passflag & (SCE_PASS_DIFFUSE|SCE_PASS_SHADOW)) {
1480 add_to_diffuse(shr->diff, shi, is, i_noshad*lacol[0], i_noshad*lacol[1], i_noshad*lacol[2]);
1483 copy_v3_v3(shr->diff, shr->shad);
1488 shadfac[3]*= phongcorr; /* note, shadfac not allowed to be stored nonlocal */
1490 if (shadfac[3]>0.0f && shi->spec!=0.0f && !(lar->mode & LA_NO_SPEC) && !(lar->mode & LA_ONLYSHADOW)) {
1492 if (!(passflag & (SCE_PASS_COMBINED | SCE_PASS_SPEC))) {
1495 else if (lar->type == LA_HEMI) {
1497 /* hemi uses no spec shaders (yet) */
1505 t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1507 if (lar->type==LA_HEMI) {
1511 t= shadfac[3]*shi->spec*spec(t, shi->har);
1513 shr->spec[0]+= t*(lacol[0] * shi->specr);
1514 shr->spec[1]+= t*(lacol[1] * shi->specg);
1515 shr->spec[2]+= t*(lacol[2] * shi->specb);
1518 /* specular shaders */
1521 if (ma->spec_shader==MA_SPEC_PHONG)
1522 specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1523 else if (ma->spec_shader==MA_SPEC_COOKTORR)
1524 specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1525 else if (ma->spec_shader==MA_SPEC_BLINN)
1526 specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1527 else if (ma->spec_shader==MA_SPEC_WARDISO)
1528 specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1530 specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1532 /* area lamp correction */
1533 if (lar->type==LA_AREA) specfac*= inp;
1535 t= shadfac[3]*shi->spec*visifac*specfac;
1537 if (ma->mode & MA_RAMP_SPEC) {
1539 do_specular_ramp(shi, specfac, t, spec);
1540 shr->spec[0]+= t*(lacol[0] * spec[0]);
1541 shr->spec[1]+= t*(lacol[1] * spec[1]);
1542 shr->spec[2]+= t*(lacol[2] * spec[2]);
1545 shr->spec[0]+= t*(lacol[0] * shi->specr);
1546 shr->spec[1]+= t*(lacol[1] * shi->specg);
1547 shr->spec[2]+= t*(lacol[2] * shi->specb);
1554 static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr)
1557 if (R.r.mode & R_SHADOW) {
1562 float /* *view, */ shadfac[4];
1563 float ir, accum, visifac, lampdist;
1564 float shaded = 0.0f, lightness = 0.0f;
1567 /* view= shi->view; */ /* UNUSED */
1570 lights= get_lights(shi);
1571 for (go=lights->first; go; go= go->next) {
1573 if (lar==NULL) continue;
1575 /* yafray: ignore shading by photonlights, not used in Blender */
1576 if (lar->type==LA_YF_PHOTON) continue;
1578 if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1579 if ((lar->lay & shi->lay)==0) continue;
1581 if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
1582 visifac= lamp_get_visibility(lar, shi->co, lv, &lampdist);
1585 if (visifac <= 0.0f) {
1586 if (shi->mat->shadowonly_flag == MA_SO_OLD)
1591 inpr= dot_v3v3(shi->vn, lv);
1593 if (shi->mat->shadowonly_flag == MA_SO_OLD)
1599 lamp_get_shadow(lar, shi, inpr, shadfac, shi->depth);
1601 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1602 /* Old "Shadows Only" */
1603 accum+= (1.0f-visifac) + (visifac)*rgb_to_grayscale(shadfac)*shadfac[3];
1606 shaded += rgb_to_grayscale(shadfac)*shadfac[3] * visifac * lar->energy;
1608 if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1609 lightness += visifac * lar->energy;
1615 /* Apply shadows as alpha */
1617 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1618 accum = 1.0f - accum/ir;
1621 if (shi->mat->shadowonly_flag == MA_SO_SHADOW) {
1622 if (lightness > 0.0f) {
1623 /* Get shadow value from between 0.0f and non-shadowed lightness */
1624 accum = (lightness - shaded) / (lightness);
1630 else { /* shadowonly_flag == MA_SO_SHADED */
1631 /* Use shaded value */
1632 accum = 1.0f - shaded;
1635 shr->alpha= (shi->alpha)*(accum);
1636 if (shr->alpha<0.0f) shr->alpha=0.0f;
1639 /* If "fully shaded", use full alpha even on areas that have no lights */
1640 if (shi->mat->shadowonly_flag == MA_SO_SHADED) shr->alpha=shi->alpha;
1641 else shr->alpha= 0.f;
1645 /* quite disputable this... also note it doesn't mirror-raytrace */
1646 if ((R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT)) && shi->amb!=0.0f) {
1649 if (R.wrld.mode & WO_AMB_OCC) {
1650 f= R.wrld.aoenergy*shi->amb;
1652 if (R.wrld.aomix==WO_AOADD) {
1653 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1654 f= f*(1.0f - rgb_to_grayscale(shi->ao));
1655 shr->alpha= (shr->alpha + f)*f;
1658 shr->alpha -= f*rgb_to_grayscale(shi->ao);
1659 if (shr->alpha<0.0f) shr->alpha=0.0f;
1662 else /* AO Multiply */
1663 shr->alpha= (1.0f - f)*shr->alpha + f*(1.0f - (1.0f - shr->alpha)*rgb_to_grayscale(shi->ao));
1666 if (R.wrld.mode & WO_ENV_LIGHT) {
1667 if (shi->mat->shadowonly_flag == MA_SO_OLD) {
1668 f= R.wrld.ao_env_energy*shi->amb*(1.0f - rgb_to_grayscale(shi->env));
1669 shr->alpha= (shr->alpha + f)*f;
1672 f= R.wrld.ao_env_energy*shi->amb;
1673 shr->alpha -= f*rgb_to_grayscale(shi->env);
1674 if (shr->alpha<0.0f) shr->alpha=0.0f;
1680 /* let's map negative light as if it mirrors positive light, otherwise negative values disappear */
1681 static void wrld_exposure_correct(float diff[3])
1684 diff[0]= R.wrld.linfac*(1.0f-expf( diff[0]*R.wrld.logfac) );
1685 diff[1]= R.wrld.linfac*(1.0f-expf( diff[1]*R.wrld.logfac) );
1686 diff[2]= R.wrld.linfac*(1.0f-expf( diff[2]*R.wrld.logfac) );
1689 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1691 Material *ma= shi->mat;
1692 int passflag= shi->passflag;
1694 memset(shr, 0, sizeof(ShadeResult));
1696 if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1699 if (ma->mode & MA_ONLYSHADOW) {
1700 shade_lamp_loop_only_shadow(shi, shr);
1704 /* envmap hack, always reset */
1705 shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0f;
1707 /* material color itself */
1708 if (passflag & (SCE_PASS_COMBINED|SCE_PASS_RGBA)) {
1709 if (ma->mode & (MA_FACETEXTURE)) {
1710 shi->r= shi->vcol[0];
1711 shi->g= shi->vcol[1];
1712 shi->b= shi->vcol[2];
1713 if (ma->mode & (MA_FACETEXTURE_ALPHA))
1714 shi->alpha= shi->vcol[3];
1716 else if (ma->mode & (MA_VERTEXCOLP)) {
1717 float neg_alpha = 1.0f - shi->vcol[3];
1718 shi->r= shi->r*neg_alpha + shi->vcol[0]*shi->vcol[3];
1719 shi->g= shi->g*neg_alpha + shi->vcol[1]*shi->vcol[3];
1720 shi->b= shi->b*neg_alpha + shi->vcol[2]*shi->vcol[3];
1723 do_material_tex(shi, &R);
1724 if (!(shi->mode & MA_TRANSP)) shi->alpha = 1.0f;
1727 shr->col[0]= shi->r*shi->alpha;
1728 shr->col[1]= shi->g*shi->alpha;
1729 shr->col[2]= shi->b*shi->alpha;
1730 shr->col[3]= shi->alpha;
1732 if ((ma->sss_flag & MA_DIFF_SSS) && !sss_pass_done(&R, ma)) {
1733 if (ma->sss_texfac == 0.0f) {
1734 shi->r= shi->g= shi->b= shi->alpha= 1.0f;
1735 shr->col[0]= shr->col[1]= shr->col[2]= shr->col[3]= 1.0f;
1738 shi->r= pow(shi->r, ma->sss_texfac);
1739 shi->g= pow(shi->g, ma->sss_texfac);
1740 shi->b= pow(shi->b, ma->sss_texfac);
1741 shi->alpha= pow(shi->alpha, ma->sss_texfac);
1743 shr->col[0]= pow(shr->col[0], ma->sss_texfac);
1744 shr->col[1]= pow(shr->col[1], ma->sss_texfac);
1745 shr->col[2]= pow(shr->col[2], ma->sss_texfac);
1746 shr->col[3]= pow(shr->col[3], ma->sss_texfac);
1751 if (ma->mode & MA_SHLESS) {
1752 shr->combined[0]= shi->r;
1753 shr->combined[1]= shi->g;
1754 shr->combined[2]= shi->b;
1755 shr->alpha= shi->alpha;
1759 if ( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) { /* vertexcolor light */
1760 shr->emit[0]= shi->r*(shi->emit+shi->vcol[0]*shi->vcol[3]);
1761 shr->emit[1]= shi->g*(shi->emit+shi->vcol[1]*shi->vcol[3]);
1762 shr->emit[2]= shi->b*(shi->emit+shi->vcol[2]*shi->vcol[3]);
1765 shr->emit[0]= shi->r*shi->emit;
1766 shr->emit[1]= shi->g*shi->emit;
1767 shr->emit[2]= shi->b*shi->emit;
1771 if (R.wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) {
1772 if (((passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT))) ||
1773 (passflag & (SCE_PASS_AO|SCE_PASS_ENVIRONMENT|SCE_PASS_INDIRECT)))
1775 if (R.r.mode & R_SHADOW) {
1776 /* AO was calculated for scanline already */
1777 if (shi->depth || shi->volume_depth)
1778 ambient_occlusion(shi);
1779 copy_v3_v3(shr->ao, shi->ao);
1780 copy_v3_v3(shr->env, shi->env); /* XXX multiply */
1781 copy_v3_v3(shr->indirect, shi->indirect); /* XXX multiply */
1787 if (passflag & (SCE_PASS_COMBINED|SCE_PASS_DIFFUSE|SCE_PASS_SPEC|SCE_PASS_SHADOW)) {
1792 lights= get_lights(shi);
1793 for (go=lights->first; go; go= go->next) {
1795 if (lar==NULL) continue;
1797 /* yafray: ignore shading by photonlights, not used in Blender */
1798 if (lar->type==LA_YF_PHOTON) continue;
1800 /* test for lamp layer */
1801 if (lar->mode & LA_LAYER) if ((lar->lay & shi->obi->lay)==0) continue;
1802 if ((lar->lay & shi->lay)==0) continue;
1804 /* accumulates in shr->diff and shr->spec and shr->shad (diffuse with shadow!) */
1805 shade_one_light(lar, shi, shr, passflag);
1808 /* this check is to prevent only shadow lamps from producing negative
1810 if (shr->spec[0] < 0) shr->spec[0] = 0;
1811 if (shr->spec[1] < 0) shr->spec[1] = 0;
1812 if (shr->spec[2] < 0) shr->spec[2] = 0;
1814 if (shr->shad[0] < 0) shr->shad[0] = 0;
1815 if (shr->shad[1] < 0) shr->shad[1] = 0;
1816 if (shr->shad[2] < 0) shr->shad[2] = 0;
1818 if (ma->sss_flag & MA_DIFF_SSS) {
1819 float sss[3], col[3], invalpha, texfac= ma->sss_texfac;
1821 /* this will return false in the preprocess stage */
1822 if (sample_sss(&R, ma, shi->co, sss)) {
1823 invalpha= (shr->col[3] > FLT_EPSILON)? 1.0f/shr->col[3]: 1.0f;
1826 copy_v3_v3(col, shr->col);
1827 mul_v3_fl(col, invalpha);
1829 else if (texfac==1.0f) {
1830 col[0]= col[1]= col[2]= 1.0f;
1831 mul_v3_fl(col, invalpha);
1834 copy_v3_v3(col, shr->col);
1835 mul_v3_fl(col, invalpha);
1836 col[0]= pow(col[0], 1.0f-texfac);
1837 col[1]= pow(col[1], 1.0f-texfac);
1838 col[2]= pow(col[2], 1.0f-texfac);
1841 shr->diff[0]= sss[0]*col[0];
1842 shr->diff[1]= sss[1]*col[1];
1843 shr->diff[2]= sss[2]*col[2];
1845 if (shi->combinedflag & SCE_PASS_SHADOW) {
1846 shr->shad[0]= shr->diff[0];
1847 shr->shad[1]= shr->diff[1];
1848 shr->shad[2]= shr->diff[2];
1853 if (shi->combinedflag & SCE_PASS_SHADOW)
1854 copy_v3_v3(shr->combined, shr->shad); /* note, no ';' ! */
1856 copy_v3_v3(shr->combined, shr->diff);
1858 /* calculate shadow pass, we use a multiplication mask */
1859 /* if diff = 0,0,0 it doesn't matter what the shadow pass is, so leave it as is */
1860 if (passflag & SCE_PASS_SHADOW && !(shr->diff[0]==0.0f && shr->diff[1]==0.0f && shr->diff[2]==0.0f)) {
1861 if (shr->diff[0]!=0.0f) shr->shad[0]= shr->shad[0]/shr->diff[0];
1862 /* can't determine proper shadow from shad/diff (0/0), so use shadow intensity */
1863 else if (shr->shad[0]==0.0f) shr->shad[0]= shr->shad[3];
1865 if (shr->diff[1]!=0.0f) shr->shad[1]= shr->shad[1]/shr->diff[1];
1866 else if (shr->shad[1]==0.0f) shr->shad[1]= shr->shad[3];
1868 if (shr->diff[2]!=0.0f) shr->shad[2]= shr->shad[2]/shr->diff[2];
1869 else if (shr->shad[2]==0.0f) shr->shad[2]= shr->shad[3];
1872 /* exposure correction */
1873 if ((R.wrld.exp!=0.0f || R.wrld.range!=1.0f) && !R.sss_points) {
1874 wrld_exposure_correct(shr->combined); /* has no spec! */
1875 wrld_exposure_correct(shr->spec);
1879 /* alpha in end, spec can influence it */
1880 if (passflag & (SCE_PASS_COMBINED)) {
1881 if ((ma->fresnel_tra!=0.0f) && (shi->mode & MA_TRANSP))
1882 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1884 /* note: shi->mode! */
1885 if (shi->mode & MA_TRANSP && (shi->mode & (MA_ZTRANSP|MA_RAYTRANSP))) {
1886 if (shi->spectra!=0.0f) {
1887 float t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
1889 if (t>1.0f) t= 1.0f;
1890 shi->alpha= (1.0f-t)*shi->alpha+t;
1894 shr->alpha= shi->alpha;
1896 /* from now stuff everything in shr->combined: ambient, AO, ramps, exposure */
1897 if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1898 if (R.r.mode & R_SHADOW) {
1899 /* add AO in combined? */
1900 if (R.wrld.mode & WO_AMB_OCC)
1901 if (shi->combinedflag & SCE_PASS_AO)
1902 ambient_occlusion_apply(shi, shr);
1904 if (R.wrld.mode & WO_ENV_LIGHT)
1905 if (shi->combinedflag & SCE_PASS_ENVIRONMENT)
1906 environment_lighting_apply(shi, shr);
1908 if (R.wrld.mode & WO_INDIRECT_LIGHT)
1909 if (shi->combinedflag & SCE_PASS_INDIRECT)
1910 indirect_lighting_apply(shi, shr);
1913 shr->combined[0]+= shi->ambr;
1914 shr->combined[1]+= shi->ambg;
1915 shr->combined[2]+= shi->ambb;
1917 if (ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->combined, shi);
1920 if (ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shi);
1922 /* refcol is for envmap only */
1923 if (shi->refcol[0]!=0.0f) {
1926 result[0]= shi->mirr*shi->refcol[1] + (1.0f - shi->mirr*shi->refcol[0])*shr->combined[0];
1927 result[1]= shi->mirg*shi->refcol[2] + (1.0f - shi->mirg*shi->refcol[0])*shr->combined[1];
1928 result[2]= shi->mirb*shi->refcol[3] + (1.0f - shi->mirb*shi->refcol[0])*shr->combined[2];
1930 if (passflag & SCE_PASS_REFLECT)
1931 sub_v3_v3v3(shr->refl, result, shr->combined);
1933 if (shi->combinedflag & SCE_PASS_REFLECT)
1934 copy_v3_v3(shr->combined, result);
1938 /* and add emit and spec */
1939 if (shi->combinedflag & SCE_PASS_EMIT)
1940 add_v3_v3(shr->combined, shr->emit);
1941 if (shi->combinedflag & SCE_PASS_SPEC)
1942 add_v3_v3(shr->combined, shr->spec);
1944 /* modulate by the object color */
1945 if ((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
1946 if (!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
1949 copy_v4_v4(obcol, shi->obr->ob->col);
1950 CLAMP(obcol[3], 0.0f, 1.0f);
1952 shr->combined[0] *= obcol[0];
1953 shr->combined[1] *= obcol[1];
1954 shr->combined[2] *= obcol[2];
1955 if (shi->mode & MA_TRANSP) shr->alpha *= obcol[3];
1959 shr->combined[3]= shr->alpha;