Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "gl"
- require "glu"
- require "glut"
- require "mathn"
- class BounceBox
- include Gl
- include Glu
- include Glut
- $window = ""
- $cube_angle = 0
- $xy = [2.0, 2.0]
- $change = [1.0, 1.0]
- $speed = 0.01
- def init_gl_window(width = 640, height = 480)
- glClearColor(0.0, 0.0, 0.0, 0)
- glClearDepth(1.0)
- glDepthFunc(GL_LEQUAL)
- glEnable(GL_DEPTH_TEST)
- glShadeModel(GL_SMOOTH)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity
- gluPerspective(45.0, width / height, 0.1, 100.0)
- glMatrixMode(GL_MODELVIEW)
- draw_gl_scene
- end
- #reshape = Proc.new do |width, height|
- def reshape(width, height)
- height = 1 if height == 0
- # Reset current viewpoint and perspective transformation
- glViewport(0, 0, width, height)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity
- gluPerspective(45.0, width / height, 0.1, 100.0)
- end
- def draw_gl_scene
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity
- glTranslatef($xy[0], $xy[1], -20.0)
- glRotatef($cube_angle, 1.0, 1.0, 1.0)
- glBegin(GL_QUADS)
- # Draw the top side in green
- glColor3f(0.0, 1.0, 0.0)
- glVertex3f( 1.0, 1.0, -1.0)
- glVertex3f(-1.0, 1.0, -1.0)
- glVertex3f(-1.0, 1.0, 1.0)
- glVertex3f( 1.0, 1.0, 1.0)
- # Draw the bottom side in orange
- glColor3f(1.0, 0.5, 0.0)
- glVertex3f( 1.0, -1.0, 1.0)
- glVertex3f(-1.0, -1.0, 1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- glVertex3f( 1.0, -1.0, -1.0)
- # Draw the front side in red
- glColor3f(1.0, 0.0, 0.0)
- glVertex3f( 1.0, 1.0, 1.0)
- glVertex3f(-1.0, 1.0, 1.0)
- glVertex3f(-1.0, -1.0, 1.0)
- glVertex3f( 1.0, -1.0, 1.0)
- # Draw the back side in yellow
- glColor3f(1.0, 1.0, 0.0)
- glVertex3f( 1.0, -1.0, -1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- glVertex3f(-1.0, 1.0, -1.0)
- glVertex3f( 1.0, 1.0, -1.0)
- # Draw the left side in blue
- glColor3f(0.0, 0.0, 1.0)
- glVertex3f(-1.0, 1.0, 1.0)
- glVertex3f(-1.0, 1.0, -1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- glVertex3f(-1.0, -1.0, 1.0)
- # Draw the right side in violet
- glColor3f(1.0, 0.0, 1.0)
- glVertex3f( 1.0, 1.0, -1.0)
- glVertex3f( 1.0, 1.0, 1.0)
- glVertex3f( 1.0, -1.0, 1.0)
- glVertex3f( 1.0, -1.0, -1.0)
- glEnd
- $cube_angle -= 0.15
- update_xy
- # Swap buffers for display
- glutSwapBuffers
- end
- def update_xy
- x, y = $xy
- xc, yc = $change
- if x < -10 || x > 10
- xc *= -1
- end
- if y < -8 || y > 8
- yc *= -1
- end
- x += xc * $speed
- y += yc * $speed
- puts "X: #{x} Y: #{y}"
- $xy = [x, y]
- $change = [xc, yc]
- end
- # The idle function to handle
- def idle
- glutPostRedisplay
- end
- def initialize
- # Initliaze our GLUT code
- glutInit;
- # Setup a double buffer, RGBA color, alpha components and depth buffer
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
- glutInitWindowSize(640, 480);
- glutInitWindowPosition(0, 0);
- $window = glutCreateWindow("Box Bang");
- glutDisplayFunc(method(:draw_gl_scene).to_proc);
- glutReshapeFunc(method(:reshape).to_proc);
- glutIdleFunc(method(:idle).to_proc);
- init_gl_window(640, 480)
- glutMainLoop();
- end
- end
Add Comment
Please, Sign In to add comment