Using OpenGL & GLUT in Visual Studio .NET 2003

A Guide to Easier Graphics Programming — By

This guide will show you how to set up a Visual Studio OpenGL/GLUT project that will compile in both Windows and Linux, as long as it is a pure OpenGL program. This guide also assumes Visual Studio .NET 2003 (and/or 2002) is already installed and you are familiar with its use.

Contents

Click here for the Quick Version of this guide (if you simply need to refresh your memory).

Step 1: GLUT Installation (only needs to be done once)   back to top ↑

Windows comes with OpenGL, and Visual Studio comes with the OpenGL libraries, but neither of them comes with GLUT. Get the newest version of GLUT here: GLUT 3.7.6 for Windows.

Put the following files in the following locations:

File Location
glut32.dll Windows XP | Server 2003: C:\WINDOWS\system\
Windows 2000: C:\WINNT\system\
glut32.lib C:\Program Files\Microsoft Visual Studio NET 2003\Vc7\PlatformSDK\Lib
glut.h C:\Program Files\Microsoft Visual Studio NET 2003\Vc7\PlatformSDK\Include\gl

Note:
If you plan on giving your program to friends to run using Windows, you must also include the glut32.dll file. If they don't have this file in the same directory as your application or in their C:\WINDOWS\system folder, the program will not run.

Step 2: Create a Visual Studio Project   back to top ↑

Because GLUT was designed to be window system independent — it makes its own windows — it is better to let GLUT run as a console application than as a native Windows application (which would require #include <windows.h>). To create an empty console project in Visual Studio, do the following:

  1. Create a new project ( File → New → Project... ). The New Project dialog will appear.
    Create a new project
  2. In the Project Types: pane, select Visual C++ Projects. Then select Win32 Console Project in the Templates: pane. Name your project and click OK. The Win32 Application Wizard dialog will appear.
    Create a Win32 Console Project
  3. Click the Application Settings tab on the left, and check the Empty Project box. Then click Finish.
    Create an empty Win32 Console Project
    Your empty console project will be created.

Step 3: Add Source Code   back to top ↑

Adding source files to the project should be familiar to you, so a detailed explanation is not necessary. There are two facts you should know, however.

  1. When you include GLUT in a program, it automatically includes the rest of the OpenGL header files. So explicitly having

    #include <GL/gl.h>
    #include <GL/glu.h>

    in either Linux or Windows isn't necessary, provided you include GLUT.

    In short, you only need to do this:

    #include <GL/glut.h>
  2. Do not use a backslash in the preprocessor #include.

    #include <GL\glut.h>No!

    Visual Studio will still compile your program if you do this, but gcc/g++ in Linux will not.

Step 4: Modify Project Properties   back to top ↑

Before compiling your project, you need to set up Visual Studio's linker so it knows where to find GLUT. To do this, you must open the Property Pages dialog for your project. There are two ways to do this:

  1. Use Visual Studio's menu option ( Project → Properties ).
    Use menu: Project -> Properties
  2. OR: Use the Solution Explorer located in the upper right corner. Right-click your project's name as shown and select Properties.
    Use Solution Explorer

Using either option, the Property Pages dialog will open. Once it appears, do the following:

  1. From the Configuration: list box, select All Configurations.
    Select All Configurations
  2. In the left pane, select the Linker subtree and then the Input option. Add the following code to the Additional Dependencies text box in the right pane.

    Copy & Paste: opengl32.lib glu32.lib glut32.lib
    Add libraries
    Now Visual Studio knows where to find GLUT.
  3. This step is optional. If you want to prevent your program from opening a console window in addition to your GLUT window when it is run, select the Command Line option in the left pane. Then add the following code to the Additional Options: text box.

    Copy & Paste: /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup
    Disable console window
    Visual Studio thinks it's still building a normal console application, which is why a console window will appear if you run your program from inside Visual Studio or by double-clicking its icon. The /SUBSYSTEM:WINDOWS command tells Visual Studio this is a windowed application, which means a console window isn't necessary. However, when Visual Studio makes windowed applications, it wants to start program execution from WinMain() or wWinMain(), which you haven't defined. Setting the /ENTRY:mainCRTStartup flag tells Visual Studio to start execution at the usual main() instead.

Note:
If you choose to disable the console window, remember that you won't be able to see any console output from your program using printf(), cout, cerr, etc. So, if you plan on having users run your program from a DOS prompt and console output needs to be seen, do not disable the console window.

Step 5: Enjoy Visual Studio   back to top ↑

Now your program is ready for development in an excellent IDE while still being portable.

Be sure to test your program on a Linux machine before submitting it for grading.

Step 6: Distributing Your Program   back to top ↑

If you're going to share your program with people for fame and glory, build a Release version so the executable isn't gigantic. More importantly, remember to include the glut32.dll file with your application, because most users won't have it in their WINDOWS/system directory.


Last Update: March 22, 2006

Valid XHTML 1.1!