Guest User

Untitled

a guest
Feb 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  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. import time
  12. import pygame
  13. from pygame.locals import *
  14.  
  15. try:
  16. from OpenGL.GL import *
  17. from OpenGL.GLU import *
  18. except ImportError:
  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 = list(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.  
  69. glColor3f(1.0, 1.0, 1.0)
  70. glBegin(GL_LINES)
  71.  
  72. """
  73. for line in CUBE_EDGES:
  74. for vert in line:
  75. pos, color = allpoints[vert]
  76. glVertex3fv(pos)
  77. """
  78.  
  79.  
  80. for x in range(-50,50):
  81. glColor3f(x*.03%1.0, x*.04%1.0, x*.05%1.0)
  82. glVertex3fv((x, 0, -100))
  83. glVertex3fv((x, 0, 100))
  84. glVertex3fv((-100, 0, x))
  85. glVertex3fv((100, 0, x))
  86.  
  87.  
  88. glEnd()
  89.  
  90.  
  91. def main():
  92. "run the demo"
  93. #initialize pygame and setup an opengl display
  94. pygame.init()
  95. pygame.display.set_mode((1024,768), OPENGL|DOUBLEBUF)
  96. glEnable(GL_DEPTH_TEST) #use our zbuffer
  97.  
  98. #setup the camera
  99. glMatrixMode(GL_PROJECTION)
  100. gluPerspective(45.0,1024/768.0,0.1,100.0) #setup lens
  101. glTranslatef(0.0, 0.0, -20.0) #move back
  102. glRotatef(60, 1, 0, 0) #orbit higher
  103.  
  104.  
  105. nt = int(time.time() * 1000)
  106.  
  107. for i in range(2**63):
  108. nt += 20
  109.  
  110. glTranslatef(0.0, 0.0, -.1)
  111.  
  112. #check for quit'n events
  113. event = pygame.event.poll()
  114. if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  115. break
  116.  
  117. #clear screen and move camera
  118. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  119.  
  120.  
  121.  
  122. #orbit camera around by 1 degree
  123. glRotatef(1, 0, 1, 0)
  124.  
  125. drawcube()
  126. pygame.display.flip()
  127. ct = int(time.time() * 1000)
  128. pygame.time.wait(max(1,nt - ct))
  129.  
  130. if i % 50 == 0:
  131. print(nt-ct)
  132.  
  133.  
  134. if __name__ == '__main__': main()
Add Comment
Please, Sign In to add comment