4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributors: Hos, Robert Wenzlaff.
24 * Contributors: 2004/2005/2006 Blender Foundation, full recode
26 * ***** END GPL LICENSE BLOCK *****
34 /* External modules: */
35 #include "MTC_matrixops.h"
36 #include "BLI_arithb.h"
37 #include "BLI_blenlib.h"
39 #include "BLI_jitter.h"
41 #include "BKE_utildefines.h"
43 #include "DNA_group_types.h"
44 #include "DNA_image_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_texture_types.h"
52 #include "BKE_global.h"
53 #include "BKE_material.h"
55 #include "BKE_texture.h"
58 #include "renderpipeline.h"
59 #include "render_types.h"
60 #include "renderdatabase.h"
61 #include "pixelblending.h"
62 #include "pixelshading.h"
63 #include "gammaCorrectionTables.h"
70 #include "rendercore.h"
73 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
74 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
75 /* only to be used here in this file, it's for speed */
76 extern struct Render R;
77 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
79 /* x and y are current pixels in rect to be rendered */
80 /* do not normalize! */
81 void calc_view_vector(float *view, float x, float y)
84 if(R.r.mode & R_ORTHO) {
85 view[0]= view[1]= 0.0;
88 /* move x and y to real viewplane coords */
90 view[0]= R.viewplane.xmin + x*(R.viewplane.xmax - R.viewplane.xmin);
93 view[1]= R.viewplane.ymin + y*(R.viewplane.ymax - R.viewplane.ymin);
95 // if(R.flag & R_SEC_FIELD) {
96 // if(R.r.mode & R_ODDFIELD) view[1]= (y+R.ystart)*R.ycor;
97 // else view[1]= (y+R.ystart+1.0)*R.ycor;
99 // else view[1]= (y+R.ystart+R.bluroffsy+0.5)*R.ycor;
104 if(R.r.mode & R_PANORAMA) {
105 float u= view[0]; float v= view[2];
106 view[0]= R.panoco*u + R.panosi*v;
107 view[2]= -R.panosi*u + R.panoco*v;
113 static void fogcolor(float *colf, float *rco, float *view)
115 float alpha, stepsize, startdist, dist, hor[4], zen[3], vec[3], dview[3];
116 float div=0.0f, distfac;
118 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
119 zen[0]= R.wrld.zenr; zen[1]= R.wrld.zeng; zen[2]= R.wrld.zenb;
123 /* we loop from cur coord to mist start in steps */
127 dview[0]= view[0]/(stepsize*div);
128 dview[1]= view[1]/(stepsize*div);
131 startdist= -rco[2] + BLI_frand();
132 for(dist= startdist; dist>R.wrld.miststa; dist-= stepsize) {
134 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
136 do_sky_tex(vec, vec, NULL, hor, zen, &alpha);
138 distfac= (dist-R.wrld.miststa)/R.wrld.mistdist;
140 hor[3]= hor[0]*distfac*distfac;
144 hor[0]= hor[0]*alpha;
145 hor[1]= hor[1]*alpha;
146 hor[2]= hor[2]*alpha;
147 addAlphaOverFloat(colf, hor);
149 VECSUB(vec, vec, dview);
154 float mistfactor(float zcor, float *co) /* dist en height, return alpha */
158 fac= zcor - R.wrld.miststa; /* zcor is calculated per pixel */
160 /* fac= -co[2]-R.wrld.miststa; */
163 if(fac< R.wrld.mistdist) {
165 fac= (fac/(R.wrld.mistdist));
167 if(R.wrld.mistype==0) fac*= fac;
168 else if(R.wrld.mistype==1);
175 /* height switched off mist */
176 if(R.wrld.misthi!=0.0 && fac!=0.0) {
177 /* at height misthi the mist is completely gone */
179 hi= R.viewinv[0][2]*co[0]+R.viewinv[1][2]*co[1]+R.viewinv[2][2]*co[2]+R.viewinv[3][2];
181 if(hi>R.wrld.misthi) fac= 0.0;
183 hi= (R.wrld.misthi-hi)/R.wrld.misthi;
188 return (1.0-fac)* (1.0-R.wrld.misi);
191 static void spothalo(struct LampRen *lar, ShadeInput *shi, float *intens)
193 double a, b, c, disc, nray[3], npos[3];
194 float t0, t1 = 0.0, t2= 0.0, t3, haint;
195 float p1[3], p2[3], ladist, maxz = 0.0, maxy = 0.0;
196 int snijp, doclip=1, use_yco=0;
202 if(R.r.mode & R_ORTHO) {
203 /* camera pos (view vector) cannot be used... */
204 /* camera position (cox,coy,0) rotate around lamp */
205 p1[0]= shi->co[0]-lar->co[0];
206 p1[1]= shi->co[1]-lar->co[1];
208 MTC_Mat3MulVecfl(lar->imat, p1);
209 VECCOPY(npos, p1); // npos is double!
212 VECCOPY(npos, lar->sh_invcampos); /* in initlamp calculated */
216 VECCOPY(nray, shi->view);
217 MTC_Mat3MulVecd(lar->imat, nray);
219 if(R.wrld.mode & WO_MIST) {
221 haint *= mistfactor(-lar->co[2], lar->co);
229 if(shi->co[2]==0.0) doclip= 0; /* for when halo at sky */
231 p1[0]= shi->co[0]-lar->co[0];
232 p1[1]= shi->co[1]-lar->co[1];
233 p1[2]= shi->co[2]-lar->co[2];
235 maxz= lar->imat[0][2]*p1[0]+lar->imat[1][2]*p1[1]+lar->imat[2][2]*p1[2];
237 maxy= lar->imat[0][1]*p1[0]+lar->imat[1][1]*p1[1]+lar->imat[2][1]*p1[2];
239 if( fabs(nray[2]) <0.000001 ) use_yco= 1;
242 /* scale z to make sure volume is normalized */
243 nray[2]*= lar->sh_zfac;
244 /* nray does not need normalization */
246 ladist= lar->sh_zfac*lar->dist;
249 a = nray[0] * nray[0] + nray[1] * nray[1] - nray[2]*nray[2];
250 b = nray[0] * npos[0] + nray[1] * npos[1] - nray[2]*npos[2];
251 c = npos[0] * npos[0] + npos[1] * npos[1] - npos[2]*npos[2];
254 if (fabs(a) < 0.00000001) {
256 * Only one intersection point...
267 else if (disc > 0.0) {
269 t1 = (-b + disc) / a;
270 t2 = (-b - disc) / a;
277 a= t1; t1= t2; t2= a;
280 /* z of intersection points with diabolo */
281 p1[2]= npos[2] + t1*nray[2];
282 p2[2]= npos[2] + t2*nray[2];
284 /* evaluate both points */
285 if(p1[2]<=0.0) ok1= 1;
286 if(p2[2]<=0.0 && t1!=t2) ok2= 1;
288 /* at least 1 point with negative z */
289 if(ok1==0 && ok2==0) return;
291 /* intersction point with -ladist, the bottom of the cone */
293 t3= (-ladist-npos[2])/nray[2];
295 /* de we have to replace one of the intersection points? */
297 if(p1[2]<-ladist) t1= t3;
304 if(p2[2]<-ladist) t2= t3;
311 else if(ok1==0 || ok2==0) return;
313 /* at least 1 visible interesction point */
314 if(t1<0.0 && t2<0.0) return;
321 /* sort again to be sure */
323 a= t1; t1= t2; t2= a;
326 /* calculate t0: is the maximum visible z (when halo is intersected by face) */
328 if(use_yco==0) t0= (maxz-npos[2])/nray[2];
329 else t0= (maxy-npos[1])/nray[1];
336 p1[0]= npos[0] + t1*nray[0];
337 p1[1]= npos[1] + t1*nray[1];
338 p1[2]= npos[2] + t1*nray[2];
339 p2[0]= npos[0] + t2*nray[0];
340 p2[1]= npos[1] + t2*nray[1];
341 p2[2]= npos[2] + t2*nray[2];
344 /* now we have 2 points, make three lengths with it */
346 a= sqrt(p1[0]*p1[0]+p1[1]*p1[1]+p1[2]*p1[2]);
347 b= sqrt(p2[0]*p2[0]+p2[1]*p2[1]+p2[2]*p2[2]);
356 *intens= c*( (1.0-a)+(1.0-b) );
358 /* WATCH IT: do not clip a,b en c at 1.0, this gives nasty little overflows
359 at the edges (especially with narrow halos) */
360 if(*intens<=0.0) return;
363 /* not needed because t0 has been used for p1/p2 as well */
364 /* if(doclip && t0<t2) { */
365 /* *intens *= (t0-t1)/(t2-t1); */
370 if(lar->shb && lar->shb->shadhalostep) {
371 *intens *= shadow_halo(lar, p1, p2);
377 static void renderspothalo(ShadeInput *shi, float *col, float alpha)
383 if(alpha==0.0f) return;
385 for(go=R.lights.first; go; go= go->next) {
388 if(lar->type==LA_SPOT && (lar->mode & LA_HALO) && lar->haint>0) {
390 spothalo(lar, shi, &i);
392 col[3]+= i*alpha; // all premul
393 col[0]+= i*lar->r*alpha;
394 col[1]+= i*lar->g*alpha;
395 col[2]+= i*lar->b*alpha;
399 /* clip alpha, is needed for unified 'alpha threshold' (vanillaRenderPipe.c) */
400 if(col[3]>1.0) col[3]= 1.0;
405 /* also used in zbuf.c */
406 int count_mask(unsigned short mask)
409 return (R.samples->cmask[mask & 255]+R.samples->cmask[mask>>8]);
413 static int calchalo_z(HaloRen *har, int zz)
416 if(har->type & HA_ONLYSKY) {
417 if(zz!=0x7FFFFFFF) zz= - 0x7FFFFF;
425 static void halo_pixelstruct(HaloRen *har, float *rb, float dist, float xn, float yn, PixStr *ps)
427 float col[4], accol[4];
428 int amount, amountm, zz, flarec;
431 accol[0]=accol[1]=accol[2]=accol[3]= 0.0;
435 amountm= count_mask(ps->mask);
438 zz= calchalo_z(har, ps->z);
442 shadeHaloFloat(har, col, zz, dist, xn, yn, flarec);
443 fac= ((float)amountm)/(float)R.osa;
444 accol[0]+= fac*col[0];
445 accol[1]+= fac*col[1];
446 accol[2]+= fac*col[2];
447 accol[3]+= fac*col[3];
453 /* now do the sky sub-pixels */
454 amount= R.osa-amount;
458 shadeHaloFloat(har, col, 0x7FFFFF, dist, xn, yn, flarec);
459 fac= ((float)amount)/(float)R.osa;
460 accol[0]+= fac*col[0];
461 accol[1]+= fac*col[1];
462 accol[2]+= fac*col[2];
463 accol[3]+= fac*col[3];
470 addalphaAddfacFloat(rb, col, har->add);
474 static void halo_tile(RenderPart *pa, float *pass, unsigned int lay)
477 rcti disprect= pa->disprect;
478 float dist, xsq, ysq, xn, yn, *rb;
482 short minx, maxx, miny, maxy, x;
484 for(a=0; a<R.tothalo; a++) {
486 if(R.test_break() ) break;
491 /* layer test, clip halo with y */
492 if((har->lay & lay)==0);
493 else if(disprect.ymin > har->maxy);
494 else if(disprect.ymax < har->miny);
497 minx= floor(har->xs-har->rad);
498 maxx= ceil(har->xs+har->rad);
500 if(disprect.xmin > maxx);
501 else if(disprect.xmax < minx);
504 minx= MAX2(minx, disprect.xmin);
505 maxx= MIN2(maxx, disprect.xmax);
507 miny= MAX2(har->miny, disprect.ymin);
508 maxy= MIN2(har->maxy, disprect.ymax);
510 for(y=miny; y<maxy; y++) {
511 int rectofs= (y-disprect.ymin)*pa->rectx + (minx - disprect.xmin);
512 rb= pass + 4*rectofs;
513 rz= pa->rectz + rectofs;
516 rd= pa->rectdaps + rectofs;
518 yn= (y-har->ys)*R.ycor;
521 for(x=minx; x<maxx; x++, rb+=4, rz++) {
525 if(dist<har->radsq) {
527 halo_pixelstruct(har, rb, dist, xn, yn, (PixStr *)*rd);
530 zz= calchalo_z(har, *rz);
532 shadeHaloFloat(har, col, zz, dist, xn, yn, har->flarec);
533 addalphaAddfacFloat(rb, col, har->add);
545 /* ---------------- shaders ----------------------- */
547 static double Normalise_d(double *n)
551 d= n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
553 if(d>0.00000000000000001) {
566 /* mix of 'real' fresnel and allowing control. grad defines blending gradient */
567 float fresnel_fac(float *view, float *vn, float grad, float fac)
571 if(fac==0.0) return 1.0;
573 t1= (view[0]*vn[0] + view[1]*vn[1] + view[2]*vn[2]);
574 if(t1>0.0) t2= 1.0+t1;
577 t2= grad + (1.0-grad)*pow(t2, fac);
579 if(t2<0.0) return 0.0;
580 else if(t2>1.0) return 1.0;
584 static double saacos_d(double fac)
586 if(fac<= -1.0f) return M_PI;
587 else if(fac>=1.0f) return 0.0;
588 else return acos(fac);
591 /* Stoke's form factor. Need doubles here for extreme small area sizes */
592 static float area_lamp_energy(float *co, float *vn, LampRen *lar)
595 double vec[4][3]; /* vectors of rendered co to vertices lamp */
596 double cross[4][3]; /* cross products of this */
597 double rad[4]; /* angles between vecs */
599 VECSUB(vec[0], co, lar->area[0]);
600 VECSUB(vec[1], co, lar->area[1]);
601 VECSUB(vec[2], co, lar->area[2]);
602 VECSUB(vec[3], co, lar->area[3]);
610 CROSS(cross[0], vec[0], vec[1]);
611 CROSS(cross[1], vec[1], vec[2]);
612 CROSS(cross[2], vec[2], vec[3]);
613 CROSS(cross[3], vec[3], vec[0]);
615 Normalise_d(cross[0]);
616 Normalise_d(cross[1]);
617 Normalise_d(cross[2]);
618 Normalise_d(cross[3]);
621 rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
622 rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
623 rad[2]= vec[2][0]*vec[3][0]+ vec[2][1]*vec[3][1]+ vec[2][2]*vec[3][2];
624 rad[3]= vec[3][0]*vec[0][0]+ vec[3][1]*vec[0][1]+ vec[3][2]*vec[0][2];
626 rad[0]= saacos_d(rad[0]);
627 rad[1]= saacos_d(rad[1]);
628 rad[2]= saacos_d(rad[2]);
629 rad[3]= saacos_d(rad[3]);
632 fac= rad[0]*(vn[0]*cross[0][0]+ vn[1]*cross[0][1]+ vn[2]*cross[0][2]);
633 fac+= rad[1]*(vn[0]*cross[1][0]+ vn[1]*cross[1][1]+ vn[2]*cross[1][2]);
634 fac+= rad[2]*(vn[0]*cross[2][0]+ vn[1]*cross[2][1]+ vn[2]*cross[2][2]);
635 fac+= rad[3]*(vn[0]*cross[3][0]+ vn[1]*cross[3][1]+ vn[2]*cross[3][2]);
637 if(fac<=0.0) return 0.0;
638 return pow(fac*lar->areasize, lar->k); // corrected for buttons size and lar->dist^2
641 static float spec(float inp, int hard)
645 if(inp>=1.0) return 1.0;
646 else if (inp<=0.0) return 0.0;
650 if(b1<0.01) b1= 0.01;
652 if((hard & 1)==0) inp= 1.0;
653 if(hard & 2) inp*= b1;
655 if(hard & 4) inp*= b1;
657 if(hard & 8) inp*= b1;
659 if(hard & 16) inp*= b1;
663 if(b1<0.001) b1= 0.0;
665 if(hard & 32) inp*= b1;
667 if(hard & 64) inp*=b1;
669 if(hard & 128) inp*=b1;
671 if(b1<0.001) b1= 0.0;
681 static float Phong_Spec( float *n, float *l, float *v, int hard, int tangent )
691 rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
692 if(tangent) rslt= sasqrt(1.0 - rslt*rslt);
694 if( rslt > 0.0 ) rslt= spec(rslt, hard);
701 /* reduced cook torrance spec (for off-specular peak) */
702 static float CookTorr_Spec(float *n, float *l, float *v, int hard, int tangent)
704 float i, nh, nv, h[3];
711 nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2];
712 if(tangent) nh= sasqrt(1.0 - nh*nh);
713 else if(nh<0.0) return 0.0;
715 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
716 if(tangent) nv= sasqrt(1.0 - nv*nv);
717 else if(nv<0.0) nv= 0.0;
726 static float Blinn_Spec(float *n, float *l, float *v, float refrac, float spec_power, int tangent)
728 float i, nh, nv, nl, vh, h[3];
729 float a, b, c, g=0.0, p, f, ang;
731 if(refrac < 1.0) return 0.0;
732 if(spec_power == 0.0) return 0.0;
734 /* conversion from 'hardness' (1-255) to 'spec_power' (50 maps at 0.1) */
736 spec_power= sqrt(1.0/spec_power);
737 else spec_power= 10.0/spec_power;
744 nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
745 if(tangent) nh= sasqrt(1.0f - nh*nh);
746 else if(nh<0.0) return 0.0;
748 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
749 if(tangent) nv= sasqrt(1.0f - nv*nv);
750 if(nv<=0.0) nv= 0.01; /* hrms... */
752 nl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
753 if(tangent) nl= sasqrt(1.0f - nl*nl);
758 vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and half-way vector */
759 if(vh<=0.0) vh= 0.01;
765 if( a < b && a < c ) g = a;
766 else if( b < a && b < c ) g = b;
767 else if( c < a && c < b ) g = c;
769 p = sqrt( (double)((refrac * refrac)+(vh*vh)-1.0) );
770 f = (((p-vh)*(p-vh))/((p+vh)*(p+vh)))*(1+((((vh*(p+vh))-1.0)*((vh*(p+vh))-1.0))/(((vh*(p-vh))+1.0)*((vh*(p-vh))+1.0))));
773 i= f * g * exp((double)(-(ang*ang) / (2.0*spec_power*spec_power)));
779 /* cartoon render spec */
780 static float Toon_Spec( float *n, float *l, float *v, float size, float smooth, int tangent)
791 rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
792 if(tangent) rslt = sasqrt(1.0f - rslt*rslt);
794 ang = saacos( rslt );
796 if( ang < size ) rslt = 1.0;
797 else if( ang >= (size + smooth) || smooth == 0.0 ) rslt = 0.0;
798 else rslt = 1.0 - ((ang - size) / smooth);
803 /* Ward isotropic gaussian spec */
804 static float WardIso_Spec( float *n, float *l, float *v, float rms, int tangent)
806 float i, nh, nv, nl, h[3], angle, alpha;
809 /* half-way vector */
815 nh = n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
816 if(tangent) nh = sasqrt(1.0f - nh*nh);
817 if(nh<=0.0) nh = 0.001f;
819 nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
820 if(tangent) nv = sasqrt(1.0f - nv*nv);
821 if(nv<=0.0) nv = 0.001f;
823 nl = n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
824 if(tangent) nl = sasqrt(1.0f - nl*nl);
825 if(nl<=0.0) nl = 0.001;
827 angle = tan(saacos(nh));
828 alpha = MAX2(rms,0.001);
830 i= nl * (1.0/(4*M_PI*alpha*alpha)) * (exp( -(angle*angle)/(alpha*alpha))/(sqrt(nv*nl)));
835 /* cartoon render diffuse */
836 static float Toon_Diff( float *n, float *l, float *v, float size, float smooth )
840 rslt = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
842 ang = saacos( (double)(rslt) );
844 if( ang < size ) rslt = 1.0;
845 else if( ang >= (size + smooth) || smooth == 0.0 ) rslt = 0.0;
846 else rslt = 1.0 - ((ang - size) / smooth);
851 /* Oren Nayar diffuse */
853 /* 'nl' is either dot product, or return value of area light */
854 /* in latter case, only last multiplication uses 'nl' */
855 static float OrenNayar_Diff(float nl, float *n, float *l, float *v, float rough )
857 float i, nh, nv, vh, realnl, h[3];
859 float Lit_A, View_A, Lit_B[3], View_B[3];
866 nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
869 nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
872 realnl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
873 if(realnl<=0.0) return 0.0;
874 if(nl<0.0) return 0.0; /* value from area light */
876 vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and halfway vector */
879 Lit_A = saacos(realnl);
880 View_A = saacos( nv );
882 Lit_B[0] = l[0] - (realnl * n[0]);
883 Lit_B[1] = l[1] - (realnl * n[1]);
884 Lit_B[2] = l[2] - (realnl * n[2]);
887 View_B[0] = v[0] - (nv * n[0]);
888 View_B[1] = v[1] - (nv * n[1]);
889 View_B[2] = v[2] - (nv * n[2]);
892 t = Lit_B[0]*View_B[0] + Lit_B[1]*View_B[1] + Lit_B[2]*View_B[2];
895 if( Lit_A > View_A ) {
904 A = 1 - (0.5 * ((rough * rough) / ((rough * rough) + 0.33)));
905 B = 0.45 * ((rough * rough) / ((rough * rough) + 0.09));
907 b*= 0.95; /* prevent tangens from shooting to inf, 'nl' can be not a dot product here. */
908 /* overflow only happens with extreme size area light, and higher roughness */
909 i = nl * ( A + ( B * t * sin(a) * tan(b) ) );
914 /* Minnaert diffuse */
915 static float Minnaert_Diff(float nl, float *n, float *v, float darkness)
920 /* nl = dot product between surface normal and light vector */
924 /* nv = dot product between surface normal and view vector */
925 nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
930 i = nl * pow(MAX2(nv*nl, 0.1), (darkness - 1) ); /*The Real model*/
932 i = nl * pow( (1.001 - nv), (darkness - 1) ); /*Nvidia model*/
937 static float Fresnel_Diff(float *vn, float *lv, float *view, float fac_i, float fac)
939 return fresnel_fac(lv, vn, fac_i, fac);
942 /* --------------------------------------------- */
943 /* also called from texture.c */
944 void calc_R_ref(ShadeInput *shi)
948 /* shi->vn dot shi->view */
949 i= -2*(shi->vn[0]*shi->view[0]+shi->vn[1]*shi->view[1]+shi->vn[2]*shi->view[2]);
951 shi->ref[0]= (shi->view[0]+i*shi->vn[0]);
952 shi->ref[1]= (shi->view[1]+i*shi->vn[1]);
953 shi->ref[2]= (shi->view[2]+i*shi->vn[2]);
955 if(shi->vlr->flag & R_SMOOTH) {
956 i= -2*( (shi->vn[0]+shi->dxno[0])*(shi->view[0]+shi->dxview) +
957 (shi->vn[1]+shi->dxno[1])*shi->view[1]+ (shi->vn[2]+shi->dxno[2])*shi->view[2] );
959 shi->dxref[0]= shi->ref[0]- ( shi->view[0]+shi->dxview+i*(shi->vn[0]+shi->dxno[0]));
960 shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*(shi->vn[1]+shi->dxno[1]));
961 shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dxno[2]));
963 i= -2*( (shi->vn[0]+shi->dyno[0])*shi->view[0]+
964 (shi->vn[1]+shi->dyno[1])*(shi->view[1]+shi->dyview)+ (shi->vn[2]+shi->dyno[2])*shi->view[2] );
966 shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*(shi->vn[0]+shi->dyno[0]));
967 shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*(shi->vn[1]+shi->dyno[1]));
968 shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dyno[2]));
973 i= -2*( shi->vn[0]*(shi->view[0]+shi->dxview) +
974 shi->vn[1]*shi->view[1]+ shi->vn[2]*shi->view[2] );
976 shi->dxref[0]= shi->ref[0]- (shi->view[0]+shi->dxview+i*shi->vn[0]);
977 shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*shi->vn[1]);
978 shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
980 i= -2*( shi->vn[0]*shi->view[0]+
981 shi->vn[1]*(shi->view[1]+shi->dyview)+ shi->vn[2]*shi->view[2] );
983 shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*shi->vn[0]);
984 shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*shi->vn[1]);
985 shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
991 /* called from ray.c */
992 void shade_color(ShadeInput *shi, ShadeResult *shr)
994 Material *ma= shi->mat;
996 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
997 shi->r= shi->vcol[0];
998 shi->g= shi->vcol[1];
999 shi->b= shi->vcol[2];
1003 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1004 shi->r= shi->vcol[0];
1005 shi->g= shi->vcol[1];
1006 shi->b= shi->vcol[2];
1008 do_material_tex(shi);
1011 if(ma->mode & (MA_ZTRA|MA_RAYTRANSP)) {
1012 if(ma->fresnel_tra!=0.0)
1013 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1016 shr->diff[0]= shi->r;
1017 shr->diff[1]= shi->g;
1018 shr->diff[2]= shi->b;
1019 shr->alpha= shi->alpha;
1022 /* ramp for at end of shade */
1023 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
1025 Material *ma= shi->mat;
1026 float col[4], fac=0;
1029 if(ma->rampin_col==MA_RAMP_IN_RESULT) {
1031 fac= 0.3*diff[0] + 0.58*diff[1] + 0.12*diff[2];
1032 do_colorband(ma->ramp_col, fac, col);
1034 /* blending method */
1035 fac= col[3]*ma->rampfac_col;
1037 ramp_blend(ma->rampblend_col, diff, diff+1, diff+2, fac, col);
1042 /* r,g,b denote energy, ramp is used with different values to make new material color */
1043 static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, float g, float b)
1045 Material *ma= shi->mat;
1046 float col[4], colt[3], fac=0;
1048 if(ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
1050 /* MA_RAMP_IN_RESULT is exceptional */
1051 if(ma->rampin_col==MA_RAMP_IN_RESULT) {
1053 diff[0] += r * shi->r;
1054 diff[1] += g * shi->g;
1055 diff[2] += b * shi->b;
1059 switch(ma->rampin_col) {
1060 case MA_RAMP_IN_ENERGY:
1061 fac= 0.3*r + 0.58*g + 0.12*b;
1063 case MA_RAMP_IN_SHADER:
1066 case MA_RAMP_IN_NOR:
1067 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1071 do_colorband(ma->ramp_col, fac, col);
1073 /* blending method */
1074 fac= col[3]*ma->rampfac_col;
1075 colt[0]= shi->r; colt[1]= shi->g; colt[2]= shi->b;
1077 ramp_blend(ma->rampblend_col, colt, colt+1, colt+2, fac, col);
1080 diff[0] += r * colt[0];
1081 diff[1] += g * colt[1];
1082 diff[2] += b * colt[2];
1086 diff[0] += r * shi->r;
1087 diff[1] += g * shi->g;
1088 diff[2] += b * shi->b;
1092 static void ramp_spec_result(float *specr, float *specg, float *specb, ShadeInput *shi)
1094 Material *ma= shi->mat;
1098 if(ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
1099 fac= 0.3*(*specr) + 0.58*(*specg) + 0.12*(*specb);
1100 do_colorband(ma->ramp_spec, fac, col);
1102 /* blending method */
1103 fac= col[3]*ma->rampfac_spec;
1105 ramp_blend(ma->rampblend_spec, specr, specg, specb, fac, col);
1110 /* is = dot product shade, t = spec energy */
1111 static void do_specular_ramp(ShadeInput *shi, float is, float t, float *spec)
1113 Material *ma= shi->mat;
1117 spec[0]= shi->specr;
1118 spec[1]= shi->specg;
1119 spec[2]= shi->specb;
1121 /* MA_RAMP_IN_RESULT is exception */
1122 if(ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
1125 switch(ma->rampin_spec) {
1126 case MA_RAMP_IN_ENERGY:
1129 case MA_RAMP_IN_SHADER:
1132 case MA_RAMP_IN_NOR:
1133 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1137 do_colorband(ma->ramp_spec, fac, col);
1139 /* blending method */
1140 fac= col[3]*ma->rampfac_spec;
1142 ramp_blend(ma->rampblend_spec, spec, spec+1, spec+2, fac, col);
1148 static void ambient_occlusion(ShadeInput *shi, ShadeResult *shr)
1150 float f, shadfac[4];
1152 if((R.wrld.mode & WO_AMB_OCC) && (R.r.mode & R_RAYTRACE) && shi->amb!=0.0) {
1153 ray_ao(shi, shadfac);
1155 if(R.wrld.aocolor==WO_AOPLAIN) {
1156 if (R.wrld.aomix==WO_AOADDSUB) shadfac[3] = 2.0*shadfac[3]-1.0;
1157 else if (R.wrld.aomix==WO_AOSUB) shadfac[3] = shadfac[3]-1.0;
1159 f= R.wrld.aoenergy*shadfac[3]*shi->amb;
1160 add_to_diffuse(shr->diff, shi, f, f, f, f);
1163 if (R.wrld.aomix==WO_AOADDSUB) {
1164 shadfac[0] = 2.0*shadfac[0]-1.0;
1165 shadfac[1] = 2.0*shadfac[1]-1.0;
1166 shadfac[2] = 2.0*shadfac[2]-1.0;
1168 else if (R.wrld.aomix==WO_AOSUB) {
1169 shadfac[0] = shadfac[0]-1.0;
1170 shadfac[1] = shadfac[1]-1.0;
1171 shadfac[2] = shadfac[2]-1.0;
1173 f= R.wrld.aoenergy*shi->amb;
1174 add_to_diffuse(shr->diff, shi, f, f*shadfac[0], f*shadfac[1], f*shadfac[2]);
1179 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1183 Material *ma= shi->mat;
1184 VlakRen *vlr= shi->vlr;
1186 float i, inp, inpr, is, t, lv[3], vnor[3], lacol[3], lampdist, ld = 0;
1187 float lvrot[3], *vn, *view, shadfac[4], soft, phongcorr; // shadfac = rgba
1192 memset(shr, 0, sizeof(ShadeResult));
1194 if((ma->mode & MA_RAYMIRROR)==0) shi->ray_mirror= 0.0;
1198 lights= &ma->group->gobject;
1203 if(ma->mode & MA_ONLYSHADOW) {
1206 if(R.r.mode & R_SHADOW) {
1208 shadfac[3]= ir= 0.0;
1209 for(go=lights->first; go; go= go->next) {
1211 if(lar==NULL) continue;
1213 /* yafray: ignore shading by photonlights, not used in Blender */
1214 if (lar->type==LA_YF_PHOTON) continue;
1216 if(lar->mode & LA_LAYER) if((lar->lay & vlr->lay)==0) continue;
1218 lv[0]= shi->co[0]-lar->co[0];
1219 lv[1]= shi->co[1]-lar->co[1];
1220 lv[2]= shi->co[2]-lar->co[2];
1222 if(lar->type==LA_SPOT) {
1223 /* only test within spotbundel */
1224 if(lar->shb || (lar->mode & LA_SHAD_RAY)) {
1227 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1228 if(inpr>lar->spotsi) {
1230 inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
1232 if(lar->shb) i = testshadowbuf(lar->shb, shi->co, shi->dxco, shi->dyco, inp);
1235 ray_shadow(shi, lar, shad);
1239 t= inpr - lar->spotsi;
1240 if(t<lar->spotbl && lar->spotbl!=0.0) {
1255 else if(lar->mode & LA_SHAD_RAY) {
1259 if( shi->facenor[0]*lv[0] + shi->facenor[1]*lv[1] + shi->facenor[2]*lv[2] > -0.01) {
1260 ray_shadow(shi, lar, shad);
1261 shadfac[3]+= shad[3];
1269 shr->alpha= (shi->alpha)*(1.0-shadfac[3]);
1273 if((R.wrld.mode & WO_AMB_OCC) && (R.r.mode & R_RAYTRACE) && shi->amb!=0.0) {
1276 ray_ao(shi, shadfac); // shadfac==0: full light
1277 shadfac[3]= 1.0-shadfac[3];
1279 f= R.wrld.aoenergy*shadfac[3]*shi->amb;
1281 if(R.wrld.aomix==WO_AOADD) {
1285 else if(R.wrld.aomix==WO_AOSUB) {
1297 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1298 shi->r= shi->vcol[0];
1299 shi->g= shi->vcol[1];
1300 shi->b= shi->vcol[2];
1303 /* envmap hack, always reset */
1304 shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0;
1307 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1308 shi->r= shi->vcol[0];
1309 shi->g= shi->vcol[1];
1310 shi->b= shi->vcol[2];
1312 do_material_tex(shi);
1315 if(ma->mode & MA_SHLESS) {
1316 shr->diff[0]= shi->r;
1317 shr->diff[1]= shi->g;
1318 shr->diff[2]= shi->b;
1319 shr->alpha= shi->alpha;
1323 if( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) { // vertexcolor light
1324 // add_to_diffuse(shr->diff, shi, 1.0, ma->emit+shi->vcol[0], ma->emit+shi->vcol[1], ma->emit+shi->vcol[2]);
1325 shr->diff[0]= shi->r*(shi->emit+shi->vcol[0]);
1326 shr->diff[1]= shi->g*(shi->emit+shi->vcol[1]);
1327 shr->diff[2]= shi->b*(shi->emit+shi->vcol[2]);
1330 // add_to_diffuse(shr->diff, shi, 1.0, ma->emit, ma->emit, ma->emit);
1331 shr->diff[0]= shi->r*shi->emit;
1332 shr->diff[1]= shi->g*shi->emit;
1333 shr->diff[2]= shi->b*shi->emit;
1336 ambient_occlusion(shi, shr);
1338 for(go=lights->first; go; go= go->next) {
1340 if(lar==NULL) continue;
1342 /* yafray: ignore shading by photonlights, not used in Blender */
1343 if (lar->type==LA_YF_PHOTON) continue;
1345 /* test for lamp layer */
1346 if(lar->mode & LA_LAYER) if((lar->lay & vlr->lay)==0) continue;
1348 /* lampdist calculation */
1349 if(lar->type==LA_SUN || lar->type==LA_HEMI) {
1350 VECCOPY(lv, lar->vec);
1354 lv[0]= shi->co[0]-lar->co[0];
1355 lv[1]= shi->co[1]-lar->co[1];
1356 lv[2]= shi->co[2]-lar->co[2];
1357 ld= sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
1362 /* ld is re-used further on (texco's) */
1363 if(lar->type==LA_AREA) {
1367 if(lar->mode & LA_QUAD) {
1370 t= lar->dist/(lar->dist+lar->ld1*ld);
1372 t*= lar->distkw/(lar->distkw+lar->ld2*ld*ld);
1377 lampdist= (lar->dist/(lar->dist+ld));
1380 if(lar->mode & LA_SPHERE) {
1394 /* init transp shadow */
1396 if(ma->mode & MA_SHADOW_TRA) shadfac[0]= shadfac[1]= shadfac[2]= 1.0;
1398 if(lar->type==LA_SPOT) {
1400 if(lar->mode & LA_SQUARE) {
1401 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0) {
1404 /* rotate view to lampspace */
1406 MTC_Mat3MulVecfl(lar->imat, lvrot);
1408 x= MAX2(fabs(lvrot[0]/lvrot[2]) , fabs(lvrot[1]/lvrot[2]));
1409 /* 1.0/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1411 inpr= 1.0f/(sqrt(1.0f+x*x));
1416 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1420 if(inpr<t) continue;
1425 if(t<lar->spotbl && lar->spotbl!=0.0) {
1429 soft= (3.0*t-2.0*t*i);
1435 if(lar->mode & LA_OSATEX) {
1436 shi->osatex= 1; /* signal for multitex() */
1438 shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/ld;
1439 shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/ld;
1440 shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/ld;
1442 shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/ld;
1443 shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/ld;
1444 shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/ld;
1449 if(lar->mode & LA_TEXTURE) do_lamp_tex(lar, lv, shi, lacol);
1451 /* dot product and reflectivity */
1452 /* inp = dotproduct, is = shader result, i = lamp energy (with shadow) */
1454 /* tangent case; calculate fake face normal, aligned with lampvector */
1455 if(vlr->flag & R_TANGENT) {
1457 Crossf(cross, lv, vn);
1458 Crossf(vnor, cross, vn);
1459 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1462 else if(ma->mode & MA_TANGENT_V) {
1464 Crossf(cross, lv, shi->tang);
1465 Crossf(vnor, cross, shi->tang);
1466 vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1470 inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
1472 /* phong threshold to prevent backfacing faces having artefacts on ray shadow (terminator problem) */
1473 if((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1474 float thresh= vlr->ob->smoothresh;
1476 phongcorr= (inp-thresh)/(inp*(1.0-thresh));
1480 else if(ma->sbias!=0.0f) {
1482 phongcorr= (inp-ma->sbias)/(inp*(1.0-ma->sbias));
1486 else phongcorr= 1.0;
1488 /* diffuse shaders */
1489 if(lar->mode & LA_NO_DIFF) {
1490 is= 0.0; // skip shaders
1492 else if(lar->type==LA_HEMI) {
1497 if(lar->type==LA_AREA) {
1499 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0) {
1500 inp= area_lamp_energy(shi->co, vn, lar);
1505 /* diffuse shaders (oren nayer gets inp from area light) */
1506 if(ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1507 else if(ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1508 else if(ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1509 else if(ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1510 else is= inp; // Lambert
1516 i*= lampdist*shi->refl;
1519 vn= shi->vn; // bring back original vector, we use special specular shaders for tangent
1520 if(ma->mode & MA_TANGENT_V)
1523 /* shadow and spec, (lampdist==0 outside spot) */
1526 if(i>0.0 && (R.r.mode & R_SHADOW)) {
1527 if(ma->mode & MA_SHADOW) {
1528 if(lar->type==LA_HEMI); // no shadow
1531 shadfac[3] = testshadowbuf(lar->shb, shi->co, shi->dxco, shi->dyco, inp);
1533 else if(lar->mode & LA_SHAD_RAY) {
1534 ray_shadow(shi, lar, shadfac);
1537 /* warning, here it skips the loop */
1538 if(lar->mode & LA_ONLYSHADOW) {
1540 shadfac[3]= i*lar->energy*(1.0-shadfac[3]);
1541 shr->diff[0] -= shadfac[3]*shi->r;
1542 shr->diff[1] -= shadfac[3]*shi->g;
1543 shr->diff[2] -= shadfac[3]*shi->b;
1548 if(shadfac[3]==0.0) continue;
1555 if(R.r.mode & R_RAYTRACE) {
1556 extern void ray_translucent(ShadeInput *shi, LampRen *lar, float *distfac, float *co);
1557 float co[3], distfac;
1559 ray_translucent(shi, lar, &distfac, co);
1561 if(distfac<0.01f*G.rt) {
1562 // printf("distfac %f\n", distfac);
1563 distfac= 1.0f - distfac/(0.01f*G.rt);
1564 shr->diff[0]+= distfac;
1565 shr->diff[1]+= distfac;
1566 shr->diff[2]+= distfac;
1572 if(shadfac[3]>0.0 && shi->spec!=0.0 && !(lar->mode & LA_NO_SPEC)) {
1574 if(lar->type==LA_HEMI) {
1575 /* hemi uses no spec shaders (yet) */
1583 t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1585 if(lar->type==LA_HEMI) {
1589 t= shadfac[3]*shi->spec*spec(t, shi->har);
1590 shr->spec[0]+= t*(lacol[0] * shi->specr);
1591 shr->spec[1]+= t*(lacol[1] * shi->specg);
1592 shr->spec[2]+= t*(lacol[2] * shi->specb);
1595 /* specular shaders */
1598 if(ma->spec_shader==MA_SPEC_PHONG)
1599 specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1600 else if(ma->spec_shader==MA_SPEC_COOKTORR)
1601 specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1602 else if(ma->spec_shader==MA_SPEC_BLINN)
1603 specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1604 else if(ma->spec_shader==MA_SPEC_WARDISO)
1605 specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1607 specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1609 /* area lamp correction */
1610 if(lar->type==LA_AREA) specfac*= inp;
1612 t= shadfac[3]*shi->spec*lampdist*specfac;
1614 if(ma->mode & MA_RAMP_SPEC) {
1616 do_specular_ramp(shi, specfac, t, spec);
1617 shr->spec[0]+= t*(lacol[0] * spec[0]);
1618 shr->spec[1]+= t*(lacol[1] * spec[1]);
1619 shr->spec[2]+= t*(lacol[2] * spec[2]);
1622 shr->spec[0]+= t*(lacol[0] * shi->specr);
1623 shr->spec[1]+= t*(lacol[1] * shi->specg);
1624 shr->spec[2]+= t*(lacol[2] * shi->specb);
1630 /* in case 'no diffuse' we still do most calculus, spec can be in shadow */
1631 if(i>0.0 && !(lar->mode & LA_NO_DIFF)) {
1632 if(ma->mode & MA_SHADOW_TRA) {
1633 add_to_diffuse(shr->diff, shi, is, i*shadfac[0]*lacol[0], i*shadfac[1]*lacol[1], i*shadfac[2]*lacol[2]);
1636 add_to_diffuse(shr->diff, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
1641 if(ma->mode & (MA_ZTRA|MA_RAYTRANSP)) {
1642 if(ma->fresnel_tra!=0.0)
1643 shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1645 if(shi->spectra!=0.0) {
1647 t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
1650 shi->alpha= (1.0-t)*shi->alpha+t;
1654 shr->alpha= shi->alpha;
1656 shr->diff[0]+= shi->r*shi->amb*shi->rad[0];
1657 shr->diff[0]+= shi->ambr;
1659 shr->diff[1]+= shi->g*shi->amb*shi->rad[1];
1660 shr->diff[1]+= shi->ambg;
1662 shr->diff[2]+= shi->b*shi->amb*shi->rad[2];
1663 shr->diff[2]+= shi->ambb;
1665 if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->diff, shi);
1666 if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shr->spec+1, shr->spec+2, shi);
1668 /* refcol is for envmap only */
1669 if(shi->refcol[0]!=0.0) {
1670 shr->diff[0]= shi->mirr*shi->refcol[1] + (1.0 - shi->mirr*shi->refcol[0])*shr->diff[0];
1671 shr->diff[1]= shi->mirg*shi->refcol[2] + (1.0 - shi->mirg*shi->refcol[0])*shr->diff[1];
1672 shr->diff[2]= shi->mirb*shi->refcol[3] + (1.0 - shi->mirb*shi->refcol[0])*shr->diff[2];
1677 /* this function sets all coords for render (shared with raytracer) */
1678 /* warning; exception for ortho render is here, can be done better! */
1679 void shade_input_set_coords(ShadeInput *shi, float u, float v, int i1, int i2, int i3)
1681 VertRen *v1, *v2, *v3;
1682 VlakRen *vlr= shi->vlr;
1684 short texco= shi->mat->texco;
1685 int mode= shi->mat->mode_l; /* or-ed result for all layers */
1688 /* for rendering of quads, the following values are used to denote vertices:
1689 0 1 2 scanline tria & first half quad, and ray tria
1690 0 2 3 scanline 2nd half quad
1691 0 1 3 raytracer first half quad
1692 2 1 3 raytracer 2nd half quad
1719 /* calculate U and V, for scanline (normal u and v are -1 to 0) */
1721 if( (vlr->flag & R_SMOOTH) || (texco & NEED_UV) ) {
1722 /* exception case for wire render of edge */
1723 if(vlr->v2==vlr->v3) {
1726 lend= VecLenf(v2->co, v1->co);
1727 lenc= VecLenf(shi->co, v1->co);
1733 u= - (1.0f - lenc/lend);
1745 float detsh, t00, t10, t01, t11;
1747 if(vlr->snproj==0) {
1748 t00= v3->co[0]-v1->co[0]; t01= v3->co[1]-v1->co[1];
1749 t10= v3->co[0]-v2->co[0]; t11= v3->co[1]-v2->co[1];
1751 else if(vlr->snproj==1) {
1752 t00= v3->co[0]-v1->co[0]; t01= v3->co[2]-v1->co[2];
1753 t10= v3->co[0]-v2->co[0]; t11= v3->co[2]-v2->co[2];
1756 t00= v3->co[1]-v1->co[1]; t01= v3->co[2]-v1->co[2];
1757 t10= v3->co[1]-v2->co[1]; t11= v3->co[2]-v2->co[2];
1760 detsh= 1.0/(t00*t11-t10*t01);
1761 t00*= detsh; t01*=detsh;
1762 t10*=detsh; t11*=detsh;
1764 if(vlr->snproj==0) {
1765 u= (shi->co[0]-v3->co[0])*t11-(shi->co[1]-v3->co[1])*t10;
1766 v= (shi->co[1]-v3->co[1])*t00-(shi->co[0]-v3->co[0])*t01;
1768 shi->dxuv[0]= shi->dxco[0]*t11- shi->dxco[1]*t10;
1769 shi->dxuv[1]= shi->dxco[1]*t00- shi->dxco[0]*t01;
1770 shi->dyuv[0]= shi->dyco[0]*t11- shi->dyco[1]*t10;
1771 shi->dyuv[1]= shi->dyco[1]*t00- shi->dyco[0]*t01;
1774 else if(vlr->snproj==1) {
1775 u= (shi->co[0]-v3->co[0])*t11-(shi->co[2]-v3->co[2])*t10;
1776 v= (shi->co[2]-v3->co[2])*t00-(shi->co[0]-v3->co[0])*t01;
1778 shi->dxuv[0]= shi->dxco[0]*t11- shi->dxco[2]*t10;
1779 shi->dxuv[1]= shi->dxco[2]*t00- shi->dxco[0]*t01;
1780 shi->dyuv[0]= shi->dyco[0]*t11- shi->dyco[2]*t10;
1781 shi->dyuv[1]= shi->dyco[2]*t00- shi->dyco[0]*t01;
1785 u= (shi->co[1]-v3->co[1])*t11-(shi->co[2]-v3->co[2])*t10;
1786 v= (shi->co[2]-v3->co[2])*t00-(shi->co[1]-v3->co[1])*t01;
1788 shi->dxuv[0]= shi->dxco[1]*t11- shi->dxco[2]*t10;
1789 shi->dxuv[1]= shi->dxco[2]*t00- shi->dxco[1]*t01;
1790 shi->dyuv[0]= shi->dyco[1]*t11- shi->dyco[2]*t10;
1791 shi->dyuv[1]= shi->dyco[2]*t00- shi->dyco[1]*t01;
1799 /* calculate punos (vertexnormals) */
1800 if(vlr->flag & R_SMOOTH) {
1801 float n1[3], n2[3], n3[3];
1803 if(shi->puno & p1) {
1804 n1[0]= -v1->n[0]; n1[1]= -v1->n[1]; n1[2]= -v1->n[2];
1806 n1[0]= v1->n[0]; n1[1]= v1->n[1]; n1[2]= v1->n[2];
1808 if(shi->puno & p2) {
1809 n2[0]= -v2->n[0]; n2[1]= -v2->n[1]; n2[2]= -v2->n[2];
1811 n2[0]= v2->n[0]; n2[1]= v2->n[1]; n2[2]= v2->n[2];
1814 if(shi->puno & p3) {
1815 n3[0]= -v3->n[0]; n3[1]= -v3->n[1]; n3[2]= -v3->n[2];
1817 n3[0]= v3->n[0]; n3[1]= v3->n[1]; n3[2]= v3->n[2];
1820 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
1821 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
1822 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
1826 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
1827 dl= shi->dxuv[0]+shi->dxuv[1];
1828 shi->dxno[0]= dl*n3[0]-shi->dxuv[0]*n1[0]-shi->dxuv[1]*n2[0];
1829 shi->dxno[1]= dl*n3[1]-shi->dxuv[0]*n1[1]-shi->dxuv[1]*n2[1];
1830 shi->dxno[2]= dl*n3[2]-shi->dxuv[0]*n1[2]-shi->dxuv[1]*n2[2];
1831 dl= shi->dyuv[0]+shi->dyuv[1];
1832 shi->dyno[0]= dl*n3[0]-shi->dyuv[0]*n1[0]-shi->dyuv[1]*n2[0];
1833 shi->dyno[1]= dl*n3[1]-shi->dyuv[0]*n1[1]-shi->dyuv[1]*n2[1];
1834 shi->dyno[2]= dl*n3[2]-shi->dyuv[0]*n1[2]-shi->dyuv[1]*n2[2];
1838 if(mode & MA_TANGENT_V) {
1839 float *s1, *s2, *s3;
1841 s1= RE_vertren_get_tangent(&R, v1, 0);
1842 s2= RE_vertren_get_tangent(&R, v2, 0);
1843 s3= RE_vertren_get_tangent(&R, v3, 0);
1844 if(s1 && s2 && s3) {
1845 shi->tang[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
1846 shi->tang[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
1847 shi->tang[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
1849 else shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1853 VECCOPY(shi->vn, shi->facenor);
1854 if(mode & MA_TANGENT_V)
1855 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1858 /* texture coordinates. shi->dxuv shi->dyuv have been set */
1859 if(texco & NEED_UV) {
1860 if(texco & TEXCO_ORCO) {
1862 float *o1, *o2, *o3;
1868 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
1869 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
1870 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
1873 dl= shi->dxuv[0]+shi->dxuv[1];
1874 shi->dxlo[0]= dl*o3[0]-shi->dxuv[0]*o1[0]-shi->dxuv[1]*o2[0];
1875 shi->dxlo[1]= dl*o3[1]-shi->dxuv[0]*o1[1]-shi->dxuv[1]*o2[1];
1876 shi->dxlo[2]= dl*o3[2]-shi->dxuv[0]*o1[2]-shi->dxuv[1]*o2[2];
1877 dl= shi->dyuv[0]+shi->dyuv[1];
1878 shi->dylo[0]= dl*o3[0]-shi->dyuv[0]*o1[0]-shi->dyuv[1]*o2[0];
1879 shi->dylo[1]= dl*o3[1]-shi->dyuv[0]*o1[1]-shi->dyuv[1]*o2[1];
1880 shi->dylo[2]= dl*o3[2]-shi->dyuv[0]*o1[2]-shi->dyuv[1]*o2[2];
1885 if(texco & TEXCO_GLOB) {
1886 VECCOPY(shi->gl, shi->co);
1887 MTC_Mat4MulVecfl(R.viewinv, shi->gl);
1889 VECCOPY(shi->dxgl, shi->dxco);
1890 MTC_Mat3MulVecfl(R.imat, shi->dxco);
1891 VECCOPY(shi->dygl, shi->dyco);
1892 MTC_Mat3MulVecfl(R.imat, shi->dyco);
1895 if(texco & TEXCO_STRAND) {
1896 shi->strand= (l*v3->accum - u*v1->accum - v*v2->accum);
1898 dl= shi->dxuv[0]+shi->dxuv[1];
1899 shi->dxstrand= dl*v3->accum-shi->dxuv[0]*v1->accum-shi->dxuv[1]*v2->accum;
1900 dl= shi->dyuv[0]+shi->dyuv[1];
1901 shi->dystrand= dl*v3->accum-shi->dyuv[0]*v1->accum-shi->dyuv[1]*v2->accum;
1904 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE))) {
1905 int j1=i1, j2=i2, j3=i3;
1907 /* to prevent storing new tfaces or vcols, we check a split runtime */
1909 /* |\ 1| or |1 /| */
1911 /* 1---2 1---2 0 = orig face, 1 = new face */
1913 /* Update vert nums to point to correct verts of original face */
1914 if(vlr->flag & R_DIVIDE_24) {
1915 if(vlr->flag & R_FACE_SPLIT) {
1922 else if(vlr->flag & R_FACE_SPLIT) {
1926 if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1929 char *cp1, *cp2, *cp3;
1931 cp1= (char *)(vlr->vcol+j1);
1932 cp2= (char *)(vlr->vcol+j2);
1933 cp3= (char *)(vlr->vcol+j3);
1935 shi->vcol[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0;
1936 shi->vcol[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0;
1937 shi->vcol[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0;
1946 float *uv1, *uv2, *uv3;
1948 uv1= vlr->tface->uv[j1];
1949 uv2= vlr->tface->uv[j2];
1950 uv3= vlr->tface->uv[j3];
1952 shi->uv[0]= -1.0 + 2.0*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1953 shi->uv[1]= -1.0 + 2.0*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1954 shi->uv[2]= 0.0; // texture.c assumes there are 3 coords
1959 dl= shi->dxuv[0]+shi->dxuv[1];
1960 duv[0]= shi->dxuv[0];
1961 duv[1]= shi->dxuv[1];
1963 shi->dxuv[0]= 2.0*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1964 shi->dxuv[1]= 2.0*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1966 dl= shi->dyuv[0]+shi->dyuv[1];
1967 duv[0]= shi->dyuv[0];
1968 duv[1]= shi->dyuv[1];
1970 shi->dyuv[0]= 2.0*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1971 shi->dyuv[1]= 2.0*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1973 if(mode & MA_FACETEXTURE) {
1974 if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1979 if(vlr->tface) render_realtime_texture(shi);
1983 shi->uv[0]= 2.0*(u+.5);
1984 shi->uv[1]= 2.0*(v+.5);
1985 shi->uv[2]= 0.0; // texture.c assumes there are 3 coords
1986 if(mode & MA_FACETEXTURE) {
1987 /* no tface? set at 1.0 */
1994 if(texco & TEXCO_NORM) {
1995 shi->orn[0]= -shi->vn[0];
1996 shi->orn[1]= -shi->vn[1];
1997 shi->orn[2]= -shi->vn[2];
1999 if(mode & MA_RADIO) {
2000 float *r1, *r2, *r3;
2002 r1= RE_vertren_get_rad(&R, v1, 0);
2003 r2= RE_vertren_get_rad(&R, v2, 0);
2004 r3= RE_vertren_get_rad(&R, v3, 0);
2006 if(r1 && r2 && r3) {
2007 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
2008 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
2009 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
2012 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2016 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2018 if(texco & TEXCO_REFL) {
2019 /* mirror reflection colour textures (and envmap) */
2020 calc_R_ref(shi); /* wrong location for normal maps! XXXXXXXXXXXXXX */
2022 if(texco & TEXCO_STRESS) {
2023 float *s1, *s2, *s3;
2025 s1= RE_vertren_get_stress(&R, v1, 0);
2026 s2= RE_vertren_get_stress(&R, v2, 0);
2027 s3= RE_vertren_get_stress(&R, v3, 0);
2028 if(s1 && s2 && s3) {
2029 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
2030 if(shi->stress<1.0f) shi->stress-= 1.0f;
2031 else shi->stress= (shi->stress-1.0f)/shi->stress;
2033 else shi->stress= 0.0f;
2035 if(texco & TEXCO_TANGENT) {
2036 if((mode & MA_TANGENT_V)==0) {
2037 /* just prevent surprises */
2038 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
2043 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2048 /* return labda for view vector being closest to line v3-v4 */
2049 /* was used for wire render */
2050 static float isec_view_line(float *view, float *v3, float *v4)
2053 float dot0, dot1, dot2, veclen, viewlen;
2056 vec[0]= v4[0] - v3[0];
2057 vec[1]= v4[1] - v3[1];
2058 vec[2]= v4[2] - v3[2];
2060 dot0 = v3[0]*vec[0] + v3[1]*vec[1] + v3[2]*vec[2];
2061 dot1 = vec[0]*view[0] + vec[1]*view[1] + vec[2]*view[2];
2062 dot2 = v3[0]*view[0] + v3[1]*view[1] + v3[2]*view[2];
2064 veclen = vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2];
2065 viewlen = view[0]*view[0] + view[1]*view[1] + view[2]*view[2];
2067 div = viewlen*veclen - dot1*dot1;
2068 if (div==0.0) return 0.0;
2070 fac = dot2*veclen - dot0*dot1;
2076 /* also used as callback for nodes */
2077 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
2080 shade_lamp_loop(shi, shr); /* clears shr */
2082 if(shi->translucency!=0.0) {
2085 VECCOPY(shi->vn, shi->vno);
2086 VecMulf(shi->vn, -1.0);
2087 VecMulf(shi->facenor, -1.0);
2088 shade_lamp_loop(shi, &shr_t);
2090 shr->diff[0]+= shi->translucency*shr_t.diff[0];
2091 shr->diff[1]+= shi->translucency*shr_t.diff[1];
2092 shr->diff[2]+= shi->translucency*shr_t.diff[2];
2093 VecMulf(shi->vn, -1.0);
2094 VecMulf(shi->facenor, -1.0);
2097 if(R.r.mode & R_RAYTRACE) {
2098 if(shi->ray_mirror!=0.0 || ((shi->mat->mode & MA_RAYTRANSP) && shr->alpha!=1.0)) {
2099 ray_trace(shi, shr);
2103 /* doesnt look 'correct', but is better for preview, plus envmaps dont raytrace this */
2104 if(shi->mat->mode & MA_RAYTRANSP) shr->alpha= 1.0;
2109 /* x,y: window coordinate from 0 to rectx,y */
2110 /* return pointer to rendered face */
2111 /* note, facenr declared volatile due to over-eager -O2 optimizations
2112 * on cygwin (particularly -frerun-cse-after-loop)
2114 void *shadepixel(RenderPart *pa, float x, float y, int z, volatile int facenr, int mask, float *col, float *rco)
2120 if(facenr< 0) { /* error */
2123 /* currently in use for dithering (soft shadow) node preview */
2124 shi.xs= (int)(x+0.5f);
2125 shi.ys= (int)(y+0.5f);
2126 shi.thread= pa->thread;
2127 shi.do_preview= R.r.scemode & R_NODE_PREVIEW;
2129 /* mask is used to indicate amount of samples (ray shad/mir and AO) */
2131 shi.depth= 0; // means first hit, not raytracing
2133 if(facenr==0) { /* sky */
2134 col[0]= 0.0; col[1]= 0.0; col[2]= 0.0; col[3]= 0.0;
2137 else if( (facenr & 0x7FFFFF) <= R.totvlak) {
2139 float alpha, fac, zcor;
2141 vlr= RE_findOrAddVlak(&R, (facenr-1) & 0x7FFFFF);
2146 shi.osatex= (shi.mat->texco & TEXCO_OSA);
2148 /* copy the face normal (needed because it gets flipped for tracing */
2149 VECCOPY(shi.facenor, vlr->n);
2150 shi.puno= vlr->puno;
2154 /* COXYZ AND VIEW VECTOR */
2155 calc_view_vector(shi.view, x, y); /* returns not normalized, so is in viewplane coords */
2157 /* wire cannot use normal for calculating shi.co */
2158 if(shi.mat->mode & MA_WIRE) {
2160 /* inverse of zbuf calc: zbuf = MAXZ*hoco_z/hoco_w */
2162 zco= ((float)z)/2147483647.0f;
2163 shi.co[2]= R.winmat[3][2]/( R.winmat[2][3]*zco - R.winmat[2][2] );
2165 fac= zcor= shi.co[2]/shi.view[2];
2167 shi.co[0]= fac*shi.view[0];
2168 shi.co[1]= fac*shi.view[1];
2173 dface= v1->co[0]*shi.facenor[0]+v1->co[1]*shi.facenor[1]+v1->co[2]*shi.facenor[2];
2175 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
2176 if(R.r.mode & R_ORTHO) {
2177 /* x and y 3d coordinate can be derived from pixel coord and winmat */
2178 float fx= 2.0/(R.winx*R.winmat[0][0]);
2179 float fy= 2.0/(R.winy*R.winmat[1][1]);
2181 shi.co[0]= (0.5 + x - 0.5*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
2182 shi.co[1]= (0.5 + y - 0.5*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
2184 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
2185 if(shi.facenor[2]!=0.0f)
2186 shi.co[2]= (dface - shi.facenor[0]*shi.co[0] - shi.facenor[1]*shi.co[1])/shi.facenor[2];
2190 zcor= 1.0; // only to prevent not-initialize
2192 if(shi.osatex || (R.r.mode & R_SHADOW) ) {
2195 if(shi.facenor[2]!=0.0f)
2196 shi.dxco[2]= (shi.facenor[0]*fx)/shi.facenor[2];
2202 if(shi.facenor[2]!=0.0f)
2203 shi.dyco[2]= (shi.facenor[1]*fy)/shi.facenor[2];
2211 div= shi.facenor[0]*shi.view[0] + shi.facenor[1]*shi.view[1] + shi.facenor[2]*shi.view[2];
2212 if (div!=0.0) fac= zcor= dface/div;
2213 else fac= zcor= 0.0;
2215 shi.co[0]= fac*shi.view[0];
2216 shi.co[1]= fac*shi.view[1];
2217 shi.co[2]= fac*shi.view[2];
2219 /* pixel dx/dy for render coord */
2220 if(shi.osatex || (R.r.mode & R_SHADOW) ) {
2221 float u= dface/(div - R.viewdx*shi.facenor[0]);
2222 float v= dface/(div - R.viewdy*shi.facenor[1]);
2224 shi.dxco[0]= shi.co[0]- (shi.view[0]-R.viewdx)*u;
2225 shi.dxco[1]= shi.co[1]- (shi.view[1])*u;
2226 shi.dxco[2]= shi.co[2]- (shi.view[2])*u;
2228 shi.dyco[0]= shi.co[0]- (shi.view[0])*v;
2229 shi.dyco[1]= shi.co[1]- (shi.view[1]-R.viewdy)*v;
2230 shi.dyco[2]= shi.co[2]- (shi.view[2])*v;
2235 /* rco might be used for sky texture */
2236 VECCOPY(rco, shi.co);
2238 /* cannot normalise earlier, code above needs it at viewplane level */
2239 fac= Normalise(shi.view);
2240 zcor*= fac; // for mist, distance of point from camera
2243 if( (shi.mat->texco & TEXCO_REFL) ) {
2244 shi.dxview= -R.viewdx/fac;
2245 shi.dyview= -R.viewdy/fac;
2249 /* calcuate normals, texture coords, vertex colors, etc */
2250 if(facenr & 0x800000)
2251 shade_input_set_coords(&shi, 1.0, 1.0, 0, 2, 3);
2253 shade_input_set_coords(&shi, 1.0, 1.0, 0, 1, 2);
2255 /* this only avalailable for scanline */
2256 if(shi.mat->texco & TEXCO_WINDOW) {
2257 shi.winco[0]= -1.0f + 2.0f*x/(float)R.winx;
2258 shi.winco[1]= -1.0f + 2.0f*y/(float)R.winy;
2261 shi.dxwin[0]= 2.0/(float)R.winx;
2262 shi.dywin[1]= 2.0/(float)R.winy;
2263 shi.dxwin[1]= shi.dxwin[2]= 0.0;
2264 shi.dywin[0]= shi.dywin[2]= 0.0;
2267 /* after this the u and v AND shi.dxuv and shi.dyuv are incorrect */
2268 if(shi.mat->texco & TEXCO_STICKY) {
2270 float *s1, *s2, *s3;
2272 if(facenr & 0x800000) {
2273 v2= vlr->v3; v3= vlr->v4;
2275 v2= vlr->v2; v3= vlr->v3;
2278 s1= RE_vertren_get_sticky(&R, v1, 0);
2279 s2= RE_vertren_get_sticky(&R, v2, 0);
2280 s3= RE_vertren_get_sticky(&R, v3, 0);
2282 if(s1 && s2 && s3) {
2284 float hox, hoy, l, dl, u, v;
2285 float s00, s01, s10, s11, detsh;
2288 Zmulx= R.winx; Zmuly= R.winy;
2290 s00= v3->ho[0]/v3->ho[3] - v1->ho[0]/v1->ho[3];
2291 s01= v3->ho[1]/v3->ho[3] - v1->ho[1]/v1->ho[3];
2292 s10= v3->ho[0]/v3->ho[3] - v2->ho[0]/v2->ho[3];
2293 s11= v3->ho[1]/v3->ho[3] - v2->ho[1]/v2->ho[3];
2295 detsh= s00*s11-s10*s01;
2296 s00/= detsh; s01/=detsh;
2297 s10/=detsh; s11/=detsh;
2299 /* recalc u and v again */
2302 u= (hox - v3->ho[0]/v3->ho[3])*s11 - (hoy - v3->ho[1]/v3->ho[3])*s10;
2303 v= (hoy - v3->ho[1]/v3->ho[3])*s00 - (hox - v3->ho[0]/v3->ho[3])*s01;
2306 shi.sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
2307 shi.sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
2311 shi.dxuv[0]= s11/Zmulx;
2312 shi.dxuv[1]= - s01/Zmulx;
2313 shi.dyuv[0]= - s10/Zmuly;
2314 shi.dyuv[1]= s00/Zmuly;
2316 dl= shi.dxuv[0]+shi.dxuv[1];
2317 shi.dxsticky[0]= dl*s3[0]-shi.dxuv[0]*s1[0]-shi.dxuv[1]*s2[0];
2318 shi.dxsticky[1]= dl*s3[1]-shi.dxuv[0]*s1[1]-shi.dxuv[1]*s2[1];
2319 dl= shi.dyuv[0]+shi.dyuv[1];
2320 shi.dysticky[0]= dl*s3[0]-shi.dyuv[0]*s1[0]-shi.dyuv[1]*s2[0];
2321 shi.dysticky[1]= dl*s3[1]-shi.dyuv[0]*s1[1]-shi.dyuv[1]*s2[1];
2326 /* ------ main shading loop -------- */
2327 VECCOPY(shi.vno, shi.vn);
2329 if(shi.mat->nodetree && shi.mat->use_nodes) {
2330 ntreeShaderExecTree(shi.mat->nodetree, &shi, &shr);
2333 /* copy all relevant material vars, note, keep this synced with render_types.h */
2334 memcpy(&shi.r, &shi.mat->r, 23*sizeof(float));
2335 shi.har= shi.mat->har;
2337 shade_material_loop(&shi, &shr);
2340 /* after shading and composit layers */
2341 if(shr.spec[0]<0.0f) shr.spec[0]= 0.0f;
2342 if(shr.spec[1]<0.0f) shr.spec[1]= 0.0f;
2343 if(shr.spec[2]<0.0f) shr.spec[2]= 0.0f;
2345 if(shr.diff[0]<0.0f) shr.diff[0]= 0.0f;
2346 if(shr.diff[1]<0.0f) shr.diff[1]= 0.0f;
2347 if(shr.diff[2]<0.0f) shr.diff[2]= 0.0f;
2349 VECADD(col, shr.diff, shr.spec);
2351 /* NOTE: this is not correct here, sky from raytrace gets corrected... */
2352 /* exposure correction */
2353 if(R.wrld.exp!=0.0 || R.wrld.range!=1.0) {
2354 if((shi.mat->mode & MA_SHLESS)==0) {
2355 col[0]= R.wrld.linfac*(1.0-exp( col[0]*R.wrld.logfac) );
2356 col[1]= R.wrld.linfac*(1.0-exp( col[1]*R.wrld.logfac) );
2357 col[2]= R.wrld.linfac*(1.0-exp( col[2]*R.wrld.logfac) );
2362 if((R.wrld.mode & WO_MIST) && (shi.mat->mode & MA_NOMIST)==0 ) {
2363 if(R.r.mode & R_ORTHO)
2364 alpha= mistfactor(-shi.co[2], shi.co);
2366 alpha= mistfactor(zcor, shi.co);
2370 if(shr.alpha!=1.0 || alpha!=1.0) {
2371 if(shi.mat->mode & MA_RAYTRANSP) {
2373 if(R.r.mode & R_UNIFIED)
2374 /* unified alpha overs everything... */
2377 /* sky was applied allready for ray transp, only do mist */
2382 fac= alpha*(shr.alpha);
2392 if(R.flag & R_LAMPHALO) {
2393 if(facenr<=0) { /* calc view vector and put shi.co at far */
2394 if(R.r.mode & R_ORTHO) {
2395 /* x and y 3d coordinate can be derived from pixel coord and winmat */
2396 float fx= 2.0/(R.rectx*R.winmat[0][0]);
2397 float fy= 2.0/(R.recty*R.winmat[1][1]);
2399 shi.co[0]= (0.5 + x - 0.5*R.rectx)*fx - R.winmat[3][0]/R.winmat[0][0];
2400 shi.co[1]= (0.5 + y - 0.5*R.recty)*fy - R.winmat[3][1]/R.winmat[1][1];
2403 calc_view_vector(shi.view, x, y);
2406 renderspothalo(&shi, col, 1.0);
2409 renderspothalo(&shi, col, col[3]);
2415 static void shadepixel_sky(RenderPart *pa, float x, float y, int z, int facenr, int mask, float *colf)
2418 float collector[4], rco[3];
2420 vlr= shadepixel(pa, x, y, z, facenr, mask, colf, rco);
2421 if(colf[3] != 1.0) {
2422 /* bail out when raytrace transparency (sky included already) */
2423 if(vlr && (R.r.mode & R_RAYTRACE))
2424 if(vlr->mat->mode & MA_RAYTRANSP) return;
2426 renderSkyPixelFloat(collector, x, y, vlr?rco:NULL);
2427 addAlphaOverFloat(collector, colf);
2428 QUATCOPY(colf, collector);
2432 /* adds only alpha values */
2433 static void edge_enhance_calc(RenderPart *pa, float *rectf)
2435 /* use zbuffer to define edges, add it to the image */
2436 int y, x, col, *rz, *rz1, *rz2, *rz3;
2437 int zval1, zval2, zval3;
2440 /* shift values in zbuffer 4 to the right, for filter we need multiplying with 12 max */
2442 if(rz==NULL) return;
2444 for(y=0; y<pa->recty; y++) {
2445 for(x=0; x<pa->rectx; x++, rz++) (*rz)>>= 4;
2452 rf= rectf+pa->rectx+1;
2454 for(y=0; y<pa->recty-2; y++) {
2455 for(x=0; x<pa->rectx-2; x++, rz1++, rz2++, rz3++, rf++) {
2457 /* prevent overflow with sky z values */
2458 zval1= rz1[0] + 2*rz1[1] + rz1[2];
2459 zval2= 2*rz2[0] + 2*rz2[2];
2460 zval3= rz3[0] + 2*rz3[1] + rz3[2];
2462 col= abs ( 4*rz2[1] - (zval1 + zval2 + zval3)/3 );
2465 if(col > (1<<16)) col= (1<<16);
2466 else col= (R.r.edgeint*col)>>8;
2471 if(col>255) fcol= 1.0f;
2472 else fcol= (float)col/255.0f;
2475 *rf+= fcol/(float)R.osa;
2487 static void edge_enhance_add(RenderPart *pa, float *rectf, float *arect)
2492 for(pix= pa->rectx*pa->recty; pix>0; pix--, arect++, rectf+=4) {
2493 if(*arect != 0.0f) {
2494 addcol[0]= *arect * R.r.edgeR;
2495 addcol[1]= *arect * R.r.edgeG;
2496 addcol[2]= *arect * R.r.edgeB;
2498 addAlphaOverFloat(rectf, addcol);
2504 /* ********************* MAINLOOPS ******************** */
2506 static void shadeDA_tile(RenderPart *pa, float *rectf, float *recta)
2510 float fcol[4], *rf, *grf, *acol= NULL;
2511 long *rd, *rectdaps= pa->rectdaps;
2512 int zbuf, samp, curmask, face, mask, fullmask;
2513 int b, x, y, full_osa, seed, crop=0;
2515 if(R.test_break()) return;
2517 /* we set per pixel a fixed seed, for random AO and shadow samples */
2518 seed= pa->rectx*pa->disprect.ymin;
2520 fullmask= (1<<R.osa)-1;
2522 /* might need it for gamma, in end of this function */
2525 /* filtered render, for now we assume only 1 filter size */
2528 rectf+= 4*(pa->rectx + 1);
2529 rectdaps+= pa->rectx + 1;
2530 if(recta) recta+= 4*(pa->rectx + 1);
2533 for(y=pa->disprect.ymin+crop; y<pa->disprect.ymax-crop; y++) {
2536 if(recta) acol= recta;
2538 for(x=pa->disprect.xmin+crop; x<pa->disprect.xmax-crop; x++, rd++, rf+=4) {
2539 BLI_thread_srandom(pa->thread, seed+x);
2541 ps= (PixStr *)(*rd);
2544 /* complex loop, because empty spots are sky, without mask */
2549 curmask= (~mask) & fullmask;
2558 /* check osa level */
2559 if(face==0) full_osa= 0;
2561 VlakRen *vlr= RE_findOrAddVlak(&R, (face-1) & 0x7FFFFF);
2562 full_osa= (vlr->flag & R_FULL_OSA);
2566 for(samp=0; samp<R.osa; samp++) {
2567 if(curmask & (1<<samp)) {
2568 xs= (float)x + R.jit[samp][0];
2569 ys= (float)y + R.jit[samp][1];
2570 shadepixel_sky(pa, xs, ys, zbuf, face, (1<<samp), fcol);
2572 if(acol && acol[3]!=0.0) addAlphaOverFloat(fcol, acol);
2574 fcol[0]= gammaCorrect(fcol[0]);
2575 fcol[1]= gammaCorrect(fcol[1]);
2576 fcol[2]= gammaCorrect(fcol[2]);
2578 add_filt_fmask(1<<samp, fcol, rf, pa->rectx);
2583 b= R.samples->centmask[curmask];
2584 xs= (float)x+R.samples->centLut[b & 15];
2585 ys= (float)y+R.samples->centLut[b>>4];
2586 shadepixel_sky(pa, xs, ys, zbuf, face, curmask, fcol);
2588 if(acol && acol[3]!=0.0) addAlphaOverFloat(fcol, acol);
2591 fcol[0]= gammaCorrect(fcol[0]);
2592 fcol[1]= gammaCorrect(fcol[1]);
2593 fcol[2]= gammaCorrect(fcol[2]);
2595 add_filt_fmask(curmask, fcol, rf, pa->rectx);
2606 rectf+= 4*pa->rectx;
2607 rectdaps+= pa->rectx;
2608 if(recta) recta+= 4*pa->rectx;
2611 if(y&1) if(R.test_break()) break;
2615 for(y= pa->rectx*pa->recty; y>0; y--, grf+=4) {
2616 grf[0] = invGammaCorrect(grf[0]);
2617 grf[1] = invGammaCorrect(grf[1]);
2618 grf[2] = invGammaCorrect(grf[2]);
2624 /* ************* pixel struct ******** */
2627 static PixStrMain *addpsmain(ListBase *lb)
2631 psm= (PixStrMain *)RE_mallocN(sizeof(PixStrMain),"pixstrMain");
2632 BLI_addtail(lb, psm);
2634 psm->ps= (PixStr *)RE_mallocN(4096*sizeof(PixStr),"pixstr");
2640 static void freeps(ListBase *lb)
2642 PixStrMain *psm, *psmnext;
2644 for(psm= lb->first; psm; psm= psmnext) {
2650 lb->first= lb->last= NULL;
2653 static void addps(ListBase *lb, long *rd, int facenr, int z, unsigned short mask)
2656 PixStr *ps, *last= NULL;
2659 ps= (PixStr *)(*rd);
2662 if( ps->facenr == facenr ) {
2671 /* make new PS (pixel struct) */
2674 if(psm->counter==4095)
2677 ps= psm->ps + psm->counter++;
2679 if(last) last->next= ps;
2688 static void make_pixelstructs(RenderPart *pa, ListBase *lb)
2690 long *rd= pa->rectdaps;
2694 int mask= 1<<pa->sample;
2696 for(y=0; y<pa->recty; y++) {
2697 for(x=0; x<pa->rectx; x++, rd++, rp++) {
2699 addps(lb, rd, *rp, *(rz+x), mask);
2706 /* supposed to be fully threadable! */
2707 void zbufshadeDA_tile(RenderPart *pa)
2710 ListBase psmlist= {NULL, NULL};
2711 float *acolrect= NULL, *edgerect= NULL;
2713 set_part_zbuf_clipflag(pa);
2715 /* allocate the necessary buffers */
2716 /* zbuffer inits these rects */
2717 pa->rectp= RE_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
2718 pa->rectz= RE_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
2719 if(R.r.mode & R_EDGE) edgerect= RE_callocN(sizeof(float)*pa->rectx*pa->recty, "rectedge");
2721 for(rl= pa->result->layers.first; rl; rl= rl->next) {
2723 /* initialize pixelstructs */
2724 addpsmain(&psmlist);
2725 pa->rectdaps= RE_callocN(sizeof(long)*pa->rectx*pa->recty+4, "zbufDArectd");
2727 if(rl->layflag & SCE_LAY_SOLID) {
2728 for(pa->sample=0; pa->sample<R.osa; pa->sample++) {
2729 zbuffer_solid(pa, rl->lay, rl->layflag);
2730 make_pixelstructs(pa, &psmlist);
2732 if(R.r.mode & R_EDGE) edge_enhance_calc(pa, edgerect);
2733 if(R.test_break()) break;
2736 else /* need to clear rectz for next passes */
2737 fillrect(pa->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2740 /* we do transp layer first, so its get added with filter in main buffer... still incorrect though */
2741 if(R.flag & R_ZTRA) {
2742 if(rl->layflag & SCE_LAY_ZTRA) {
2743 acolrect= RE_callocN(4*sizeof(float)*pa->rectx*pa->recty, "alpha layer");
2744 zbuffer_transp_shade(pa, acolrect, rl->lay, rl->layflag);
2748 /* shades solid and adds transparent layer */
2749 if(rl->layflag & SCE_LAY_SOLID)
2750 shadeDA_tile(pa, rl->rectf, acolrect);
2752 SWAP(float *, acolrect, rl->rectf);
2756 if(R.r.mode & R_EDGE)
2757 edge_enhance_add(pa, rl->rectf, edgerect);
2759 if(rl->layflag & SCE_LAY_HALO)
2760 halo_tile(pa, rl->rectf, rl->lay);
2762 if(rl->passflag & SCE_PASS_Z)
2763 convert_zbuf_to_distbuf(pa, rl);
2765 /* free stuff within loop! */
2770 RE_freeN(pa->rectdaps); pa->rectdaps= NULL;
2775 RE_freeN(pa->rectp); pa->rectp= NULL;
2776 RE_freeN(pa->rectz); pa->rectz= NULL;
2778 if(edgerect) RE_freeN(edgerect);
2784 /* ------------------------------------------------------------------------ */
2786 /* supposed to be fully threadable! */
2787 void zbufshade_tile(RenderPart *pa)