Saturday, June 6, 2009

OpenGL Introduction sans the nonsense

This post is intended to make you feel at ease with using the OpenGL API. Here, I will cover the functionality of the low level procedures along with a brief overview of any graphics library in general.

It is naturally intimidating to see the highly complex computer displays, or the cutting edge 3D graphics and visuals of a movie scene. But it might surprise you to know that whatever be the case, the end result of that ultra realistic 3D world is a sequence of flattened images on a 2D screen. What actually happens is that a set of functions are implemented to calculate the depths, the lighting, the surface normals etc. (all of which I will explain clearly later) in the virtual world and finally map the 3D images onto a 2D plane. Thankfully, the implementation of these functions (which vary from library to library) are abstracted from the programmer.
Any known graphics library adheres to the following simple (at times) steps:
  1. Establish a co-ordinate system for the screen onto which the image is projected.
  2. Establish a co-ordinate system for the virtual world.
  3. Implement a function that allows the programmer to draw a point ( the smallest point one can draw is a pixel). It is this basic primitive that allows a programmer to draw in complex shapes and objects.
  4. Allow a way to specify normals to any surface or point (this helps in lighting and also when we want to simulate physics) .
  5. Provide methods to operate on matrices (Matrices are used to translate, rotate or scale
  6. Provide other common methods that might make the programmer's life easy.
Now lets jump straight to OpenGL.
Most books introduce you to a simple OpenGL program to start off with. But I feel there is a need to break this tradition. So, ill first do the explaining part and then show you the program.

OpenGL like all other graphic libraries encapsulates the aforesaid points in some way or the other. This will be clear if i explain in common language the structure of a typical OpenGL program. Keep in mind that i am using GLUT.

  • Include necessary files
  • Write a main function like any C/C++ program. And in main call a few glut functions which will set up and handle your window in which you are going to draw. To be more specific you will be setting up the display modes, the window sizes, window positions, menus in your windows(if there are any) etc.
  • Also in the main function after the initialization done in the previous step, you register the call back functions for necessary events. Further explaination is required here. Now in any window based program the code that is to be executed next is decided based on events. For instance when you click a mouse button, a mouse button event is generated, and to act on this event and perform the necessary action the programmer implements a call back function for mouse events. But how does GLUT know that the function that is implemented is a mouse call back? Hence the call back function is registered with GLUT as being a mouse event based call back function.
  • Now these call back functions that were just registered have to be implemented to actually be of any use. So that is done next. Also note that since registering a call back means you have to implement them before your main function.
And thats it! Thats how simple an OpenGL program is.
The next post presents a simple OpenGL program where a sine function is plotted on screen.

No comments:

Post a Comment