Using OpenGL & GLUT in Visual Studio .NET 2003

A Guide to Easier Graphics Programming — By Jordan Bradford

Visual Studio .NET is a fantastic IDE, and with it you can write programs that will run in both Windows and Linux with no fuss. This guide will show you how to set up a Visual Studio project that will do just that, provided it is a pure OpenGL application using GLUT. 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: 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
glut32.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.
  2. Visual Studio can also recognize the forward slash as a directory designation in #include directives — Windows normally uses a backslash — so you shouldn't rewrite your code with one:

    #include <GL\glut.h>

    If you use a backslash in Linux, gcc will complain.

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. 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 given code to the Additional Dependencies text box in the right pane. This tells Visual Studio where to find GLUT. (Copy & Paste: opengl32.lib glu32.lib glut32.lib )
    Add libraries
  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 given code to the Additional Options: text box.
    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 fools Visual Studio into thinking 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.

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. Your computer at home is most likely superior to the ones in the lab, so inefficient code might not be apparent.


Last Update: April 10, 2004

Valid XHTML 1.1!