stage_3d.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #! /usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of LibreLight.
  5. LibreLight is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2 of the License.
  8. LibreLight is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with LibreLight. If not, see <http://www.gnu.org/licenses/>.
  14. (c) 2012 micha@uxsrv.de
  15. """
  16. import sys
  17. import pygame as pg
  18. #from pygame.locals import *
  19. import OpenGL.GL as gl
  20. import OpenGL.GLU as glu
  21. def wall(v=(0,0,0),size=(10,10),rgb=(0.2,0.2,0.2)):
  22. s=size
  23. edges = ( #kanten
  24. ( s[0]+v[0], s[1]+v[1],0+v[2]),
  25. ( s[0]+v[0], -s[1]+v[1],0+v[2]),
  26. (-s[0]+v[0], -s[1]+v[1],0+v[2]),
  27. (-s[0]+v[0], s[1]+v[1],0+v[2]),
  28. )
  29. gl.glBegin(gl.GL_QUADS)
  30. for e in edges:
  31. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  32. gl.glVertex3fv(e)
  33. gl.glVertex3fv(edges[0])
  34. gl.glEnd()
  35. def floor(v=(0,0,0),size=(10,8),rgb=(0.2,0.2,0.2)):
  36. s=size
  37. edges = ( #kanten
  38. ( s[0]+v[0],-0.2+v[1], s[1]+v[2]),
  39. ( s[0]+v[0],-0.2+v[1], -s[1]+v[2]),
  40. (-s[0]+v[0],-0.2+v[1], -s[1]+v[2]),
  41. (-s[0]+v[0],-0.2+v[1], s[1]+v[2]),
  42. )
  43. gl.glBegin(gl.GL_QUADS)
  44. for e in edges:
  45. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  46. gl.glVertex3fv(e)
  47. gl.glVertex3fv(edges[0])
  48. gl.glEnd()
  49. _z_init = 1
  50. def zerror():
  51. global _z_init
  52. edges = ( #kanten
  53. ((1,0,0),(0,0,0)),
  54. ((0,1,0),(0,0,0)),
  55. ((0,0,1),(0,0,0)),
  56. )
  57. i=0
  58. for e in edges:
  59. rgb=[0,0,0]
  60. rgb[i]=1
  61. if _z_init:
  62. print(rgb)
  63. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  64. gl.glBegin(gl.GL_LINES)
  65. for x in e:
  66. gl.glVertex3fv(x)
  67. gl.glEnd()
  68. i+=1
  69. _z_init = 0
  70. import random
  71. class Spot():
  72. def __init__(self,v=(0,0,0)):
  73. self.v = v
  74. self.l = 3
  75. self.v2 = [v[0],v[1]+5,v[2]+3]
  76. self.dir = random.randint(0,1)
  77. self.delta = 0
  78. self.delta2 = random.random()/100
  79. def draw(self):
  80. if self.dir:
  81. self.delta += (0.01 +self.delta2)
  82. else:
  83. self.delta -= (0.01 +self.delta2)
  84. if self.delta < -0.5:
  85. self.dir = 1
  86. if self.delta > 0.5:
  87. self.dir = 0
  88. self.v2[2] += self.delta
  89. edges = ( #kanten
  90. self.v,
  91. self.v2,
  92. )
  93. i=0
  94. gl.glBegin(gl.GL_LINES)
  95. #print("-----")
  96. for e in edges:
  97. #print("spot",e)
  98. rgb=[1,1,1]
  99. #rgb[i]=0.2
  100. gl.glColor4f(rgb[0], rgb[1], rgb[2], 0)
  101. #print(e)
  102. gl.glVertex3fv(e)
  103. i+=1
  104. if i >= len(edges):
  105. i = 0
  106. gl.glEnd()
  107. def test():
  108. gl.glBegin(gl.GL_LINES)
  109. # gl.glBegin(gl.GL_QUADS)
  110. x=(0,0,0)
  111. gl.glVertex3fv(x)
  112. x=(1,0,0)
  113. gl.glVertex3fv(x)
  114. x=(0,0,1)
  115. gl.glVertex3fv(x)
  116. x=(0,1,1)
  117. gl.glVertex3fv(x)
  118. gl.glEnd()
  119. def event_read():
  120. inc = 1
  121. for event in pg.event.get():
  122. move_x = 0
  123. move_y = 0
  124. move_z = 0
  125. rot_x = 0
  126. rot_y = 0
  127. rot_z = 0
  128. if event.type== pg.QUIT:
  129. print("quit")
  130. pg.quit()
  131. quit()
  132. sys.exit()
  133. if event.type == pg.KEYDOWN:
  134. print(event.key,event)
  135. print(event.mod)
  136. if event.key == pg.K_LEFT:
  137. if event.mod == 1:
  138. move_x = inc
  139. else:
  140. rot_z = 1
  141. if event.key == pg.K_RIGHT:
  142. if event.mod == 1:
  143. move_x = -inc
  144. else:
  145. rot_z = -1
  146. if event.key == pg.K_UP:
  147. if event.mod == 1:
  148. move_z = inc
  149. else:
  150. rot_x = 1
  151. if event.key == pg.K_DOWN:
  152. if event.mod == 1:
  153. move_z = -inc
  154. else:
  155. rot_x = -1
  156. gl.glTranslatef(move_x,move_z,move_y)
  157. a = 0
  158. for r in [rot_z,rot_x,rot_y]:
  159. if r == 0:
  160. a += 1
  161. continue
  162. deg = 10
  163. if r > 0:
  164. d = deg
  165. if r < 0:
  166. d = -deg
  167. gl.glRotatef(d,a,1,0)
  168. a += 1
  169. #pg.init() #pulseaudio assert error after some time
  170. #pg.font.init()
  171. #pg.mixer.init() #pulsaudio assert error after some time
  172. pg.display.init()
  173. pg.key.set_repeat(1,100)
  174. pg.display.set_caption('LibreLight 3D Stage')
  175. display= (400,400)
  176. display= (800,600)
  177. pg.display.set_mode(display,pg.DOUBLEBUF|pg.OPENGL)
  178. glu.gluPerspective( 45, (display[0]/display[1]), 0.1, 80.0)
  179. gl.glTranslatef(0.0,0.0,-50)
  180. gl.glRotatef(25,2,1,0)
  181. gl.glRotatef(25,0,1,0)
  182. spots = []
  183. for z in [1,3,5]:
  184. s = Spot(v=(-4,z,0))
  185. spots.append(s)
  186. s = Spot(v=(-2,z,0))
  187. spots.append(s)
  188. s = Spot(v=(2,z,0))
  189. spots.append(s)
  190. s = Spot(v=(4,z,0))
  191. spots.append(s)
  192. frame = 0
  193. while True:
  194. try:
  195. pg.display.set_caption('LibreLight 3D Stage {: 10} frame'.format(frame))
  196. frame += 1
  197. event_read()
  198. #gl.glRotatef(1,1,1,1)
  199. gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT)
  200. floor(v=(0,-2.2,20),size=(30,30),rgb=(.1,.1,.1)) # hall
  201. wall(v=(0,5,-8),size=(20,8),rgb=(.2,.2,.3)) # backdrop
  202. wall(v=(0,-1.2,8),size=(10,1),rgb=(.3,.2,.2)) # stage-border
  203. #floor(v=(0,-2.2,20),size=(10,10),rgb=(.3,.2,.2))
  204. floor() # stage-plain
  205. zerror() # zerror-cross
  206. for s in spots:
  207. s.draw()
  208. pg.display.flip()
  209. pg.time.wait(10)
  210. except Exception as e:
  211. print("Exception ",e)
  212. #finally:
  213. # print("end -- frame:",frame)