What is OpenGL?
- What is OpenGL?
- What is GLUT?
- How does OpenGL work?
- How can I use GLUT?
- Which commands do I need to work with OpenGL?
- How can I draw a simple triangle with OpenGL?
- End of theory! Let's start.
OpenGL provides a set of commands to render a threedimensional scene. That means you provide the data in an OpenGL-useable form and OpenGL will show this data on the screen (render it). It is developed by many companies and it is free to use. You can develop OpenGL-applications without licencing.
OpenGL is a hardware- and system-independent interface. An OpenGL-application will work on every platform, as long as there is an installed implementation.
Because it is systemindependant, there are no functions to create windows etc., but there are helper functions for each platform. A very useful thing is GLUT.What is GLUT?
GLUT is a complete API written by Mark Kilgard which lets you create windows and handle the messages. It exists for several platforms, that means that a program which uses GLUT can be compiled on many platforms without (or at least with very few) changes in the code.How does OpenGL work?
OpenGL bases on the state variables. There are many values, for example the color, that remain after being specified. That means, you can specify a color once and draw several polygons, lines or what ever with this color then. There are no classes like in DirectX. However, it is logically structured. Before we come to the commands themselves, here is another thing:
To be hardware independent, OpenGL provides its own data types. They all begin with "GL". For example GLfloat, GLint and so on. There are also many symbolic constants, they all begin with "GL_", like GL_POINTS, GL_POLYGON. Finally the commands have the prefix "gl" like glVertex3f(). There is a utility library called GLU, here the prefixes are "GLU_" and "glu". GLUT commands begin with "glut", it is the same for every library. You want to know which libraries coexist with the ones called before? There are libraries for every system, Windows has the wgl*-Functions, Unix systems glx* and so on.
A very important thing is to know, that there are two important matrices, which affect the transformation from the 3d-world to the 2d-screen: The projection matrix and the modelview matrix. The projection matrix contains information, how a vertex let's say a "point" in space shall be mapped to the screen. This contains, whether the projection shall be isometric or from a perspective, how wide the field of view is and so on. Into the other matrix you put information, how the objects are moved, where the viewer is and so on.
Don't like matrices? Don't be afraid, you probably won't really see them, at least at the beginning. There are commands that do all the maths for you.
Some basic commands are explained later in this tutorial.How can I use GLUT?
GLUT provides some routines for the initialization and creating the window (or fullscreen mode, if you want to). Those functions are called first in a GLUT application:
In your first line you always write glutInit(&argc, argv). After this, you must tell GLUT, which display mode you want single or double buffering, color index mode or RGB and so on. This is done by calling glutInitDisplayMode(). The symbolic constants are connected by a logical OR, so you could use glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE). In later tutorials we will use some more constants here.
After the initialization you call glCreateWindow() with the window name as parameter.
Then you can (and should) pass some methods for certain events. The most important ones are "reshape" and "display". In reshape you need to (re)define the field of view and specify a new area in the window, where OpenGL is allowed to draw to.
Display should clear the so called color buffer let's say this is the sheet of paper and draw our objects.
You pass the methods by glut*Func(), for example glutDisplayFunc(). At the end of the main function you call glutMainLoop(). This function doesn't return, but calls the several functions passed by glut*Func.Which commands do I need to work with OpenGL?
Here I'll explain the commands you need in "reshape" and at the beginning of "display".
As mentioned before, there are two (ok, in reality there are three, but let's make it easier) matrices. Which one is current is controlled by "glMatrixMode()". You can call it either with GL_PROJECTION or GL_MODELVIEW. In Reshape you specify a new projection matrix. After turning it current, you load an identity matrix into it, that means "deleting" the former projection matrix. Now you can apply a new field of view by using a glu* command: gluPerspective. You pass the y-angle of view, the aspect ratio (the window's witdth / height), and a near and far clipping distance. The objects or parts of objects which are not between those distances will not be visible. Careful: Don't take a too small near clipping distance, you could get wrong-looking results! After this, you make the modelview matrix currrent and also load an identity matrix.
Display has to clear the color buffer. Therefore you call
After the initialization of GLUT, you should have defined a clear color using glClearColor(). It takes four arguments, red, green and blue and an alpha value. Set alpha = 0.0.
To get clear red, you would call glClearColor(1.0,0.0,0.0,0.0);
Drawing the object is explained in the next section.
Last but not least you define the "viewport", the area on the window where OpenGL shall paint to.
Therefore you use glViewport(0,0,x,y); with x and y as the size of your window: You get them from the reshape parameters. Here is how to define the function headers of the two routines:
void Reshape ( int x, int y );
void Display ( void );How can I draw a simple triangle with OpenGL? Having cleared the colorbuffer, you can pass some data to OpenGL. It is very simple: You specify some vertices, which are used in a certain way. The way is defined by glBegin(). For now let's use GL_POLYGON as parameter which tells OpenGL to use the following vertices as corners of one polygon.
After passing the data to OpenGL, you have to call glEnd(). This method doesn't take any arguments.
To provide a vertex , you can call one of some glVertex*() routines. * stands for a combination of the number of arguments (two-dimensional, three-dimensional or four-dimensional) + the data type (f for float, d for double and i for int).
To specify a 3d vertex with integer coordinates, you call glVertex3i().
To finish rendering, you must call glFlush() at the end of your display-function.
That's all we need. Just call glVertex*() three times between glBegin and glEnd.
To change the current color in RGB(A)-mode, you call glColor*(). Again * stands for the number of arguments (three for RGB and four for RGBA)+ the data type. If you take a floating point type as parameters, note that 1.0 is full intensity for the color component. Also note, that colors are blended, when they are different for one or more vertices in the polygon.
One last thing: If you want to add some vertices to our polygon, you must note that OpenGL cannot render concave polygons (for performance reasons). Then you have to tesselate it, ie make some smaller ones from it (add a new glBegin / glEnd pair).End of theory! Let's start. Assuming you're using Visual C++ you first have to create a console application. The only required include file is "GL\glut.h". It includes "GL\gl.h" and "GL\glu.h", so we needn't do so. If you don't have GLUT (you need the headers and the dll), download it here. You now can write your first OpenGL application!
Have a look at my tutorials to see the result.
Any comments? Conact me!