from pyglet.gl import * from pyglet import window from pyglet import clock from pyglet.window import key GLfloat3 = GLfloat * 3 GLfloat4 = GLfloat * 4 CUBE_DEG_PER_S = 30.0 LIGHT_DEG_PER_S = 90.0 CUBE_SIZE = 9.0 # XXX - is there a bug here? originally set to 256 but that ends # up heavily segmenting the cube and really bad performance RESOLUTION = 9 flashLightPos = [0.0, 0.0, 0.0, 1.0] flashLightDir = [0.0, 0.0, -1.0] flashLightColor = [0.2, 0.2, 0.2, 1.0] redLightColor = [0.5, 0.1, 0.2, 1.0] redLightPos = [10.0, 0.0, 5.0, 1.0] greenLightColor = [0.1, 0.6, 0.2, 1.0] greenLightPos = [0.0, 0.0, 10.0, 1.0] m_cubeAngle = 0.0 m_lightAngle = 0.0 m_pSphere = None m_flashlightOn = False def DrawPlane(size, step): glBegin(GL_QUADS) z = 0 while z < size: x = 0 while x < size: glVertex3f(x, 0.0, z) glVertex3f(x + step, 0.0, z) glVertex3f(x + step, 0.0, z + step) glVertex3f(x, 0.0, z + step) x += step z += step glEnd() def DrawCube(size, resolution): step = size / resolution glPushMatrix() glTranslatef(-size / 2, -size / 2, -size / 2) glNormal3f(0.0, -1.0, 0.0) # top glPushMatrix() glTranslatef(0.0, size, 0.0) glScalef(1.0, -1.0, 1.0) DrawPlane(size, step) glPopMatrix() # bottom DrawPlane(size, step) # left glPushMatrix() glRotatef(90.0, 0.0, 0.0, 1.0) glScalef(1.0, -1.0, 1.0) DrawPlane(size, step) glPopMatrix() # right glPushMatrix() glTranslatef(size, 0.0, 0.0) glRotatef(90.0, 0.0, 0.0, 1.0) DrawPlane(size, step) glPopMatrix() # front glPushMatrix() glTranslatef(0.0, 0.0, size) glRotatef(90.0, -1.0, 0.0, 0.0) DrawPlane(size, step) glPopMatrix() # back glPushMatrix() glRotatef(90.0, -1.0, 0.0, 0.0) glScalef(1.0, -1.0, 1.0) DrawPlane(size, step) glPopMatrix() glPopMatrix() def Init(): global m_pSphere glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) # set up the flashlight glEnable(GL_LIGHT0) glLightfv(GL_LIGHT0, GL_POSITION, GLfloat4(*flashLightPos)) glLightfv(GL_LIGHT0, GL_AMBIENT, GLfloat4(*flashLightColor)) glLightfv(GL_LIGHT0, GL_DIFFUSE, GLfloat4(*flashLightColor)) glLightfv(GL_LIGHT0, GL_SPECULAR, GLfloat4(*flashLightColor)) glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat3(*flashLightDir)) # setup static red light glEnable(GL_LIGHT1) glLightfv(GL_LIGHT1, GL_AMBIENT, GLfloat4(*redLightColor)) glLightfv(GL_LIGHT1, GL_DIFFUSE, GLfloat4(*redLightColor)) glLightfv(GL_LIGHT1, GL_SPECULAR, GLfloat4(*redLightColor)) # set up moving green light glEnable(GL_LIGHT2) glLightfv(GL_LIGHT2, GL_AMBIENT, GLfloat4(*greenLightColor)) glLightfv(GL_LIGHT2, GL_DIFFUSE, GLfloat4(*greenLightColor)) glLightfv(GL_LIGHT2, GL_SPECULAR, GLfloat4(*greenLightColor)) m_pSphere = gluNewQuadric() def SetupProjection(width, height): if not height: height = 1 # set up the viewport glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60.0, 1.0 * width / height, 1.0, 1000.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def Prepare(dt): global m_cubeAngle, m_lightAngle m_cubeAngle += CUBE_DEG_PER_S * dt if m_cubeAngle > 360.0: m_cubeAngle = 0.0 m_lightAngle += LIGHT_DEG_PER_S * dt if m_lightAngle > 360.0: m_lightAngle = 0.0 def Shutdown(): global m_pSphere if m_pSphere: gluDeleteQuadric(m_pSphere) m_pSphere = None def Render(): global m_flashlightOn, m_cubeAngle # clear screen and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() gluLookAt(0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); if m_flashlightOn: glEnable(GL_LIGHT0) else: glDisable(GL_LIGHT0) # position the red light glLightfv(GL_LIGHT1, GL_POSITION, GLfloat4(*redLightPos)) # draw the red light glPushMatrix() glDisable(GL_LIGHTING) glTranslatef(redLightPos[0], redLightPos[1], redLightPos[2]) glColor4f(*redLightColor) gluSphere(m_pSphere, 0.2, 10, 10) glEnable(GL_LIGHTING) glPopMatrix() # position and draw the green light glPushMatrix() glDisable(GL_LIGHTING) glRotatef(m_lightAngle, 1.0, 0.0, 0.0) glRotatef(m_lightAngle, 0.0, 1.0, 0.0) glLightfv(GL_LIGHT2, GL_POSITION, GLfloat4(*greenLightPos)) glTranslatef(greenLightPos[0], greenLightPos[1], greenLightPos[2]) glColor4f(*greenLightColor) gluSphere(m_pSphere, 0.2, 10, 10) glEnable(GL_LIGHTING) glPopMatrix() # set up cube's material cubeColor = GLfloat4(0.6, 0.7, 1.0, 1.0) cubeSpecular = GLfloat4(1.0, 1.0, 1.0, 1.0) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, cubeColor) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cubeSpecular) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0) # position and draw the cube glPushMatrix() glRotatef(m_cubeAngle, 1.0, 1.0, 0.0) glRotatef(m_cubeAngle, 0.0, 0.0, 1.0) DrawCube(CUBE_SIZE, RESOLUTION) glPopMatrix() def KeyboardHandler(symbol, modifiers): global m_flashlightOn if symbol == key.ESCAPE: win.has_exit = True elif symbol == key.SPACE: m_flashlightOn = not m_flashlightOn def main(): global win win = window.Window(width=800, height=600, visible=False) win.on_resize = SetupProjection win.on_key_press = KeyboardHandler Init() win.set_visible() while not win.has_exit: win.dispatch_events() tick = clock.tick() Prepare(tick) Render() win.flip() Shutdown() if __name__ == '__main__': main()