Homework 2: Due Friday, September 12, 2003, 9AM (11/100 points)
Homework 2 Overview
The aim of this project is to get you comfortable with working in 2D using OpenGL calls, as well as applying window-to-viewport mapping in zooming and mouse selection routines.You will continue to extend miniGL, and structure things in such a way that the user has a choice of either choosing openGL routines or choosing your routines. Again, make sure you don't develop standalone applications, but continue to extend miniGL and modify it to perform the assigned tasks. Recall that running your program with the -openGL option should make miniGL call pure openGL calls while running the -cs4731GL option runs your own home-grown code. Note: At this point, you have no homegrown algorithm implementations. Here goes:
Preparation Step: Read more polyline files in GRS format:
The polyline file you drew in homework 1 was in GRS format, a homegrown format. Here are a few more GRS polyline files to work with:
birdhead.dat
dragon.dat
house.dat
knight.dat
rex.dat
scene.dat
usa.dat
vinci.dat
The basic structure of the GRS files are:
- 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.
So, first create a HW2 folder, copy your HW1 files over and compile them. Then give your HW1 files the equivalent HW2 names. 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.
- Be careful with how you pass parameters to the glViewport( ), glOrtho2D( ) calls (Practice exercise 3.2.1 on page 86 of Hill may prove useful). Make sure you understand how they work.
- The "extent" line of the GRS file will be used to set the world window i.e. passed to glOrtho2D( )
- 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 hw1) 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).
- Note that a lot of the GRS files start with comments. You can either manually cut those out or read them in and dump 'em 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:of (0,640,0,480) should work!!- Important note: At this point, you should be comfortable working with and extending miniGL. OpenGL has 100s of commands and only the ones which were anticipated that you will need have been implemented. If in your program design or work, you would like to use a command which is not supported yet, you should feel comfortable reading the miniGL sources and extending it. To add a new miniGL command, you simply have to edit the following files:
minigl.h openGL.h openGL.cpp cs4731GL.h cs4731GL.cppIt is easy to follow how other commands were included.
Summary of Your program behavior
Your program should have the following behavior and user (keyboard and mouse) interaction capabilities when you run it using the -openGL option of miniGL:
Event: A key is pressed:In summary, hitting the 'p' key puts you in the p state which does polyline drawings as above and hitting the 'm' key puts you in the m state which draws the mandelbrot set (explained below).
- 'p' key: (state p) Response: the program creates a 5x5 non-distorted tiling of the 9 polyline files provided (8 new ones above + dino.dat). The position of polyline files is completely random such that repeatedly hitting the 'p' key produces new arrangements of 5x5 tiles. In this state, if a user clicks on any of the 5x5 tiles, the polyline file in that tile becomes the current drawing. The screen is erased and the polyline in that tile is redrawn to fill the entire screen. Note: State p is the default state so that once your program starts up, it goes into state p. i.e. draws 5x5 tiles above.
- 'm' key: (state m) Response: Draw the Mandelbrot set. (Explained below)
- esc: Response: The program terminates.
(state m) Mandelbrot set drawing and zooming: You will implement the mandelbrot set fractal with zooming capabilities. Write an application that draws a portion of the Mandelbrot set. Initially the entire Mandelbrot set is shown as an array of colored pixels (a raster), using the default window (with opposite corners: -1.5 + j1.2 and 0.5 - j1.2). The user may then designate an aligned rectangle R, by clicking two times inside the drawing area. Your program responds to the selection of R by redrawing the portion of the mandelbrot set which lies inside the selected rectangle R, to fill the entire drawing area. There must be no aspect ratio distortion. (If the user only clicks once before doing some other action such as pressing a key or clicking outside the drawing area, the first click is forgotten.) Choose colors for pixels that produce exquisite pictures, as discussed in class. For instance, points inside the Mandelbrot set are black, those just outside are a brilliant yellow, and for points further outside the color changes gracefully towards a blue or green.
Choose a suitable number of iterations in the dwell( ) calculation. e.g. 100. The number of iterations chosen will influence the picture you end up with, so experiment and choose a suitable number of iterations. You can then hardcode this number after a few experiments to decide works well for you.
Extra Credit (Worth 10 points): Simply make sure that reshape works for both the m and p states and also make sure. 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 fill the new window
Submission: Submit all your executable, miniGL, source, Make and source files making sure that the examples (bounce, gears, etc) are still working. Create documentation for your programs and submit them along with the projects. The documentation does not have to be unnecessarily long. Simply explain briefly what each example does and tie in your filenames. Most importantly, tell the TA how to run your program. Put everything in one directory on ccc.wpi.edu, compile it and make sure it runs. It is preferred that your code compiles use ccc.wpi.edu. If you worked previously on cpu.wpi.edu, port your code accordingly, then tar everything up. Again, the command to tar everything is:
tar cvf FirstName_LastName_hw2.tar *
General Hints
Here are a more few hints you might find useful:
- Assuming you've derived a HW2 class (your actual class names may be different), you need to modify your hw02::keyboard( ) in order to react to keyboard input from the user and your hw02::mouse( ) to react to mouse input.
- The following sections of Hill may be useful in doing your work:
- Section 2.4 of Hill (pp. 63 - 67, 5 pages) explains simple mouse and keyboard interaction using openGL.
- Practice exercise 3.2.1 (pp. 86-87, 2 pages) is about using gluOrtho2D( ) and glViewport( ) to do window-to-viewport mapping using OpenGL
- Example 3.2.4 of Hill, (pp. 88-89, 2 pages) tells you how to do tiling
- Section 3.2.2 (pp. 92 - 95, 4 pages) on how to set Window-to-Viewport mappings, while preserving aspect ratios i.e. no distortion.
- Section 9.6 (pp. 504-513, 9 pages) explains how to do the Mandelbrot set
- Read example 2.4.2, figure 2.38 (pp 64, 0.5 page), to understand how to collect mouse points two different sets of mouse points. Since this routine is actually for drawing the selected screen rectangle, you will probably need to modify this
- For the extra credit 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.