Entry 2923
Ruby-OpenGL Base Script
Submitted by akaiiro
on Jan. 4, 2010 at 11:59 p.m.
Language: Ruby. Code size: 3.2 KB.
# Purpose: Basic script to build OpenGL applications with Ruby and ruby-opengl # based upon the example 2 from Nehe's Tutorial require "gl" require "glu" require "glut" include Gl, Glu, Glut class GlBase ### GlBase.new # Main function. # Sets the initial parameters of the application and starts it # def initialize @width = 580 # window initial width @height = 480 # window initial height # initialize glut and set projection parameters glutInit glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH # set window atributes and create the window glutInitWindowSize @width, @height glutInitWindowPosition 0, 0 @window = glutCreateWindow "An OpenGL program" # define the glut functions that control the program glutDisplayFunc method(:display).to_proc glutReshapeFunc method(:reshape).to_proc glutKeyboardFunc method(:keyboard).to_proc glutIdleFunc method(:idle).to_proc # init the window init_window @width, @height # define what is going to be draw display # start glutMainLoop end ### # Sets al the necesary parameters for the visualization of the scene # var width initial width of the window # var height initial height of the window def init_window(width, height) # set colot, depth and shade parameters glClearColor 1.0, 1.0, 1.0, 1.0 glClearDepth 1.0 glDepthFunc GL_EQUAL glEnable GL_DEPTH_TEST glShadeModel GL_SMOOTH # set projection paramaters glMatrixMode GL_PROJECTION glLoadIdentity #calculate and set perspective according to window size gluPerspective 45.0, @width / @height, 0.1, 100.0 end ### # Modifies projection parameters if the window is resized # var width new window width # var height new window height def reshape(width, height) height = 1 if height == 0 glViewport 0, 0, width, height glMatrixMode GL_PROJECTION glLoadIdentity gluPerspective 45.0, width / height, 0.1, 100.0 end ### # Watches the keyboard and triggers actions if specific key is pressed # var key pressed key # var x unused # var y unused def keyboard(key, x, y) case key when ?\e glutDestroyWindow @window exit 0 end glutPostRedisplay end ### # Defines what to do if the application is inactive def idle glutPostRedisplay end ### # Defines what is going to be draw in the screen def display # reset color and depth buffers glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT # sets modelview glMatrixMode GL_MODELVIEW glLoadIdentity # translates view glTranslatef 0.0, 0.0, -10.0 ### scene code here ### ####################### # moves buffers so the image is displayed glutSwapBuffers end end GlBase.new
This snippet took 0.02 seconds to highlight.
Back to the Entry List or Home.