Guest User

Untitled

a guest
Jun 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. # Copyright © 2009 Preston Lee. All rights reserved.
  2.  
  3. require 'star'
  4. require 'asynchronous_object'
  5.  
  6. class StarfieldStar < Star
  7. include Processing::Proxy
  8.  
  9. def draw
  10. push_matrix
  11. translate(@x, @y, @z)
  12. box(8)
  13. pop_matrix
  14. end
  15. end
  16.  
  17. class Starfield < Processing::App
  18.  
  19. load_library :opengl
  20.  
  21. NUM_STARS = 200;
  22. CAMERA_SPEED = 20 # Pixels per wall second.
  23. CAMERA_ROTATE_SPEED = 0.08 # Radians per wall second.
  24. FRAME_RATE = 30 # Target frame per second.
  25.  
  26. attr_accessor :stars, :in_setup
  27. full_screen
  28.  
  29. def setup
  30. if in_setup == nil # guard against repeatedly setting up opengl?
  31. library_loaded?(:opengl) ? setup_opengl : render_mode(P3D)
  32. in_setup = true
  33. end
  34. frame_rate FRAME_RATE
  35.  
  36. @moved = false
  37. @x = 0
  38. @y = 0
  39. @z = 0
  40. @rz = 0
  41.  
  42. @mouse_last_x = nil
  43. @mouse_last_y = nil
  44. @active = true
  45. @active_mutex = Mutex.new
  46. @stars = []
  47. NUM_STARS.times do
  48. @stars << StarfieldStar.new(1.0 / FRAME_RATE)
  49. end
  50. text_font load_font("Univers66.vlw.gz"), 10
  51.  
  52. end
  53.  
  54.  
  55. def draw
  56. background 0 # Paint the entire screen solid black.
  57. fill(255)
  58. color(100,255,255)
  59. sphere(100)
  60. # text("Moving starfield demo aoenu hreouh rcohurcoeuh arochuoaentuhoe u.", 0, 0, 0)
  61. if !@moved
  62. push_matrix
  63. scale(5.0, 5.0, 5.0)
  64. # translate(0, 100, 0)
  65. text("Multi-threaded starfield simulation.", 0, 80, 0)
  66. text("Move: A, S, D, W, R, F, Arrow Keys", 0, 100, 0)
  67. text("Roll: Z, C", 0, 110, 0)
  68. text("Look: click and drag mouse.", 0, 120, 0)
  69. text("Pause/Resume: TAB", 0, 130, 0)
  70. text("PrestonLee.com", 0, 150, 0)
  71. pop_matrix
  72. else
  73. # puts "MOVED"
  74. end
  75.  
  76.  
  77. @stars.each do |s|
  78. push_matrix
  79. s.draw
  80. pop_matrix
  81. end
  82.  
  83. move_camera_for_frame
  84. # move_for_frame()
  85. # scale(5.0, 5.0, 5.0)
  86. # text('aoeuaoeuao euaoeuaoeu')
  87. # translate(@x, @y, @z)
  88. # rotate_z(@rz)
  89.  
  90. end
  91.  
  92. def setup_opengl
  93. render_mode OPENGL
  94. hint DISABLE_OPENGL_ERROR_REPORT
  95. hint ENABLE_OPENGL_4X_SMOOTH
  96. end
  97.  
  98. def mouse_released
  99. @mouse_last_x = nil
  100. @mouse_last_y = nil
  101. end
  102.  
  103. def mouse_dragged
  104. @moved = true
  105. @mouse_last_x = mouse_x if @mouse_last_x.nil?
  106. @mouse_last_y = mouse_y if @mouse_last_y.nil?
  107.  
  108. dx = @mouse_last_x - mouse_x
  109. dy = @mouse_last_y - mouse_y
  110.  
  111. begin_camera
  112. if dx != 0
  113. # puts "#{mouse_x} #{mouse_y}"
  114. rotate_y radians(-dx) * 0.1
  115. end
  116. if dy != 0
  117. rotate_x radians(dy) * 0.1
  118. end
  119.  
  120. end_camera
  121.  
  122. @mouse_last_x = mouse_x
  123. @mouse_last_y = mouse_y
  124. end
  125.  
  126.  
  127. def key_pressed
  128. @moved = true
  129. # puts "KEY_PRESSED: #{key_code}"
  130. handle_camera_change_start
  131. handle_pause_and_resume
  132. end
  133.  
  134. def handle_pause_and_resume
  135. case key_code
  136. when TAB:
  137. @active_mutex.synchronize do
  138. @stars.each do |s|
  139. @active ? s.deactivate : s.activate
  140. end
  141. @active = !@active
  142. end
  143. end
  144. end
  145.  
  146. def key_released
  147. # puts "KEY_RELEASED: #{key_code}"
  148. handle_camera_change_stop
  149. end
  150.  
  151. def handle_camera_change_start
  152. begin_camera
  153. case key_code
  154. when UP:
  155. @camera_move_z = -1
  156. when DOWN, 's', 'o':
  157. @camera_move_z = 1
  158. when LEFT:
  159. @camera_move_x = -1
  160. when RIGHT:
  161. @camera_move_x = 1
  162. end
  163.  
  164. case key
  165. when 'w', ',':
  166. @camera_move_z = -1
  167. when 's', 'o':
  168. @camera_move_z = 1
  169. when 'a':
  170. @camera_move_x = -1
  171. when 'd', 'e':
  172. @camera_move_x = 1
  173. when 'r', 'p':
  174. @camera_move_y = -1
  175. when 'f', 'u':
  176. @camera_move_y = 1
  177. when 'z', ';':
  178. @camera_rotate_z = -1
  179. when 'c', 'j':
  180. @camera_rotate_z = 1
  181. end
  182.  
  183. end_camera
  184. end
  185.  
  186. def handle_camera_change_stop
  187. begin_camera
  188. case key_code
  189. when UP, DOWN, 'w', ',', 's', 'o':
  190. @camera_move_z = 0
  191. when LEFT, RIGHT, 'a', 'd', 'e':
  192. @camera_move_x = 0
  193. end
  194.  
  195. case key
  196. when 'w', ',', 's', 'o':
  197. @camera_move_z = 0
  198. when 'a', 'd', 'e':
  199. @camera_move_x = 0
  200. when 'r', 'p', 'f', 'u':
  201. @camera_move_y = 0
  202. when 'z', ';', 'c', 'j':
  203. @camera_rotate_z = 0
  204. end
  205. end_camera
  206. end
  207.  
  208. def move_camera_for_frame
  209. begin_camera
  210. @dx = (@camera_move_x || 0) * CAMERA_SPEED
  211. @dy = (@camera_move_y || 0) * CAMERA_SPEED
  212. @dz = (@camera_move_z || 0) * CAMERA_SPEED
  213. @drz = (@camera_rotate_z || 0) * CAMERA_ROTATE_SPEED
  214. @x += @dx
  215. @y += @dy
  216. @z += @dz
  217. @rz += @drz
  218. translate(@dx, 0, 0) if !@camera_move_x.nil? && @camera_move_x != 0
  219. translate(0, @dy, 0) if !@camera_move_y.nil? && @camera_move_y != 0
  220. translate(0, 0, @dz) if !@camera_move_z.nil? && @camera_move_z != 0
  221. rotate_z(@drz) if !@camera_rotate_z.nil? && @camera_rotate_z != 0
  222. end_camera
  223. end
  224.  
  225. end
Add Comment
Please, Sign In to add comment