Department of Computer Science
Worcester Polytechnic Institute

CS-543: Computer Graphics
Project 2
Due: September 30, 2008 at 11:59pm

Objective: In this project, you learn about viewport mapping in OpenGL.

Preparation: You should be able to copy and modify your Makefile from Project 0 for this project.

Coding: The aim of this part is to get you comfortable applying window-to-viewport mapping in zooming and mouse selection routines.

Here goes:

  1. Step 1: Read more polyline files in GRS format

    The polyline file you drew in Project 0 was in GRS format, a homegrown format. Here are a few more GRS polyline files to work with:

    The basic structure of a GRS file is:

    • A number of comment lines, followed by a line starting with at least one asterisk: '*'.
    • The "extent" of the figure: (left, top, right, bottom).
    • The number of polylines in the figure.
    • The list of polylines: each starts with the number of points in the polyline, followed by the (x, y) pairs for each point.
    • Note that a lot of the GRS files start with comments. You should read them in and ignore them in your program.
    • The format for dino.dat is a little different in that it doesn't have the window dimensions (or comments) right at the top. Therefore, off the bat, a program which reads other GRS files without problems will have new problems with your old dino.dat file. You can either throw in a dummy extents window at the top of dino.dat or come up with a solution that works.

      Hint: A value of ( 0, 640, 0, 480 ) should work!!

    First make a copy of your Project 0 program(s) and compile the copy. Next, modify your code that previously read in dino.dat so that you can replace dino.dat filename with any of the above .dat files. The plan is that when you save and compile it, the new file is drawn on the screen, just like dino.dat. There are a few things you should note for your implementation:

    1. Be careful with how you pass parameters to the glViewport( ), glOrtho2D( ) calls (Practice exercise 3.2.1 on page 98 of Hill may prove useful). Make sure you understand how they work.
    2. The "extent" line of the GRS file will be used to set the world window i.e., passed to glOrtho2D( )
    3. If you look at the vertex coordinates of some of the GRS files, they are specified in floating point numbers, so you'll have to switch to float or double number formats. For instance, the x and y coordinates of the vertex are currently (in Project 0) being read in as integers and their values will be truncated badly if you don't change them to floating point numbers. Also, remember that glVertex2i( ) uses integers, so you'll need a different call to work with floats. Also, in general, using a glVertex3f( ) with the z value set to zero (see examples) can be used in place of glVertex2f( ) commands. i.e., glVertex3f( x, y, 0 ) usually works same as glVertex2f( x, y ).
  2. Step 2: Thumbnails + Bigger Picture


    Summary of Program Behavior
    The figure above shows the layout of a polyline viewer you should produce. On the left and right edges of your viewport, 10 thumbnails (8 new thumbnails + 2 occurrences of dino.dat) of the polyline files should be lined up. These 10 thumbnails should ALWAYS be visible at the left and right edges of the viewport whenever your program is running. The area in the middle of the thumbnails is the main drawing area in which all drawing will be done. On start up, choose one of the polyline files to draw in the main drawing area as a default. The thumbnails and main drawing automatically resize if the viewport is expanded or resized.

    Clicking on any thumbnails should make the corresponding polyline file the current drawing and draw a larger version of the polyline file to fill the main drawing area while maintaining aspect ratio. This is also known as the state 'p'. More details on the program behavior is given below.

    Your program should also have the following behavior and user (keyboard and mouse) interaction capabilities when you run it:

    Event: A key is pressed:

    • 'd' key (state d) Response: Draw three dots in the main drawing area (polyline thumbnails still at left and right edges).
    • 's' key (state s) Response: Draw the Sierpinski gasket in the main drawing area (polyline thumbnails still at left and right edges).
    • 'h' key: (state h) Response: Flip the polyline file drawn in the main drawing area along the HORIZONTAL drawing axis (polyline thumbnails still at left and right edges).
    • 'v' key: (state v) Response: Flip the polyline file drawn in the main drawing area along the VERTICAL drawing axis (polyline thumbnails still at left and right edges).
    • 't' key: (state t) Response: An 8x8 tiling of the "current polyline file" is drawn in the "main drawing space" (polyline thumbnails still at left and right edges).
    • esc: key: Response: The program terminates.

    Note: State 'p' is the initial state when your program starts.

    Here is a state-transition matrix to help clarify things:


    ...to one of these states, do what it says in the cell.
    p d s h v t
    To go
    from
    one of
    these
    states...
    p Click on a thumbnail Press 'd' Press 's' Press 'h' Press 'v' Press 't'
    d Click on a thumbnail Press 'd' Press 's'


    s Click on a thumbnail Press 'd' Press 's'


    h Click on a thumbnail Press 'd' Press 's'
    Press 'v'
    v Click on a thumbnail Press 'd' Press 's' Press 'h'

    t Click on a thumbnail Press 'd' Press 's'


    State transition summary:

    • From any state, clicking on a thumbnail takes you back to 'p'.
    • From any state, you can press 'd' or 's' to go into the corresponding state.
    • Flipping can only be accomplished from states 'p', 'h', and 'v'.
    • You can only enter 't' from 'p'.

    Make sure that reshape works for all states (p, d, s, h, v, t). i.e., if the user grabs the lower right corner of the window and increases or reduces the screen window size, whatever was drawn in it before is redrawn to the largest possible size, without distortion.


Documentation: You must create adequate documentation, both internal and external, along with your assignment. The best way to produce internal documentation is by including inline comments. The preferred way to do this is to write the comments as you code. Get in the habit of writing comments as you type in your code. A good rule of thumb is that all code that does something non-trivial should have comments describing what you are doing. This is as much for others who might have to maintain your code, as for you (imagine you have to go back and maintain code you have not looked at for six months -- this WILL happen to you in the future!).

I use these file and function (method) headers, in my code. Please adopt these (or the official CS ones) for all your assignments. The file header should be used for both ".h" and ".c" (or ".cpp") files.

Create external documentation for your program and submit it along with the project. The documentation does not have to be unnecessarily long, but should explain briefly what each part of your program does, and how your filenames tie in. Most importantly, tell me how to compile and run your program.


What to
Turn in:
Submit everything you need to compile and run your program (source files, data files, etc.)

BEFORE YOU SUBMIT YOUR ASSIGNMENT, put everything in one directory on ccc.wpi.edu, compile it, and make sure it runs. Then tar everything up into a single archive file.

The command to ZIP everything, assuming your code is in a directory "proj2", is:

zip FirstName_LastName_proj2.zip proj2

To submit your work, email me the resulting ZIP file.


General Hints: Here are a few more hints you might find useful:
  • You need to modify your keyboard( ) function in order to react to keyboard input from the user and your mouse( ) function to react to mouse input.
  • Think for a moment (okay, maybe a few moments), what you would consider the most difficult part of this project. How are you going to do that part?
  • The following sections of Hill may be useful in doing your work:
    • Section 2.4 of Hill (pp. 71-76) explains simple mouse and keyboard interaction using openGL.
    • Practice exercise 3.2.1 (pp. 98-100) is about using gluOrtho2D( ) and glViewport( ) to do window-to-viewport mapping using OpenGL
    • Example 3.2.4 of Hill, (pp. 100-101) tells you how to do tiling
  • For the reshape part, after a user changes your screen window (viewport) dimensions by dragging the lower right corner, simply call glViewport again using the new width and height, and then redraw using glutPostRedisplay( ).
Note: Don't blindly call glOrtho2D and glViewport without thinking about how they work.

Academic
Honesty:
Remember the policy on Academic Honesty: You may discuss the assignment with others, but you are to do your own work. The official WPI statement for Academic Honesty can be accessed HERE.


Back to course page.