import pyglet
from pyglet.gl import *
from pyglet.window import key
from OpenGL.GLUT import *
WINDOW = 400
INCREMENT = 5
class Window(pyglet.window.Window):
xRotation = yRotation = 30
def __init__(self, width, height, title=''):
super(Window, self).__init__(width, height, title)
glClearColor(0, 0, 0, 1)
glEnable(GL_DEPTH_TEST)
def on_draw(self):
self.clear()
glPushMatrix()
glRotatef(self.xRotation, 1, 0, 0)
glRotatef(self.yRotation, 0, 1, 0)
glBegin(GL_QUADS)
glColor3ub(255, 255, 255)
glVertex3f(50,50,50)
glColor3ub(255, 255, 0)
glVertex3f(50,-50,50)
glColor3ub(255, 0, 0)
glVertex3f(-50,-50,50)
glVertex3f(-50,50,50)
glColor3f(0, 0, 1)
glVertex3f(-50,50,-50)
glEnd()
glPopMatrix()
def on_resize(self, width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspectRatio = width / height
gluPerspective(35, aspectRatio, 1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -400)
def on_text_motion(self, motion):
if motion == key.UP:
self.xRotation -= INCREMENT
elif motion == key.DOWN:
self.xRotation += INCREMENT
elif motion == key.LEFT:
self.yRotation -= INCREMENT
elif motion == key.RIGHT:
self.yRotation += INCREMENT
if __name__ == '__main__':
Window(WINDOW, WINDOW, 'Pyglet Colored Cube')
pyglet.app.run()Copy