OpenGL is at times called a conventional renderer. The method used in producing an image is by generating it scanline by scanline. Raytracing is another rendering method. We will not discuss here the method but show how we can create a shadow by appling the ray trace technique.
A point in the scene is in the shadow of an object when that object blocks the light that normally illuminates the point. In ray tracing, the decision is based on "casting" a arrow (ray) from the point to the light source. If the ray intersects an object in the world on its way to the light, then the point is in the shadow of that object with respect to the light source. We investigate in the tutorial how to use a ray for computing a shadow on a plane and, in particular, the points in a rectangle in the x-z coordinate plane. Further, we use three objects in our investigation -- a sphere, a hollow cylinder (one without end caps) and a solid cylinder (one with end caps).
To carry out the necessary calculations, we must first construct a mathematical model for the ray. We use the parametric equations of a line for this purpose. Let (px, py, pz) denote the coordinates of the point in question and let (dx, dy, dz) be the vector "pointing to" the light source. If the light is either a spotlight or an omni light, then the vector is the difference between coordinates of the light's position and the point. In the case of a directional light, the vector is the light's direction. Using t as the parameter, the equations for the points (x, y, z) on the line are as follows:
x = dx * t + px y = dy * t + py z = dz * t + pzNext, we need mathematical descriptions for the objects in order to calculate whether the ray (line) intersects the object and, in turn, if the object casts a shadow at the point. The discussion for each of the three objects follows.
(x - cx)^2 + (y - cy)^2 + (z- cz)^2 = r^2where (cx, cy, cz) is the center of the sphere and r is its radius. Placing each of the formulae for x, y and z into the above equation, we obtain a quadratic equation in the parameter t. If we solve the quadratic equation for t, we would find the values of t, if any, at which the line intersects the sphere. However, we only need to know if there is a solution versus finding the actual solution. By using the quadratic formula, we can determine from the discriminant if a solution exits. In particular, a solution exists and, in turn, the point is in the shadow of the sphere when the discriminant is greater than or equal to zero.
x^2 + z^2 = r^2Substituting the parameter formulae for x and z into this equation again gives us a quadratic equation in the variable t. As with the sphere, we can look at the discriminant in the quatratic fomula to determine if there is a solution. However, a solution in this case only tells us if the ray intersects the "extended" wall of the cylinder. Therefore, we need to find the actual solutions for t and use them to find the y-coordinates of the points of intersection. Using the solutions for t, if any, in the equation for y, we obtain the two distances from the xz-coordinate plane at which intersections occur. If either of the values for y is between the base of the cylinder and its top, then the ray intersects the cylinder and the point is in the shadow of the cylinder.
Our strategy for this object is first check whether the ray intersects the top cap of the solid cylinder. If the ray intersects this cap independent of it intersecting a wall first, the point is in the shadow of the cylinder. If the ray does not intersect the top cap, then we check for intersection with the wall of the cylinder in exactly the same manner as for a hollow cylinder.
Determining whether the ray intersects the top cap is straight forward. First note that the cap is a disk and lies in a plane parallel to the xz-coordinate plane. Thus, we use the y equation in the ray's parametric equations and find the value of t for which the ray intersects this plane. Next, we use this value for t and determine the (x,z) coordinates of the point of intersection. Last, we compute the distance of the point (x,z) from the center of the disk. If this distance is less than or equal to the radius of the disk, then the ray intersects the top cap.