teaching machines

Visual Studio, CMake, and OpenGL

January 3, 2013 by . Filed under cs455, spring 2013.

I am not familiar with the Windows development environment (and I don’t feel like I need to be). For this reason, I do not enjoy developing software on Windows. However, Windows is what we have available to us in Phillips 115. In this post I will describe how I get around having to know Windows but still allow you to get my OpenGL starter code up and running in this lab.

You may want to set up your personal machines too. Be warned, however. I do not have the time resources or the know-how to support setting up your own machine for graphics development. The graphics cards in Phillips 115 are decent. Yours may not be.

The code I share with you in lectures will be developed on Linux. So that you may take this exact same code and build it on Windows, I am using a tool called CMake. This is a cross-platform build system. We create one file named CMakeLists.txt listing our desired executables and the source code and libraries our executables depend on. Based on that file, CMake will generate makefiles, or a Visual Studio project, or an XCode project, and so on.

Machine setup

This is the process LTS used to set up the lab machines:

1. Install the latest OpenGL drivers from your graphics card manufacturer.
2. Install CMake 2.8 or newer.
3. Install GLUT. There are three files that need to be in certain places in order for CMake to find them:
• glut32.dll goes in C:\WINDOWS\system32
• glut.h goes in C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\gl
• glut32.lib goes in C:\Program Files\Microsoft SDKs\Windows\v7.0A\lib
4. Install GLEW. Distribute the files similarly to GLUT. There are multiple header files.

Building a Visual Studio project

1. Grab the skeleton *.zip I have placed in the course directory on the W: drive.
2. Unzip it some place where it won’t get wiped. That means no C: drive and no Desktop.
3. Run CMake. Choose the unzipped directory as the source directory. For the build directory, I suggested you pick a sibling directory to the source directory.
4. Click Configure. Target Visual Studio 10 in the dialog that pops up. Pray for no errors. (If you missed this, go to File / Delete Cache and configure again.)
5. Click Generate. Open the *.sln file in the build directory.

Running a project

1. One Visual Studio solution holds many projects. There is a project for each executable. Right-click on the executable you want to run and choose Set as Startup Project.
2. Run the executable.
3. Standard output goes to a DOS console—not to Visual Studio. In debug mode, the window automatically closes when your executable finishes. This makes viewing output difficult. If you want to keep the window open until a keypress, run the executable in release mode with Control-F5. Or set a breakpoint.

If you want your project to stay decoupled from Visual Studio, do not change your project configuration through Visual Studio. Rather, add files through the source directory that you pointed CMake to, and edit CMakeLists.txt accordingly. (Feel free to edit the contents of files already in your project through Visual Studio. Just don’t add new ones.)

To add shader code, put the shader files in the shaders directory in the source directory. Whenever you need a path to one of these shaders in your C++ code, use something like SHADERS_DIR "/bulbous.vert.glsl". In CMakeLists.txt, I’ve defined a preprocessor macro named SHADERS_DIR that evaluates to the shaders directory in your project source.

add_executable(myexe source1.cpp source2.cpp ${COMMON}) target_link_libraries(myexe${GLUT_LIBRARIES} ${GLEW_LIBRARY}${OPENGL_LIBRARIES})
cmake ../path/to/source/dir/which/contains/CMakeLists.txt
make