Mercurial > pygame
annotate examples/glcube.py @ 3172:d1b367fc08bc tests-returncode
Use a return code of 1 when the tests fail.
| author | Thomas Kluyver <takowl@gmail.com> |
|---|---|
| date | Wed, 09 Jan 2013 10:13:57 +0000 |
| parents | 6c5040262b53 |
| children |
| rev | line source |
|---|---|
| 159 | 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: | |
|
1610
6c5040262b53
implement *_tags.py module replacement to *_test.py level tags.
Lenard Lindstrom <len-l@telus.net>
parents:
300
diff
changeset
|
19 print ('The GLCUBE example requires PyOpenGL') |
| 159 | 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 = ( | |
| 203 | 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) | |
| 159 | 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] | |
| 202 | 64 glColor3fv(color) |
| 65 glVertex3fv(pos) | |
| 159 | 66 glEnd() |
| 67 | |
| 202 | 68 glColor3f(1.0, 1.0, 1.0) |
| 159 | 69 glBegin(GL_LINES) |
| 70 for line in CUBE_EDGES: | |
| 71 for vert in line: | |
| 72 pos, color = allpoints[vert] | |
| 202 | 73 glVertex3fv(pos) |
| 159 | 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 | |
| 202 | 88 glTranslatef(0.0, 0.0, -3.0) #move back |
| 89 glRotatef(25, 1, 0, 0) #orbit higher | |
| 159 | 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 | |
| 202 | 102 glRotatef(1, 0, 1, 0) |
| 159 | 103 |
| 104 drawcube() | |
| 105 pygame.display.flip() | |
| 300 | 106 pygame.time.wait(10) |
| 159 | 107 |
| 108 | |
| 109 if __name__ == '__main__': main() |
