Computer Science 155: Graphics
Spring 1998
Homework 1

The early due date is Saturday, January 31 before midnight
The regular due date is Sunday, February 1 before midnight.
The late due date is Monday, February 2 before midnight.

This assignment has two parts; a scanconversion component and an OpenGL assignment. Please remember to put your name, name of program, and compiling instructions at the top of your files. In addition, give instructions for running your program at the top of the file (for example, what the mouse controls do, what actions are associated with certain key presses, etc.) Finally, please comment each function (explaining briefly what it does and what its arguments are) and provide sufficient additional comments for the graders to understand the logic of the program.

  1. [60 points] In this part of the assignment you will implement Bresenham's Algorithm for scanconverting line segments with arbitrary slopes. Your program should include the dimensions.h file that you obtained for the last assignment. The dimensions specified in this file will correspond to the height and width of the framebuffer.

    Write a function called line that takes as input the (integer) coordinates of the two endpoints of a line segment (arguments x1, y1, x2, y2) and writes the pixels corresponding to the line segment from (x1, y1) to (x2, y2) into a ``framebuffer'' (just a 2-dimensional array) with the dimensions specified in dimensions.h. For example, if you set the parameters FRAME_BUFFER_WIDTH and FRAME_BUFFER_HEIGHT in dimensions.h to be 256, then you have a framebuffer with x- and y- coordinates ranging from 0 to 255. You should use simple scissoring to avoid plotting points that are not in the framebuffer. (As we pointed out in class, this isn't the most efficient algorithm, but it will suffice for now.) You may wish to make the framebuffer dimensions small for debugging purposes (say 8 by 8 or 16 by 16 - they must be powers of 2 though).

    (Note: Recall that in class we noted that in OpenGL, and in virtually all graphics systems, the graphics programmer plots points in their own specified coordinate system. These points are then translated into the framebuffer's coordinates. In this assignment you will be drawing line segments directly in the framebuffer's coordinates. Later, we'll add in the mechanism to convert between user-specified coordinates and framebuffer coordinates.)

    The "main" function of your program should take in the coordinates x1, y1, x2, y2 as command-line arguments (using argc and argv). The array should be printed to standard output row-by-row, beginning with the bottom row and scanning left-to-right. For each pixel in the framebuffer that is on, output three integers in the range 0 to 255 to indicate the rgb color of the pixel. (0 indicates the absence of that color and 255 indicates maximum intensity of that color. For example, the triplet 255 0 0 corresponds to bright red and the triplet 255 255 255 corresponds to bright white.)

    Make sure that your implementation works for all slopes and for special cases such as horizontal and vertical lines. Their should be no floating point arithmetic in the code.

    Notice that this assignment need not be completed on the SGI's. However, once the code is working, it will be nice to run on the SGI's and pipe the output to the display program (you'll need to recompile it each time you change the dimensions in dimensions.h) to see the drawing of the actual lines. Since your program will output the line segment to standard output and display takes its input from standard input, you can use the Unix pipe facility to make display render your lines on the screen.

    Place your working program in a file called hw1a.cc in your homework submission directory in /coursework/graphics. (The graders will determine the time of submission using the timestamp on the file.)

  2. [40 points] The OpenGL part of the assignment is an open-ended opportunity for you to play with OpenGL. First, read Chapter 1 from the OpenGL Programming Guide and Mark Kilgard's article on GLUT. Next, take a look at the programs test0.cc, test1.cc, test2.cc, and test3.cc in your notes and also available in the directory /cs/cs155/OpenGL/lecture2 on the SGI's and on turing. You are welcome to copy these programs and modify them to get a sense of how they work. Notice that OpenGL programs on turing requires the additional -lsocket "command" when compiling.

    Notice that test2.cc animates a spinning triangle but the animation is choppy. The reason is that the program is constantly erasing the framebuffer and drawing another rotated triangle. However, the video controller is taking the contents of the framebuffer and displaying it on your screen roughly 60 times per second. It's quite likely that the video controller will display the contents of the framebuffer in the middle of the scan conversion process, resulting in the display of something less than the entire triangle. This is why the animation looks bad. The program test3.cc ameliorates this problem by using a technique called double buffering. The SGI's have two framebuffers: the back buffer and the front buffer. All drawing is performed in the back buffer. The video controller only displays the front buffer. The glClear call in the first line of display clears the back buffer. The two polygons are then scanconverted into the back buffer. In the meantime, the display controller is displaying the front buffer. When the drawing is done, the call to glutSwapBuffers() swaps the two buffers so that the back buffer now becomes the front buffer and the front buffer now becomes the back buffer. In this way, the video controller is only displaying images that have been fully scanconverted.

    Whew! So here's the assignment. Write an OpenGL program that uses double-buffering to do animation. You can do anything you want, but make your program interactive so that it's behavior is altered in some way by mouse or keyboard input. Have fun with it!

    Place your working program in a file called hw1b.cc in your homework submission directory.

    Last modified January 20, 1998 by Ran Libeskind-Hadas