OpenGL

Overview

Open GL is an API for 3-D Graphics. In other words it has hundreds of functions you call in your 3-D applications.

OpenGL is only concerned with drawing, not with windows or any user interface concerns. Basically you have to get an O.S window, and in the window's device context, set up a pixel format and create a rendering context. Then you can use OpenGL functions to draw in that window.

Since this is a real pain, you can obtain a quick-and-dirty, portable windowing layer called GLUT that lets you effortlessly create windows that support OpenGL drawing (It's similar to the way Java's AWT is portable). With GLUT you get keyboard, mouse and spaceball interaction, but no "widget" support.

OpenGL drawing is done un a 3-D abstract co-ordinate system which means you are NOT concerned with pixel coordinates. The coordinate system is (by default) right handed.

OpenGL consists of two parts

GL for basic drawing

-AND-

GLU for advanced utilities and "higher level" routines

and again, GLUT is a seperate product

Physical Components

OpenGL is O.S and platform-independent. You can use it on Unix, Windows, etc. It is also language independent; you can get bindings for C, Ada etc. These notes only describe C++ programming with GLUT. You can use either the Microsoft C/C++/C# compiler (for Windows only) or the GNU compiler (for Windows OR Unix). Typical configurations are

GNU Compiler under Linux(RedHat 6.2)
/usr/X11R6/include/GL /usr/X11R6/lib/ /usr/X11R6/lib/
GL gl.h libGL.la libGL.so
GLU glu.h libGLU.la libGLU.so
GLUT glut.h libglut.la libglut.so
GNU Compiler under Windows
/gcc/include/gl/ /gcc/lib/gcc-lib %WINSYS%/
GL gl.h libopengl32.a opengl32.dll
GLU glu.h libglu32.a glu32.dll
GLUT glut.h libglut32.a glut32.dll
Microsoft Compiler under Windows
  %vc%/include/ %vc%/lib/ %WINSYS%/
GL gl.h opengl32.lib opengl32.dll
GLU glu.h glu32.lib glu32.dll
GLUT glut.h glut32.lib glut32.dll

Writing Code

Compiling/Linking:

Since glut.h includes glu.h and gl.h (and ultimately windows.h or xlib.h). Simply begin the code with:

    #include <GL/glut.h>

With gcc you (Win32 or Unix) you'll probably want to write makefiles, the typical command to compile and link under Unix is:

    gcc -I/usr/X11R6/include -L/usr/X11R6/bin -o myprogram myprogram.c -lglut lGLU -lGL

Better to configure an IDE to automatically use these options though.

With Microsoft compiler/linker use the following linker settings:

    /entry:mainCRTStartup /subsystem:windows

to avoid annoying console windows popping up.

Basic structure of a simple program using GLUT

    int main(int argc, char** argv){

	glutInit(&argc,argv);
	glutInitDisplayMode(___|____|____); --> GLUT_SINGLE
						GLUT_DOUBLE
						GLUT_RGB
						GLUT_RGBA
						GLUT_INDEX
						GLUT_DEPTH
						GLUT_ACCUM
						GLUT_STENCIL
						GLUT_MULTISAMPLE
						GLUT_STEREO

	glutInit WindowPosition(___, ____);
	glutInit WindowSize(___, ____)
	glutCreate Window("Title of Window");
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutIdleFunc(idle);
          | Entry	|
	  | Mouse	|
	  | Motion	|
	  | Timer	|
	  | .      	|
	  | .           |
	myInit();
	glutMainLoop();
    }

NOTE: Only display() is required by glut. The others are optional.

Basic Drawing

    glBegin(______________); //(see chart below)
        glVertex ___ ___ ___ (           );
        ....
        ....
        ....
    glEnd();

NOTE: In general you should end drawing with glFlush();

opengl1.gif

Not all OpenGL commands can go between glBegin and glEnd. The ones that can, include glVertex glColor, glIndex, glNormal, glEvalCoord, glCalllot, glTextCord, glEdgeFlag, glMaterial...etc.

Because OpenGL is a C API, there are many variations to function names.

        glVertex___ ___ ___
		 |   |   |
		 2   b	 v
		 3   s   or nothing
		 4   i
		     f
		     d

OpenGL drawing is really all about putting glVertex calls inside glBegin and glEnd. Of course there are functions in GLU and GLUT which generate complex shapes for you; these are implemented internally with glVertex calls. For realistic drawings you need to also set up cameras, lights, textures, etc.

Viewing

opengl2.gif

What you normally do

  1. Set up camera with gluPerspective when the matrix mode is GL_PROJECTION. This is typically done once at the beginning and everytime the window is resized. (You might also call glViewport at resize time also).
  2. Point the camera with gluLookAt when the matrix mode is GL_MODELVIEW. Do this at each animation frame.