[Home]OpenGL

ec2-3-236-19-251.compute-1.amazonaws.com | ToothyWiki | RecentChanges | Login | Webcomic

OpenGL is a set of APIs designed to model 3D objects and scenery.  At a basic level it tracks and applies matrix operations to sets of points.

Z buffer and render pipelines


Each pixel on the OpenGL display has an entry in the Z-buffer, modelled as a floating point number.  The entry describes the "depth" of the displayed pixel from the viewer.  When rendering this value can be used to decide whether objects are in front or behind the currently visible pixel.

The Z buffer allows multiple render pipelines to simultaneously render objects, merging their outputs to a single bitmap (usually pixels closer to the user are displayed, whereas pixels further away are discarded).

Basic render pipeline functionality


Commands are fed to OpenGL in the form of a stream.  A simple stream could look like this:

    /* Set up position of the object */
    glMatrixMode?( GL_MODELVIEW );
    glLoadIdentity?();
    glTranslatef( 0.0f, 0.0f, -10.0f);
    glRotatef( 45.0f, 1.0f, 0.0f, 0.0f);

    /* Objects to operate on (one square YZ in plane) */
    glBegin( GL_QUADS );

    glVertex3f( 0.0f, 0.0f, 0.0f );
    glVertex3f( 0.0f, 1.0f, 0.0f );
    glVertex3f( 0.0f, 1.0f, 1.0f );
    glVertex3f( 0.0f, 0.0f, 1.0f );

    glEnd();

The ModelView? matrix handles positions and rotations of objects.  This is applied to the supplied Vertex points.  The final stage applies the Projection matrix to convert to a x-y coordinates on the screen.  The polygon can then be rasterised.

Configuring reusable command streams


A key feature of OpenGL is the ability to store command-lists for replay.  This allows entire objects to be drawn as a subroutine, without the processor resubmitting OpenGL commands.  The basic syntax is:

    /* Define Subroutine 1 */
    glNewList?( 1, GL_COMPILE );
      glBegin( GL_QUADS );
        glVertex3f( 0.0f, 0.0f, 0.0f );
        glVertex3f( 0.0f, 1.0f, 0.0f );
        glVertex3f( 0.0f, 1.0f, 1.0f );
        glVertex3f( 0.0f, 0.0f, 1.0f );
      glEnd();
    glEndList?();

    /* Call subroutine to display object twice */
    glCallList?( 1 );
    glTranslatef( 5.0f, 0.0f, 0.0f);
    glCallList?( 1 );

Colour, Lighting and Textures


Simple object colours can be specified using the glColor3f(...) command.  This is the simplest colour scheme where the colour of the drawn object is not altered by its orientation.

When lighting is enabled, each object includes a "normal vector" to help the rasteriser select the appropriate shade of colour.  A selection of "Material" properties are available to define how the light sources affects the colour selection.

Define the light source as follows:

    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glLightfv( GL_LIGHT0, GL_POSITION, <position> );
    glLightfv( GL_LIGHT0, GL_DIFFUSE, <colour and intensity> );

Specify the surface normal and material properties as part of the Vertex lists:

    glBegin( GL_QUADS );
      glNormal3f( 1.0f, 0.0f, 0.0f );
      glMaterialfv( GL_FRONT, GL_DIFFUSE, <colour> );

      glVertex3f( 0.0f, 0.0f, 0.0f );
      glVertex3f( 0.0f, 1.0f, 0.0f );
      glVertex3f( 0.0f, 1.0f, 1.0f );
      glVertex3f( 0.0f, 0.0f, 1.0f );
    glEnd();

A textured object is one that has part of a bitmap mapped onto its surface.  Usually all the required textures are loaded into OpenGL in advance as one large bitmap, and only the appropriate parts of the bitmap referenced.

Configure the texture bitmap:

    GLubyte textureBitmap[ 4 * TEXTURE_SIZE * TEXTURE_SIZE ];
    glEnable( GL_TEXTURE_2D );
    glTexParameterf?( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf?( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    glTexParameterf?( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexEnvf?( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );

    glTexImage2D?( GL_TEXTURE_2D,
                  0, 3,
                  TEXTURE_SIZE, TEXTURE_SIZE, 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, textureBitmap );

Map the specified part of the bitmap onto the object:
    glBegin( GL_QUADS );
      glTexCoord2f?( 1.0f, 1.0f );    glVertex3f( 0.0f, 0.0f, 0.0f );
      glTexCoord2f?( 0.0f, 1.0f );    glVertex3f( 0.0f, 1.0f, 0.0f );
      glTexCoord2f?( 0.0f, 0.0f );    glVertex3f( 0.0f, 1.0f, 1.0f );
      glTexCoord2f?( 1.0f, 0.0f );    glVertex3f( 0.0f, 0.0f, 1.0f );
    glEnd();

It is possible to enable these features simultaneously for more realistic scenes.

Programmable Shaders


The basic model view used in OpenGL works well for accurate CAD/CAM diagrams, but does not have the flexibility to produce pretty 3D graphics for games.  OpenGL shaders allow custom rendering techniques to be specified with executable code.

OpenCL?


In its most abstract form, OpenGL capable graphics cards operate as highly parallel vector processors with instruction sets and capabilities similar to traditional Altivec and Cray supercomputers (OpenGL was envisaged by SGI).  The general structure of large data-set matrix transforms fits in well with many image processing and scientific algorithms.  The Open Compute Langage (OpenCL?) is a C based extension to OpenGL introduced in OpenGL 3.0 that allows traditional parallel processing algorithms to be easily ported to run on GPUs.

WebGL


OpenGL ES 2.0 has been integrated into Firefox 4.0 and Chrome.  It is a very thin wrapper around the standard OpenGL APIs.

ec2-3-236-19-251.compute-1.amazonaws.com | ToothyWiki | RecentChanges | Login | Webcomic
Edit this page | View other revisions | Recently used referrers | List subpages
Last edited February 22, 2011 10:08 pm (viewing revision 4, which is the newest) (diff)
Search: