OpenGL Programming/Basics/Structure
Setting up a New Project
[edit | edit source]Depending on which app you plan to code in, you might need to set up a new C++ project before you begin. The instructions for this vary based on the program, so check your program's manual if you are unsure about how to do this.
GLUT
[edit | edit source]Many OpenGL applications take advantage of a cross-platform (Windows, Mac and Linux compatible) toolkit called GLUT, which is used in conjunction with OpenGL. GLUT is used to draw windows and handle mouse and keyboard events, which OpenGL cannot do on its own. There are alternatives to GLUT, but GLUT is the simplest GUI toolkit that runs cross-platform, so we assume you have GLUT installed.
There are many disadvantages to GLUT, which are related to its inflexibility. Since GLUT has to work the same across three very different operating systems, you don't have any platform-specific features. GLUT itself cannot take advantage of operating-system provided widgets (buttons, pulldown menus). Your application will not be able to change window-level menus (although context-menus can be customized). If these limitations are dealbreakers for your project, you'll have to use an alternative to GLUT. These will be discussed later in this wikibook. For now, just sit tight with GLUT.
My First OpenGL Program
[edit | edit source]Open up (or create) your main.cpp file. You should be familiar with the basic format of this file:
#include <iostream> int main(int argc, char *argv[]) { return 0; }
Including the OpenGL Headers
[edit | edit source]We're going to need to add some includes here, so we can use OpenGL in our application. Depending on how and where you installed OpenGL, you might have to tweak these paths a bit.
#include <GL/gl.h> #include <GL/glut.h> #include <GL/glu.h>
This includes the basic OpenGL commands, GLUT and a utility toolkit called GLU which is useful later. Note that if your operating system handles filenames case sensitive (e.g. Linux) you must capitalize the GL before compiling.
If using Visual Studio, make sure to include windows.h before any of the OpenGL headers.
Creating a Window
[edit | edit source]Now that we have our library, we can begin to design our interface by creating our window inside our int main() function:
#ifdef WIN32 //if using windows then do windows specific stuff. #define WIN32_LEAN_AND_MEAN //remove MFC overhead from windows.h which can cause slowness #define WIN32_EXTRA_LEAN #include <windows.h> #endif #include <GL/gl.h> #include <GL/glut.h> #include <GL/glu.h> void display() { /* empty function required as of glut 3.0 */ } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(800,600); glutCreateWindow("Hello World"); glutDisplayFunc(display); glutMainLoop(); return 0; }
We first call glutInit(), which starts up GLUT for our use. Next, we set up a display mode. For now, just use the settings we've provided here for glutInitDisplayMode. You might tweak these settings later. Then we tell GLUT how big we want our window to be; 800 by 600 is OK for now. Finally, we actually create the window with glutCreateWindow(), passing the window title as an argument, and give control of our program to GLUT through glutMainLoop(). Never forget to call glutMainLoop() when you use GLUT!
You might have noticed something so far: every function is prefixed with glut-. When we start actually using OpenGL itself, every function will be prefixed with just a gl-. This is for organization.
Try compiling this application. Hopefully, you should get an 800x600 window with the title Hello World. If you don't, refer to the installation section of this wikibook and consider reinstalling.
Getting Ready to Draw Stuff
[edit | edit source]Most simple OpenGL applications consist of only three elements:
- A setup function, which configures everything before you start actually drawing.
- A display function, which does the actual drawing, and
- int main(), which we've mostly configured above. int main just calls the setup function, and tells GLUT to use the display function when it needs to.
Some OpenGL code might look sort of like this:
#ifndef WIN32 //if using windows then do windows specific stuff. #define WIN32_LEAN_AND_MEAN //remove MFC overhead from windows.h which can cause slowness #define WIN32_EXTRA_LEAN #include <windows.h> #endif #include <GL/gl.h> #include <GL/glut.h> #include <GL/glu.h> void setup() { /* empty function nothing to setup yet */ } void display() { /* empty function required as of glut 3.0 */ } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(800,600); glutCreateWindow("Hello World"); setup(); glutDisplayFunc(display); glutMainLoop(); return 0; }
This code itself doesn't draw anything, since display() is empty.
Next Chapter OpenGL_Programming/Basics/Rectangles