~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to examples/glcube.py

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
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
 
9
hard work for us. :]
 
10
"""
 
11
 
 
12
import pygame
 
13
from pygame.locals import *
 
14
 
 
15
try:
 
16
    from OpenGL.GL import *
 
17
    from OpenGL.GLU import *
 
18
except:
 
19
    print 'The GLCUBE example requires PyOpenGL'
 
20
    raise SystemExit
 
21
 
 
22
 
 
23
 
 
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
 
29
 
 
30
 
 
31
CUBE_POINTS = (
 
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)
 
36
)
 
37
 
 
38
#colors are 0-1 floating values
 
39
CUBE_COLORS = (
 
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)
 
42
)
 
43
 
 
44
CUBE_QUAD_VERTS = (
 
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)
 
47
)
 
48
 
 
49
CUBE_EDGES = (
 
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),
 
52
)
 
53
 
 
54
 
 
55
 
 
56
def drawcube():
 
57
    "draw the cube"
 
58
    allpoints = zip(CUBE_POINTS, CUBE_COLORS)
 
59
 
 
60
    glBegin(GL_QUADS)
 
61
    for face in CUBE_QUAD_VERTS:
 
62
        for vert in face:
 
63
            pos, color = allpoints[vert]
 
64
            glColor3fv(color)
 
65
            glVertex3fv(pos)
 
66
    glEnd()
 
67
 
 
68
    glColor3f(1.0, 1.0, 1.0)
 
69
    glBegin(GL_LINES)
 
70
    for line in CUBE_EDGES:
 
71
        for vert in line:
 
72
            pos, color = allpoints[vert]
 
73
            glVertex3fv(pos)
 
74
 
 
75
    glEnd()
 
76
 
 
77
 
 
78
def main():
 
79
    "run the demo"
 
80
    #initialize pygame and setup an opengl display
 
81
    pygame.init()
 
82
    pygame.display.set_mode((640,480), OPENGL|DOUBLEBUF)
 
83
    glEnable(GL_DEPTH_TEST)        #use our zbuffer
 
84
 
 
85
    #setup the camera
 
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
 
90
 
 
91
 
 
92
    while 1:
 
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):
 
96
            break
 
97
 
 
98
        #clear screen and move camera
 
99
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
 
100
 
 
101
        #orbit camera around by 1 degree
 
102
        glRotatef(1, 0, 1, 0)                    
 
103
 
 
104
        drawcube()
 
105
        pygame.display.flip()
 
106
        pygame.time.wait(10)
 
107
 
 
108
 
 
109
if __name__ == '__main__': main()