Setting up OpenGL with Visual Studio

Go to OpenGL Home

Today we start a new series of tutorials in OpenGL called “From zero to hero” where we will try to cover pretty much all Computer Graphics render techniques used in modern games. This series is divided into a few chapters with different degrees of difficulty and each chapter contains a number of tutorials, that we believe, they fit in that range of difficulty.

As you progress some Math, Programming and C++ skills are required. I’m creating these tutorials on a Windows 8.1 OS using Visual Studio 2013 as an IDE.

Chapter I – Beginner Level 

How else could we start this series, better than the classic “Hello World” example? Well, OpenGL is a bit more complicated because OpenGL is not initialized by default and we don’t have a window where to display anything. So, consider that setting up, initializing and creating a new window could be our first “Hello World” example :).

At the end of this chapter you should be able to create a small engine / framework for OpenGL and understand how to draw a triangle or a square.

Quad and Triangle

At the end of Chapter I we should have this

Setting up OpenGL in Visual Studio 2013:

Before you start this tutorial, I wrote another tutorial showing a super fast way to set up OpenGL in Visual Studio using NuGet Package Manager here. So you have to decide which way you go, the results are the same :). However I strongly recommend you to do this tutorial to understand how things work behind.

Still here?, then first download:

  •  FreeGLUT, which will help us setting up a window, initializing OpenGL contexts and getting input from mouse/keyboard. Beside FreeGLUT there are other libraries that can be used with OpenGL. eg: SDL, GLFW, GLUT, GLOW, native code, etc. Make sure you download FreeGLUT 2.8.1.
  • GLEW (GL Extension Wrangler), which adds support for the newest OpenGL versions eg. OpenGL  version 4.5. This means that we can access and play with OpenGL’s newest functions.

Now open Visual Studio and create a New Project.

Create New Project

Create New Project(Click to zoom)

Pick Visual C++ and select Empty Project:

Empty Project

Empty Project

Choose a good location. Mine is F:\OpenGL_tutorials\ and enter a name for this project. I named it 1_Setting_OpenGL but feel free to put what you want. Careful: make sure you don’t have any spaces in your path and your path is a valid one otherwise you can end up with a compile error called MSB3073: The command ” ” exited with code 1.

Now click OK and we are ready to create our main.cpp file which is our entry point for our program. Press CTR+SHIFT+A to add a new file and call it main.cpp



We can write down the main function declaration and build and run the program by pressing F5 key:

int main(int argc, char **argv)
  return 0;

Now we are finally ready to set up FreeGLUT and GLEW in Visual Studio 2013. There are two ways do to that:

  1. From Visual Studio we set up paths to the lib/include folders where we extracted FreeGLUT and GLEW
  2. Copy those important files (.libs, .h) from FreeGLUT and GLEW in our project in a folder called Dependencies or Libraries or however you want to call it. After that we tell Visual Studio to reference this internal folder. This way, when you copy your project to another PC you will still have these important files with the project and you don’t have to set them up again in Visual Studio. Also you have much more control over your project.

I will go with the second option, even if it takes a little more work to do. First things first, we create a folder called Dependencies. Here we will put FreeGLUT’s and GLEW’s required files (.libs and .h) in two subfolders called freeglut and glew.

Create a new folder: Dependencies

Create a new folder: Dependencies

Let’s start with GLEW because it’s easier.

  • Extract GLEW archive somewhere else. Not in your project.
  • Create a new folder called glew in Dependencies folder from your project.
  • Go where you extracted GLEW, there should be an include folder with a GL subfolder with only 3 files glew.h,, wglew.h. Copy all of them in your glew directory from your project.
  • Go again where you extracted GLEW, there should be an lib directory with a Release directory inside, choose Win32 directory and copy glew32.lib in your glew directory from your project.
Glew Directory in Dependencies

Glew folder in Dependencies

Now let’s prepare FreeGLUT (version 2.8.1 at the moment)

  • Extract Freeglut archive somewere else. Not in you project.
  • Create a new folder called freeglut in Dependencies folder in your project.
  • Unlike GLEW, we have to compile Freeglut, because there are no dll or lib files. Go where you extracted Freeglut archive and there should be a VisualStudio folder. For this version of Freeglut in VisualStudio folder there is only 2008, 2010, 2012 versions of Visual Studio. Open 2012 folder, and there should be a freeglut.sln, open it and load all projects (OK them ‘all 😀 ). Now be sure that freeglut is your main project in Solution Explorer. Also select how you want to compile, Win32 or x64. By default is Win32. Press F5 and compile.
  • After you compile, a lib folder was created (root archive directory) with a x86 folder and a Debug folder inside. There you can find the freeglut.lib. Copy it to your freeglut directory in your project.
  • Go again where you extracted Freeglut and there should be a include folder with a GL subfolder inside. There are four .h files: freeglut.h,  freeglut_ext.h,  freeglut_std.h and glut.h. Copy them to your freeglut  folder from your project.
freeglut directory in Dependencies

freeglut folder  in Dependencies

Now files are ready but they are still not linked with Visual Studio project. To do this open your project again and go to properties:

Open Properties Visual Studio

Open Properties Visual Studio

Here we select Linker > General. Find Additional Library Directories field and write freeglut and glew directories that we just created above.

Copy-Paste text : Dependencies\freeglut;Dependencies\glew
Settin up OpenGL in Visual Studio 2013

Settin up OpenGL in Visual Studio 2013

Now under Linker go to Input and the first line is Additional Dependencies. Here we have to provide our .lib file names. Notice that opengl32.lib is part of Windows SDK. Just add this .lib names near the existing ones:

Copy-Paste text : opengl32.lib;freeglut.lib;glew32.lib;
Setting up OpenGL with Visual Studio 2013

Setting up OpenGL with Visual Studio 2013

Click OK and now we are ready to initialize OpenGL and create a new window in our next tutorial.

If you want to build for a x64 architecture, go through above steps but make sure you copy from x64 folders and you build Freeglut with x64 selected. Also your project must be compiled using x64.

Common mistakes:

  1. Adding Dependency folder in Solution Explorer. It’s not necessary
  2. Removing other libs from Additional Dependencies
  3. Invalid paths
  4. Copying wrong libs. You will get LNK errors.
  5. Using FreeGLUT for x64 and GLEW for x86 or vice versa. LNK errors

Follow me on Twitter and stay tuned with next tutorials.

blog comments powered by Disqus