OpenGL/GLUT study note

1. Introduction

1.1. Why OpenGL?

  • Stanford-centric history:
  • Relatively portable – runs on Macs, MS Windows, Unix, Linux, BeOS, OS/2, etc.
  • Industry standard (like Microsoft’s DirectX)

2. What does OpenGL do?

Draw stuff “onscreen”, in 2D and 3D

3. What doesn’t it do?

Windows, menus, define what “onscreen” is, share the display, input devices

4. GLUT as a windowing interface

  • Makes up for most of the important things in the “What doesn’t it do” section
  • OpenGL + GLUT + {insert your windowing system here} make a pretty complete combination.
  • OpenGL/GLUT app should be portable across the various Unix platforms and Win32

5. Overview of libraries

(From diagram in OpenGL course notes from Siggraph 97)

5. GLUT overview

  • Function names begin with “glut”
  • Mostly used for one-time initialization
  // GLUT Window Initialization:
 glutInit(&argc, argv);
 glutInitWindowSize(g_Width, g_Height);
 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("CS248 GLUT example");
  • Callback model (like xsupport)
  // Register callbacks:
 glutDisplayFunc (display);

5. OpenGL overview

  • Function names prefixed with “gl”
  • Function names may have suffixes indicating the data types
Number of Components Data Type Vector
2 – (x, y) b – byte omit “v” for scalar form
3 – (x, y, z) ub – unsigned byte  (e.g. glVertex2f(x, y); )
4 – (x, y, z, w) s – unsigned short
us – unsigned short
i – int
ui – unsigned int
f – float
d – double
  • examples


glBegin, glEnd to specify polygons

glColor* specifies the current color (used for subsequently defined vertices)

glNormal* specifies the current normal

glVertex* specified vertices of polygons

  • State management
    • many functions apply to current state, which is contained in a glXContext (or equivalent in Win32)
    • have lots of other functions to select/modify current state (e.g. glEnable, glDisable)
    • glGet{Boolean, Double, Float, Integer}v functions are used to get the state
    • glGetError

6. Rendering in OpenGL – basic 2D

  • How to get a basic 2D drawing onscreen
    • Clearing screen – glClearColor, glClear
    • Viewport – glViewport
    • Orthographic projection – glOrtho
    • Make polygons with glBegin/glEnd
    • Specifying colors – glColor*
    • Specify vertices – glVertex* in side a pair of glBegin/glEnd
    • If double buffering, swap the front and back buffers – glSwapBuffers (or glutSwapBuffers
    • To avoid seeing partially drawn image use two buffers: front and back.
      • front buffer is displayed
      • back buffer is written into

7. Rendering in OpenGL 3D

7.1. Overview of pipeline


  • Transformation, 2 matrices
  • Backface culling – no need to draw hidden facets
  • Clipping – to viewing frustum
  • Depth test


  • Diffuse, specular, ambient
  • Multiple lights
  • Depends on normal vectors — glNormal


  • Based on light position, materials, normals
  • Calculated for each vertex, interpolated to fill polygon (Gouraud shading)

7.2. Transformation model

object (world) coordinates

  •  modelview

viewing (positioning and aiming camera): gluLookAt

model: glRotate, glTranslate, glScale

  • eye (camera) coordinates
  • projection (fov, projection: glOrtho, gluPerspective, glFrustum)
  • clip coordinates
  • perspective division (by w)
  • normalized device coordinates
  • viewport transformation (size and position of image)
  • screen coordinates.

glLoadIdentity – set current matrix to identity

  • (most commands: current matrix * specified matrix)

glMatrixMode – chooses between modelview and projection matrix stacks.

General transformations: glMultMatrix, glLoadMatrix

Note: matrix m[4][4] – C array.  Then m[i][j] – ith column and jth row of OGL transformation matrix.  C stores arrays in row-major order, while OpenGL expect column-major matrices.

glPushMatrix, glPopMatrix

7.3. Rendering primitives

Vertices, polygons, glBegin/glEnd as before

7.4 Z buffer (hidden surface removal)


clear depth buffer when clearing frame buffer

glEnable (GL_DEPTH_TEST), glDepthFunc

7.5. Lighting and Shading

  • Per vertex color calculation based on lighting conditions.
  • Uses shading normals – possibly different from geometric normals.
  • Shading model – GL_FLAT, GL_SMOOTH
  • Model – local lighting.
  • Light sources
  • To use:
  1. Set up light sources – glLightfv – position diffuse
  2. Set up materials – glMaterialfv – diffuse, specular, ambient, shininess
  3. Specify geometry with normals – glNormal3f.
  4. glShadeModel(GL_SMOOTH);
  5. glEnable(GL_LIGHTING);
  6. glEnable(GL_LIGHT0);

7.6. Texture mapping

  • Enabling texture modes
    • Enable texturing – glEnable (GL_TEXTURE_2D)
    • Bind texture to context, make it current (avoid downloading texture multiple times) – glBindTexture
    • Download texture – glTexImage2D
    • Alternatively, build mipmaps and load the texture – gluBuild2DMipmaps
    • Set filtering options – glTexParameteri
  • Specifying texture coordinates
    • By hand glTexCoord*
    • Automatically generate texture coordinates based on the position of each vertex- glTexGen

8. Further Reading

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s