Setting Up: OpenGL + GLUT + Windows + Visual Studio
I was recently helping a friend get set up running OpenGL and I realized that most of the instructions available online for this are fairly outdated. So, here’s a quick run through to set the record straight. This guide is meant for beginners who just want to get a simple OpenGL desktop app going on a Windows machine.
This guide assume you have the following installed:
- Windows 7 or 8
- Visual Studio 2008, 2010, or 2012
By convention, we assume the OS is installed to the C drive and VS is installed to a folder such as “C:\Program Files (x86)\Microsoft Visual Studio 10.0″ (for 2010, 2008 maps to 9.0 and 2012 to 11.0).
Installing GLUT
The first step is to download GLUT (the OpenGl Utility Toolkit). This library provides a slew of helper functions for working with OpenGL, including setting up a window. Grab the latest binaries from here. Unzip this and copy the binaries out into the following directories. Be aware that these are 32-bit binaries. You will need to create the “gl” folder in “\Microsoft Visual Studio 10.0\VC\include\”, as it doesn’t already exist.
File
|
Location
|
|---|---|
| glut32.dll | C:\Windows\SysWOW64\ |
| glut.h | C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\gl\ |
| glut32.lib | C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\ |
File
|
Location
|
|---|---|
| glut32.dll | C:\Windows\System32\ |
| glut.h | C:\Program Files\Microsoft Visual Studio 10.0\VC\include\gl\ |
| glut32.lib | C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\ |
Creating the VS project
Now boot up Visual Studio. You’ll want to create a simple Win32 console application. In VS 2010, open the New Project dialog and look at the list of templates. Select “Visual C++\Win32\Win32 Console Application”. Click through the Wizard that appears, leaving the defaults in place.
Linking the libraries
At this point you should get a solution with a couple of default files and a “_tmain” entry function. Right-click the project you created, go to Properties, then open Configuration Properties\Linker\Input and add the following to the “Additional Dependencies” field: “opengl32.lib;glut32.lib;glu32.lib”. Close the settings.
Writing the code
Modify your main .cpp file to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| #include "stdafx.h"#include <gl/glut.h>// One-time initializtion logicvoid init( void ){ printf( "OpenGL version: %s\n", (char*)glGetString(GL_VERSION)); printf( "OpenGL renderer: %s\n", (char*)glGetString(GL_RENDERER)); //Configure basic OpenGL settings glClearColor(0.0, 0.0, 0.0, 1.0); glShadeModel(GL_SMOOTH); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D);}// Main drawing routine. Called repeatedly by GLUT's main loopvoid display( void ){ //Clear the screen and set our initial view matrix glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //TODO: Perform drawing here //We just drew to the back buffer. Now we need to swap that with the //front buffer to show it on screen. glutSwapBuffers();}// Entry point - GLUT setup and initializationint main( int argc, char** argv ){ glutInit( &argc, argv ); glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (800, 600); glutInitWindowPosition (100, 100); glutCreateWindow( "OpenGL Test" ); glutDisplayFunc( display ); init(); glutMainLoop(); return 0;} |
Hit F5 to build and run. You should get a window with a black screen. Pat yourself on the back. You’re now ready to start coding with OpenGL.