Computer Science 155: Graphics
Spring 1998
Homework 3

The early due date is Saturday, February 14 before midnight.
The regular due date is Sunday, February 15 before midnight.
The late due date is Monday, February 16 before midnight.


In this assignment you will be implementing the foundations of 3D OpenGL. In the next assignment you will add z-buffering, perspective projection, and arbitrary camera position. In this assignment, you should handle the following functions:

  1. mglInit()
    This function simply initializes the framebuffer to black. The framebuffer is a 3-dimensional array of integers whose dimensions are FRAME_BUFFER_WIDTH by FRAME_BUFFER_HEIGHT by 3. The first two dimensions are specified in the dimensions.h file. The third dimension is used to store the red, green, and blue color values from 0 to 255 each.
  2. mglColor(int r, int g, int b)
    This function sets the drawing color to the appropriate rgb intensities from 0 to 255 for each color. Intensities less than 0 are clamped to 0 and intensities above 255 are clamped to 255. (OpenGL also has a floating point color mode where color intensities are in the range from 0 to 1. You don't need to worry about this here.)
  3. mglViewport(int sw_x, int sw_y, int ne_x, int ne_y)
    This function establishes the southwest and northeast pixel coordinates of the viewport in the framebuffer. This function simply sets four global variables.
  4. mglOrtho(float l, float r, float b, float t, float near, float far)
    You can assume that this function is only called after mglViewport. This function constructs two matrices. The first matrix, called R maps points from this orthographic viewing volume to the canonical viewing volume (-1 to 1 in each of x, y, and z). The second matrix, called S maps points from the canonical viewing volume (world coordinates) to the viewport (screen coordinates). It is recommended that this transformation keep the z-coordinate. (It will be useful for z-buffering in the next assignment.)
  5. mglLoadIdentity()
    This function loads the identity matrix into the global modelview matrix. Remember that this matrix is 4 by 4.
  6. mglScale(float sx, float sy, float sz)
    This function computes the appropriate scaling matrix and multiplies it on the right-hand side of the modelview matrix.
  7. mglTranslate(float tx, float ty, float tz)
    This function computes the appropriate translation matrix and multiplies it on the right-hand side of the modelview matrix.
  8. mglRotatex(float angle), mglRotatey(float angle), mglRotatez(float angle)
    Unlike regular OpenGL, you will implement three rotate functions, one for rotating about the x-axis, one for the y-axis, and one for the z-axis. You do not need to worry about rotating about an arbitrary line in 3-space. Each of these functions computes a rotation matrix and multiplies it on the right-hand side of the modelview matrix.
  9. mglBegin(int object_type)
    This function precedes a list of mglVertex calls and sets the type of object to be drawn to object_type. The object_type can be either MGL_LINES or MGL_POLYGONS. This function also initializes the vertex list (the linked list of vertices) to be NULL. If MGL_LINES is selected, then the number of vertices specified is assumed to be even and a sequence of lines are rendered between points 1 and 2, points 3 and 4, etc. It there are an odd number of vertices, the last vertex is ignored.
  10. mglVertex(float x, float y, float z)
    This function adds another vertex to the end of the current object's vertex list.
  11. mglEnd()
    This function performs the following sequence of operations:
    1. First, the modelview matrix is applied to each vertex.
    2. Next, the orthographic volume is transformed into the canonical viewing volume (-1 to 1 in each of x, y, and z) by applying matrix R (see the description in mglOrtho() above).
    3. Clipping against the canonical viewing volume is applied to the object in 3-dimensions. You should clip polygons against the canonical viewing volume but analytic line clipping is not required. (If you choose to implement analytic line clipping, you will be awarded 15 bonus points and a piece of chocolate. Only do this if you have finished everything else.)
    4. Next, each point of the object is mapped into the viewport (screen coordinates) using matrix S (see the description in mglOrtho() above).
    5. Finally, the object is scanconverted into the framebuffer. The AET algorithm is applied if the object is a polygon and Bresenham's algorithms is applied for line segments.
  12. mglFlush()
    This function sends the rgb framebuffer to standard output. (This way you can pipe your output to display which simulates the video controller for our system.)