Page 1 of 1

Getting Started With Modern OpenGL 4.2

#1 jmd_   User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 12-May 12

Posted 07 June 2012 - 03:00 AM

This tutorial is about getting started with modern OpenGL and assumes knowledge of C/C++. For this tutorial, you will need a fairly new graphics card and the latest drivers. Specifically, a graphics card that implements OpenGL 4.2 so this means you must have what is generally known as a DirectX 11 card. In this tutorial, we will compile the open source toolkit Freeglut, the GL extension loader Gl3w, and we will check that we can run OpenGL 4.2 core programs. I will use the free Mingw compiler. You can use Visual Studio if you prefer, but I started using Mingw back when Visual Studio wasn't available for personal use so I will stick to what I know best. You're on your own if you use Visual Studio.

So why do we need Freeglut?
Freeglut is an open source toolkit that abstracts all the OS specific code that every OpenGL program requires. This prevents us from having to learn Win32 API or GLX if you use Linux. If you don't already know OpenGL, it saves you from having to write a lot of boiler plate code just to get started. Once you have the basics of OpenGL, using a toolkit is entirely optional and can be dropped if you don't want the extra dependencies. For simple tutorials and demos, Freeglut is the way to go to get started.

What is Gl3w?
Gl3w is a utility that loads all the core OpenGL 4.2 functions and extensions for us. (You could also use Glew, but it loads deprecated functions as well as the core ones, so you have to be more careful when you use it.)The alternative is to spend time manipulating function pointers and loading extensions by hand which can get tedious very quick. Again, later you can drop the Gl3w utility if you prefer and load in everything by hand, but for now we will use the library.

Installing Freeglut
So now we can get started. The first thing to do is download, compile, and install Freeglut. Why do we compile Freeglut when we can get the binaries? We compile it because we are programmers and because we can. Further, Freeglut is open source so it defeats the purpose if all we do is use pre-built binaries. Download Freeglut from

Next, open a command window and navigate to the Freeglut source code. I assume that Mingw is on the path. Type the following compiler commands:
gcc -O2 -c -DFREEGLUT_EXPORTS *.c -I../include
gcc -shared -o freeglut32.dll *.o -Wl,--enable-stdcall-fixup,--out-implib,libfreeglut32.a -lopengl32 -lglu32 -lgdi32 -lwinmm
gcc -O2 -c -DFREEGLUT_STATIC *.c -I../include
ar rcs libfreeglut32_static.a *.o

Freeglut should compile without any problem. Next, find the newly built files freeglut32.dll, libfreeglut32.a, libfreeglut32_static.a and the include directory. Copy the header files from the include directory to the ..\Mingw\include\GL directory. The path ..\Mingw is the install location of Mingw on disk. Put freeglut32.dll into ..\Mingw\bin and put libfreeglut32.a and libfreeglut32_static.a into ..\Mingw\lib. Make sure that ..\Mingw\bin is on the path. That takes care of the installation of Freeglut.

Installing Gl3w
For Gl3w, you should have Python 2.XX installed. Download the Python script located at Run the Python script and it will download the core header files from and it will create a source file that can be compiled. Copy the downloaded header files to ..\Mingw\include\GL3 directory. The source file gl3w.c we will link with our application. I renamed it to gl3w.cpp and modified it to compile with g++, but it should work just the same.

The Code
Now we can write a minimal program to check that we can run OpenGL 4.2 core profile programs. The following is a simple program that creates a window and paints it black. I named it test.cpp:
#include <iostream>
#include <GL3/gl3w.h>
#include <GL/freeglut.h>

// display callback
void display() {

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    // request version 4.2
    glutInitContextVersion(4, 2);
    // core profile 

    // double buffered, depth, color w/ alpha
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("OpenGL Test");

    if (gl3wInit()) {
	    std::cerr << "Failed to initialize." << std::endl;
	    return -1;
    if (!gl3wIsSupported(4, 2)) {
	    std::cerr << "OpenGL 4.2 not supported" << std::endl;
	    return -1;

	std::cout << "OpenGL " << glGetString(GL_VERSION) << "\nGLSL "
    return 0;

Compiling and Executing
The program creates a 640 x 480 window and prints out the GL version and GLSL version. It's straightforward to compile:
g++ -Wall -c test.cpp gl3w.cpp
g++ -o test.exe test.o gl3w.o -lfreeglut32 -lopengl32

Finally, we can run the executable test.exe:

Posted Image

Is This A Good Question/Topic? 1
  • +

Page 1 of 1