Exercises Week 1
EXERCISES FOR WEEK 1
week1-1

Using graph paper, pencil, and an X-Y coordinate system, draw a simple line design consisting of straight lines with at least 10 vertices (with integer coordinates). Draw a rectangular (not square) "frame" around the picture. This framed area will be the area you will display on the screen. Make note of the coordinates of the left, right, bottom, and top of the frame.

week1-2

Using the demo program hello1.cpp as a guide, write a program that will display only the points of your picture. Choose a window size of 300 (width) by 400 (height). Notice that you will use the coordinates for left, right, bottom, top of the frame as the first four parameters in the glOrtho( ) function. The last two parameters of the function will always be (-1.0, 1.0) as long as we are using only two dimensions (near, far) .

  • Change the size of the points.
  • Change the name of the window.
  • Change the size of the window:
    • Make it larger
    • Make it smaller
    • Make it square
    • Make the width much greater than the height
  • Change the position of the window.
  • Use different colors for the points.

Name the program week1_2.cpp

 

week1-3

Again, using the points you defined in exercise 1, change the GL_POINTS parameter to each of the following, in turn, and note the difference in the outcome.

  • GL_LINES,
  • GL_LINE_STRIP,
  • GL_LINE_LOOP,
  • GL_TRIANGLES,
  • GL_TRIANGLE_STRIP,
  • GL_TRIANGLE_FAN,
  • GL_QUADS,
  • GL_QUAD_STRIP

 

week1-4

Now choose the appropriate parameters to correctly draw the picture you created in exercise 1. Be sure to choose the width and height of the rectangular window you create to match the ratio of the width and height of the frame you drew around it. This ensures that your design looks like the drawing your made. Save the program as week1_4.cpp

 

week1-5

Change the previous program (week1_4.cpp) so that the left, right, bottom, and top parameters to the glOrtho( ) function is different from the coordinates of the frame you drew:

  • choose coordinates so that you zoom in on a portion of the design. Save the program as week1_5a.cpp
  • choose coordinates so that you zoom out from the design. Save the program as week1_5b.cpp

 

week1-6

Change your design (using program week1_4.cpp) so that you use floating point values for the points rather than integer values. What additional changes must be made to the program? Save the program as week1_6.cpp

week1-7

Copy the file /classes/cs3014/files/week1/birchtree.jpg to your directory. Use xv (or gimp) to display the image on the screen.

  • if using xv:
    • Using the right mouse button, bring up the menu for xv. Save the image using the format PPM(ascii). Now use xv to display the new file, making sure it looks like the original.
  • if using gimp:
    • Using "Save As", name the file "birchtree.ppm", and then select ASCII (instead of raw) as the file format.

Now open the .ppm file using a text editor.

  • Identify where it displays the number of columns and rows in the image.
  • Identify where it displays the maximum value for each of the 3 colors (RGB).

Now write a simple C++ program (not necessary to use OpenGL) that

  • opens the .ppm file,
  • ignores the lines in the header (number of cols and rows, max value for each color)
  • reads in the R, G, B values for each pixel,
  • writes them to another file, birchtree_GRB.ppm, so that red and green values are reversed.

Display the new image using xv (or gimp).

Try other changes, such as

  • only displaying one color value (making the other two color values for each pixel 0)
  • displaying only two color values for each pixel (making the other value 0)
  • assume you only have 16 shades of each color available instead of 256. Divide 256 into 8 equal sections, giving you only 8 values from 0 to 255 (inclusive); as you read in each color value, round to the nearest of your 8 values before writing to the new file.
week1-8

Write another fairly simple C++ program (not necessary to use OpenGL) that changes a .ppm file so that it has black stripes across it. That is, have your program determine the number of rows and columns in the image and, using that info, change some of the pixel values to all zeros, resulting in black pixels. You could start by making every other row black, but this will not be as dramatic as making several consecutive rows black. For example, the first 10 rows could remain unchanged, then the next 10 change to black, next 10 unchanged, etc.

 

week1-9

Copy the multiplewin.cpp file to your directory. Look it over carefully and make sure you understand what is going on in the program. Compile and run it.

  • Make a change to the program so that all 3 windows do exactly the same as window 1. That is, all 3 windows will have the same background color and draw the same four points; they will simply be labeled differently and appear in different locations on the screen. Save the program under the name multiplewin2.cpp; compile and run it.

  • Make an additional change to multiplewin2.cpp: change the frame size (NOT the window size) that is displayed in each of the three windows. Change it in such a way that
    • in Window#1 only the upper left (blue) point is displayed, approximately in the center of the window.
    • in Window#2 only the upper right (red) point is displayed, approximately in the center of the window.
    • in Window#3 only the lower right (green) point is displayed, approximately in the center of the window.

Note: This means you will need 3 different init functions and register the callback for each with the appropriate window. Previously we used the same init function for all 3.

 


week1-10

Change each of your programs so that the ESC key can be used to gracefully exit the program. Make sure you include this feature in all future programs you write.