danabr75-opengl 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.autotest +29 -0
- data/.gemtest +0 -0
- data/.gitignore +6 -0
- data/.travis.yml +19 -0
- data/Gemfile +8 -0
- data/History.md +101 -0
- data/MIT-LICENSE +18 -0
- data/Manifest.txt +142 -0
- data/README.rdoc +105 -0
- data/Rakefile +170 -0
- data/danabr75-opengl.gemspec +32 -0
- data/examples/NeHe/NeHe.png +0 -0
- data/examples/NeHe/crate.png +0 -0
- data/examples/NeHe/glass.png +0 -0
- data/examples/NeHe/nehe_lesson02.rb +117 -0
- data/examples/NeHe/nehe_lesson03.rb +123 -0
- data/examples/NeHe/nehe_lesson04.rb +132 -0
- data/examples/NeHe/nehe_lesson05.rb +182 -0
- data/examples/NeHe/nehe_lesson06.rb +186 -0
- data/examples/NeHe/nehe_lesson07.rb +240 -0
- data/examples/NeHe/nehe_lesson08.rb +255 -0
- data/examples/NeHe/nehe_lesson09.rb +203 -0
- data/examples/NeHe/nehe_lesson11.rb +176 -0
- data/examples/NeHe/nehe_lesson12.rb +203 -0
- data/examples/NeHe/nehe_lesson16.rb +211 -0
- data/examples/NeHe/nehe_lesson19.rb +209 -0
- data/examples/NeHe/nehe_lesson36.rb +308 -0
- data/examples/NeHe/particle.png +0 -0
- data/examples/NeHe/star.png +0 -0
- data/examples/NeHe/tim.png +0 -0
- data/examples/OrangeBook/3Dlabs-License.txt +33 -0
- data/examples/OrangeBook/brick.frag +36 -0
- data/examples/OrangeBook/brick.rb +378 -0
- data/examples/OrangeBook/brick.vert +41 -0
- data/examples/OrangeBook/particle.frag +17 -0
- data/examples/OrangeBook/particle.rb +408 -0
- data/examples/OrangeBook/particle.vert +38 -0
- data/examples/README +16 -0
- data/examples/RedBook/aapoly.rb +143 -0
- data/examples/RedBook/aargb.rb +121 -0
- data/examples/RedBook/accanti.rb +164 -0
- data/examples/RedBook/accpersp.rb +217 -0
- data/examples/RedBook/alpha.rb +125 -0
- data/examples/RedBook/alpha3D.rb +160 -0
- data/examples/RedBook/bezcurve.rb +107 -0
- data/examples/RedBook/bezmesh.rb +139 -0
- data/examples/RedBook/checker.rb +126 -0
- data/examples/RedBook/clip.rb +97 -0
- data/examples/RedBook/colormat.rb +137 -0
- data/examples/RedBook/cube.rb +71 -0
- data/examples/RedBook/depthcue.rb +101 -0
- data/examples/RedBook/dof.rb +206 -0
- data/examples/RedBook/double.rb +107 -0
- data/examples/RedBook/drawf.rb +93 -0
- data/examples/RedBook/feedback.rb +147 -0
- data/examples/RedBook/fog.rb +168 -0
- data/examples/RedBook/font.rb +153 -0
- data/examples/RedBook/hello.rb +81 -0
- data/examples/RedBook/image.rb +139 -0
- data/examples/RedBook/jitter.rb +207 -0
- data/examples/RedBook/light.rb +157 -0
- data/examples/RedBook/lines.rb +130 -0
- data/examples/RedBook/list.rb +113 -0
- data/examples/RedBook/material.rb +276 -0
- data/examples/RedBook/mipmap.rb +158 -0
- data/examples/RedBook/model.rb +115 -0
- data/examples/RedBook/movelight.rb +134 -0
- data/examples/RedBook/pickdepth.rb +181 -0
- data/examples/RedBook/planet.rb +110 -0
- data/examples/RedBook/quadric.rb +160 -0
- data/examples/RedBook/robot.rb +117 -0
- data/examples/RedBook/select.rb +198 -0
- data/examples/RedBook/smooth.rb +97 -0
- data/examples/RedBook/stencil.rb +165 -0
- data/examples/RedBook/stroke.rb +169 -0
- data/examples/RedBook/surface.rb +168 -0
- data/examples/RedBook/teaambient.rb +134 -0
- data/examples/RedBook/teapots.rb +184 -0
- data/examples/RedBook/tess.rb +185 -0
- data/examples/RedBook/texbind.rb +149 -0
- data/examples/RedBook/texgen.rb +171 -0
- data/examples/RedBook/texturesurf.rb +130 -0
- data/examples/RedBook/varray.rb +161 -0
- data/examples/RedBook/wrap.rb +150 -0
- data/examples/misc/OGLBench.rb +338 -0
- data/examples/misc/anisotropic.rb +196 -0
- data/examples/misc/fbo_test.rb +357 -0
- data/examples/misc/font-glut.rb +47 -0
- data/examples/misc/glfwtest.rb +30 -0
- data/examples/misc/plane.rb +161 -0
- data/examples/misc/readpixel.rb +66 -0
- data/examples/misc/sdltest.rb +36 -0
- data/examples/misc/trislam.rb +829 -0
- data/ext/opengl/GL/gl.h +2115 -0
- data/ext/opengl/GL/glext.h +11770 -0
- data/ext/opengl/common.h +372 -0
- data/ext/opengl/conv.h +247 -0
- data/ext/opengl/extconf.rb +19 -0
- data/ext/opengl/fptr_struct.h +912 -0
- data/ext/opengl/funcdef.h +279 -0
- data/ext/opengl/gl-1.0-1.1.c +3345 -0
- data/ext/opengl/gl-1.2.c +155 -0
- data/ext/opengl/gl-1.3.c +447 -0
- data/ext/opengl/gl-1.4.c +346 -0
- data/ext/opengl/gl-1.5.c +225 -0
- data/ext/opengl/gl-2.0.c +665 -0
- data/ext/opengl/gl-2.1.c +57 -0
- data/ext/opengl/gl-3.0.c +502 -0
- data/ext/opengl/gl-enums.c +4853 -0
- data/ext/opengl/gl-enums.h +14528 -0
- data/ext/opengl/gl-error.c +110 -0
- data/ext/opengl/gl-error.h +25 -0
- data/ext/opengl/gl-ext-3dfx.c +27 -0
- data/ext/opengl/gl-ext-arb.c +880 -0
- data/ext/opengl/gl-ext-ati.c +41 -0
- data/ext/opengl/gl-ext-ext.c +885 -0
- data/ext/opengl/gl-ext-gremedy.c +41 -0
- data/ext/opengl/gl-ext-nv.c +679 -0
- data/ext/opengl/gl.c +265 -0
- data/ext/opengl/gl_buffer.c +202 -0
- data/ext/opengl/glimpl.c +187 -0
- data/ext/opengl/glimpl.h +47 -0
- data/ext/opengl/opengl.c +9 -0
- data/lib/gl.rb +1 -0
- data/lib/opengl.rb +79 -0
- data/lib/opengl/bindings_version.rb +4 -0
- data/lib/opengl/implementation.rb +38 -0
- data/lib/opengl/test_case.rb +86 -0
- data/test/dummy.xorg.conf +140 -0
- data/test/test_gl.rb +45 -0
- data/test/test_gl_10_11.rb +1363 -0
- data/test/test_gl_12.rb +71 -0
- data/test/test_gl_13.rb +221 -0
- data/test/test_gl_14.rb +218 -0
- data/test/test_gl_15.rb +258 -0
- data/test/test_gl_20.rb +417 -0
- data/test/test_gl_21.rb +436 -0
- data/test/test_gl_ext_arb.rb +508 -0
- data/test/test_gl_ext_ati.rb +28 -0
- data/test/test_gl_ext_ext.rb +608 -0
- data/test/test_gl_ext_gremedy.rb +29 -0
- data/test/test_gl_ext_nv.rb +349 -0
- data/test/test_glimpl.rb +23 -0
- data/test/test_opengl_buffer.rb +122 -0
- data/utils/README +6 -0
- data/utils/enumgen.rb +108 -0
- data/utils/extlistgen.rb +90 -0
- metadata +326 -0
@@ -0,0 +1,132 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Name: nehe_lesson04.rb
|
3
|
+
# Purpose: An implementation of NeHe's OpenGL Lesson #04
|
4
|
+
# using ruby-opengl http://nehe.gamedev.net/
|
5
|
+
|
6
|
+
require 'opengl'
|
7
|
+
require 'glu'
|
8
|
+
require 'glut'
|
9
|
+
|
10
|
+
# Add GL and GLUT namespaces in to make porting easier
|
11
|
+
include Gl
|
12
|
+
include Glu
|
13
|
+
include Glut
|
14
|
+
|
15
|
+
# Placeholder for the window object
|
16
|
+
$window = ""
|
17
|
+
# Angle for the triangle Global
|
18
|
+
$triangle_angle = 0
|
19
|
+
# Angle for the quadrilateral Global
|
20
|
+
$quad_angle = 0
|
21
|
+
|
22
|
+
def init_gl_window width = 640, height = 480
|
23
|
+
# Background color to black
|
24
|
+
glClearColor 0.0, 0.0, 0.0, 0
|
25
|
+
# Enables clearing of depth buffer
|
26
|
+
glClearDepth 1.0
|
27
|
+
# Set type of depth test
|
28
|
+
glDepthFunc GL_LEQUAL
|
29
|
+
# Enable depth testing
|
30
|
+
glEnable GL_DEPTH_TEST
|
31
|
+
# Enable smooth color shading
|
32
|
+
glShadeModel GL_SMOOTH
|
33
|
+
|
34
|
+
glMatrixMode GL_PROJECTION
|
35
|
+
glLoadIdentity
|
36
|
+
# Calculate aspect ratio of the window
|
37
|
+
gluPerspective 45.0, width / height, 0.1, 100.0
|
38
|
+
|
39
|
+
glMatrixMode GL_MODELVIEW
|
40
|
+
|
41
|
+
draw_gl_scene
|
42
|
+
end
|
43
|
+
|
44
|
+
#reshape = Proc.new do |width, height|
|
45
|
+
def reshape width, height
|
46
|
+
height = 1 if height == 0
|
47
|
+
|
48
|
+
# Reset current viewpoint and perspective transformation
|
49
|
+
glViewport 0, 0, width, height
|
50
|
+
|
51
|
+
glMatrixMode GL_PROJECTION
|
52
|
+
glLoadIdentity
|
53
|
+
|
54
|
+
gluPerspective 45.0, width / height, 0.1, 100.0
|
55
|
+
end
|
56
|
+
|
57
|
+
#draw_gl_scene = Proc.new do
|
58
|
+
def draw_gl_scene
|
59
|
+
# Clear the screen and depth buffer
|
60
|
+
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
|
61
|
+
|
62
|
+
# Reset the view
|
63
|
+
glMatrixMode GL_MODELVIEW
|
64
|
+
glLoadIdentity
|
65
|
+
|
66
|
+
# Move left 1.5 units and into the screen 6.0 units
|
67
|
+
glTranslatef -1.5, 0.0, -6.0
|
68
|
+
|
69
|
+
# Rotate the triangle on the Y-axis
|
70
|
+
glRotatef $triangle_angle, 0.0, 1.0, 0.0
|
71
|
+
# Draw a triangle
|
72
|
+
glBegin GL_POLYGON do
|
73
|
+
glColor3f 1.0, 0.0, 0.0
|
74
|
+
glVertex3f 0.0, 1.0, 0.0
|
75
|
+
glColor3f 0.0, 1.0, 0.0
|
76
|
+
glVertex3f 1.0, -1.0, 0.0
|
77
|
+
glColor3f 0.0, 0.0, 1.0
|
78
|
+
glVertex3f -1.0, -1.0, 0.0
|
79
|
+
end
|
80
|
+
|
81
|
+
# Move right 3 units
|
82
|
+
glLoadIdentity
|
83
|
+
glTranslatef 1.5, 0.0, -6.0
|
84
|
+
|
85
|
+
# Draw a quadrilateral
|
86
|
+
# Rotate the quad on the X-axis
|
87
|
+
glRotatef $quad_angle, 1.0, 0.0, 0.0
|
88
|
+
# Set it to a blue color one time only
|
89
|
+
glColor3f 0.5, 0.5, 1.0
|
90
|
+
glBegin GL_QUADS do
|
91
|
+
glVertex3f -1.0, 1.0, 0.0
|
92
|
+
glVertex3f 1.0, 1.0, 0.0
|
93
|
+
glVertex3f 1.0, -1.0, 0.0
|
94
|
+
glVertex3f -1.0, -1.0, 0.0
|
95
|
+
end
|
96
|
+
|
97
|
+
$triangle_angle += 0.2
|
98
|
+
$quad_angle -= 0.15
|
99
|
+
|
100
|
+
# Swap buffers for display
|
101
|
+
glutSwapBuffers
|
102
|
+
end
|
103
|
+
|
104
|
+
# The idle function to handle
|
105
|
+
def idle
|
106
|
+
glutPostRedisplay
|
107
|
+
end
|
108
|
+
|
109
|
+
# Keyboard handler to exit when ESC is typed
|
110
|
+
keyboard = lambda do |key, x, y|
|
111
|
+
case key
|
112
|
+
when ?\e
|
113
|
+
glutDestroyWindow $window
|
114
|
+
exit 0
|
115
|
+
end
|
116
|
+
glutPostRedisplay
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
# Initliaze our GLUT code
|
121
|
+
glutInit
|
122
|
+
# Setup a double buffer, RGBA color, alpha components and depth buffer
|
123
|
+
glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
|
124
|
+
glutInitWindowSize 640, 480
|
125
|
+
glutInitWindowPosition 0, 0
|
126
|
+
$window = glutCreateWindow "NeHe Lesson 04 - ruby-opengl version"
|
127
|
+
glutDisplayFunc :draw_gl_scene
|
128
|
+
glutReshapeFunc :reshape
|
129
|
+
glutIdleFunc :idle
|
130
|
+
glutKeyboardFunc keyboard
|
131
|
+
init_gl_window 640, 480
|
132
|
+
glutMainLoop
|
@@ -0,0 +1,182 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'opengl'
|
4
|
+
require 'glu'
|
5
|
+
require 'glut'
|
6
|
+
|
7
|
+
class Lesson05
|
8
|
+
include Gl
|
9
|
+
include Glu
|
10
|
+
include Glut
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@pyramid_angle = 0
|
14
|
+
@cube_angle = 0
|
15
|
+
|
16
|
+
# Initliaze our GLUT code
|
17
|
+
glutInit
|
18
|
+
# Setup a double buffer, RGBA color, alpha components and depth buffer
|
19
|
+
glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
|
20
|
+
glutInitWindowSize 640, 480
|
21
|
+
glutInitWindowPosition 0, 0
|
22
|
+
@window = glutCreateWindow "NeHe Lesson 05 - ruby-opengl version"
|
23
|
+
glutDisplayFunc :draw_gl_scene
|
24
|
+
glutReshapeFunc :reshape
|
25
|
+
glutIdleFunc :idle
|
26
|
+
glutKeyboardFunc :keyboard
|
27
|
+
init_gl_window 640, 480
|
28
|
+
glutMainLoop
|
29
|
+
end
|
30
|
+
|
31
|
+
def init_gl_window width = 640, height = 480
|
32
|
+
# Background color to black
|
33
|
+
glClearColor 0.0, 0.0, 0.0, 0
|
34
|
+
# Enables clearing of depth buffer
|
35
|
+
glClearDepth 1.0
|
36
|
+
# Set type of depth test
|
37
|
+
glDepthFunc GL_LEQUAL
|
38
|
+
# Enable depth testing
|
39
|
+
glEnable GL_DEPTH_TEST
|
40
|
+
# Enable smooth color shading
|
41
|
+
glShadeModel GL_SMOOTH
|
42
|
+
|
43
|
+
glMatrixMode GL_PROJECTION
|
44
|
+
glLoadIdentity
|
45
|
+
# Calculate aspect ratio of the window
|
46
|
+
gluPerspective 45.0, width / height, 0.1, 100.0
|
47
|
+
|
48
|
+
glMatrixMode GL_MODELVIEW
|
49
|
+
|
50
|
+
draw_gl_scene
|
51
|
+
end
|
52
|
+
|
53
|
+
def reshape width, height
|
54
|
+
height = 1 if height == 0
|
55
|
+
|
56
|
+
# Reset current viewpoint and perspective transformation
|
57
|
+
glViewport 0, 0, width, height
|
58
|
+
|
59
|
+
glMatrixMode GL_PROJECTION
|
60
|
+
glLoadIdentity
|
61
|
+
|
62
|
+
gluPerspective 45.0, width / height, 0.1, 100.0
|
63
|
+
end
|
64
|
+
|
65
|
+
def draw_gl_scene
|
66
|
+
# Clear the screen and depth buffer
|
67
|
+
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
|
68
|
+
|
69
|
+
# Reset the view
|
70
|
+
glMatrixMode GL_MODELVIEW
|
71
|
+
glLoadIdentity
|
72
|
+
|
73
|
+
# Move left 1.5 units and into the screen 6.0 units
|
74
|
+
glTranslatef -1.5, 0.0, -6.0
|
75
|
+
|
76
|
+
# Rotate the pyramid on the Y-axis
|
77
|
+
glRotatef @pyramid_angle, 0.0, 1.0, 0.0
|
78
|
+
|
79
|
+
# Draw a pyramid
|
80
|
+
glBegin GL_POLYGON do
|
81
|
+
# Draw front side of pyramid
|
82
|
+
glColor3f 1.0, 0.0, 0.0
|
83
|
+
glVertex3f 0.0, 1.0, 0.0
|
84
|
+
glColor3f 0.0, 1.0, 0.0
|
85
|
+
glVertex3f -1.0, -1.0, 1.0
|
86
|
+
glColor3f 0.0, 0.0, 1.0
|
87
|
+
glVertex3f 1.0, -1.0, 1.0
|
88
|
+
|
89
|
+
# Draw right side of pyramid
|
90
|
+
glColor3f 1.0, 0.0, 0.0
|
91
|
+
glVertex3f 0.0, 1.0, 0.0
|
92
|
+
glColor3f 0.0, 0.0, 1.0
|
93
|
+
glVertex3f 1.0, -1.0, 1.0
|
94
|
+
glColor3f 0.0, 1.0, 0.0
|
95
|
+
glVertex3f 1.0, -1.0, -1.0
|
96
|
+
|
97
|
+
# Draw back side of pyramid
|
98
|
+
glColor3f 1.0, 0.0, 0.0
|
99
|
+
glVertex3f 0.0, 1.0, 0.0
|
100
|
+
glColor3f 0.0, 1.0, 0.0
|
101
|
+
glVertex3f 1.0, -1.0, -1.0
|
102
|
+
glColor3f 0.0, 0.0, 1.0
|
103
|
+
glVertex3f -1.0, -1.0, -1.0
|
104
|
+
|
105
|
+
# Draw left side of pyramid
|
106
|
+
glColor3f 1.0, 0.0, 0.0
|
107
|
+
glVertex3f 0.0, 1.0, 0.0
|
108
|
+
glColor3f 0.0, 0.0, 1.0
|
109
|
+
glVertex3f -1.0, -1.0, -1.0
|
110
|
+
glColor3f 0.0, 1.0, 0.0
|
111
|
+
glVertex3f -1.0, -1.0, 1.0
|
112
|
+
end
|
113
|
+
|
114
|
+
glLoadIdentity
|
115
|
+
# Move right 3 units
|
116
|
+
glTranslatef 1.5, 0.0, -7.0
|
117
|
+
|
118
|
+
# Draw a cube
|
119
|
+
# Rotate the cube on the X, Y and Z axis
|
120
|
+
glRotatef @cube_angle, 1.0, 1.0, 1.0
|
121
|
+
# Set it to a blue color one time only
|
122
|
+
glBegin GL_QUADS do
|
123
|
+
# Draw the top side in green
|
124
|
+
glColor3f 0.0, 1.0, 0.0
|
125
|
+
glVertex3f 1.0, 1.0, -1.0
|
126
|
+
glVertex3f -1.0, 1.0, -1.0
|
127
|
+
glVertex3f -1.0, 1.0, 1.0
|
128
|
+
glVertex3f 1.0, 1.0, 1.0
|
129
|
+
# Draw the bottom side in orange
|
130
|
+
glColor3f 1.0, 0.5, 0.0
|
131
|
+
glVertex3f 1.0, -1.0, 1.0
|
132
|
+
glVertex3f -1.0, -1.0, 1.0
|
133
|
+
glVertex3f -1.0, -1.0, -1.0
|
134
|
+
glVertex3f 1.0, -1.0, -1.0
|
135
|
+
# Draw the front side in red
|
136
|
+
glColor3f 1.0, 0.0, 0.0
|
137
|
+
glVertex3f 1.0, 1.0, 1.0
|
138
|
+
glVertex3f -1.0, 1.0, 1.0
|
139
|
+
glVertex3f -1.0, -1.0, 1.0
|
140
|
+
glVertex3f 1.0, -1.0, 1.0
|
141
|
+
# Draw the back side in yellow
|
142
|
+
glColor3f 1.0, 1.0, 0.0
|
143
|
+
glVertex3f 1.0, -1.0, -1.0
|
144
|
+
glVertex3f -1.0, -1.0, -1.0
|
145
|
+
glVertex3f -1.0, 1.0, -1.0
|
146
|
+
glVertex3f 1.0, 1.0, -1.0
|
147
|
+
# Draw the left side in blue
|
148
|
+
glColor3f 0.0, 0.0, 1.0
|
149
|
+
glVertex3f -1.0, 1.0, 1.0
|
150
|
+
glVertex3f -1.0, 1.0, -1.0
|
151
|
+
glVertex3f -1.0, -1.0, -1.0
|
152
|
+
glVertex3f -1.0, -1.0, 1.0
|
153
|
+
# Draw the right side in violet
|
154
|
+
glColor3f 1.0, 0.0, 1.0
|
155
|
+
glVertex3f 1.0, 1.0, -1.0
|
156
|
+
glVertex3f 1.0, 1.0, 1.0
|
157
|
+
glVertex3f 1.0, -1.0, 1.0
|
158
|
+
glVertex3f 1.0, -1.0, -1.0
|
159
|
+
end
|
160
|
+
|
161
|
+
@pyramid_angle += 0.2
|
162
|
+
@cube_angle -= 0.15
|
163
|
+
# Swap buffers for display
|
164
|
+
glutSwapBuffers
|
165
|
+
end
|
166
|
+
|
167
|
+
def idle
|
168
|
+
glutPostRedisplay
|
169
|
+
end
|
170
|
+
|
171
|
+
def keyboard key, x, y
|
172
|
+
case key
|
173
|
+
when ?\e
|
174
|
+
glutDestroyWindow @window
|
175
|
+
exit 0
|
176
|
+
end
|
177
|
+
glutPostRedisplay
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
Lesson05.new if $0 == __FILE__
|
@@ -0,0 +1,186 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'opengl'
|
4
|
+
require 'glu'
|
5
|
+
require 'glut'
|
6
|
+
require 'chunky_png'
|
7
|
+
|
8
|
+
class Lesson06
|
9
|
+
include Gl
|
10
|
+
include Glu
|
11
|
+
include Glut
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@textures = nil
|
15
|
+
@xrot = 0.0
|
16
|
+
@yrot = 0.0
|
17
|
+
@zrot = 0.0
|
18
|
+
@fullscreen = true
|
19
|
+
|
20
|
+
glutInit
|
21
|
+
|
22
|
+
glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
|
23
|
+
glutInitWindowSize 640, 480
|
24
|
+
glutInitWindowPosition 0, 0
|
25
|
+
|
26
|
+
@window = glutCreateWindow "NeHe Lesson 06 - ruby-opengl version"
|
27
|
+
|
28
|
+
glutDisplayFunc :draw_gl_scene
|
29
|
+
glutReshapeFunc :reshape
|
30
|
+
glutIdleFunc :idle
|
31
|
+
glutKeyboardFunc :keyboard
|
32
|
+
|
33
|
+
reshape 640, 480
|
34
|
+
load_texture
|
35
|
+
init_gl
|
36
|
+
|
37
|
+
glutMainLoop
|
38
|
+
end
|
39
|
+
|
40
|
+
def init_gl
|
41
|
+
glEnable GL_TEXTURE_2D
|
42
|
+
glShadeModel GL_SMOOTH
|
43
|
+
glClearColor 0.0, 0.0, 0.0, 0.5
|
44
|
+
glClearDepth 1.0
|
45
|
+
glEnable GL_DEPTH_TEST
|
46
|
+
glDepthFunc GL_LEQUAL
|
47
|
+
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
|
48
|
+
end
|
49
|
+
|
50
|
+
def reshape width, height
|
51
|
+
width = width.to_f
|
52
|
+
height = height.to_f
|
53
|
+
height = 1.0 if height.zero?
|
54
|
+
|
55
|
+
glViewport 0, 0, width, height
|
56
|
+
|
57
|
+
glMatrixMode GL_PROJECTION
|
58
|
+
glLoadIdentity
|
59
|
+
|
60
|
+
gluPerspective 45.0, width / height, 0.1, 100.0
|
61
|
+
|
62
|
+
glMatrixMode GL_MODELVIEW
|
63
|
+
glLoadIdentity
|
64
|
+
end
|
65
|
+
|
66
|
+
def draw_gl_scene
|
67
|
+
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
|
68
|
+
glMatrixMode GL_MODELVIEW
|
69
|
+
glLoadIdentity
|
70
|
+
glTranslatef 0.0, 0.0, -7.0
|
71
|
+
|
72
|
+
glRotatef @xrot, 1.0, 0.0, 0.0
|
73
|
+
glRotatef @yrot, 0.0, 1.0, 0.0
|
74
|
+
glRotatef @zrot, 0.0, 0.0, 1.0
|
75
|
+
|
76
|
+
glBindTexture GL_TEXTURE_2D, @textures[0]
|
77
|
+
|
78
|
+
glBegin GL_QUADS do
|
79
|
+
# front face
|
80
|
+
glTexCoord2f(0.0, 1.0)
|
81
|
+
glVertex3f(-1.0, -1.0, 1.0)
|
82
|
+
glTexCoord2f(1.0, 1.0)
|
83
|
+
glVertex3f( 1.0, -1.0, 1.0)
|
84
|
+
glTexCoord2f(1.0, 0.0)
|
85
|
+
glVertex3f( 1.0, 1.0, 1.0)
|
86
|
+
glTexCoord2f(0.0, 0.0)
|
87
|
+
glVertex3f(-1.0, 1.0, 1.0)
|
88
|
+
|
89
|
+
# back face
|
90
|
+
glTexCoord2f(0.0, 1.0)
|
91
|
+
glVertex3f(-1.0, -1.0, -1.0)
|
92
|
+
glTexCoord2f(1.0, 1.0)
|
93
|
+
glVertex3f(-1.0, 1.0, -1.0)
|
94
|
+
glTexCoord2f(1.0, 0.0)
|
95
|
+
glVertex3f( 1.0, 1.0, -1.0)
|
96
|
+
glTexCoord2f(0.0, 0.0)
|
97
|
+
glVertex3f( 1.0, -1.0, -1.0)
|
98
|
+
|
99
|
+
# top face
|
100
|
+
glTexCoord2f(0.0, 1.0)
|
101
|
+
glVertex3f(-1.0, 1.0, -1.0)
|
102
|
+
glTexCoord2f(1.0, 1.0)
|
103
|
+
glVertex3f(-1.0, 1.0, 1.0)
|
104
|
+
glTexCoord2f(1.0, 0.0)
|
105
|
+
glVertex3f( 1.0, 1.0, 1.0)
|
106
|
+
glTexCoord2f(0.0, 0.0)
|
107
|
+
glVertex3f( 1.0, 1.0, -1.0)
|
108
|
+
|
109
|
+
# bottom face
|
110
|
+
glTexCoord2f(1.0, 1.0)
|
111
|
+
glVertex3f(-1.0, -1.0, -1.0)
|
112
|
+
glTexCoord2f(1.0, 0.0)
|
113
|
+
glVertex3f( 1.0, -1.0, -1.0)
|
114
|
+
glTexCoord2f(0.0, 0.0)
|
115
|
+
glVertex3f( 1.0, -1.0, 1.0)
|
116
|
+
glTexCoord2f(0.0, 1.0)
|
117
|
+
glVertex3f(-1.0, -1.0, 1.0)
|
118
|
+
|
119
|
+
# right face
|
120
|
+
glTexCoord2f(1.0, 0.0)
|
121
|
+
glVertex3f( 1.0, -1.0, -1.0)
|
122
|
+
glTexCoord2f(0.0, 0.0)
|
123
|
+
glVertex3f( 1.0, 1.0, -1.0)
|
124
|
+
glTexCoord2f(0.0, 1.0)
|
125
|
+
glVertex3f( 1.0, 1.0, 1.0)
|
126
|
+
glTexCoord2f(1.0, 1.0)
|
127
|
+
glVertex3f( 1.0, -1.0, 1.0)
|
128
|
+
|
129
|
+
# left face
|
130
|
+
glTexCoord2f(0.0, 0.0)
|
131
|
+
glVertex3f(-1.0, -1.0, -1.0)
|
132
|
+
glTexCoord2f(0.0, 1.0)
|
133
|
+
glVertex3f(-1.0, -1.0, 1.0)
|
134
|
+
glTexCoord2f(1.0, 1.0)
|
135
|
+
glVertex3f(-1.0, 1.0, 1.0)
|
136
|
+
glTexCoord2f(1.0, 0.0)
|
137
|
+
glVertex3f(-1.0, 1.0, -1.0)
|
138
|
+
end
|
139
|
+
|
140
|
+
@xrot += 0.003
|
141
|
+
@yrot += 0.002
|
142
|
+
@zrot += 0.004
|
143
|
+
|
144
|
+
glutSwapBuffers
|
145
|
+
end
|
146
|
+
|
147
|
+
def idle
|
148
|
+
glutPostRedisplay
|
149
|
+
end
|
150
|
+
|
151
|
+
def keyboard key, x, y
|
152
|
+
case key
|
153
|
+
when ?\e
|
154
|
+
glutDestroyWindow @window
|
155
|
+
exit 0
|
156
|
+
when 'F' then
|
157
|
+
@fullscreen = !@fullscreen
|
158
|
+
|
159
|
+
if @fullscreen then
|
160
|
+
glutFullScreen
|
161
|
+
else
|
162
|
+
glutPositionWindow 0, 0
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
glutPostRedisplay
|
167
|
+
end
|
168
|
+
|
169
|
+
def load_texture
|
170
|
+
png = ChunkyPNG::Image.from_file(File.expand_path('../NeHe.png', __FILE__))
|
171
|
+
|
172
|
+
height = png.height
|
173
|
+
width = png.width
|
174
|
+
|
175
|
+
image = png.to_rgba_stream.each_byte.to_a
|
176
|
+
|
177
|
+
@textures = glGenTextures 1
|
178
|
+
glBindTexture GL_TEXTURE_2D, @textures[0]
|
179
|
+
glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
|
180
|
+
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
|
181
|
+
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
Lesson06.new
|