Tuesday, November 13, 2007

Example 3-1 : Transformed Cube

I've made a small jump ahead and went on to chapter 3, "Viewing". Some code from chapter 2 will be posted here at a later date (it's just that that code resides on another system now).

This is the code that sets off Chapter 3:

# Example 3-1 : Transformed Cube

from pyglet.gl import *
from pyglet import window
from OpenGL.GLUT import *

win = window.Window()

def init():
glClearColor (0.0, 0.0, 0.0, 0.0)
glShadeModel (GL_FLAT)

def reshape (w, h):
glViewport (0, 0, w, h)
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
glMatrixMode (GL_MODELVIEW)

def display():
glClear (GL_COLOR_BUFFER_BIT)
glColor3f (1.0, 1.0, 1.0)
glLoadIdentity () # clear the matrix
# viewing transformation
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glScalef (1.0, 2.0, 1.0) # modeling transformation
glutWireCube (1.0)
glFlush ()

init()

win.on_resize = reshape

while not win.has_exit:
win.dispatch_events()
win.clear()
display()
win.flip()


As the GLUT library is not being wrapped in pyglet, we'll have to import it from within OpenGL (get it here). Mind that GLUT are but helper libraries (such as the famous teapot), and the pyglet people have good reasons not to include it in their module.

Also, with what has been learned in chapter 2 of the red book, a WireCube() function shouldn't be too hard to write.

No comments: