CIS Logo SVC Logo

   Computing & Information Systems


Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++

Graphics Programming

The programs in this section and most of the explanation on this web page were written by student Daniel Lioi. Some editing of the material was done by Br. David Carlson.

The purpose of this page is to introduce you to 3D graphics in C++. A short section of background information is followed by a series of example graphics programs. Most are written using Direct X 8, though one is written using Open GL. For more information on Direct X, consult the Microsoft Direct X Home Page. For more information on Open GL, go to The examples were compiled and tested on Windows 2000 PCs running Microsoft Visual C++ 6.0 and Visual C++ .NET. You might need to make some changes if your setup is different.

The Direct X examples use Direct3D8. To use Direct3D8 you will need to have the DirectX8 SDK installed. Just download the SDK from Microsoft and run the install program. (Note that version 8 is still available, though version 9 is the newest as of this writing. We have not tested these programs with version 9, so you might want to stick to version 8.) Note that you must make a new, empty Windows application (project) in which to place the code for each example. See the Simple Graphics section of these web pages for brief directions on how to do this under both the 6.0 and .NET version of Visual C++. It is recommended that you create new .cpp and .h files under each project and paste in the desired code using the clipboard (edit, copy and edit, paste), since saving a code file from within a browser can lead to some unwanted changes to the file. Saving the .bmp files from within your browser should work fine, however.

For the Direct X examples you will also need to add the proper .lib files to your project. You can't go wrong by including d3dx8.lib and d3d8.lib, though in some cases you only need one or the other. To add these .lib files in Visual C++ 6.0, go to Project, Settings, and then to the Link tab. Make sure that you are changing the settings for all configurations. Then type the names of the above libraries into the Object/library modules box (with spaces between the library names). In Visual C++ .NET, you add these .lib files by right clicking your project in the Solution Explorer, clicking on Properties, making sure that the Configuration pull-down is set to "All Configurations", opening the Linker folder, clicking on Input, and typing the names of the above libraries into the Additional Dependencies box. Of course, in your program code you also need to include the associated header files. However, these conveniently have the same names (except for the extensions). The example programs already have the proper header files included, so you don't have to worry about this unless you create your own examples.

For the OpenGL example, do not add the libraries mentioned above. Instead, add the libraries glu32.lib, glaux.lib, and Opengl32.lib. Windows 2000 systems and later should have these and the associated header files already installed, so that all you have to do is to type these library names into the linker box mentioned in the preceding paragraph.


Everyone seems to be amazed by 3D Graphics. To understand how this works you'll need a small piece of paper or cardboard. Cut the piece of paper into a triangle. Hold up your triangle and look at it with one eye closed. Now rotate it around in different directions. You'll notice that no matter what angle you look at it from, its outline or silhouette is still a triangle, though shaped differently. To make these different triangles involves some trigonometry, which we can handle in software or hardware.

Now if you take many triangles and put them together side to side and vertex to vertex, you can create an approximate representation of almost anything: a cube, a person, a car, or anything else you want to make. Then if you want to rotate or move this complex object, all that has to be done is to rotate or move all of those triangles.

Next we might want to add color or texture. Without these we would have a white blur on our screen with an outline that looks like our overall object (perhaps a car or cube). This is far from looking 3-dimensional. By adding a color to each triangle or vertex between triangles we create a shading pattern that makes triangles stand out against each other. We can also add textures to make an object look more like what it really is, such as a crate or television.

The next step in making something look realistic is shading it. The next time that you are outside at night look around where there are lights. Notice how the ground and objects around it are brighter near the object and darker further away. Lighting helps to create realism. In a program, lighting is handled by vertex shading. There is a lot of complex mathematics involved in the procedure for lighting, but can simplify the idea of it this way: The location of a vertex (or point) relative to the light determines its shading value, which indicates its degree of darkness. Imagine for example a cube with a light shining on one side of it. The side toward the light will be brighter, while the sides away from the light will be darker.

In some cases we might include more detail in a 3D graphics image. For example, we might want a shiny spot on a glass surface, or a surface that we can see through, shadows, and so on. These are all features that are used to create realism. The amount of realism that you need depends on what you are doing. If you are creating a game you want as much realism as you can get without slowing down the game. If you are creating a program to create a computer-generated picture or movie you'll want extremely high realism. In other cases a high degree of realism may not be needed.

Note that vertices have x, y, and z values (coordinates). The coordinates of a vertex specify the location of the vertex. Vertices can also have color, and if they are using a texture they will have texture coordinates. Texture coordinates store color values for each point (u, v) in a triangle or other polygon. These u and v numbers are usually in the range of 0 to 1. They aren't actually pixel measurements. Instead, think of u or v as a percent distance.

When a vertex is going to be lit it also needs a normal vector to the surface at that point. A normal vector is a vector of length one that points directly away from the surface. On a flat surface this is easy to understand: the normal vector is simply perpendicular to the surface. On a curved surface this is a bit more complicated. The normal vector affects how the vertex will be shaded due to lighting.

Example Graphics Programs

The above probably gives enough background on 3D graphics to get you started. The next step is to look at some of the sample programs. Try reading through these program and the comments contained in them. You might also want to consult a good book on 3D graphics. In running the examples, use Alt F4 to end each program. Please note that some of the files that have the same names in the examples below are not identical. Be sure to use the version of each file that is listed for the particular example that you are working on.


Related Item

Simple Graphics (Introductory Topic)

Back to the main page for Software Design Using C++

Author: Daniel Lioi
Last updated: August 27, 2009