Monday, 12 August 2013

Setting up freeglut and GLTools with Visual Studio 2010

It's good to be writing a tutorial after a long time and there are a number of reasons for that. First of all, I have been really busy with a lot of work and research (well actually I still am!). On the other hand, it is only until recently that I have been struggling with a setup which has little tutorials documented, while there seems to be a lot of beginner developers facing the same problem as I am.

This particular tutorial deals with setting up a Microsoft Visual C++ 2010 Project for use with examples found in the OpenGL Superbible 5th Edition. The book has a section which details the same process for a Visual C++ 2008 project, which is completly different than this tutorial. As always, I have tried to keep everything simple and straightforward so even a person who has no knowledge about these settings can make them work.

Getting the required libraries

The examples in OpenGL Superbible 5th Edition use two libraries, freeglut and GLTools. So before starting anything, we have to get compiled version of these libraries. You can download the library files I am using for this setup from:

Freeglut_GLTools Zip

Creating a Windows Console Project

I am explaining this for completeness sake, however most of you might already know how to do this, if so you can skip to the next section.

Open Microsoft Visual Studio 2010, and from the File menu select, New->Project

From the window select Win32 Console Application under Visual C++. Give your project a name, here I have named it "FirstProject" since it is our first OpenGL project (You can also choose the project folder location). Click OK when done. The project setup wizard will appear. Click Next. On the Application Settings window that appears next, check Empty Project and click Finish to create and Empty Project for our OpenGL Application.

Modifying the Project Settings

To use the freeglut and GLTools libraries downloaded from Freeglut_GLTools Zip, unzip the contents to a known location. The folder inside contains three sub directories named bin, lib, include. For this part we will need to copy lib and include folders inside our project folder. In the above example setting we will copy the folders inside "C:\Users\masad\Desktop\Opengl_Tutorials\FirstProject\FirstProject" folder.

Now we need to tell Visual Studio location and the names of these folders. For this right click on FirstProject from the solution explorer and select Properties as shown below:

In the project properties that appear, first change the Configuration option to All Configurations. Next go to Configuration Properties->VC++ Directories from the right hand navigation panel.

Now we need to specify location of the include and lib folders we copied earlier. Add the following strings in the start of each corresponding entry:
  1. For Include Directories add the string ./include;
  2. For Library Directories add the string ./lib;
Alternatively if you have placed these folders at some other locations on your hard drive, then you need to write that location here. I personally find copying the folders to the project folder much more convenient, as the folder location is easier to specify and there are less chances of making a mistake.

We have specified the location of the library and the source code, however now we need to inform the Linker about the library files contained within these folders that we may want to link to our project when compiling.

For this, navigate to Linker->Input configurations from the navigation panel.

In the Additional Dependencies entry add the following line specifying the two library files to be used by our project:
  • freeglut.lib; gltools.lib;
Note that each entry in these configuration settings is seperated by a semi-colon (;). That is the reason we are specifying the above two libraries seperated by ;

We are now done with settings. Just need one last bit, before we are up and running the examples.
Click OK to save the project settings and return back to the project window.

Compiling a Sample Code from OpenGL Superbible

For this process, the first thing we need is an example code. From the OpenGL Superbible Example codes copy a code file you wish to compile and execute. I am using the Bounce.cpp example from Chapter 2 and copying to the project folder at "C:\Users\masad\Desktop\Opengl_Tutorials\FirstProject\FirstProject" for the above example.

Add the cpp file to Source Files of your project by selecting Source Files->Add->Existing Item

From the Solution Configuration options select Release instead of Debug (which is there by default)

Before compiling and running our project binaries we need to modify the code slightly.
In the Bounce.cpp code delete the line which defines freeglut as static library.
This line of code is:

Now you are ready to compile and execute your project. You will see that the compiled binary gives an error of Missing DLLs. This is where the bin folder in the Freeglut_GLTools Zip file comes in use. Copy the contents of this bin folder into the Release folder containing the .EXE binary. For the above example this folder can be found at: "C:\Users\masad\Desktop\Opengl_Tutorials\FirstProject\Release"

 Thats all! And as always I hope this helps anyone coming to this blog.