NeHe2 for OpenGLContext

This document discusses the NeHe2 tutorial by Jeff Molofee. It introduces basic geometric-primitive rendering using glBegin/glEnd and glVertex, and the use of the Render customisation point for Contexts.

See NeHe1 for discussion of the basic Context setup procedure here...

from OpenGLContext import testingcontext
BaseContext, MainFunction = testingcontext.getInteractive()
from OpenGL.GL import *

class TestContext( BaseContext ):

As seen before, we sub-class BaseContext.  This time, however, we set initialPosition to (0,0,0).  By default the Context positions the camera at 0,0,10, looking toward the origin (0,0,0) so that geometry defined at the origin is visible.  The NeHe tutorial, however, repositions the camera itself, so we need to override the default position.

	# set initial camera position, tutorial does the re-positioning
initialPosition = (0,0,0)

Now we override the "Render" method of the Context.  The Render method is called by the system after the viewpoint and projection matrices are established, and can be used for simple rendering of geometry and the like.

Note: this is something of an over-simplification, Render is only called if there is no scenegraph object reported by the Context.  By default, however, there is no scenegraph, so we don't worry about it in the NeHe tutorial translations.

The "mode" object passed to Render is a render mode/pass object which describes the current rendering mode and provides various services.  We won't worry about it for this example.

	def Render( self, mode = 0):
"""Render the geometry for the scene."""
BaseContext.Render( self, mode )

By default OpenGLContext enables face-culling, which eliminates backward-facing polygons.  The tutorial assumes the feature is disabled, so we disable it.

		glDisable( GL_CULL_FACE )

The rest of the Render function is taken from the original NeHe tutorial.

		## Moves the drawing origin 6 units into the screen 
## and 1.5 units to the left
glTranslatef(-1.5,0.0,-6.0);
## Starts the geometry generation mode
glBegin(GL_TRIANGLES)
glVertex3f( 0.0, 1.0, 0.0)
glVertex3f(-1.0, -1.0, 0.0)
glVertex3f( 1.0, -1.0, 0.0)
glEnd()

## Moves the drawing origin again,
## cumulative change is now (1.5,0.0,6.0)
glTranslatef(3.0,0.0,0.0);

## Starts a different geometry generation mode
glBegin(GL_QUADS)
glVertex3f(-1.0,-1.0, 0.0)
glVertex3f( 1.0,-1.0, 0.0)
glVertex3f( 1.0, 1.0, 0.0)
glVertex3f(-1.0, 1.0, 0.0)
glEnd();

And finally, we make the script run MainFunction with an instance of our TestContext if the module is run as a script.

if __name__ == "__main__":
## We only want to run the main function if we
## are actually being executed as a script
MainFunction ( TestContext)

You can find the complete code for this sample in OpenGLContext/tests/nehe2.py