Setting up OpenGL with Visual Studio
This tutorial will teach you how to get OpenGL set up with Visual Studio. I will not be covering older or deprecated parts of OpenGL, so this tutorial should be applicable for some time to come. Also, I will attempt to use as few external libraries as possible, though some will be needed.
Before we get started, there are a few assumptions I will be making. — Moderate C++ knowledge — Modern graphics card with support for OpenGL 3.x or later — Use of Visual Studio Express 2013 — Windows 7 64-bit environment
These assumptions are not all necessarily rules, and the operating system and IDE are simply what I am using. Alright, let’s get started!
Install GLFW
One of the first requirements for using OpenGL is something that OpenGL itself does not handle, and that is creating a window. Unfortunately, every operating system has their own way to do this as well. Since we are using OpenGL, there is a good chance that we are trying to remain compatible with other operating systems and environments. The good news is, there is a fantastic library that will handle this for us. The even better news is, it doesn't do much else.
From the site; “GLFW is an Open Source, multi-platform library for creating windows with OpenGL contexts and receiving input and events.” This is perfect, it will set up our window for us, and help us gather mouse and keyboard input. You can check it out at www.glfw.org and download the latest 32-bit binaries for it. As of this writing the latest version is GLFW 3.0.4, but the next version is about 87% complete.
Once you've downloaded the binaries, go ahead and extract them somewhere on your machine. We will need to move a few of the files around for our project. I will list below the files that you need to copy, then the location you need to copy them to. This assumes a default installation of Visual Studio 2013 in the default directory.
path\glfw\include\GLFW\glfw3.h -> path\visualStudio\VC\include
path\glfw\lib-msvc110\glfw3.lib -> path\visualStudio\VC\lib
path\glfw\lib-msvc110\glfw3.dll -> C:\Windows\System32
Link to GLFW
Once you've copied these files over you can go ahead and start up Visual Studio if you haven’t already. Go ahead and create an empty project and save it. Now go to:
Project -> ProjectName Properties
At the top of the window where it says Configuration, select “All Configurations” from the drop down menu. Now go here:
Configuration Properties -> Linker -> Input
Once in this menu, notice the Additional Dependencies option. Go ahead and select it and choose “Edit”. In the window that opens, enter these three entries on seperate lines:
glfw3.lib
opengl32.lib
glu32.lib
Go ahead and press “OK”, then apply the changes and close the window.
Now everything should be installed and linked properly. To test that everything is working, go ahead and create a .cpp file and call it Main.cpp. Here is the sample code that you can use to test:
#include <glfw3.h>
#include <thread>
int main()
{
glfwInit();
std::this_thread::sleep_for(std::chrono::seconds(1));
glfwTerminate();
}
What this will do is open up an empty console window, then close it after 1 second. Not very useful, but it is just a quick check to ensure everything is set up correctly.
Install GLEW
Great! Now that GLFW is installed and working, we will need to do a very similar process for GLEW. GLEW is the OpenGL Extension Wrangler, it basically helps with loading OpenGL Extensions and saves you a whole lot of time. You can download it at http://glew.sourceforge.net/ just make sure you pick up the binaries. As before, here are the files that you need to copy:
path\glew\include\GL\glew.h -> path\visualStudio\VC\include
path\glew\lib\Release\Win32\glew32s.lib -> path\visualStudio\VC\lib
path\glew\bin\Release\Win32\glew32.dll -> C:\Windows\System32
Link to GLEW
Now go ahead and open up your project properties again, and go back into the Linker, ensuring that the configuration is still set to All Configurations. Add this line:
glew32s.lib
The Code
Now that everything is finally installed and linked up properly, here is a simple application that will open a window, allow you to close it by pressing escape, and will also test that GLEW is working (prints a 1 in the console):
#define GLEW_STATIC
#include <glew.h>
#include <glfw3.h>
#include <iostream>
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Windowed
GLFWwindow * window = glfwCreateWindow(800, 600, “OpenGL”, nullptr, nullptr);
// Fullscreen
/*GLFWwindow * window = glfwCreateWindow(800, 600, “OpenGL”, glfwGetPrimaryMonitor(), nullptr);*/
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
std::cout << vertexBuffer << std::endl;
// Main application loop
while(!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
glfwTerminate();
return 0;
}