Hello World: Opengl Course, Part 1
Hello World: Opengl Course, Part 1
PROGRAMMING OPENGL
HELLO 3D
WORLD THOMAS G E RUGE
After a broad overview and a little bit of theory, the breathed life into Mesa, and the library has since
first part of the course shows how OpenGL been further developed by him and many other
programs are constructed, using Hello World as an developers all over the world.
example.
OpenGL was originally developed by SGI and in Principles
ages long past was called IrisGL. The primary aim of
developing IrisGL was to develop a programmable OpenGL is always in a defined state, which is set by
interface for the graphics workstations of SGI. This condition variables; this means that OpenGL is a
programmable interface was initially intended to be State Machine. An example of a condition variable
hardware-independent, future-proof and to meet is the current colour with which the individual
the special demands of 3D graphics programming. primitives are rendered (drawn). These are set using
It soon emerged that IrisGL is not only of the command glColor3f(red, green, blue) and this
interest for SGI workstations, but can also help then applies for all drawn objects until a second
other workstations to get up and running in terms glColor3f(..) command changes the colour.
of graphics. The OpenGL Architectural Review All objects under OpenGL must be composed
Board (ARB) was founded in 1992 by major from 10 different primitives. The instruction set in
manufacturers of graphics workstations such as SGI, the library comprises some 200 commands, all
Sun, Hewlett-Packard and others. The task of the starting with gl. OpenGL does in fact contain all the
ARB is to guide the further development of OpenGL simple operations for programming interactive 3D
and introduce new functionalities in later versions. graphics, but it cannot do real 3D bodies such as
OpenGL has now become the commonest cylinders and dice, only points, lines and polygons.
hardware-independent interface for 3D All complex bodies have to be constructed by
programming. the 3D developer out of simple primitives. To make
The latest version is OpenGL 1.2. There are the work a bit simpler for the applications
OpenGL implementations for Irix, Solaris, HP-UX, developer, hard-working 3D experts have developed
Linux, Windows and a few other operating systems. some programming libraries, a few of which we
In the Linux world the OpenGL clone Mesa is the shall be introducing in the course of this series of
most common one. The American Brian Paul articles. The two most important expansions are
OPENGL PROGRAMMING
3D transformations
The elementary transformations in 3D graphics Rotations about the three axes of the co-ordinate grid by
programming are translation, rotation and scaling of points the angle a:
in 3D space. So that these operations are shown in a
standard format and can be programmed easily, cos α 0 − sin α 0
1 0 0 0
transformations take the general form: b = M • a. So as to 0 1 0 0
0 cos α sin α 0 R( e y , α ) =
display not only rotations or scalings, but also translations R( e x , α ) = sin α 0 cos α 0
0 − sin α cos α 0
in this linear form, a homogenous co-ordinate xw is 0 0 0 1
0 0 0 1
inserted into the space of the transformations. In
mathematical jargon, we have embedded the affine space
cos α sin α 0 0
R3 in the projective space P(R4) and can thereby display any
− sin α cos α 0 0
affine figure (transformation) through a matrix R( e z , α ) =
0 0 1 0
multiplication.
0 0 0 1
ax
a
a= y Rotations about any axis can be assembled from rotations
az
about the co-ordinate axes:
aw
bx
b R(p , α ) = R( e x , α ) ⋅ R( e y , α ) ⋅ R( e z , α )
b= y
bz
bw
t xx t xy t xz t xw
t yx t yy t yz t yw
M=
t zx t zy t zz t zw Rotation about any point
t wx t wy t wz t ww
The rotation matrices described above define rotations
The three transformations referred to above then look like about the origin, but often an object needs to be rotated
this: about a different point u. To do this, we link together
translations and rotation in such a way that the point about
Translations which the rotation takes place serves as origin. This means
that we make a change to the co-ordinate system:
We can displace objects in space at will with translations
Scalings
Scalings make objects bigger and smaller
PROGRAMMING OPENGL
#include /GL/glut.h>
void DrawScene(void)
{
//set background colour (dark red)
glClearColor (0.5, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
return EXIT_SUCCESS;
}
PROGRAMMING OPENGL
Listing 2:Teapot.c
#include <stdio.h> {
#include <stdlib.h> glLoadIdentity();
glTranslatef(posx, posy, posz);
#include <GL/glut.h> glRotatef(xangle, 1.0, 0.0, 0.0);
glRotatef(yangle, 0.0, 1.0, 0.0);
// Condition variable }
// mouse movement
int mousemotion; void DrawScene(void)
int mousex, mousey; {
// Delete buffers
// Initialise model orientation glClearColor (0.5, 0.0, 0.0, 0.0);
GLfloat xangle = 4; /* for rotation */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat yangle = 120;
//Model position // Calculation of new model coordinates
GLfloat posx = 0, posy = 0, posz = 0; recalcModelPos();
// Teapot painting
//Callback Function: Reaction of mouse clicks glutSolidTeapot(0.6);
void mouse(int button, int state, int x, int y)
{ // Foreground and background buffer change
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { glutSwapBuffers();
mousemotion = 1; }
mousex = x;
mousey = y; //Initialising function
} void myinit()
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { {
mousemotion = 0; GLfloat light_position[] = {0.0, 0.0, -1.0, 1.0 };
}
} //First GL-Light fall
} glutMainLoop();
}
//new Model position calculation return EXIT_SUCCESS;
void recalcModelPos(void) }
OPENGL PROGRAMMING
complete, we swap foreground and background could also be omitted. These displacements will also
buffers, so the image just drawn can be seen. be discussed later on in this series of articles.
The depth or z-buffer ensures that areas With glRotatef(xangle, 1.0, 0.0, 0.0) and
covered by other areas cannot be seen. In the z- glRotatef(yangle, 0.0, 1.0, 0.0) we multiply the
buffer each pixel in the OpenGL window is assigned matrix again with rotations about the x and the y-
a z-value. If a new pixel is to be set during axis. This matrix is then used later in DrawScene(). In
rendering, a check is made as to whether its z-value this function, the buffer must first be cleared, as
is greater than that of a pixel that has already been well as the z-buffer, so as not to receive any
set. If so, the new pixel comes before the old one incorrect depth information. Next, with
and can be drawn, otherwise not. recalcModelPos() the matrix is calculated with the
If all parameters have been set and if the new model co-ordinates. This matrix is then to be
OpenGL window has been created, an initialisation multiplied by all points of the teapot.
function myinit() is called up. This function was The teapot is created, as the result of the
introduced for better legibility of the program. instruction glutSolidTeapot(0.6), with a size 0.6.
The next step is to set the diverse callback There is nothing behind this command but the
functions. So glutMouseFunc(mouse) sets the placing of polygons, similar to the way we called up The author
function to be called up when pressing or releasing the arrow in the first program with glVertex2f. Thomas G E Ruge has been a
a mouse button. glutMotionFunc(motion) sets the GLUT has more fixed, pre-defined objects. If you’d converted Linux fan since
callback function for mouse movements. So that like to experiment with other objects, try out Kernel 0.98 and is interested in
the program also responds to keyboard events, we glutWireTorus(..) or glutSolidDodecahedron(..). everything to do with 3D or
also call up glutKeyboardFunc(keyb). The rest of The teapot was drawn in the invisible virtual reality. He has been
main() is equivalent to HelloOpenGL. background buffer and appears with the command driving forward the technical
glutSwapBuffers() in the foreground. In the construction of the Virtual
Callback functions in detail initialisation function myinit() we create, using Reality Laboratory in the
glLightfv(GL_ LIGHT0, GL_POSITION, research department of
In void mouse(int button, int state, int x, int y) the light_position), a light at the position (0, 0, -1) and Siemens AG for nearly six years
position of the mouse in the OpenGL window is switch it on using glEnable(GL_ LIGHT0). and is involved in the
queried, buffered in x and y and registers whether the The last two commands set the z-buffer mode. possibilities of high-end VR
left mouse button was pressed. The function makes a glDepthFunc(GL_LEQUAL) draws only pixels whose with Linux clusters.
note with mousemotion of the condition of the left z-value is less than that of the pixel already drawn.
mouse button and takes it into account later in the glEnable(GL_DEPTH_TEST) activates the z-buffer
function motion(int x, int y). The function motion(..) calculation. Finally, the compilation:
calculates, from the current mouse position (x,y) and
gcc -I . -c Teapot.c
the mouse position at the time when the left mouse
gcc -o Teapot Teapot.o -lGL \
button has been pressed (mousex, mousey), the angle -lGL -lglut -lGLU
about which the object should be rotated (xangle,
yangle). The call up of glutPostRedisplay() ensures that If everything fits, when you call up teapot, as in
the scene is actually redrawn and by calling up the picture, a white teapot should appear, which
DrawScene() the new position of the teapot is responds to the input of the letters l and o and to
calculated in recalcModelPos(). mouse actions.
The callback function keyb(unsigned char
keyPressed, int x, int y) responds to the two letters l What’s next?
and o. When the user enters l the light calculation is
activated with glEnable(GL_LIGHTING), o That’s the end of our journey into the realms of
deactivates the light again with the aid of OpenGL and GLUT. We have looked at the options
glDisable(GL_LIGHTING). These three callback for responses to the various inputs by the user and
functions cover all events that the program has to conjuring and moving polygons on the screen. The
process. next installment will be about the primitives of
The next function recalcModelPos() is called up OpenGL, how the world can become a bit more
by DrawScene() to calculate the new position of the colourful and how edges can be smoothed with the
teapot. Generally every transformation (rotation, aid of normal vectors. ■
displacement, scaling) can be described using four-
dimensional matrix-vector multiplications. This is Info
explained in more detail in the 3D transformations
boxout. OpenGL uses the matrix stack for such Woo, Neider, Davis, Shreiner: Open GL Programming Guide 3rd ed., Verlag
multiplications. Addison Wesley Longman Inc.,1999
In recalcModelPos() with glLoadIdentity() the J. Encarnacao : Graphische DV 1, Oldenbourg Verlag GmbH
identity matrix is loaded onto the stack. glTranslatef OpenGL homepage: http://www.opengl.org
(posx, posy, posz) multiplies this matrix by a matrix Mesa homepage: http://www.mesa3d.org
which displaces an object by the co-ordinates posx,
posy, posz. But since they all contain 0 this step ■