Article: Portable openGL programming in C++ »
FERDY CHRISTANT - JAN 22, 2007 (08:22:47 PM)
|Every now and then, I get tired of the usual business web application, no matter the platform. It is at times like these, that I want to do something more creative, and fun. For a while now, I've been having ideas about using OpenGL to implement something cool. They are ideas that I probably will never implement due to a lack of time, but I decided to at least learn something about the basics of openGL.|
This short article explains how you can get started in OpenGL programming, and how to develop portable code. You would think that others have done this before, which is true. Yet, I could not find any concise article on how to achieve exactly what I wanted. I will not bother you with what OpenGL is, you can find that out yourself here.
I've chosen to develop my OpenGL code on Windows using Visual C++, which is part of Visual Studio. You can download the free express edition here, or use Visual Studio if you're lucky to have a license for it. Note that the code can be produced in any IDE, but this seems like the easy choice on Windows. Later I might add setup instructions for Eclipse.
Setting up the IDE
With our IDE in place, it is time to create a first project and configure the link settings. Luckily, this is described in detail here. Be sure to follow the exact instructions in that article. But wait, we're not done yet. In order for the resulting .exe file to be usable on other computers that do not have Visual C++ installed, we need to change two more settings:
In the project properties Window, section C/C++ ->Code generation, set the "Runtime library" value to "Multi-threaded (/MT)" for the release configuration (upper left dropdown), and "Multi-threaded Debug (/MTd)" for the debug configuration.
Adding an example
With the IDE and our project properly configured, it is now time to test some example code. Download the triangle example from this list of examples, and extract the file to a location on your hard drive. Next, in Visual C++, right-click on your project in the solution explorer, and chose "Add->Existing item..". Browse to the main.cpp file of the example and import it.
To run the example, click the play button in Visual C++. When successful, a window with a triangle will appear:
Distributing the application
You have just run the application from Visual C++ successfully. This has resulted in a .exe file in the debug directory of your application. You should not distribute that .exe file to your users, however. First, we need to build a release, not a debug assembly. A release assembly results in a significantly smaller .exe file. To do so, in Visual C++ go to Build->Configuration Manager, and then set the active solution configuration to "release". Next, rebuild the application, by right-clicking on the project in the solution explorer, and chosing "Rebuild".
The resulting .exe file can be found in the "release" directory of your project. Note that you still cannot distribute this .exe file by itself to your end users. The reason is that the application depends on two .dll files: glut32.dll and opengl32.dll. Opengl32.dll is shipped in Windows 2000 and Windows XP by default, glut32.dll is not. To be absolutely sure that your .exe will run on any Windows version, you need to ship both files with the .exe, in the same directory, or create an installer that installs these files in the window system directories. Unfortunately, it is not possible to statically link to these libraries.
Our OpenGL application is now portable across all Windows platforms, which is great, but what about other platforms? Technically, apart from the way of building the application, nothing about the code is platform-specific. This means that our code is fully portable. I will demonstrate this in a future follow-up to this article.
This idea I am having, what led me to OpenGL in the first place. I've been thinking about 3D interfaces applied to common desktop functionality. For example, a 3D way to navigate and search through files. While I do question the added value of such an interface, it would be one awesome experiment. Now that I have OpenGL development properly setup, I am excited and frustrated at the same time. Excited that I have the power to implement the experiment for real. Frustrated that I'm a graphic programming rookie and will probably never have the time to implement the project.