stage_3d.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import pygame as pg
  2. #from pygame.locals import *
  3. import OpenGL.GL as gl
  4. import OpenGL.GLU as glu
  5. def wall(v=(0,0,0),size=(10,10),rgb=(0.2,0.2,0.2)):
  6. s=size
  7. edges = ( #kanten
  8. ( s[0]+v[0], s[1]+v[1],0+v[2]),
  9. ( s[0]+v[0], -s[1]+v[1],0+v[2]),
  10. (-s[0]+v[0], -s[1]+v[1],0+v[2]),
  11. (-s[0]+v[0], s[1]+v[1],0+v[2]),
  12. )
  13. gl.glBegin(gl.GL_QUADS)
  14. for e in edges:
  15. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  16. gl.glVertex3fv(e)
  17. gl.glVertex3fv(edges[0])
  18. gl.glEnd()
  19. def floor(v=(0,0,0),size=(10,8),rgb=(0.2,0.2,0.2)):
  20. s=size
  21. edges = ( #kanten
  22. ( s[0]+v[0],-0.2+v[1], s[1]+v[2]),
  23. ( s[0]+v[0],-0.2+v[1], -s[1]+v[2]),
  24. (-s[0]+v[0],-0.2+v[1], -s[1]+v[2]),
  25. (-s[0]+v[0],-0.2+v[1], s[1]+v[2]),
  26. )
  27. gl.glBegin(gl.GL_QUADS)
  28. for e in edges:
  29. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  30. gl.glVertex3fv(e)
  31. gl.glVertex3fv(edges[0])
  32. gl.glEnd()
  33. _z_init = 1
  34. def zerror():
  35. global _z_init
  36. edges = ( #kanten
  37. ((1,0,0),(0,0,0)),
  38. ((0,1,0),(0,0,0)),
  39. ((0,0,1),(0,0,0)),
  40. )
  41. i=0
  42. for e in edges:
  43. rgb=[0,0,0]
  44. rgb[i]=1
  45. if _z_init:
  46. print(rgb)
  47. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  48. gl.glBegin(gl.GL_LINES)
  49. for x in e:
  50. gl.glVertex3fv(x)
  51. gl.glEnd()
  52. i+=1
  53. _z_init = 0
  54. import random
  55. class Spot():
  56. def __init__(self,v=(0,0,0)):
  57. self.v = v
  58. self.l = 3
  59. self.v2 = [v[0],v[1]+5,v[2]+3]
  60. self.dir = random.randint(0,1)
  61. self.delta = 0
  62. def draw(self):
  63. if self.dir:
  64. self.delta += 0.01
  65. else:
  66. self.delta -= 0.01
  67. if self.delta < -0.5:
  68. self.dir = 1
  69. if self.delta > 0.5:
  70. self.dir = 0
  71. self.v2[2] += self.delta
  72. edges = ( #kanten
  73. self.v,
  74. self.v2,
  75. )
  76. i=0
  77. gl.glBegin(gl.GL_LINES)
  78. #print("-----")
  79. for e in edges:
  80. #print("spot",e)
  81. rgb=[1,1,1]
  82. #rgb[i]=0.2
  83. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  84. #print(e)
  85. gl.glVertex3fv(e)
  86. i+=1
  87. if i >= len(edges):
  88. i = 0
  89. gl.glEnd()
  90. def test():
  91. gl.glBegin(gl.GL_LINES)
  92. # gl.glBegin(gl.GL_QUADS)
  93. x=(0,0,0)
  94. gl.glVertex3fv(x)
  95. x=(1,0,0)
  96. gl.glVertex3fv(x)
  97. x=(0,0,1)
  98. gl.glVertex3fv(x)
  99. x=(0,1,1)
  100. gl.glVertex3fv(x)
  101. gl.glEnd()
  102. def event_read():
  103. inc = 1
  104. for event in pg.event.get():
  105. move_x = 0
  106. move_y = 0
  107. move_z = 0
  108. rot_x = 0
  109. rot_y = 0
  110. rot_z = 0
  111. if event.type== pg.QUIT:
  112. pg.quit()
  113. quit()
  114. if event.type == pg.KEYDOWN:
  115. print(event.key,event)
  116. print(event.mod)
  117. if event.key == pg.K_LEFT:
  118. if event.mod == 1:
  119. move_x = inc
  120. else:
  121. rot_z = 1
  122. if event.key == pg.K_RIGHT:
  123. if event.mod == 1:
  124. move_x = -inc
  125. else:
  126. rot_z = -1
  127. if event.key == pg.K_UP:
  128. if event.mod == 1:
  129. move_z = inc
  130. else:
  131. rot_x = 1
  132. if event.key == pg.K_DOWN:
  133. if event.mod == 1:
  134. move_z = -inc
  135. else:
  136. rot_x = -1
  137. gl.glTranslatef(move_x,move_z,move_y)
  138. a = 0
  139. for r in [rot_z,rot_x,rot_y]:
  140. if r == 0:
  141. a += 1
  142. continue
  143. deg = 10
  144. if r > 0:
  145. d = deg
  146. if r < 0:
  147. d = -deg
  148. gl.glRotatef(d,a,1,0)
  149. a += 1
  150. pg.init()
  151. pg.key.set_repeat(1,100)
  152. pg.display.set_caption('LibreLight 3D Stage')
  153. display= (400,400)
  154. display= (800,600)
  155. pg.display.set_mode(display,pg.DOUBLEBUF|pg.OPENGL)
  156. glu.gluPerspective( 45, (display[0]/display[1]), 0.1, 80.0)
  157. gl.glTranslatef(0.0,0.0,-50)
  158. gl.glRotatef(25,2,1,0)
  159. gl.glRotatef(25,0,1,0)
  160. spots = []
  161. for z in [1,3,5]:
  162. s = Spot(v=(-4,z,0))
  163. spots.append(s)
  164. s = Spot(v=(-2,z,0))
  165. spots.append(s)
  166. s = Spot(v=(2,z,0))
  167. spots.append(s)
  168. s = Spot(v=(4,z,0))
  169. spots.append(s)
  170. frame = 0
  171. while True:
  172. pg.display.set_caption('LibreLight 3D Stage {: 10} frame'.format(frame))
  173. frame += 1
  174. event_read()
  175. #gl.glRotatef(1,1,1,1)
  176. gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT)
  177. floor(v=(0,-2.2,20),size=(30,30),rgb=(.1,.1,.1)) # hall
  178. wall(v=(0,5,-8),size=(20,8),rgb=(.2,.2,.3)) # backdrop
  179. wall(v=(0,-1.2,8),size=(10,1),rgb=(.3,.2,.2)) # stage-border
  180. #floor(v=(0,-2.2,20),size=(10,10),rgb=(.3,.2,.2))
  181. floor() # stage-plain
  182. zerror() # zerror-cross
  183. for s in spots:
  184. s.draw()
  185. pg.display.flip()
  186. pg.time.wait(10)