I am playing around with a simple code that generates a shaded sphere. I don't yet fully understand the math but I am figuring it out as I play with the code. I was wondering how one might implement specular shading into this based on this code. Any suggestions?
for (y=0;y<screenHeight;y++)
for (x=0;x<screenWidth;x++)
if (sqr((x-xcenter)*(x-xcenter)+(y-ycenter)*(y-ycenter))<radius)
{
vx=(x-xcenter);
vy=(y-xcenter);
vz=sqr(radius*radius-vx*vx-vy*vy);
vl=sqr(vx*vx+vy*vy+vz*vz);
co_angle=(lx*vx+ly*vy+lz*vz)/(ll*vl);
pixel=co_angle*255;
}
I was looking at this thread and the second image is what I am after. But I also don't fully understand the math there either: Trouble with Phong Shading
Thanks in advance.
To add reflection first you need mirror light vector by surface normal
l
- (yellow) to light source n
- (aqua) surface normal r
- (green) reflected light direction e
- (orange) to eye/camera direction p
- (red) - rendered pixel position q
- (magenta) - reflection midpoint So how to do it?
n,p,e,l
are knowns or can be easily computed l
is constant for all pixels l=light_pos-p; l/=|l|;
l=light_pos-p; l/=|l|;
e=eye_pos-p; e/=|e|;
q
I use dot product for this q=p+(n*dot(l,n));
r
vector is easy r=(p+l)+2*(q-(p+l))-p=2*(qp)-l;
now you have reflection vector r
m=light_color*dot(r,e)+ambient_light;
r
ca=cos(ang)=dot(r,e);
you do not need ang
directly cosine is fine ca=pow(ca,5.0)
can use any exponent m=(light_color*0.5*(ca+dot(r,e)))+ambient_light;
p
with color=surface_color*m;
Hope I didn't forget something it is a while I coded something like this...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.