stage_3d.py 6.5 KB

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