Beginning Device-independent Graphics Programming with OpenGL (using GL, GLU & GLUT)

Khan MNM Sadh
inside the insight
Published in
3 min readSep 28, 2009

We have already set up GLUT and OpenGL [↗]. Now we can start writing codes from the ground up.

Create an Empty C++ Win32 Console application and add a C++ source file.

Add necessary library headers as follow:

#include <stdlib.h>

#include <GL/glut.h>

Note» alteration of order will cause generation of error message.

Now let us write the main function that will perform the required initializations and start the event-processing loop. All the functions in GLUT have the prefix glut and those, which perform some kind of initialization, have the prefix glutInit (GL has gl and GLU has glu prefixes as well).

Note» Please do not start coding until I say it is time.

At first, we initialize glut using the function,

void glutInit( //init GLUT lib & negotiate a session with the window system.

int *argc, //pointer to unmodified argc var of main

char **argv //pointer to unmodified argv var of main

);

Then establish the window’s position:

void glutInitWindowPosition(

int x, //number of pixels from the left of the screen

int y //number of pixels from the top of the screen

); //for both deafult: -1 (positioned by system)

Now, choose the window size:

void glutInitWindowSize(

int width, //widht of window

int height //height of window

);

Unavoidable! Define the display mode:

void glutInitDisplayMode(unsigned int mode);

There are options for selecting modes. See documentation for more. We will use following for most of the cases,

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

Have completed above steps, the window can be created like this,

int glutCreateWindow(char *title);

» Here, title is a string that will be shown as the title of the displayed window.

`

Everything is set up; the kitchen is ready! Now start cooking. Yep, but you at least have to draw something before you really do the “hello world” of glut. So, we need the define a drawing function and call it from main, with the following,

void glutDisplayFunc( //sets the display callback for the current window.

void (*func)(void) //name of function to be called when window is redrawn

);

» It is illegal to pass a Null as function and the return type of the display function should be void.

`

One last thing, tell glut to enter the event processing loop by calling,

void glutMainLoop(void);

Now, it is time we write some code, an example follows

#include <GL/glut.h>

void renderScene(void) {

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glFlush();
}

void main(int argc, char **argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);

glutInitWindowPosition(100,100);

glutInitWindowSize(320,320);

glutCreateWindow(“nafSadh- GLUT Tutorial”);

glutDisplayFunc(renderScene);

glutMainLoop();

}

O.k., you have written your first (may be) program in glut, it is time you do some artisanship. If you run the code above, two windows will come, one the console window, another is OpneGL window. When the OpenGL window is resized, the triangle is distorted. To solve this, we need to add a functionality that handles the resizing. The glut function for this is,

void glutReshapeFunc(void (*func)(int width, int height));

This should be called just before the main loop, an example follow,

#include <GL/glut.h>

void renderScene(void) {

… … …

}

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
}

void main(int argc, char **argv) {

… … …

glutDisplayFunc(renderScene);

glutReshapeFunc(changeSize); ☜

glutMainLoop();

}

Of course, what I included here is inadequately sufficient, but hopefully enough for a QUICK START. Regardless of anyone else’s effort, I must first mention, Lighthouse 3D’s tutorial was most helpful for me. Beside these, following are some other links I must share:

--

--