Math 155A - Introduction to Computer Graphics – Winter 2018
Sam Buss,  Univ. of California, San Diego

Project #1 – Create two hexagonal prisms with smooth and flat colors.

Due date: Friday, January 19, 9:00pm.

Goals:  Gain some basic familiarity with triangle fans and triangle strips.  Learn how to make triangles of solid color as well as how to shade colors smoothly.  See how to use key controls to control viewpoint, and toggle wireframe and toggle culling of backfaces.

What to hand in:  When you are done, place your C++ files, executable, and Visual Studio solution together in a separate folder in your PC computer account in the APM basement labs. If you were able to create a suitable image file showing z-fighting (PNG or JPEG or BMP files are all OK), please also place this in the same folder. There should be nothing in this folder except your files for this homework assignment, and the creation/modification dates should match the turn in date.   The program must compile and run on these computers. 

Grading will be personalized and one-on-one with one of the TAs Jonathan Conder or Xiudi Tang or with Sam Buss.  Your program must run on the PC lab, you must come into the PC lab and meet one of us.  You will have to show your source code, run the program, make changes on the spot to your program and recompile as requested by the grader, and be able to explain how your program works and why it renders what it does.  The grading should be completed promptly, but no later than the due date for the next programming assignment.


1.      Download the HexPrismProj program from the zip file  Extract these into a directory named HexPrismProj (or something similar). Be sure to save this in your networked, permanent folder. Files left elsewhere on the computer lab computers are likely to be to erased automatically!  (The full URL for the zip file is:

2.      There is an executable "HexPrismDemo.exe" that shows (more-or-less) how your program should end up.  This is a 32-bit version and should run on the computer lab, and most other Windows machines.
Experiment with this program.  Notice the following items and keyboard commands.

a.      There are two hexagonal prisms shown in the scene.

b.      The arrow keys (left, right, up down) control the view position.  The scene moves only when you press an arrow key.  (No animation yet.)

c.      The "w" key toggles wire-frame mode.  

d.      The “c” key toggles whether back faces are culled. This makes a difference only in wireframe mode.

e.      The shape on the left is smooth shaded,.  The shape on the right is flat-shaded. 

f.       The colors on six sides of the hexagonal prism are fully saturated as red, yellow, green, cyan, blue and magenta.  The color on the top is a light gray (0.7, 0.7, 0.7) and the color on the base is a dark gray (0.3, 0.3, 0.3)

g.      The exact same numeric values are used for colors on the smooth shaded shape. 

3.      Find the source files “HexPrismProj.cpp”, “ShaderMgrSAM.cpp”, “ShaderMgr.h”,LinearR3.cpp”, “LinearR3.h”, “LinearR4.cpp”, “LinearR4.h”, and “MathMisc.h”, and use them to create an empty Visual C++ project.  Instructions for this are in Project #0 handout from last week. You must create a new empty C++ project, put all the .h and .cpp files in the same folder as the Solution .sln file, and add them to the project (Project … Add Existing File …”).

4.      If you get compile errors about missing the GLFW or GLEW header files or linkage errors about not being to find .lib files, it means they have not yet been added by ACMS to the system you are using. In this case, make sure you are compiling for x64 (not x86); for this see the dropdown item near the top of the screen. Then follow the directions in Project #0 to download the GLFW and GLEW files, adding them to the same folder as your solution (.sln) file.  This should not happen on any of the machines in APM B325 except possibly machine #12.  If it does happen, please let Professor Buss know.
It may be a week or so before the glew and glfw files are in place for the other computer lab rooms in the basement of APM.

5.      Examine the source code in HexPrismProj.cpp and run this program.  This program acts somewhat like the HexPrismDemo.exe.  However, it draws tetrahedra instead of the hexagonal prisms.   When you examine the source, do the following:

a.      Figure out how the vertices of the tetrahedron on specified with positions and colors in the routine mySetupGeometries().

b.      See how glBindVertexArray and glDrawArrays are used in myRenderScene to draw the tetrahedron.  The tetrahedron vertices are set so that the tetrahedron is centered at the origin.

c.      Understand how the loop in myRenderScene works. When i=0, it uses the shader program which does smooth shading. When i=1, it uses the shader program which does flat shading. For i=0, it shifts the tetrahedron left, down the negative x-axis.  For i=0, it shifts the tetrahedron right, along the positive x-axis.

d.      Examine the code to understand the OpenGL commands that control the wireframe mode turning off and on.  This is changed by the ‘w’ (wireframe) key, the routine key_callback using glPolygonMode.

e.      Examine the code to understand the OpenGL commands that turn culling of back faces on and off.  This is changed by the ‘c’ (cull) key, the routine key_callback using glEnable and glDisable.

f.       Recommended: See SimpleDrawModern’s code from Project #0 to see how to combine multiple geometries one scene. Or, see either SimpleDrawModern and SimpleAnimModern from the updated textbook website at for examples of how to use multiple geometries in one scene.

6.      Re-write the code HexPrismProj to draw hexagonal prisms instead of tetrahedra.  It is suggested to use two triangle fans and a triangle strip.  However, you do not need to build with the exact same geometry, as long as the appearance of the smoothed, non-wireframe, hexagonal prisms is the same.
For this Tetrahedron, rewrite the appropriate parts of mySetupGeometries() and myRenderScene(). You only need to create the vertex data for a single hexagonal prism in mySetupGeometries(). (That is, the same data generates both hexagonal prisms.) Use the following conventions.

a.      Suggested: In mySetupGeometries(), design the hexagonal prism to be centered at the origin, to have height two and radius one.

b.      Suggested: Let the hexagonal prism be formed from one triangle strip and two triangle fans.

c.      Required: Match the colors of the HexPrismDemo program, except you do not need exactly match the light and dark gray colors.  Use red, yellow, green, cyan, blue and magenta for the sides.

d.     If necessary: adjust the translations in myRenderScene() to appropriately position the hexagonal prisms.

e.      If necessary: change the scale factor from 0.5 in myRenderScene() so that the hexagonal prisms are the right size.

f.       Probably not necessary: You can also adjust the values of Xmin, Xmax, Ymin, Ymax, Zmin and Zmax on lines 95-97.  These values control what (x,y,z) points can be rendered without going outside the borders of the window or being too near or too far away from the viewer.

g.      Necessary: Change the value of NumObjects on line 60 (you probably want it to equal 3). Also, add index value for you geometric objects (to replace iTetra).  (Compare to how this is done in SimpleDrawAnim.)

7.      Be sure that all faces of the hexagonal prisms are facing in the correct outward direction.  Be careful about specifying vertices in the right order.  There should not be any holes in your tetrahedra.  It is OK to use glFrontFace(...)  if you wish to.

8.      Understand the difference between flat and smooth shading.  Be able to discuss the differences with the TA grading your program, and to explain how the shading is caused by the source code.

9.      Examine carefully the way the program works in wire frame mode.  Do you notice anything unusual as the hexagonal prisms are rotated in wire frame mode?  Are there any artifacts due to aliasing? (E.g., jagged lines, or crawling visual artifacts.)  Can you see any z-fighting?  For z-fighting, you will see isolated pixels that are the wrong color. (See item 10.)  These effects may be subtle.   If you have trouble seeing aliasing problems, try slowing down the motion by change the value of deltaTheta to a smaller value such as 0.005 or 0.002 instead of 0.01: then recompile and hold down arrow key and watch the edges of the hexagonal prisms. To see z-fighting, use only wire-frame mode and be sure that back faces are not culled.  It is the edges of the front face and back faces that are z-fighting with each other.
These phenomena may well be different on different machines!  There is a tendency for OpenGL implementations of lines and of wire-frame mode to have small bugs and this may be part of what you see.)  However, you may not be able to create z-fighting on a home computer!
Be ready to discuss what you see with your TA during grading. 

10.   If you are able to see any z-fighting: Make a screenshot showing the z-fighting and save the image as PNG or BMP file. z-fighting will be visible only in wire-frame mode, only if back faces are not culled, and only on the flat-shaded tetrahedron. z-fighting will show one of the edges having a few pixels replaced with a different color (due the fact that the same edge is drawn twice, with different colors). The best way to find the z-fighting is to tilt the image by rotating up or down, making the window large to enlarge the image, and slowing down the rate by replacing the value of the variable deltaAngle to a smaller value such as 0.005.   Then use the arrow keys to rotate, and watch carefully for pixels flashing different colors. The lab computers are usually able to show z-fighting, but most other computers (with different implementations of OpenGL) might not.
To form the screenshot of a window on a PC, hold down the “ALT” and the “RIGHT-SHIFT” buttons and press the “PRINT SCREEN” (PRT-SCN) buttons. Then open an image program such as Paint, paste the screenshot image in, and save to disk as a .bmp or .png file.  It is recommended to not use jpeg files or especially not to use a photo editor to view the file, as they will reduce the image quality.

11.   Turn in the project as described above.

Program grading: Scale of 0 to 10.  Personal grading session with a TA or the professor.  For grading, be ready to discuss any of the above topics, plus be prepared to make small modifications to the source code and recompile.