Modern OpenGL : Setting Up

Before we can start learning OpenGL, we have to set everything up for the compiler/linker/IDE/libraries. My favorite combination right now is the Code::Blocks IDE (with MingW compiler) + GLFW for windows/input/timing. One of my least favorite things is setting this stuff up and making everything work together. I'm going to attempt to walk you guys through this thing from beginning to end.

Install Code::Blocks with MingW

The first thing to do is install the IDE and compiler. Head over to Code::Blocks Download Page and click Download the binary release. I'm going to assume you're using Windows because Linux is for nerds and hippies. Anywho, hit one of the download links for the combined CodeBlocks + MingW installer (like the Sourceforge.net one) and save the installation executable somewhere:

Next, run it, go through the default install, and start it up. I'm going to assume that you've installed it to C:\Program Files\CodeBlocks\ for the sake of simplicity. When it starts up for the first time, CodeBlocks should detect that MingW is installed and autoselect it as the compiler to use with the IDE; just continue with this default. Next its going to ask you if you want to associate C/C++ files with the program; this one is your choice; I did it. After closing the Tip Of The Day...who actually finds that box valuable, by the way? Somewhere out there is a program that has the meaning of life along with a formal proof for the simultaneous existence and non-existence of God buried in the Tip-of-the-Day. Humanity will never find ultimate truth because we all just close the TOD boxes. For now, join the masses and just close the box along with the Script Console that pops up. I don't know what this is for and thus declare it to be useless.

I bet 90% of you didn't follow that and just clicked through; you probably got it all right. So lets test our IDE + compiler setup so far make sure it can compile a simple program. First make a new empty project by hitting File-->New-->Project the select the little icon for Empty Project in the box that pops up. Fill in the name of the project and the path that it will stored in. For example, I named mine HelloWorld and used the path C:\Code\. This will put all your files related to this project in C:\Code\HelloWorld\. Click through the rest of it and you now have a workspace and empty project. Next you have to make a source code file; hit File-->New-->Empty File and click Yes when the box asks you if you want to include it in the current project. Then it will prompt you to save it; call it, for example, C:\Code\HelloWorld\hello.c. Now type the following into the new file and hit File-->Save all files:

#include ‹stdio.h›
int main()
{
	printf("Hello World!\n");
	return 0;
}

Now hit Build-->Build and run and verify that a command line box pops up with "Hello World!" displayed in it. If a box appears and then quickly disappears, try adding while(1){} right before the return. This will make the program loop until closed instead of quickly exiting. If you run into problems later on and can't find the bug, always try out Build-->Clean followed by Build-->Rebuild before pulling your hair out in frustration. If all went well, you now have an IDE + compiler combo working.

Get OpenGL and GLFW working

GLFW provides some basic functionality for our OpenGL programs: cool stuff like creating the window and OpenGL context and handling keyboard/mouse input. As you'll see, it makes building a basic framework for our app really easy. Another library that provides similar functionality is SDL, but I've been liking GLFW lately; so lets set it up. First, click over to the GLFW Download page and grab the Windows binary zip file. Next upzip it into a folder somewhere; I'll be assuming C:\glfw-temp\.

Now we gotta move a couple of files out of that temporary C:\glfw-temp\ folder into other places. Copy/move the following files:

You can also put the DLL file into the same folder that the program EXE runs from if you like. Now go ahead and delete the original C:\glfw-temp\ folder; you don't need it anymore. Now open up that HelloWorld project that you made earlier. We're going to set this project up for GLFW and OpenGL. Click on Project-->Build options... and hit the Linker settings tab. Click the Add button under the 'Link libraries' box, type opengl32 in the textbox that pops up, and hit ok. Finally, in the 'Other linker options' box, type -lglfw (those are 'L's). When you're done, the window should look something like this:

At this point, you can indeed start writing GLFW + OpenGL programs. However, you will not have access to the full functionality that OpenGL can provide. Screw that noise, do one last thing and you'll be able to take full advantage of the API and your video card's power. Read the following below...

Make GLEW work

HAHAHAHA I TRICKED YOU! Just when you thought we were finally done installing stuff and getting it linked; theres more! GLEW is an awesome library that automagically lets OpenGL access fancy new features by loading extensions for you. This is kind of a pain and I don't understand half of it. The moral of the story is...just get GLEW working and you'll be able to do anything with OpenGL that your video card + drivers can handle: Deal? Alright, head over to the GLEW Sourceforge page and grab the relevant binary. Unzip it somewhere handy, I'll use C:\glew-temp\ as an example.

Now copy/move the following files out of that C:\glew-temp\ folder into their new destinations listed below. After that, delete the temporary folder along with the rest of the files in it.

Finally, add glew32s to the 'Link libraries' box in Project-->Build options.... Also be sure to shift the glew32s item above the opengl32 item. The window should now look like this:

Now you should be good to go and ready get coding with OpenGL.

Test it!

Alright, now lets see if it all worked. Copy and paste the contents of the .c source code file below into your project. Just overwrite the little 'Hello World' program thats in there. Cross your fingers and hit Build-->Build and run. If all goes well, you should get no errors and a 800x600 black window will appear with a spinning triangle inside it. If this is the case, then congratulations: you are all set up to start learning modern OpenGL. If you errors or the program crashes, double-check the steps on this page. Try googling the error along with keywords like 'glew' and 'glfw'. It seems like a lot of folks have difficulties setting this stuff up (myself included), so chances are that your issue has been seen before and the web has an answer for it. If you guys see anything in this article that could be improved or is flat-out wrong, shoot me an email about it. In the meantime, test out your setup with this fairly modern and very simple code:

OpenGL + GLFW + GLEW Testing Code : [ ogltest.c ]

Good luck!