C481 B581 Computer Graphics
Dana Vrajitoru

OpenGL

Portable library for fast, interactive, 2D, and 3D computer graphics.

Some advantages:

A collection of data types and functions, also called commands.

Components of OpenGL:


GLUT Introduction

Some GLUT functions

glutInit(&argc, argv) To be called in the beginning of the program.
glutCreateWindow(title) Creates the application window. All GL command will be displayed on this window.
glutDisplayFunc(function) To be called for every update type of event.
glutIdleFunc(function) Called repeatedly if the user doesn't input anything (animation).
glutMainLoop() To be called to launch the application (GUI).
glutMouseFunc(function) Callback function for any mouse event.


Geometrical commands

glBegin, glEnd - delimit the vertices of a primitive or of a group of similar primitives
void glBegin( GLenum mode );
void glEnd( void );

Between glBegin and glEnd, a list of vertex primitives, for example
void glVertex3f (GLfloat x, GLfloat y, GLfloat z );

Consider the list pi = glVertex(xi, yi, zi); 1 < i < n .

Geometrical mode:
GL_POINTS Each pi is drawn as a point.
GL_LINES Each (p2i, p2i+1) makes a line segment.
GL_LINE_STRIP Each (pi, pi+1) makes a line segment.
GL_LINE_LOOP Similar to line strip, plus (pn, p1) also makes a line segment.
GL_TRIANGLES Each (p3i, p3i+1, p3i+2) makes a triangle.
GL_TRIANGLE_STRIP Each (pi, pi+1, pi+2) makes a triangle.
GL_TRIANGLE_FAN Each (pi, pi+1), i>0, makes a triangle with p1.
GL_QUADS Each (p4i, p4i+1, p4i+2, p4i+3) makes a quadrilateral.
GL_QUAD_STRIP Each (p2i, p2i+1), (p2i+3, p2i+2) makes a line quadrilateral.
GL_POLYGON. A single polygon with n sides.

Color in OpenGL

Interpolating the Color

Display Lists


Matrix Mode in OpenGL
There are 3 matrices in OpenGL, Only one of them is active at any time.

All of the transformations are defined as matrices that are multiplied with the active matrix.
See page 68 from the textbook.

Matrix Operations

The matrix stack Example: Translate (obj1 Rotate (obj2) obj3) obj4
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(tx, ty, tz);
obj1;
glPushMatrix();
glRotatef(...);
obj2;
glPopMatrix();
obj3;
glPopMatrix();
obj4;


Case Study: the Spinning Cube

Programs: cube.c, cubeview.c from the textbook CD.
Goal: define a cube of  edge=2, with the center in the origin.

Camera location and orientation (textbook, page 234):
gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);

Changing the Point of View

parallel projection (cube.c) perspective projection (cubeview.c)
perspective, the close plane cuts the cube persective centered front view