COMP 321 ODELNotes OpenGL
COMP 321 ODELNotes OpenGL
5. I N T R O D U C T I O N T O O P E N G L P R O G R A M M I N G
Components of Open GL
There are three main components of Open GL, i.e.
(ii) GLU
This is the is the OpenGL Graphics System Utility library, a utilities library
that compliments OpenGL by providing support for mipmapping, matrix
manipulation, polygon tessellation, quadrics (e.g. circles, spheres, cylinders),
NURBS, and error handling. It is defined in the header file glu.h
(iii) GLUT
This is the interface for GLUT (OpenGL Utility Toolkit), another utilities
library that provides a platform-independent user interface. It is defined in
the header file glut.h
Header files
An Open GL program typically includes the windows.h (if using a Windows
platform), gl.h and glut.h header files.
main() function
Every C program contains main() which is responsible for:
1. Initializing OpenGL, GLU and GLUT;
2. Creating a window using GLUT;
3. Initializing the data structures needed to output the scene;
4. Notifying GLUT which functions will handle window events;
5. Notifying GLUT to wait for events.
void init(void);
void display(void);
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
3
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-5.0, 5.0, 5.0, -5.0);
glutSwapBuffers();
}
/**********************************************************************/
Discussion
We first included the header files windows.h, gl.h and glut.h, and declared
prototypes for user-defined functions init and display.
In OpenGL, matrices are used to manage the view. There are two matrix modes,
projection and modelview. Projection is used to set up the viewport and
clipping boundry, while modelview is used to rotate, translate and scale
objects quickly.
The statement glLoadIdentity(); loads the identity matrix into the current matrix
state (in this case the projection matrix). You can consider this the resetting
matrix...it resets everything back to zero. Next comes the call to glOrtho.
This function sets up a clipping volume. You can think of a clipping volume as
a box in which your drawing commands are rendered. As the viewer, we are
positioned outside the box, looking in the front. What we see is whatever
is inside the box, projected onto the flat surface that is the side. Anything outside
the box is invisible.
The statement glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0); function creates an
orthographic view i.e. one with no perspective. We'll get to perspective drawing
later in the tutorial. The prototype of glOrtho is
void glOrtho(double left, double right, double bottom, double top, double
near, double far);
Next, we bring up something about OpenGL i.e. function calls -- they often come
in different forms. For example, take glColor, which sets the current foreground
color. This function has several prototypes, for example: glColor3f(float, float,
float); glColor4f(float, float, float, float); glColor3d(double, double, double);
glColor3i(int, int, int); etc.
As you can see, OpenGL functions are usually formatted as: gl Color
3 f
However, even though most OpenGL functions can accept any type, it's usually
best to pass floats. OpenGL uses floating point values for all its internal
calculations, so passing any other type is a waste of time since OGL will
just convert it back to floating point anyway.
The next function i.e. glutSwapBuffers() swaps the back buffer to the screen
buffer. In double-buffered mode, the drawing commands do not draw to the
screen. Rather, they draw to an offscreen buffer. Once you are done drawing,
you copy the entire back buffer to the screen at once, thus producing smooth
animation. Of course, in this simple example there is no animation, but
without a double buffer even moving the window around the screen will cause
the rectangle to flicker. Besides, it's good to get into the habit of producing
smooth graphics!
Got the hang of it? Then lets plot some points with OpenGL. We can use the
function glVertex to specifiy vertices, flanked by calls to glBegin and glEnd:
6
glEnd();
Note that we now have one line for every two vertices. If you specify an odd
number, the last vertex is ignored.
glEnd();
See? Nothing to it!
Now, lets take what we've learned so far and draw some 3D using OpenGL. As
7
you can see, the following program bears more than a passing resemblence to
the previous one (GLUT is so nice that way), with some changes to the
display() function. One thing you will notice is that I change the current
color with glColor before specifiying some of the vertices. When OpenGL sees
a polygon with vertices that have different colors, it draws the figure by
smoothly shading from one color to the next. In this example I've created an
abstract shape made out of one square surrounded by four triangles. One
point of the triangles is red, while the other two are blue. This creates a
smoothing purple effect across the face of the triangle.
I'm also using this example to demonstrate some of the UI routines that GLUT
uses. In this case we are going to be using the function glutKeyboardFunc().
This function defines a callback handler that will be called whenever a key
is pressed while our window has focus.
/**********************************************************************/
/******************************************/
/* Example 2: Drawing in 3D */
/******************************************/
#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void init(void)
{
8
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 1.0); /* center square */
glVertex3f(-3.0, -3.0, 0.0);
glVertex3f(3.0, -3.0, 0.0);
glVertex3f(3.0, 3.0, 0.0);
glVertex3f(-3.0, 3.0, 0.0);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); /* now draw the four triangles */
glVertex3f(0.0, 6.5, 0.0);
glColor3f(0.0, 0.0, 0.9f);
glVertex3f(-3.0, 3.0, 0.0);
glVertex3f(3.0, 3.0, 0.0);
Note that I use the glRotate function to rotate the view when the u, d, l, or
r keys are pressed; don't worry, we haven't gone over this yet. I'll cover it
in the next section, for now I put it in to illustrate the 3D nature of our
shape. You may want to play around with this demo before continuing on to the
next section. What else can you draw? What happens to the shading in the demo
if you call glShadeModel(GL_FLAT)? Once you feel comfortable working with
3D
space, we can move on to the next section...