![]() |
OpenGL Tutorials for Absolute Beginners
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 OpenGLAs 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 :
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-devOpenGL ProgrammingAs 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);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 )
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,
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);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 ProgramHere is how the complete programs looks like: FileName : myRect.c Code:
#include<stdio.h>Compiling and RunningWhile compiling, you will need to link to the installed GL and glut libraries. Code:
gcc -Wall myRect.c -o myRect -lglut -lGLUConclusionNow 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 :) |
Re: OpenGL Tutorials for Absolute Beginners
What differences does occur when the platform is Windows?
I don't believe that there is be much of a difference for this tutorial. Why don't you explain it for both platforms by editing the Linux related parts like "For Linux, you do it this way. And for Windows, you do it this way..."? |
Re: OpenGL Tutorials for Absolute Beginners
Thanks AhmedHan for the suggestion.
Yes, using the Glut library for windowing, there won't be much difference for windows and linux except for the specific library installations. I'll take care to consider widely used platforms for my future articles. |
| All times are GMT +5.5. The time now is 09:29. |