Getting Started with OpenGL

Let's see what OpenGL is, what it does and doesn't do, and how to get started writing code.

Overview

Open GL is a state-based, procedural API for 3-D Graphics. In has hundreds of functions you call in your 3-D applications. It is a drawing-only library; it does not deal with windows, menus, input devices, or any kind of interaction.

OpenGL consists of two parts: GL, the core library, and GLU, which has a bunch of convenience functions. Here are just some of them:

GL
glAccum glActiveTexture glAlphaFunc glAreTexturesResident glArrayElement glAttachShader glBegin glBeginQuery glBindAttribLocation glBindBuffer glBindTexture glBitmap glBlendColor glBlendEquation glBlendEquationSeparate glBlendFunc glBlendFuncSeparate glBufferData glBufferSubData glCallList glCallLists glClear glClearAccum glClearColor glClearDepth glClearIndex glClearStencil glClientActiveTexture glClipPlane glColor glColorMask glColorMaterial glColorPointer glColorSubTable glColorTable glColorTableParameter glCompileShader glCompressedTexImage1D glCompressedTexImage2D glCompressedTexImage3D glCompressedTexSubImage1D glCompressedTexSubImage2D glCompressedTexSubImage3D glConvolutionFilter1D glConvolutionFilter2D glConvolutionParameter glCopyColorSubTable glCopyColorTable glCopyConvolutionFilter1D glCopyConvolutionFilter2D glCopyPixels glCopyTexImage1D glCopyTexImage2D glCopyTexSubImage1D glCopyTexSubImage2D glCopyTexSubImage3D glCreateProgram glCreateShader glCullFace glDeleteBuffers glDeleteLists glDeleteProgram glDeleteQueries glDeleteShader glDeleteTextures glDepthFunc glDepthMask glDepthRange glDetachShader glDisable glDisableClientState glDisableVertexAttribArray glDrawArrays glDrawBuffer glDrawBuffers glDrawElements glDrawPixels glDrawRangeElements glEdgeFlag glEdgeFlagPointer glEnable glEnableClientState glEnableVertexAttribArray glEnd glEndList glEndQuery glEvalCoord glEvalMesh glEvalPoint glFeedbackBuffer glFinish glFlush glFog glFogCoord glFogCoordPointer glFrontFace glFrustum glGenBuffers glGenLists glGenQueries glGenTextures glGet glGetActiveAttrib glGetActiveUniform glGetAttachedShaders glGetAttribLocation glGetBufferParameteriv glGetBufferPointerv glGetBufferSubData glGetClipPlane glGetColorTable glGetColorTableParameter glGetCompressedTexImage glGetConvolutionFilter glGetConvolutionParameter glGetError glGetHistogram glGetHistogramParameter glGetLight glGetMap glGetMaterial glGetMinmax glGetMinmaxParameter glGetPixelMap glGetPointerv glGetPolygonStipple glGetProgram glGetProgramInfoLog glGetQueryObject glGetQueryiv glGetSeparableFilter glGetShader glGetShaderInfoLog glGetShaderSource glGetString glGetTexEnv glGetTexGen glGetTexImage glGetTexLevelParameter glGetTexParameter glGetUniform glGetUniformLocation glGetVertexAttrib glGetVertexAttribPointerv glHint glHistogram glIndex glIndexMask glIndexPointer glInitNames glInterleavedArrays glIsBuffer glIsEnabled glIsList glIsProgram glIsQuery glIsShader glIsTexture glLight glLightModel glLineStipple glLineWidth glLinkProgram glListBase glLoadIdentity glLoadMatrix glLoadName glLoadTransposeMatrix glLogicOp glMap1 glMap2 glMapBuffer glMapGrid glMaterial glMatrixMode glMinmax glMultMatrix glMultTransposeMatrix glMultiDrawArrays glMultiDrawElements glMultiTexCoord glNewList glNormal glNormalPointer glOrtho glPassThrough glPixelMap glPixelStore glPixelTransfer glPixelZoom glPointParameter glPointSize glPolygonMode glPolygonOffset glPolygonStipple glPopAttrib glPopClientAttrib glPopMatrix glPopName glPrioritizeTextures glPushAttrib glPushClientAttrib glPushMatrix glPushName glRasterPos glReadBuffer glReadPixels glRect glRenderMode glResetHistogram glResetMinmax glRotate glSampleCoverage glScale glScissor glSecondaryColor glSecondaryColorPointer glSelectBuffer glSeparableFilter2D glShadeModel glShaderSource glStencilFunc glStencilFuncSeparate glStencilMask glStencilMaskSeparate glStencilOp glStencilOpSeparate glTexCoord glTexCoordPointer glTexEnv glTexGen glTexImage1D glTexImage2D glTexImage3D glTexParameter glTexSubImage1D glTexSubImage2D glTexSubImage3D glTranslate glUniform glUnmapBuffer glUseProgram glValidateProgram glVertex glVertexAttrib glVertexAttribPointer glVertexPointer glViewport glWindowPos
GLU
gluBeginCurve gluBeginPolygon gluBeginSurface gluBeginTrim gluBuild1DMipmapLevels gluBuild1DMipmaps gluBuild2DMipmapLevels gluBuild2DMipmaps gluBuild3DMipmapLevels gluBuild3DMipmaps gluCheckExtension gluCylinder gluDeleteNurbsRenderer gluDeleteQuadric gluDeleteTess gluDisk gluEndCurve gluEndPolygon gluEndSurface gluEndTrim gluErrorString gluGetNurbsProperty gluGetString gluGetTessProperty gluLoadSamplingMatrices gluLookAt gluNewNurbsRenderer gluNewQuadric gluNewTess gluNextContour gluNurbsCallback gluNurbsCallbackData gluNurbsCallbackDataEXT gluNurbsCurve gluNurbsProperty gluNurbsSurface gluOrtho2D gluPartialDisk gluPerspective gluPickMatrix gluProject gluPwlCurve gluQuadricCallback gluQuadricDrawStyle gluQuadricNormals gluQuadricOrientation gluQuadricTexture gluScaleImage gluSphere gluTessBeginContour gluTessBeginPolygon gluTessCallback gluTessEndContour gluTessEndPolygon gluTessNormal gluTessProperty gluTessVertex gluUnProject gluUnProject4

To deal with windows and interaction, you can either learn about rendering contexts and pixel formats and other ways that your operating system connects with OpenGL (via glx, wgl, or cgl). Or, you can use an operating-system independent layer for this purpose, such as GLUT, GLFW, SDL, CPW, SFML, and several others.

GLUT is pretty sparse, but it is popular and actually documented on the OpenGL web site and in the OpenGL books.

GLUT
glutAddMenuEntry glutAddSubMenu glutAttachMenu glutBitmapCharacter glutBitmapLength glutBitmapWidth glutButtonBoxFunc glutChangeToMenuEntry glutChangeToSubMenu glutCopyColormap glutCreateMenu glutCreateSubWindow glutCreateWindow glutDestroyMenu glutDestroyWindow glutDeviceGet glutDialsFunc glutDisplayFunc glutEnterGameMode glutEntryFunc glutEstablishOverlay glutExtensionSupported glutForceJoystickFunc glutFullScreen glutGameModeGet glutGameModeString glutGet glutGetColor glutGetModifiers glutIdleFunc glutIgnoreKeyRepeat glutInit glutInitDisplayMode glutInitDisplayString glutInitWindowPosition glutJoystickFunc glutKeyboardFunc glutKeyboardUpFunc glutLayerGet glutLeaveGameMode glutMainLoop glutMenuStatusFunc glutMotionFunc glutMouseFunc glutOverlayDisplayFunc glutPopWindow glutPositionWindow glutPostOverlayRedisplay glutPostRedisplay glutRemoveMenuItem glutRemoveOverlay glutReportErrors glutReshapeFunc glutReshapeWindow glutSetColor glutSetCursor glutSetMenu glutSetupVideoResizing glutSetWindow glutSetWindowTitle glutShowOverlay glutShowWindow glutSolidCone glutSolidCube glutSolidDodecahedron glutSolidIcosahedron glutSolidOctahedron glutSolidSphere glutSolidTeapot glutSolidTetrahedron glutSolidTorus glutSpaceballButtonFunc glutSpaceballMotionFunc glutSpaceballRotateFunc glutSpecialFunc glutSpecialUpFunc glutStopVideoResizing glutStrokeCharacter glutStrokeLength glutStrokeLengthf glutStrokeWidth glutStrokeWidthf glutSwapBuffers glutTabletButtonFunc glutTabletMotionFunc glutTimerFunc glutUseLayer glutVideoPan glutVideoResizeGet glutVideoResize glutVisibilityFunc glutWarpPointer glutWindowStatusFunc

A Better Way to Look at OpenGL

It's better to look at the mojor functional areas.

Starting with OpenGL 2.0, shaders are programmable, so you might want to learn the Open GL Shading Language (GLSL).

Good to Know

Some non-technical things you should know:

Physical Components

In case you have issues configuring your system, or you just like to know how things work, or you are a power user, it's good to find out how OpenGL is packaged on your system. Typical configurations are

GNU Compiler under Linux
Includes
/usr/X11R6/include/GL
Static Libs
/usr/X11R6/lib/
Dynamic Libs
/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
Includes
/gcc/include/gl/
Static Libs
/gcc/lib/gcc-lib/
Dynamic Libs
%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
  Includes
%vc%/include/
Static Libs
%vc%/lib/
Dynamic Libs
%WINSYS%/
GL gl.h opengl32.lib opengl32.dll
GLU glu.h glu32.lib glu32.dll
GLUT glut.h glut32.lib glut32.dll
Xcode Compiler under OS/X
/System/Library/Frameworks/OpenGL.framework/Headers /System/Library/Frameworks/OpenGL.framework/Libraries
GL gl.h libGL.dylib
GLU glu.h libGLU.dylib
/System/Library/Frameworks/GLUT.framework/Headers /System/Library/Frameworks/GLUT.framework/Libraries
GLUT glut.h

Of course, your configuration may be quite different — look around.

Installing OpenGL and GLUT

To install on Ubuntu:

To install on Windows:

To install on Mac OS/X:

Writing Code

How exactly you write, build, and run OpenGL applications depends on:

Assuming C and GLUT, you'll put the following at the beginning of every program:

#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

Write your app. Start with some of my examples or one from Dr. Dionisio (Recommended).

To build under Linux, you'll probably use a line like this:

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

or save yourself the trouble and put the includes and libraries in a standard directory, in which case you need only say:

gcc -o myprogram myprogram.c -lglut lGLU -lGL

Or maybe just write a makefile. On a Mac you will almost certainly write:

gcc -framework OpenGL -framework GLUT -o myprogram myprogram.c

or if your Mac is old, it's probably:

gcc -framework Foundation -framework OpenGL -framework GLUT -o myprogram myprogram.c

To build under Windows:

gcc -D_STDCALL_SUPPORTED -o myprogram myprogram.c glut32.lib -lglu32 -lopengl32

The command above assumes glut32.lib is in the current directory, so adjust the command if necessary.

And, of course, replace gcc with g++ if your code is in C++.