Class: OpenGLCube

Inherits:
Object
  • Object
show all
Includes:
Gl, Glu, Glut
Defined in:
lib/opengl-cube.rb

Instance Method Summary collapse

Constructor Details

#initializeOpenGLCube

Returns a new instance of OpenGLCube.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opengl-cube.rb', line 8

def initialize
  @mouse_left_button_down = false
  @mouse_right_button_down = false

  @mouse_pos_x = 0
  @mouse_pos_y = 0

  @cube_angle_x = 0
  @cube_angle_y = 0
  @cube_angle_z = 0

  # 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 "OpenGL Cube"
  glutDisplayFunc :draw_gl_scene
  glutReshapeFunc :reshape
  glutIdleFunc :idle
  glutKeyboardFunc :keyboard
  glutMouseFunc :mouse
  glutMotionFunc :motion
  glutPassiveMotionFunc :passive_motion
  init_gl_window 640, 480
  glutMainLoop
end

Instance Method Details

#draw_gl_sceneObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/opengl-cube.rb', line 71

def draw_gl_scene
  # Clear the screen and depth buffer
  glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT

  # Reset the view
  glMatrixMode GL_MODELVIEW
  glLoadIdentity
  # Move right 3 units
  glTranslatef 0.0, 0.0, -7.0

  # Draw a cube
  # Rotate the cube on the X, Y and Z axis
  glRotatef @cube_angle_y, 0.0, 1.0, 0.0
  glRotatef @cube_angle_x, 1.0, 0.0, 0.0
  glRotatef @cube_angle_z, 0.0, 0.0, 1.0

  # Set it to a blue color one time only
  glBegin GL_QUADS do
    # 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
  end

  #@pyramid_angle += 0.2
  #@cube_angle -= 0.15
  # Swap buffers for display
  glutSwapBuffers
end

#idleObject



133
134
135
# File 'lib/opengl-cube.rb', line 133

def idle
  glutPostRedisplay
end

#init_gl_window(width = 640, height = 480) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opengl-cube.rb', line 37

def init_gl_window width = 640, height = 480
  # Background color to black
  glClearColor 0.0, 0.0, 0.0, 0
  # Enables clearing of depth buffer
  glClearDepth 1.0
  # Set type of depth test
  glDepthFunc GL_LEQUAL
  # Enable depth testing
  glEnable GL_DEPTH_TEST
  # Enable smooth color shading
  glShadeModel GL_SMOOTH

  glMatrixMode GL_PROJECTION
  glLoadIdentity
  # Calculate aspect ratio of the window
  gluPerspective 45.0, width / height, 0.1, 100.0

  glMatrixMode GL_MODELVIEW

  draw_gl_scene
end

#keyboard(key, x, y) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/opengl-cube.rb', line 137

def keyboard key, x, y
  case key
  when ?\e
    glutDestroyWindow @window
    exit 0
  when ?a
    @cube_angle_y -= 15.0
  when ?d 
    @cube_angle_y += 15.0
  when ?w
    @cube_angle_x -= 15.0
  when ?s
    @cube_angle_x += 15.0
  end
  glutPostRedisplay
end

#motion(x, y) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/opengl-cube.rb', line 183

def motion x, y
  if @mouse_left_button_down
    @cube_angle_y = @mouse_pos_x - x
    @cube_angle_x = @mouse_pos_y - y
  elsif @mouse_right_button_down
    @cube_angle_z = @mouse_pos_x - x
  end
end

#mouse(button, state, x, y) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/opengl-cube.rb', line 154

def mouse button, state, x, y
  case button
  when GLUT_LEFT_BUTTON
    case state
    when GLUT_DOWN
    	@mouse_pos_x = x
    	@mouse_pos_y = y
    	@mouse_left_button_down = true
    when GLUT_UP 
    	@cube_angle_x = @mouse_pos_x - x
    	@cube_angle_y = @mouse_pos_y - y
    	@mouse_pos_x = x
    	@mouse_pos_y = y
      @mouse_left_button_down = false
    end
  when GLUT_RIGHT_BUTTON
    case state
    when GLUT_DOWN
      @mouse_pos_x = x
      @mouse_pos_y = y
      @mouse_right_button_down = true
    when GLUT_UP 
      @cube_angle_z = @mouse_pos_x - x
      @mouse_right_button_down = false
    end
  end
  glutPostRedisplay
end

#passive_motion(x, y) ⇒ Object



192
193
# File 'lib/opengl-cube.rb', line 192

def passive_motion x, y
end

#reshape(width, height) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/opengl-cube.rb', line 59

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