CPSC 324, Spring 2004
Lab 2: OpenGL 2D
FOR THIS LAB, we turn to graphics programming with OpenGL and GLUT. For now, we will stick to two-dimensional drawing. There are two exercises for this lab. You will not come close to finishing them during the lab; try to spend part of the lab getting started on each program. Both exercises are due next Wednesday. Turn in printouts of your programs. You should also make a web page for this lab. The web page should contain screen shots of graphics produced by the programs that you write. You can make a screen shot using the "ksnapshot" program. You can run this from the command line or select Utilities/Desktop/KSnapshot from the KDE Start menu. Gimp can also be used to take screen shots, but it seems to be buggy in Gimp 1.3 -- you can use the older version of Gimp and choose the Screenshot command in the Acquire menu.
Exercise 1: The first exercise is to create either a still image or an animation using OpenGL. Start by copying one of the programs open_gl_2dstarter.cc (for a still image) or open_gl_2danimation_starter.cc (for an animation) from the directory /home/cs324 into your own directory. To avoid confusion, it's probably a good idea to rename the file.
Your drawing commands should go in the display() function, replacing the commands in the original program that draw the text and triangle. To organize your drawing, you might want to define some subroutines that you can call in display(). If you are doing an animation, remember that display() only draws one frame of the animation -- you can look at the value of frameCt to find out which frame you should draw. You could also define other global variables to use in the animation; these variables should be updated for the next frame in the timer() routine. As its set up, the program uses x and y coordinates that range from -1 to 1. If you want to use different coordinates, change the line "gluOrtho2D(-1,1,-1,1);" in the initTransformation() routine. It might be more convenient to use a larger range of coordinates, such as from 0 to 100.
Use the mesacomp command to compile your program. This command is defined on the cslab computers as an alias that calls the g++ compiler with the options that are necessary for compiling and linking OpenGL/GLUT programs.
When your program is complete, run it and take a screen shot. For an animation, take several screen shots at several different times. Post the screen shots on your web page. Turn in a printout of your program next Wednesday. After the assignment is due, you can post a link to your source code, if you want.
Grading will be based partially on ambition. Do not just make a simple modification of the sample program. Try to draw something interesting or attractive.
Exercise 2: The file /home/cs324/gl_draw.cc contains an OpenGL program that lets the user draw black lines by clicking and dragging on a drawing area. The program also shows a "menu" area, which contains the commands "Clear" and "Undo".
Copy gl_draw.cc into your own directory. For the second exercise of this lab, you should add features to this program. As a minimum:
- Make it possible to use different colors. Add items to the menu that the user can click to select the color. There are two possibilities: Add words or little colored rectangles to represent a small number of colors. Or have some way to specify the red, green, and blue components of the desired color. (We can discuss possibilities in class.) You will need a global variable to keep track of the user's currently selected color.
- Make it possible to draw filled rectangles and outlines of rectangles, in addition to drawing lines. Add items to the menu that let the user select the type of figure that will be drawn. You will need a global variable to keep track of the user's currently selected figure type.
For full credit, your program should give the user some visual feedback about which options are currently selected. For example, you could draw a white rectangle around the menu item that represents the currently selected figure.
You can also implement some other features for extra credit. For example: Wide and/or stippled lines. A Redo operation. Ovals and Filled ovals. Save-to-file and Load-from-file menu commands. Or to be really ambitious, the ability to select and edit figures that have already been created. Please discuss your ideas with me.
To accommodate the extra options, you will have to modify the struct ItemData data structure so that it holds the extra information (color, type of figure, line width). You will have to modify drawItem() to draw the correct figures. And there will have to be modifications in the three routines that respond when the user clicks and drags on the drawing area: handleStartDraw, handleContinueDraw, and handleFinishDraw.
To implement the new menu items, you can modify the drawMenu and handleMenuMouse commands.
For your web page, you should draw a picture using your program and take a screen shot of the window. Turn in a printout of your program next Wednesday. After the assignment is due, you can post a link to your source code, if you want.
Note that a compiled version of a completed program that meets the basic requirements can be found at /home/cs324/gl_draw_complete
David Eck, January 2004