0% found this document useful (0 votes)
65 views9 pages

COMP 321 ODELNotes OpenGL

OpenGL is a library for rendering 2D and 3D graphics. It consists of three main components - OpenGL (gl), GLU, and GLUT. OpenGL provides low-level graphics functions and is hardware independent. Programs using OpenGL typically include header files, contain a main function to initialize OpenGL and create a window, and define display and initialization functions. Common OpenGL primitives include points, lines, polygons, and triangles which are used to build up complex 3D scenes.

Uploaded by

Owen Clevers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views9 pages

COMP 321 ODELNotes OpenGL

OpenGL is a library for rendering 2D and 3D graphics. It consists of three main components - OpenGL (gl), GLU, and GLUT. OpenGL provides low-level graphics functions and is hardware independent. Programs using OpenGL typically include header files, contain a main function to initialize OpenGL and create a window, and define display and initialization functions. Common OpenGL primitives include points, lines, polygons, and triangles which are used to build up complex 3D scenes.

Uploaded by

Owen Clevers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

1

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

5.1: OVERVIEW OF OPEN GL

What is Open GL?


OpenGL is short for Open Graphics Library. It is the interface between a
graphics’ program and the graphics hardware. It is realized as aset of inbuilt
functions (or library of code) in C programming language.

Components of Open GL
There are three main components of Open GL, i.e.

(i) Gl (or just Open gl)


It’s the main components of Open GL that interfaces the other two
components. It’s defined in the header file gl.h

(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

Key characteristics of Open GL


 It is streamlined. In other words, it provides low-level functionality. For
example, all objects are built from points, lines and convex polygons. Higher
level objects like cubes are implemented as six four-sided polygons.
 It is system-independent. It does not assume anything about hardware or
operating system and is only concerned with efficiently rendering
mathematically described scenes. As a result, it does not provide any
windowing capabilities.
 It is a state machine. At any moment during the execution of a program there
is a current model transformation, a current color, a current light type and
position, etc. Consequently, when an object is drawn, its color is determined
by the current color and light and its final projection onto the screen is
determined by the current transformation.
 It is a rendering pipeline. The rendering pipeline consists of the following
steps:
i. Defines objects mathematically.
ii. Arranges objects in space relative to a viewpoint.
2

iii. Calculates the color of the objects.


iv. Rasterizes the objects.
OpenGL supports features like 3-dimensions, lighting, anti-aliasing, shadows,
textures, depth effects, etc.

5.2: THE PROGRAM STRUCTURE

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.

Example 1: A program that creates a window and draws a rectangle in it


#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>

void init(void);
void display(void);

int main (int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("My First OpenGL Program");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
3

glColor3f(0.0, 0.0, 1.0);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
}

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.

Next, we called the glutInit function as glutInit(&argc, argv);, which provides


general initialization of the GLUT library using command-line parameters from the
operating system (i.e. int main (int argc, char **argv)).

Next we initialize the display mode as


glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
This tells OpenGL that we want a double buffered window, with RGB color.

The next few lines create the window:


glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("My First OpenGL Application");
Note the window’s size, position on the screen and title (caption).

Next, we initialize OpenGL, i.e.


void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0); /* set the background
(clearing) color to
RGB(0,0,0) -- black */
glColor3f(0.0, 0.0, 1.0); /* set the foreground color
to blue */
glMatrixMode(GL_PROJECTION); /* Initialize the
matrix
state */
glLoadIdentity();
4

glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);


}

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);

Now, we look at lines


glutDisplayFunc(display);
glutMainLoop();
The first function sets the function that GLUT will use whenever it needs to
update the view. We then call glutMainLoop() which actually runs the program.
From this point on our work is done; GLUT will handle the details of managing
the window and calling our painting function to display it.

Here is the display function again:


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-5.0, 5.0, 5.0, -5.0);
glutSwapBuffers();
}

The first thing we do is call glClear with GL_COLOR_BUFFER_BIT parameter.


This will clear the window with the color we specified earlier using
glClearColor. Next, we actually draw our rectangle, using the glRectf
function.
5

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!

5.3: OUTPUT PRIMITIVES

Before we start drawing 3D objects, let’s quickly go over some fundamentals.


You will find drawing to a 3D canvas is in fact very similar to plain old 2D
applications. This is because OpenGL uses a coodinate system called the
Cartesian plane. The difference here is that instead of having two axis which
stretch horizontally and vertically, we add a third axis, the z (or depth)
axis. You can think of this as a line which runs through the origin (0,0 on
a Cartesian coordinate system), going in the direction from away from you to
straight towards you (in OpenGL, the positive z-axis always points towards
you, while the negative points away). Using this system, we can represent a
point in 3D space, called a "vertex", with three coordinites, representing x,
y, and z. For example:

(0,0,0) <-- The origin, the center of our defined space.


(2,0,4) <-- 2 units to the right, 4 units towards us, on the center of the
y-axis.
(3,-4,-2) <-- 3 units to the right, 4 units down, and 2 units away from us.

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

glBegin(GL_POINTS); /* We want to draw points */


glVertex3f(2.0, 0.0, 4.0); /* Specify a couple of vertices */
glVertex3f(3.0, -4.0, -2.0);
glEnd(); /* End points */
As you can see, glBegin tells OpenGL we want to start drawing (as well as
WHAT
we want to start drawing), and glEnd tells it to stop. The great thing about
OGL's method of 3D drawing is it's flexibility -- lets say we want to draw some
lines:
glBegin(GL_LINES); /* lets do some lines now */

glVertex3f(6.0, 4.0, 2.0); /* here's one */


glVertex3f(2.0, -4.0, 3.3);

glVertex3f(5.0, 8.0, 8.0); /* here's another */


glVertex3f(-4.7, 5.0, -3.0);

glVertex3f(0.0, 0.0, 0.0); /* and another! */


glVertex3f(6.0, -1.0, -7.0);

glEnd();

Note that we now have one line for every two vertices. If you specify an odd
number, the last vertex is ignored.

Now lets do some shapes. OpenGL specifies 6 different polygon primitives:


GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS,
GL_QUAD_STRIP, and
GL_POLYGON. Triangle and quad strips are shortcuts for building polygons
next to each other, and likewise a triangle fan is a group of triangles that share
a center point. GL_POLYGON can specify a general polygon with any number
of vertices. The one you should use most often is GL_TRIANGLES, since most
graphic accelerators are optimized for triangle operations. Here's an
example of a generic triangle:
glBegin(GL_TRIANGLES);

glVertex3f(-3.0, -3.0, 2.0);


glVertex3f(3.0, -3.0, -1.0);
glVertex3f(0.0, 3.0, 4.0);

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);

int main (int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(50, 50);
glutCreateWindow("A 3D Object");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard); /* set keyboard handler */
glutMainLoop();
return 0;
}

void init(void)
{
8

glClearColor(0.0, 0.0, 0.0, 0.0);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15.0, 15.0, -15.0, 15.0, -15.0, 15.0);
}

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);

glColor3f(0.0, 0.0, 0.9f);


glVertex3f(-3.0, -3.0, 0.0);
glVertex3f(-3.0, 3.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-6.5, 0.0, 0.0);

glColor3f(1.0, 0.0, 0.0);


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);

glColor3f(1.0, 0.0, 0.0);


glVertex3f(6.5, 0.0, 0.0);
glColor3f(0.0, 0.0, 0.9f);
glVertex3f(3.0, 3.0, 0.0);
glVertex3f(3.0, -3.0, 0.0);
glEnd();
glutSwapBuffers();
}
9

void keyboard(unsigned char key, int x, int y)


{
/* this is the keyboard event handler
the x and y parameters are the mouse
coordintes when the key was struck */
switch (key)
{
case 'u':
case 'U':
glRotatef(3.0, 1.0, 0.0, 0.0); /* rotate up */
break;
case 'd':
case 'D':
glRotatef(-3.0, 1.0, 0.0, 0.0); /* rotate down */
break;
case 'l':
case 'L':
glRotatef(3.0, 0.0, 1.0, 0.0); /* rotate left */
break;
case 'r':
case 'R':
glRotatef(-3.0, 0.0, 1.0, 0.0); /* rotate right */
}
display(); /* repaint the window */
}
/**********************************************************************/

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...

You might also like