Homework 2

22C:151 Introduction to Computer Graphics
Due Tuesday, September 16, 2008

(35 points) Write an OpenGL program that allows interactive creation and manipulation of filled squares, rectangles, polygons, and so on.


I will not give complete specifications for the program. It is up to you to design the details of the visual presentation and user interaction. However, the following functionality is required:


Shape creation

You should think carefully about the interaction involved in shape creation. One way to create a rectangle is to drag out a region, interpreting the position of the "left mouse down" event as one corner of the rectangle, and the position of the subsequent "mouse up" event as the diagonally opposite corner.

For polygons, you don't know ahead of time how many vertices there will be. A sequence of click and drag gestures can be used to define a polygon. Each "left mouse down" and subsequent "mouse up" event defines a new edge of the polygon, starting from the most recently created vertex. The first "mouse down", of course, will need to define the position of the first vertex.

In polygon creation mode, an additional requirement is that you must be able to "undo" the most recent vertex creation. For example, if you're in the middle of creating a polygon and have defined four points so far, you should be able (by a middle button click or other means) to remove the most recently created vertex, leaving you with three again (from which point you can add a new fourth vertex and so on).

For the "additional shape or feature of your own design" you may choose something such as circles (approximated as a regular polygon with a user controlled number of sides), regular n-gons (with user control of n), etc. Or, if not a new shape, add something like stippling or variable edge thickness.

Shape overlap and visibility

You need to maintain a sorted list of shapes so that they can be drawn in the right order (i.e. recently selected shapes should be "in front of" others, etc.). You might be tempted to try to use z values and 3D routines to take care of this, but we suggest you use and manipulate a sorted linked list instead and simply draw the shapes in the sorted order (back to front).

Sample solution and other helpful files

A sample executable (Linux and Mac) program demonstrating the kinds of things the program should do, as well as several pieces of code and notes that may be useful, are available here. A README file describes all the provided files.

GLUT Reference pages

The OpenGL Programming Guide is a bit skimpy on GLUT (the GL Utility Toolkit which allows you to do simple window management and mouse and keyboard interaction), especially on the GLUT menu-related functions. You will find the The OpenGL Utility Toolkit (GLUT) Programming Interface API Version 3 helpful; it is GLUT documentation written by Mark Kilgard, the author of GLUT (NOTE: it is not 100% up to date with the very newest versions of GLUT, but this shouldn't matter for the class).