I just want to make a small note about what GLUT, SDL and OpenGL actually are before I discuss setting them up. (This is information I have come up with myslef from researching about the internet and may not be 100% accurate. Wikipedia will probably give much more accurate and correct information about OpenGL, SDL and GLUT. Also be sure to check out the official sites in the references at the bottom of the page)
OpenGL – OpenGL is a Graphical API for rendering 2D and 3D graphics. At the time of writing its current release is 4.0.2. There are also exists versions of OpenGL for embedded systems such as mobile devices. This is called OpenGL ES. The Sony Playstation 3 and Playstation Portable both support OpenGL ES v1.3. Apple’s iPhone/ iPad/ iPod Touch all support both OpenGL ES 1.3 and 2.0.
GLUT – GLUT stands for OpenGL Utility Toolkit. OpenGL doesn’t natively support setting up a window or taking in user input. GLUT is made up of a number of libraries that will setup a window that an OpenGL Scene can be rendered to and can take input from the keyboard and mouse. It also provided code to draw some basic 3D shapes such as Cones, Cube and a Teapot. GLUT is no longer maintained. Due to license restrictions, GLUT had to be re-implemented to make it Open Source. Two implementations are FreeGLUT and OpenGLUT. GLUT is mostly outdated and is currently mainly used in education for teaching OpenGL. There are more modern libraries that provide support for more methods of input into an OpenGL application as well as other functionality that benefit OpenGL – SDL is one such library.
SDL – SDL stands for Simple DirectMedia Layer. It can be used to render 2D scenes and access user input from keyboard, mouse and game pad, basic audio playback, loading a number if image formats for use as textures as well as some other functionality. SDL windows can be used to render 3D scenes drawn in OpenGL and thus all features provided by SDL can be used in OpenGL applications.
The Visual Studio Project that I provide (link available at bottom of page) should produce something similar to the screenshot below when compiled and run successfully.

The spheres are being drawn using glutSolidSphere(…) and glutWireSphere(…) from the GLUT library.
The arrow keys rotate the spheres and pressing Esc will exit the program. Handling input and the Windowing system are being done using SDL.
The texture is being loaded as an SDL_Surface and is then converted to GLuint for use as a texture. Texture was taken from http://www.bestgameever.com/images/Skysphere_Black.png.
Lighting has also been enabled.
Setting Up Visual Studio and your System Environment
Before continuing, it is important I mention that you are following this tutorial at your own risk. I cannot be held liable in the unlikley event that you damage your machine or system setup.
I am assuming that you already have a copy of Visual Studio 2008 downloaded and installed on your system. As long as your college or university is registered, students can obtain a free copy of Visual Studio 2008 Professional from Microsoft's Dreamspark website - www.dreamspark.com.
Step 1 – Download SDL and GLUT files
You will need to have the necessary SDL and GLUT files downloaded. I have put together the files that I used when setting up my environment and have included them in THIS zip file. Using this zip file all you will have to do is download and extract it and then do the following:
- Copy the contents of the lib files folder to C:\Program Files\Microsoft Visual Studio 9.0\VC\lib
- Copy the contents of the include files folder to C:\Program Files\Microsoft Visual Studio 9.0\VC\include
- Copy the contents of the dll files folder to C:\Windows\System32
(I cannot confirm this but have read that if you are using Vista 64 bit copy the contents of dll files to C:\Windows\SysWOW64 instead of the directory above)
There are other tutorials that ask you to add new Include and Library paths for a custom location where you may choose to store files above on your system. This is accessed and setup through Tools->Options and then selecting Projects and Solutions->VC++ Directories and then adding the directory locations under Include and Library. I personally prefer just adding the needed files to VS's default directory as it makes things easier for me when moving between machines.
The versions of SDL that I have included in my zip file are SDL v1.1.14, SDL_Image v1.2, SDL_Mixer v1.2, SDL_ttf v2.0. These are the latest versions available as of 21/06/2010.
Alternatively you can download the latest versions of SDL from the following sites should you find that my ZIP files are out of date:
SDL - http://www.libsdl.org/download-1.2.php
SDL_Image - http://www.libsdl.org/projects/SDL_image/
SDL_Mixer - http://www.libsdl.org/projects/SDL_mixer/
SDL_TTF - http://www.libsdl.org/projects/SDL_ttf/
Other SDL Extensions are available at http://www.libsdl.org/projects/
GLUT is no longer maintained. The version I include in the download is GLUT v3.7.6. I downloaded it from Nate Robins’ website http://www.xmission.com/~nate/glut.html. Nate Robins also has a useful program (Nate Robins Tutors) to demonstrate various features of OpenGL. It is available at http://www.xmission.com/~nate/tutors.html.
Step 2 – Setting Up a Visual Studio Project
Having setup your Desktop Environment, you then must setup your Visual Studio Project. Step 1 only needs to be done once. This step will need to be done for every Visual Studio Project you create.
With Visual Studio open Create a new Project
File->New->Project

In the New Project window select Win32 Console Application
This will display the following Window

Click Next to go to Application Settings and make sure that you put a tick in Empty Project.

Now click Finish. In your new Project click on Project -> Properties

In the window that appears, on the left select Configuration Properties->General
On the options on the right for Character Set select Use Multi-Byte Character Set

I am not sure of the reason for doing this but it has fixed some problems for me in some of my own SDL projects.
Now on the Left options select Configuration Properties->Linker->Input
In the right settings under Additional Dependencies type the following:
| SDL.lib SDLmain.lib SDL_mixer.lib SDL_image.lib SDL_TTF.lib |

Now click on OK.
That is your Project setup for OpenGL and SDL.
You should note that will have to do this for both Debug and Release Project Properties. For this just repeat Step 2 for both Release and Debug settings selected for Build Options.

Step 3 – Adding Code
With your Desktop Environment and Project Settings nowsetup you can add a new C++ file to your project:.


Now copy THIS code into the .cpp file you just added to your project.
Alternatively you cannot download the entire SimpleSDL program at the bottom of the page.
Compiling and running your project should produce the following

If this doesn’t work make sure that you have followed the above steps correctly.
The SimpleSDL project is available from here.
Step 4 – Glut Shapes in SDL OpenGL Application
With everything above working you should now be able to download the SDL Basics project from here and it should produce the following when compiled and run.

Downloads
Here are the files that I discuss and use in the tutorial.
GLUT and SDL Setup Files - Files needed to allow development with SDL and GLUT
SimpleSDL Project - Project that will display a simple purple screen
BasicsSDL Project - Dispays three glutSpheres in a SDL window and takes input using SDL input
Feedback
If you have any problems or feedback please feel fee to contact me at david.bogues@braindeadclown.com. I cannot guarantee I will be able to get back to you if I am very busy but I will try.
Reference
I used a lot of forums when I was initially figuring out how to setup SDL and GLUT on VS 2008 but it was that long ago I cannot remember the exact addresses of most of them. These are the general references that I used:
Official OpenGL website - http://www.opengl.org/
Official SDL website - http://www.libsdl.org/
Lazy Foo - http://www.lazyfoo.net/ - also an excellent source of tutorials for pure SDL programming
GameDev.net - http://www.gamedev.net/ - not so much the site, but I definately got some information from their forums.
|