What is OpenGL?
OpenGL is a set of standard API's for developing 2D and 3D graphics applications. The standard provides it in the form of a library which can be linked along wih your program (could be C/C++). OpenGL includes vast scope of API's which can be used to render figures, textures, shading, etc. The man pages are available along with openGL SDK documentation.
Getting Started With OpenGL
As such, there is lot more to discuss when we talk about OpenGL, but to start with a simple program would be a good breather for a novice. Here we'll be writing a program to draw a rectangle to get a hold of basic know-how of OpenGL programming.
The article assumes :
- Platform as Linux
- Native programming language as C
Glut libraries i.e. the openGL Utility toolkit needs to be installed which provides a platform independent windowing along with the window event handling for interactive graphics programs.
Use following commands to install openGL and Glut
Code:
sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev sudo apt-get install freeglut3
OpenGL Programming
As unusual, OpenGL programming with C also has main() as the starting point. There would be glut calls to initialise/create windows for the openGL to draw onto followed by GL calls to do the actual drawing.
First of all, call
Code:
glutInit(&argc, argv)
Next call is,
Code:
glutInitWindowSize(window-width, window-height); glutInitWindowPosition(x-coord, y-coord);
Then we need to call
Code:
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)
Please refer the man page for more modes accepted.
Creating a window,
Code:
glutCreateWindow("Basic OpenGL Example")
Next is,
Code:
glutIdleFunc(drawRect)
Global idle callback function is called during no event-handling. All rendering is done during the idle callbacks as to not to affect the responsiveness of the interactive events.
The next call is an openGL call,
Code:
glViewport( 0, 0, window-width, window-height )
We have to set the current matrix mode by
Code:
glMatrixMode( GL_PROJECTION )
- GL_PROJECTION - for camera positioning
- GL_MODELVIEW - for various transformations
- GL_TEXTURE - as the name suggests it is used for texturing
We can enable the depth testing, which is a part of graphics pipeline, by following call
Code:
glEnable( GL_DEPTH_TEST )
Code:
gluPerspective( 45, (float)window-width/window-height, .1, 100 )
The frustum consists of a clipping plane nearest to camera eye (called the near clipping plane) and a farthest plane from the camera eye (called far clipping plane).
Please note, any object before to the near clipping plane and farther to the far clipping plane would be clipped.
Here in our call,
- The First parameter is the angle of the camera view frustum = 45 degrees
- The second parameter is the aspect ratio = our window aspect ratio in our case
- The third parameter is the distance of the camera eye from the near clipping plane = 0.1 units
- Fourth parameter is the distance of the camera eye from the far clipping plane = 100 units
Code:
glMatrixMode( GL_MODELVIEW )
Code:
glutMainLoop()
Coming to the drawing part, lets enter the drawRect() method.
The first call would be,
Code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
It is a good programming practice to clear all enabled buffers before starting to draw or in between frames of animation.
Alongwith, we need to load an identity matrix before transformations which are relative to the current loaded matrix. Hence, if we want the translation/rotation to happen with respect to origin, then we need to load the identity matrix first.
Code:
glLoadIdentity()
Code:
glTranslatef(0,5, -20)
The actual drawing is done as:
Code:
glBegin(GL_QUADS); glColor3ub(255, 0, 0); glVertex2f(0, 3); glVertex2f(6, 3); glColor3ub(255, 255, 0); glVertex2f(6, 0); glVertex2f(0, 0); glEnd()
Here we are specifying 2D vertices in float values to draw a two colored rectangle.
Color is set through glColor(), and every vertex is drawn with the previous set glColor() call. The color is mentioned in RGB format i.e. Red, Green, Blue.
We need to explicitly call,
Code:
glutSwapBuffers()
Thats it, we are done with openGL programming to draw a colored rectangle

The complete Program
Here is how the complete programs looks like:
FileName : myRect.c
Code:
#include<stdio.h>
#include<GL/glut.h>
void drawRect();
int main (int argc, char**argv)
{
int windowWidth = 640;
int windowHeight = 480;
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Basic OpenGL Example");
glutIdleFunc(drawRect);
glViewport( 0, 0, windowWidth, windowHeight );
glMatrixMode( GL_PROJECTION );
glEnable( GL_DEPTH_TEST );
gluPerspective( 45, (float)windowWidth/windowHeight, .1, 100 );
glMatrixMode( GL_MODELVIEW );
glutMainLoop();
return 0;
}
void drawRect()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,5, -20);
glBegin(GL_QUADS);
glColor3ub(255, 0, 0);
glVertex2f(0, 3);
glVertex2f(6, 3);
glColor3ub(255, 255, 0);
glVertex2f(6, 0);
glVertex2f(0, 0);
glEnd();
glutSwapBuffers();
}
Compiling and Running
While compiling, you will need to link to the installed GL and glut libraries.
Code:
gcc -Wall myRect.c -o myRect -lglut -lGLU

Conclusion
Now we know how to write a basic drawing program using openGL and most importantly we understand what each line of code means. Since, openGL programming needs the knowldge of its API's, hence the more you know and understand and with the ability to imagine, the more you enhance on your graphics programming skills.
For drawing animations, we can add a varying static parameter , whose value changes after every run. Hence, that would lead to a different frame each time, creating an animation. As an example, in the above program, provide the z-value of gltranslate() call as a static variable in our method drawRect(), whose value is incremented in each run. Wondering, what the animation will look like? Try and enjoy playing
poornaMoksha, shabbir
likes this