3
"""Draw a cube on the screen. every frame we orbit
4
the camera around by a small amount and it appears
5
the object is spinning. note i've setup some simple
6
data structures here to represent a multicolored cube,
7
we then go through a semi-unopimized loop to draw
8
the cube points onto the screen. opengl does all the
13
from pygame.locals import *
16
from OpenGL.GL import *
17
from OpenGL.GLU import *
19
print 'The GLCUBE example requires PyOpenGL'
24
#some simple data for a colored cube
25
#here we have the 3D point position and color
26
#for each corner. then we have a list of indices
27
#that describe each face, and a list of indieces
28
#that describes each edge
32
(0.5, -0.5, -0.5), (0.5, 0.5, -0.5),
33
(-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),
34
(0.5, -0.5, 0.5), (0.5, 0.5, 0.5),
35
(-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5)
38
#colors are 0-1 floating values
40
(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 0),
41
(1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1)
45
(0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, 4),
46
(4, 5, 1, 0), (1, 5, 7, 2), (4, 0, 3, 6)
50
(0,1), (0,3), (0,4), (2,1), (2,3), (2,7),
51
(6,3), (6,4), (6,7), (5,1), (5,4), (5,7),
58
allpoints = zip(CUBE_POINTS, CUBE_COLORS)
61
for face in CUBE_QUAD_VERTS:
63
pos, color = allpoints[vert]
68
glColor3f(1.0, 1.0, 1.0)
70
for line in CUBE_EDGES:
72
pos, color = allpoints[vert]
80
#initialize pygame and setup an opengl display
82
pygame.display.set_mode((640,480), OPENGL|DOUBLEBUF)
83
glEnable(GL_DEPTH_TEST) #use our zbuffer
86
glMatrixMode(GL_PROJECTION)
87
gluPerspective(45.0,640/480.0,0.1,100.0) #setup lens
88
glTranslatef(0.0, 0.0, -3.0) #move back
89
glRotatef(25, 1, 0, 0) #orbit higher
93
#check for quit'n events
94
event = pygame.event.poll()
95
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
98
#clear screen and move camera
99
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
101
#orbit camera around by 1 degree
102
glRotatef(1, 0, 1, 0)
105
pygame.display.flip()
109
if __name__ == '__main__': main()