A mathematical and visual look at the Dot Product in regards to shading.

The math behind computer graphics.

Date Created:Friday December 29th, 2006 03:41 AM

Date Modified:Friday August 01st, 2008 02:26 AM

//more math to explain the dot product L is the Vector from the point on the surface(Ps) to the light source(P), hence L = P-Ps. Ln is L normalized. N is the Shading normal for the surface. Nn is N normalized. Light is at (1,5,0) Point is at (0,0,0) P = (1,5,0) Ps = (0,0,0) //calculate the vector of the light to the point by subrating the two points L = P-Ps L = (1,5,0) //calculate the distance of the two points: distance(P,Ps) = sqrt( (P.x-Ps.x)^2 + (P.y-Ps.y)^2 ) distance(P,Ps) = sqrt( 1^2 + 5^2 ) distance(P,Ps) = sqrt(26) //normalize L by dividing each component by the distance of the vector L Ln = ( 1/sqrt(26), 5/sqrt(26) , 0 ) Ln = ( 0.196116, 0.980581, 0 ) N = ( 1, 1, 0 ) //calculate length of N, subtracting the origin from the vector length(N) = sqrt( (1-0)^2 + (1-0)^2 ) length(N) = sqrt(2) length(N) = 1.41421 //normalize N Nn = ( 1/1.41421, 1/1.41421, 0) Nn = ( 0.707, 0,707, 0 ) //take the dot product of Ln and Nn dot(Ln,Nn) = ( (Ln.x)(Nn.x) + (Ln.y)(Nn.y) ) dot(Ln,Nn) = ( (0.196116)(0.707) + (0.980581)(0.707) ) dot(Ln,Nn) = 0.83205 //the dot product is equal to the cosine of the angle inbetween the vectors dot(Ln,Nn) = cos@ //the angle is between the vectors is equal to the arccosine of the dot product @ = acos( dot(Ln,Nn) ) SHADING THE SURFACE! @ = 33.6901 33.6901 degrees away from being at a 0 degree angle from the light if it was directly lined up, the dot product would return 0, hence the cos(0) is 1 the dot product returns the percentage that the light will shade the surface! dot(Ln,Nn) = 0.83205 = 83% shading from light