On my old blog, CodieCode, I wrote a tutorial on how to set up an OpenGL, C++ and GLUT environment using the CodeBlocks IDE and MinGW compiler on a Windows XP and Vista machine. This was a popular post which seemed to help quite a few people. However time moves on, software updates and operating systems change. I've decided to update this tutorial with Windows 7 (though this should still work fine on XP and Vista) and the latest version of CodeBlocks and MinGW. Hope it helps :)
I would like to add that if you're serious about learning OpenGL, I think it's a must that you own "The Red Book" (OpenGL Programming Guide by Dave Shreiner). You can buy it from Amazon or wherever you normally buy your coding books. Either way it's a fantastic resource to have and it's helped me countless times in the past.
If you’re interested in coding in OpenGL and C++, a great way to start is by using GLUT. GLUT takes care of a lot of the difficulties in setting up an OpenGL project and lets you get started on your project quick and easily. In this tutorial I will guide you through installing and setting up the software you’ll need (CodeBlocks, MinGW and GLUT).
Before you can start you’re going to need a few things:
Windows – I've updated the steps below to run on Windows 7, Windows XP and Vista, If you manage to get this working on other operating systems, let me know :)
OpenGL – I’m not going to go into how to install OpenGL in this tutorial as most people will be using Windows XP or later and OpenGL comes ready in these operating systems.
GLUT – To download the GLUT files you’ll need click here and download ‘glut.zip’.
IDE (Integrated Development Environment) – While it’s possible to use an editor (such as Notepad++) and makefiles. I find the easiest and most efficient way to code is using a good IDE (be careful there’s some not-so-good IDE's out there). The best that I’ve come across is CodeBlocks (I’m not a fan of using Visual Studio for OpenGL but you can use that too if you wish) and it’s this IDE that we’ll be setting up today.
C++ Compiler – The compiler I’ll be using is MinGW, there are many advantages to using this but I wont go into these here.
So without further ado, lets start:
Download CodeBlocks and MinGW from the CodeBlocks website. Be sure to select the binary installer (.exe file) which comes with the MinGW compiler. As of writing this tutorial the file you need is: 'codeblocks-10.05mingw-setup.exe' (found by clicking on 'Downloads' > 'Download the binary release').
Download the GLUT file’s you’ll need by clicking here and downloading ‘glut.zip’
Install CodeBlocks and MinGW by running the '.exe' file you downloaded in Step 1. The default install options are sufficient.
Extract the 'Glut.zip' file you downloaded in Step 2 and do the following:-
Copy glut.dll into your 'C:\Windows\System32' folder (If you're using Windows 7 64-bit, you'll need to copy this file into 'C:\Windows\sysWOW64').
Copy glut.h into the ‘include\GL’ folder in the MinGW compiler folder. If you installed CodeBlocks and MinGW to the default directory in Step 3, this folder will be 'C:\Program Files\CodeBlocks\MinGW\include\GL'.
Copy libglut32.a into the 'MinGW\lib' folder. If you installed CodeBlocks and MinGW to the default directory in Step 3, this folder will be 'C:\Program Files\CodeBlocks\MinGW\lib'.
Now you have all the required software installed you can create a new OpenGL and GLUT project. CodeBlocks has an option for creating a GLUT project when creating a new project, we won't be using this option today though as I want to show you how to link your files:
Startup CodeBlocks and select 'File > New > Project…', in the new project window select 'Empty project' and click the 'Go' button.
In the wizard that follows press 'Next' to the first page (the one welcoming you to the wizard) and in the second page enter a project title name into the textbox labelled 'Project title:' and leave the other 3 textboxes with their default properties (you can change these if you like, it’s upto you) and click 'Next'. In this final wizard page, ensure that 'GNU GCC Compiler' is selected in the drop-down menu box labeled 'Compiler:', leave the other options with their default setting and click 'Finish'.
Once your project has been created, right click on the project in the Workspace explorer frame on the left of the screen and select 'Build options…'. In this options window select your project name in the tree to the left of the window (do not select either Debug or Release) and open the 'Linker settings' tab.
In this tab, click the 'Add' button below the 'Link libraries:' text box and in the textbox next to 'File:' enter 'opengl32' (without the quotes) and click 'OK'. (See Picture 1.1 Below)
Picture 1.1 - add 'opengl32' to the linker
Do the same again, this time entering 'glu32'. And once again entering 'glut32'. (See Picture 1.2 Below)
Picture 1.2 - your linker settings should look like this
You now have a project setup for OpenGL and GLUT. Let’s create a simple program to check everything works.
Download my simple glut c++ file by clicking here and downloading 'levelbylevel_glut_tutorial_main.zip'. This application will draw a lit sphere to a window with a black background.
Extract the .zip file and open up the main.cpp file in Notepad. Select all the text and click 'Edit > Copy'
In CodeBlocks select 'File > New > File…', in the new file window select 'Empty File' and click the 'Go' button. In the wizard that opens select 'Next', then enter 'main.cpp' for the filename and click 'Save'. (See Picture 1.3 Below)
Picture 1.3 - Enter 'main.cpp' for the file name
Put a tick in the tickboxes next to 'Debug' and 'Release' and then click the 'OK' button. (See Picture 1.4 Below)
Picture 1.5 - Select both debug and release
This should create a new empty file. Now click 'Edit > Paste' to copy in the code from the file above. You’re now ready to compile and run. Simply select 'Build > Build' to compile, hopefully if you followed all the steps above you should have no build errors. To run select 'Build > Run'. (See Picture 1.5 Below)
Picture 1.5 - Success!
I recently moved the site over to a codeigniter powered site, unfortunately this meant I lost the many many comments left for this tutorial over the time it's been up. I really appreciate everyone who took the time to post comments and feedback. Thank you so much.