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 wonderi
To add reflection first you need mirror light vector by surface normal
l
- (yellow) to light sourcen
- (aqua) surface normalr
- (green) reflected light directione
- (orange) to eye/camera directionp
- (red) - rendered pixel positionq
- (magenta) - reflection midpointSo how to do it?
n,p,e,l
are knowns or can be easily computedl
is constant for all pixelsl=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*(q-p)-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 fineca=pow(ca,5.0)
can use any exponentm=(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...