opengl-cube 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/opengl-cube ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'opengl-cube'
3
+
4
+ OpenGLCube.new if $0 == __FILE__
@@ -0,0 +1,185 @@
1
+ require 'opengl'
2
+
3
+ class OpenGLCube
4
+ include Gl
5
+ include Glu
6
+ include Glut
7
+
8
+ def initialize
9
+ @mouse_left_button_down = false
10
+
11
+ @mouse_pos_x = 0
12
+ @mouse_pos_y = 0
13
+
14
+ @cube_angle_x = 0
15
+ @cube_angle_y = 0
16
+
17
+ # Initliaze our GLUT code
18
+ glutInit
19
+ # Setup a double buffer, RGBA color, alpha components and depth buffer
20
+ glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
21
+ glutInitWindowSize 640, 480
22
+ glutInitWindowPosition 0, 0
23
+ @window = glutCreateWindow "OpenGL Cube"
24
+ glutDisplayFunc :draw_gl_scene
25
+ glutReshapeFunc :reshape
26
+ glutIdleFunc :idle
27
+ glutKeyboardFunc :keyboard
28
+ glutMouseFunc :mouse
29
+ glutMotionFunc :motion
30
+ glutPassiveMotionFunc :passive_motion
31
+ init_gl_window 640, 480
32
+ glutMainLoop
33
+ end
34
+
35
+ def init_gl_window width = 640, height = 480
36
+ # Background color to black
37
+ glClearColor 0.0, 0.0, 0.0, 0
38
+ # Enables clearing of depth buffer
39
+ glClearDepth 1.0
40
+ # Set type of depth test
41
+ glDepthFunc GL_LEQUAL
42
+ # Enable depth testing
43
+ glEnable GL_DEPTH_TEST
44
+ # Enable smooth color shading
45
+ glShadeModel GL_SMOOTH
46
+
47
+ glMatrixMode GL_PROJECTION
48
+ glLoadIdentity
49
+ # Calculate aspect ratio of the window
50
+ gluPerspective 45.0, width / height, 0.1, 100.0
51
+
52
+ glMatrixMode GL_MODELVIEW
53
+
54
+ draw_gl_scene
55
+ end
56
+
57
+ def reshape width, height
58
+ height = 1 if height == 0
59
+
60
+ # Reset current viewpoint and perspective transformation
61
+ glViewport 0, 0, width, height
62
+
63
+ glMatrixMode GL_PROJECTION
64
+ glLoadIdentity
65
+
66
+ gluPerspective 45.0, width / height, 0.1, 100.0
67
+ end
68
+
69
+ def draw_gl_scene
70
+ # Clear the screen and depth buffer
71
+ glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
72
+
73
+ # Reset the view
74
+ glMatrixMode GL_MODELVIEW
75
+ glLoadIdentity
76
+ # Move right 3 units
77
+ glTranslatef 0.0, 0.0, -7.0
78
+
79
+ # Draw a cube
80
+ # Rotate the cube on the X, Y and Z axis
81
+ glRotatef @cube_angle_y, 0.0, 1.0, 0.0
82
+ glRotatef @cube_angle_x, 1.0, 0.0, 0.0
83
+
84
+ # Set it to a blue color one time only
85
+ glBegin GL_QUADS do
86
+ # Draw the top side in green
87
+ glColor3f 0.0, 1.0, 0.0
88
+ glVertex3f 1.0, 1.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
+ # Draw the bottom side in orange
93
+ glColor3f 1.0, 0.5, 0.0
94
+ glVertex3f 1.0, -1.0, 1.0
95
+ glVertex3f -1.0, -1.0, 1.0
96
+ glVertex3f -1.0, -1.0, -1.0
97
+ glVertex3f 1.0, -1.0, -1.0
98
+ # Draw the front side in red
99
+ glColor3f 1.0, 0.0, 0.0
100
+ glVertex3f 1.0, 1.0, 1.0
101
+ glVertex3f -1.0, 1.0, 1.0
102
+ glVertex3f -1.0, -1.0, 1.0
103
+ glVertex3f 1.0, -1.0, 1.0
104
+ # Draw the back side in yellow
105
+ glColor3f 1.0, 1.0, 0.0
106
+ glVertex3f 1.0, -1.0, -1.0
107
+ glVertex3f -1.0, -1.0, -1.0
108
+ glVertex3f -1.0, 1.0, -1.0
109
+ glVertex3f 1.0, 1.0, -1.0
110
+ # Draw the left side in blue
111
+ glColor3f 0.0, 0.0, 1.0
112
+ glVertex3f -1.0, 1.0, 1.0
113
+ glVertex3f -1.0, 1.0, -1.0
114
+ glVertex3f -1.0, -1.0, -1.0
115
+ glVertex3f -1.0, -1.0, 1.0
116
+ # Draw the right side in violet
117
+ glColor3f 1.0, 0.0, 1.0
118
+ glVertex3f 1.0, 1.0, -1.0
119
+ glVertex3f 1.0, 1.0, 1.0
120
+ glVertex3f 1.0, -1.0, 1.0
121
+ glVertex3f 1.0, -1.0, -1.0
122
+ end
123
+
124
+ #@pyramid_angle += 0.2
125
+ #@cube_angle -= 0.15
126
+ # Swap buffers for display
127
+ glutSwapBuffers
128
+ end
129
+
130
+ def idle
131
+ glutPostRedisplay
132
+ end
133
+
134
+ def keyboard key, x, y
135
+ case key
136
+ when ?\e
137
+ glutDestroyWindow @window
138
+ exit 0
139
+ when ?a
140
+ @cube_angle_y -= 15.0
141
+ when ?d
142
+ @cube_angle_y += 15.0
143
+ when ?w
144
+ @cube_angle_x -= 15.0
145
+ when ?s
146
+ @cube_angle_x += 15.0
147
+ end
148
+ glutPostRedisplay
149
+ end
150
+
151
+ def mouse button, state, x, y
152
+ case button
153
+ when GLUT_LEFT_BUTTON
154
+ case state
155
+ when GLUT_DOWN
156
+ @mouse_pos_x = x
157
+ @mouse_pos_y = y
158
+ @mouse_left_button_down = true
159
+ when GLUT_UP
160
+ @cube_angle_x = @mouse_pos_x - x
161
+ @cube_angle_y = @mouse_pos_y - y
162
+ @mouse_pos_x = x
163
+ @mouse_pos_y = y
164
+ end
165
+ when GLUT_RIGHT_BUTTON
166
+ case state
167
+ when GLUT_DOWN then puts "Right down"
168
+ when GLUT_UP then puts "Right up"
169
+ end
170
+ end
171
+ glutPostRedisplay
172
+ end
173
+
174
+ def motion x, y
175
+ if @mouse_left_button_down
176
+ @cube_angle_y = @mouse_pos_x - x
177
+ @cube_angle_x = @mouse_pos_y - y
178
+ end
179
+ end
180
+
181
+ def passive_motion x, y
182
+ end
183
+ end
184
+
185
+ OpenGLCube.new if $0 == __FILE__
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'hola'
3
+
4
+ class OpenGLCubeTest < Test::Unit::TestCase
5
+ def setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opengl-cube
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gao Chao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem play with cube rendered by opengl
15
+ email: reterclose@gmail.com
16
+ executables:
17
+ - opengl-cube
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/opengl-cube.rb
22
+ - test/test_opengl-cube.rb
23
+ - bin/opengl-cube
24
+ homepage: https://github.com/reterVision/opengl-cube
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements:
43
+ - opengl, v0.8.0.pre1
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.24
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: OpenGL with Ruby!
49
+ test_files:
50
+ - test/test_opengl-cube.rb