Guest User

Untitled

a guest
Jun 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. require "gl"
  2. require "glu"
  3. require "glut"
  4. require "mathn"
  5.  
  6. class BounceBox
  7.  
  8. include Gl
  9. include Glu
  10. include Glut
  11.  
  12. $window = ""
  13. $cube_angle = 0
  14.  
  15. $xy = [2.0, 2.0]
  16. $change = [1.0, 1.0]
  17. $speed = 0.01
  18.  
  19. def init_gl_window(width = 640, height = 480)
  20. glClearColor(0.0, 0.0, 0.0, 0)
  21. glClearDepth(1.0)
  22. glDepthFunc(GL_LEQUAL)
  23. glEnable(GL_DEPTH_TEST)
  24. glShadeModel(GL_SMOOTH)
  25.  
  26. glMatrixMode(GL_PROJECTION)
  27. glLoadIdentity
  28. gluPerspective(45.0, width / height, 0.1, 100.0)
  29.  
  30. glMatrixMode(GL_MODELVIEW)
  31.  
  32. draw_gl_scene
  33. end
  34.  
  35. #reshape = Proc.new do |width, height|
  36. def reshape(width, height)
  37. height = 1 if height == 0
  38.  
  39. # Reset current viewpoint and perspective transformation
  40. glViewport(0, 0, width, height)
  41.  
  42. glMatrixMode(GL_PROJECTION)
  43. glLoadIdentity
  44.  
  45. gluPerspective(45.0, width / height, 0.1, 100.0)
  46. end
  47.  
  48. def draw_gl_scene
  49. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  50. glMatrixMode(GL_MODELVIEW)
  51. glLoadIdentity
  52. glTranslatef($xy[0], $xy[1], -20.0)
  53.  
  54. glRotatef($cube_angle, 1.0, 1.0, 1.0)
  55.  
  56. glBegin(GL_QUADS)
  57. # Draw the top side in green
  58. glColor3f(0.0, 1.0, 0.0)
  59. glVertex3f( 1.0, 1.0, -1.0)
  60. glVertex3f(-1.0, 1.0, -1.0)
  61. glVertex3f(-1.0, 1.0, 1.0)
  62. glVertex3f( 1.0, 1.0, 1.0)
  63. # Draw the bottom side in orange
  64. glColor3f(1.0, 0.5, 0.0)
  65. glVertex3f( 1.0, -1.0, 1.0)
  66. glVertex3f(-1.0, -1.0, 1.0)
  67. glVertex3f(-1.0, -1.0, -1.0)
  68. glVertex3f( 1.0, -1.0, -1.0)
  69. # Draw the front side in red
  70. glColor3f(1.0, 0.0, 0.0)
  71. glVertex3f( 1.0, 1.0, 1.0)
  72. glVertex3f(-1.0, 1.0, 1.0)
  73. glVertex3f(-1.0, -1.0, 1.0)
  74. glVertex3f( 1.0, -1.0, 1.0)
  75. # Draw the back side in yellow
  76. glColor3f(1.0, 1.0, 0.0)
  77. glVertex3f( 1.0, -1.0, -1.0)
  78. glVertex3f(-1.0, -1.0, -1.0)
  79. glVertex3f(-1.0, 1.0, -1.0)
  80. glVertex3f( 1.0, 1.0, -1.0)
  81. # Draw the left side in blue
  82. glColor3f(0.0, 0.0, 1.0)
  83. glVertex3f(-1.0, 1.0, 1.0)
  84. glVertex3f(-1.0, 1.0, -1.0)
  85. glVertex3f(-1.0, -1.0, -1.0)
  86. glVertex3f(-1.0, -1.0, 1.0)
  87. # Draw the right side in violet
  88. glColor3f(1.0, 0.0, 1.0)
  89. glVertex3f( 1.0, 1.0, -1.0)
  90. glVertex3f( 1.0, 1.0, 1.0)
  91. glVertex3f( 1.0, -1.0, 1.0)
  92. glVertex3f( 1.0, -1.0, -1.0)
  93. glEnd
  94.  
  95. $cube_angle -= 0.15
  96. update_xy
  97.  
  98. # Swap buffers for display
  99. glutSwapBuffers
  100. end
  101.  
  102. def update_xy
  103. x, y = $xy
  104. xc, yc = $change
  105.  
  106. if x < -10 || x > 10
  107. xc *= -1
  108. end
  109.  
  110. if y < -8 || y > 8
  111. yc *= -1
  112. end
  113.  
  114. x += xc * $speed
  115. y += yc * $speed
  116.  
  117. puts "X: #{x} Y: #{y}"
  118.  
  119. $xy = [x, y]
  120. $change = [xc, yc]
  121. end
  122.  
  123. # The idle function to handle
  124. def idle
  125. glutPostRedisplay
  126. end
  127.  
  128. def initialize
  129. # Initliaze our GLUT code
  130. glutInit;
  131. # Setup a double buffer, RGBA color, alpha components and depth buffer
  132. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
  133. glutInitWindowSize(640, 480);
  134. glutInitWindowPosition(0, 0);
  135. $window = glutCreateWindow("Box Bang");
  136. glutDisplayFunc(method(:draw_gl_scene).to_proc);
  137. glutReshapeFunc(method(:reshape).to_proc);
  138. glutIdleFunc(method(:idle).to_proc);
  139. init_gl_window(640, 480)
  140. glutMainLoop();
  141. end
  142. end
Add Comment
Please, Sign In to add comment