OpenGL help session

  1. Introduction

  2. GLUT overview

  3. OpenGL overview

  4. Rendering in OpenGL – basic 2D

  5. Rendering in OpenGL 3D

    1. Overview of pipeline

      Viewing

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

      Lighting

      Diffuse, specular, ambient
      Multiple lights
      Depends on normal vectors -- glNormal

      Shading

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

      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

      Rendering primitives

      Vertices, polygons, glBegin/glEnd as before

      Z buffer (hidden surface removal)

      glClear(GL_DEPTH_BUFFER_BIT);
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_LESS);

      clear depth buffer when clearing frame buffer

      glEnable (GL_DEPTH_TEST), glDepthFunc

      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:

          Set up light sources - glLightfv - position diffuse

          Set up materials - glMaterialfv - diffuse, specular, ambient, shininess

          Specify geometry with normals - glNormal3f.

        glShadeModel(GL_SMOOTH);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);

      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

  6. Performance considerations

    1. Triangle strips - good

        glBegin(GL_TRIANGLE_STRIP);

      Vertex arrays - often good

        store vertex related data (coordinates, colors, normals, etc.) into few arrays

        use just few function calls - reduces overhead.

        To use:

          - enable arrays - glEnableClientState

          - store data - glVertexPointer, glColorPointer, etc.

          - draw geometry -

            glArrayElement (all arrays) or glVertex*v, glColor*v, etc. - for individual arrays

            glDrawElements, glDrawRangeElements


          glBegin(GL_TRIANGLES);
          glArrayElement(2);
          glArrayElement(3);
          glArrayElement(4);
          glEnd();

      Display lists - sometimes good

        glNewList(1, GL_COMPILE);
        /* Draw the model */
        glEndList();

        /* Later on... */
        glCallList(1);

        Immediate mode (default) - primitives are sent to display right away.

        Retained mode - primitives are stored and can be redisplayed with different state.

        Allows the OpenGL implementation to "precompile" the OpenGL calls and optimize.

        Disadvantage - not editable.

      Texture mapping

      Mipmaps - power of 2 size is best.

      Hardware vs. software

  7. Gotchas

    1. Lighting - have you enabled lighting and light sources?

      Clipping esp. near/far planes - is your scene inside viewing frustum?

      Are you looking in the right direction?

      Avoid the dreaded black screen -

      make small reversible changes to your code until you get a feel for what does what

OpenGL Web page

http://www.opengl.org/

Review session notes from 1999

http://graphics.stanford.edu/courses/cs248-99/OpenGLSession/248opengl.html