WPI Computer Science Department

Computer Science Department
------------------------------------------

CS 4731 Homework 1 D term 2014
Due by Thursday, March 27, 2014 at 11.59PM (12.5/100 points)


Homework 1: Mipmap polylines & Barnsley Fern

Homework 1 Overview

The aim of this project is to get you comfortable with working in 2D using OpenGL 4.X. You will set viewports, world windows, draw a mipmap polyline pattern, render the Barnsley Fern fractal and explore keyboard and mouse interaction routines. Here goes:

Step 1: Read more polyline files in GRS format:

Use the following starter code that has been tested and works in the zoolab. This ensures that your code runs on the zoolab machines right away. You can get the HW1 starter code [ HERE ] The following polyline files are in the GRS format:

dino.dat
birdhead.dat
dragon.dat
house.dat
knight.dat
rex.dat
scene.dat
usa.dat
vinci.dat

The structure of GRS files is:


First, modify the starter code to read in one of the GRS files. The plan is that when you re-compile it, the polyline file is drawn on the screen. There are a few things you should note for your implementation.
  1. Be careful with how you pass parameters to the glViewport( ), Ortho2D( ) commands. 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 Ortho2D( )
  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 use float or double formats as appropriate. For instance, the x and y coordinates of the vertices of dino.dat are integers, while the vertices in the other GRS files are floating point numbers.
  4. Note that a lot of the GRS files start with comments. You can either manually cut those out or read them in and dump them in your program.
  5. 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 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 world window of (0,640,0,480) should work!!
Step 2: Draw the mipmap polyline pattern: Modify the program to draw the polylines in the format shown in the figure below.

Iteration 1 diagram Iteration 2 diagram Iteration 3 diagram
Divide the screen into 4 quadrants. Randomly pick any of the polyline files. In the top-left quadrant, draw the randomly chosen polyline in red. In the top-right quadrant, draw the same randomly chosen polyline in green. In the bottom-left quadrant, draw the same randomly chosen polyline in blue. If your goal was to end at iteration 1, in quadrant 4 (bottom right), redraw the same polyline in red. If not, draw the entire pattern in the 4th quadrant (iteration 2). Repeat the pattern for iteration 3. Whenever you reach the last iteration, draw the polyline selected for that level in red in the 4th quadrant.

At every iteration level, the polyline drawn is picked randomly from the 9 given polylines so that a different polyline file is drawn for each new iteration level. For instance, polyline 1 may be randomly picked and drawn in all 3 quadrants of iteration 1, polyline 2 may be randomly picked and drawn in all 3 quadrants of iteration 2 and so on. Set up the entire thing as a recursion so that you can go up to 6 iterations deep. If you are unable to set up recursion and go till 6 iterations, just calculate values and hardcode 3 iterations. You will lose some points for not being able to set up the recursion.

Finally, after you divide up the screen, the aspect ratio of the mipmap cells may not match the aspect ratio of the polyline file you want to draw. Make sure the polyline file is not squashed. To do so, maintain aspect ratio within each mipmap cell. i.e. draw the polyline file as large as you can within the cell while maintaining aspect ratio.

Step 3. Render the Fern: As practice type in code to draw the Sierpinski gasket. Once that's working, modify the Sierpinski gasket code so that when you run your program, instead of the sierpinski gasket, the fractal called the Fern is drawn. A description of the Fern now follows. Fern diagram
{Ref: Peitgen: Science of Fractals, p.221 ff}
{Barnsley & Sloan, "A Better way to Compress Images"
 BYTE, Jan 1988, p.215}

  {The four affine transformations:}
  {taken from Barnsley & Sloan p.217}
   a[1]:= 0.0; b[1] := 0.0; c[1] := 0.0; d[1] := 0.16;
  tx[1] := 0.0; ty[1] := 0.0;

  a[2]:= 0.2; b[2] := 0.23; c[2] :=-0.26; d[2] := 0.22;
  tx[2] := 0.0; ty[2] := 1.6;

  a[3]:= -0.15; b[3] := 0.26; c[3] := 0.28; d[3] := 0.24;
  tx[3] := 0.0; ty[3] := 0.44;

  a[4]:= 0.85; b[4] := -0.04; c[4] := 0.04; d[4] := 0.85;
  tx[4] := 0.0; ty[4] := 1.6;

--------------------------------------

Choose index randomly to be:
        1 with probability .01
        2 with prob. .07
        3 with prob. .07
        4 with prob. .85

Each new point (new.x,new.y) is formed from the prior point
(old.x,old.y) using the rule:

    new.x := a[index] * old.x + c[index] * old.y + tx[index];
    new.y := b[index] * old.x + d[index] * old.y + ty[index];

and a dot is drawn at point new. The initial point is  old = (0.0, 0.0).

Summary of Your program behavior

After the practice, now re-organize your program to have the following behavior. Control the drawing of the patterns above using keyboard controls. When the program starts as default, the Fern is drawn. Your program should then respond to the following key strokes
  • key m: Clear screen and draw the mipmap polyline pattern (iteration 1).
  • key p: Clear screen and draw the mipmap polyline pattern one iteration more (i.e. if you were at iteration 1, redraw the polyline at iteration 2)
  • key l: Clear screen and draw the mipmap polyline pattern one iteration less (i.e. if you were at iteration 2, redraw the polyline at iteration 1)
  • Key f: Clear screen and draw the Fern
Notes: No OpenGL fixed function commands (glBegin, glVertex, etc) or immediate mode drawing commands should be used in your program. All drawing should be done using shaders, retained mode and Vertex Buffer Objects, similar to the examples in your textbook.


General Hints

Here are a more few hints you might find useful:
  • In order for your program to respond to keyboard strokes, you will need to write and register a keyboard callback function.

Submitting Your Work

Make sure to double-check that everything works before submitting. Submit all your executable and source files. Put all your work files (Visual Studio solution, OpenGL program, shaders, executable and input files into a folder and zip it. Essentially, after your project is complete, just zip the project directory created by Visual Studio. Submit your zip file using web-based turnin. Do not email me your program or submit it via dropbox

Create documentation for your program and submit it along with the project inside the zip file. Your documentation can be either a pure ASCII text or Microsoft Word file. The documentation does not have to be long. Briefly describe the structure of your program, what each file turned in contains. Explain briefly what each module does and tie in your filenames. Most importantly, give clear instructions on how to compile and run your program. MAKE SURE IT RUNS IN THE ZOOLAB before submission. Name your zip file according to the convention FirstName_lastName_hw1.zip


[Feedback] [Search Our Web] [Help & Index]

[Return to the WPI Homepage] [Return to CS Homepage]

mailto:emmanuel@cs.wpi.edu