This document discusses the NeHe5 tutorial by Jeff Molofee. It draws a closed shapes (a pyramid and a box) rather than individual polygons.
See NeHe4 for discussion of the Context
setup procedure here...
from OpenGLContext import testingcontext
BaseContext, MainFunction = testingcontext.getInteractive()
from OpenGL.GL import *
import time
class TestContext( BaseContext ):
# set initial camera position, tutorial does the re-positioning
initialPosition = (0,0,0)
def Render( self, mode = 0):
"""Draw scene geometry"""
BaseContext.Render( self, mode )
glDisable( GL_LIGHTING)
glTranslatef(-1.5,0.0,-6.0);
glRotated( time.time()%(3.0)/3 * 360, 0,1,0)
We refactor the tutorial code to create a method for drawing the pyramid object and cube objects, instead of including the code in the main Render method (just for neatness sake). The rest of the Render function is all stuff we've seen before.
self.drawPyramid()
glLoadIdentity()
glTranslatef(1.5,0.0,-6.0);
glRotated( time.time()%(1.0)/1 * -360, 1,0,0)
self.drawCube()
def OnIdle( self, ):
"""Request refresh of the context whenever idle"""
self.triggerRedraw(1)
return 1
The actual rendering code as two methods, taken directly from the
original tutorial.
def drawPyramid( self ):
"""Draw a multicolored pyramid"""
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0)
glVertex3f( 0.0, 1.0, 0.0)
glColor3f(0.0,1.0,0.0)
glVertex3f(-1.0,-1.0, 1.0)
glColor3f(0.0,0.0,1.0)
glVertex3f( 1.0,-1.0, 1.0)
glColor3f(1.0,0.0,0.0)
glVertex3f( 0.0, 1.0, 0.0)
glColor3f(0.0,0.0,1.0)
glVertex3f( 1.0,-1.0, 1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f( 1.0,-1.0, -1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 0.0, 1.0, 0.0);
glColor3f(0.0,1.0,0.0);
glVertex3f( 1.0,-1.0, -1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0, -1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 0.0, 1.0, 0.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0,-1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-1.0,-1.0, 1.0);
glEnd()
def drawCube( self ):
"""Draw a multicolored cube"""
glBegin(GL_QUADS);
glColor3f(0.0,1.0,0.0)
glVertex3f( 1.0, 1.0,-1.0)
glVertex3f(-1.0, 1.0,-1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f( 1.0, 1.0, 1.0)
glColor3f(1.0,0.5,0.0)
glVertex3f( 1.0,-1.0, 1.0)
glVertex3f(-1.0,-1.0, 1.0)
glVertex3f(-1.0,-1.0,-1.0)
glVertex3f( 1.0,-1.0,-1.0)
glColor3f(1.0,0.0,0.0)
glVertex3f( 1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0,-1.0, 1.0)
glVertex3f( 1.0,-1.0, 1.0)
glColor3f(1.0,1.0,0.0)
glVertex3f( 1.0,-1.0,-1.0)
glVertex3f(-1.0,-1.0,-1.0)
glVertex3f(-1.0, 1.0,-1.0)
glVertex3f( 1.0, 1.0,-1.0)
glColor3f(0.0,0.0,1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0,-1.0)
glVertex3f(-1.0,-1.0,-1.0)
glVertex3f(-1.0,-1.0, 1.0)
glColor3f(1.0,0.0,1.0)
glVertex3f( 1.0, 1.0,-1.0)
glVertex3f( 1.0, 1.0, 1.0)
glVertex3f( 1.0,-1.0, 1.0)
glVertex3f( 1.0,-1.0,-1.0)
glEnd()
And finally, as always, 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/nehe5.py