Apr 97 Challenge
Volume Number: 13
Issue Number: 4
Column Tag: Programmer's Challenge
Programmer's Challenge
By Bob Boonstra, Westford, MA
Projection
For the Challenge this month, we return to the topic of computer graphics - you'll be
solving a simplified rendering problem. Your Challenge is to create the image formed
by a set of polygons on a specified projection plane, as viewed from a specific
viewpoint, and as illuminated from a point light source. You will need to perform
hidden surface elimination, create shadows caused by the light source, and project the
image as it would be seen by someone at the viewpoint. You will be performing
multiple projections from a given viewpoint, so this Challenge includes an
initialization routine as well as a calculation routine, both of which are included for
timing purposes in determining the winner.
The prototype for the code you should write is:
#define kMAXPOINTS 10
typedef struct My2DPoint {/* point in z==0 plane */
float x2D; /* x coordinate */
float y2D; /* y coordinate */
typedef struct My3DPoint {
float x3D; /* x coordinate */
float y3D; /* y coordinate */
float z3D; /* z coordinate */
typedef struct My3DDirection {
float thetaX; /* angle in radians */
float thetaY; /* angle in radians */
float thetaZ; /* angle in radians */
My3DDirection planeNormal; /* normal vector to plane */
My3DPointplaneOrigin; /* origin of plane in 3D space */
typedef struct MyPolygon {
long numPoints; /* number of points in polygon */
My2DPointthePoint[kMAXPOINTS]; /* polygon in z==0 plane */
MyPlanepolyPlane; /* rotate/translate z==0 plane to this plane */
RGBColor polyColor/* the color to draw this polygon */
void InitProjection(
My3DPoint*viewPoint,/* viewpoint from which to project */
My3DPoint*illumPoint, /* viewpoint from which to draw shadow */
void *storage,/* auxiliary storage preallocated for your use */
long storageSize/* number of bytes of storage */
);
void CalcProjection(
GWorldPtroffScreen, /* GWorld to draw projection */
MyPolygonthePolys[],/* polygons to project */
long numPolys,/* number of polygons to project */
My3DPoint*viewPoint,/* viewpoint from which to project */
My3DPoint*illumPoint, /* illumination point from which to draw
shadow */
void *storage,/* auxiliary storage preallocated for your use */
long storageSize/* number of bytes of storage */
);
Your InitProjection routine will be provided with a pointer to auxiliary storage
(storageSize bytes, at least 1MB) preallocated for your use, along with the viewPoint
from which projections are to be made and the illumPoint location of an illumination
source from which shadows are to be created. InitProjection may perform any
calculations that may be useful for multiple CalcProjection calls that follow.
CalcProjection will be provided the same parameters given to InitProjection, along
with the number (numPolys) and location of the polygons to be projected, and the
offScreen GWorld in which the projection is to be drawn. CalcProjection should
calculate the way thePolys would look from viewPoint, projected onto a projection
plane normal to the viewPoint vector and passing through the origin. Hidden surface
elimination must be performed so that obscured polygons or parts of polygons are not
seen. The image of the projection is to be rendered in the GWorld pointed to by
offScreen, with the projection plane mapped to the z==0 plane in the GWorld. Polygons
must be rendered in the appropriate polyColor, subject to the limitations of the
GWorld. Polygons are the same color on both sides. Parts of the projection plane not