glcube.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python
  2. """Draw a cube on the screen. every frame we orbit
  3. the camera around by a small amount and it appears
  4. the object is spinning. note i've setup some simple
  5. data structures here to represent a multicolored cube,
  6. we then go through a semi-unoptimized loop to draw
  7. the cube points onto the screen. opengl does all the
  8. hard work for us. :]
  9. """
  10. import pygame
  11. from pygame.locals import *
  12. try:
  13. from OpenGL.GL import *
  14. from OpenGL.GLU import *
  15. except ImportError:
  16. print ('The GLCUBE example requires PyOpenGL')
  17. raise SystemExit
  18. #some simple data for a colored cube
  19. #here we have the 3D point position and color
  20. #for each corner. then we have a list of indices
  21. #that describe each face, and a list of indieces
  22. #that describes each edge
  23. CUBE_POINTS = (
  24. (0.5, -0.5, -0.5), (0.5, 0.5, -0.5),
  25. (-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),
  26. (0.5, -0.5, 0.5), (0.5, 0.5, 0.5),
  27. (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5)
  28. )
  29. #colors are 0-1 floating values
  30. CUBE_COLORS = (
  31. (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 0),
  32. (1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1)
  33. )
  34. CUBE_QUAD_VERTS = (
  35. (0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, 4),
  36. (4, 5, 1, 0), (1, 5, 7, 2), (4, 0, 3, 6)
  37. )
  38. CUBE_EDGES = (
  39. (0,1), (0,3), (0,4), (2,1), (2,3), (2,7),
  40. (6,3), (6,4), (6,7), (5,1), (5,4), (5,7),
  41. )
  42. def drawcube():
  43. "draw the cube"
  44. allpoints = list(zip(CUBE_POINTS, CUBE_COLORS))
  45. glBegin(GL_QUADS)
  46. for face in CUBE_QUAD_VERTS:
  47. for vert in face:
  48. pos, color = allpoints[vert]
  49. glColor3fv(color)
  50. glVertex3fv(pos)
  51. glEnd()
  52. glColor3f(1.0, 1.0, 1.0)
  53. glBegin(GL_LINES)
  54. for line in CUBE_EDGES:
  55. for vert in line:
  56. pos, color = allpoints[vert]
  57. glVertex3fv(pos)
  58. glEnd()
  59. def init_gl_stuff():
  60. glEnable(GL_DEPTH_TEST) #use our zbuffer
  61. #setup the camera
  62. glMatrixMode(GL_PROJECTION)
  63. glLoadIdentity()
  64. gluPerspective(45.0,640/480.0,0.1,100.0) #setup lens
  65. glTranslatef(0.0, 0.0, -3.0) #move back
  66. glRotatef(25, 1, 0, 0) #orbit higher
  67. def main():
  68. "run the demo"
  69. #initialize pygame and setup an opengl display
  70. pygame.init()
  71. fullscreen = True
  72. pygame.display.set_mode((640,480), OPENGL|DOUBLEBUF|FULLSCREEN)
  73. init_gl_stuff()
  74. going = True
  75. while going:
  76. #check for quit'n events
  77. events = pygame.event.get()
  78. for event in events:
  79. if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  80. going = False
  81. elif event.type == KEYDOWN:
  82. if event.key == pygame.K_f:
  83. if not fullscreen:
  84. print("Changing to FULLSCREEN")
  85. pygame.display.set_mode((640, 480), OPENGL | DOUBLEBUF | FULLSCREEN)
  86. else:
  87. print("Changing to windowed mode")
  88. pygame.display.set_mode((640, 480), OPENGL | DOUBLEBUF)
  89. fullscreen = not fullscreen
  90. init_gl_stuff()
  91. #clear screen and move camera
  92. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  93. #orbit camera around by 1 degree
  94. glRotatef(1, 0, 1, 0)
  95. drawcube()
  96. pygame.display.flip()
  97. pygame.time.wait(10)
  98. if __name__ == '__main__': main()