danabr75-opengl 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +29 -0
  3. data/.gemtest +0 -0
  4. data/.gitignore +6 -0
  5. data/.travis.yml +19 -0
  6. data/Gemfile +8 -0
  7. data/History.md +101 -0
  8. data/MIT-LICENSE +18 -0
  9. data/Manifest.txt +142 -0
  10. data/README.rdoc +105 -0
  11. data/Rakefile +170 -0
  12. data/danabr75-opengl.gemspec +32 -0
  13. data/examples/NeHe/NeHe.png +0 -0
  14. data/examples/NeHe/crate.png +0 -0
  15. data/examples/NeHe/glass.png +0 -0
  16. data/examples/NeHe/nehe_lesson02.rb +117 -0
  17. data/examples/NeHe/nehe_lesson03.rb +123 -0
  18. data/examples/NeHe/nehe_lesson04.rb +132 -0
  19. data/examples/NeHe/nehe_lesson05.rb +182 -0
  20. data/examples/NeHe/nehe_lesson06.rb +186 -0
  21. data/examples/NeHe/nehe_lesson07.rb +240 -0
  22. data/examples/NeHe/nehe_lesson08.rb +255 -0
  23. data/examples/NeHe/nehe_lesson09.rb +203 -0
  24. data/examples/NeHe/nehe_lesson11.rb +176 -0
  25. data/examples/NeHe/nehe_lesson12.rb +203 -0
  26. data/examples/NeHe/nehe_lesson16.rb +211 -0
  27. data/examples/NeHe/nehe_lesson19.rb +209 -0
  28. data/examples/NeHe/nehe_lesson36.rb +308 -0
  29. data/examples/NeHe/particle.png +0 -0
  30. data/examples/NeHe/star.png +0 -0
  31. data/examples/NeHe/tim.png +0 -0
  32. data/examples/OrangeBook/3Dlabs-License.txt +33 -0
  33. data/examples/OrangeBook/brick.frag +36 -0
  34. data/examples/OrangeBook/brick.rb +378 -0
  35. data/examples/OrangeBook/brick.vert +41 -0
  36. data/examples/OrangeBook/particle.frag +17 -0
  37. data/examples/OrangeBook/particle.rb +408 -0
  38. data/examples/OrangeBook/particle.vert +38 -0
  39. data/examples/README +16 -0
  40. data/examples/RedBook/aapoly.rb +143 -0
  41. data/examples/RedBook/aargb.rb +121 -0
  42. data/examples/RedBook/accanti.rb +164 -0
  43. data/examples/RedBook/accpersp.rb +217 -0
  44. data/examples/RedBook/alpha.rb +125 -0
  45. data/examples/RedBook/alpha3D.rb +160 -0
  46. data/examples/RedBook/bezcurve.rb +107 -0
  47. data/examples/RedBook/bezmesh.rb +139 -0
  48. data/examples/RedBook/checker.rb +126 -0
  49. data/examples/RedBook/clip.rb +97 -0
  50. data/examples/RedBook/colormat.rb +137 -0
  51. data/examples/RedBook/cube.rb +71 -0
  52. data/examples/RedBook/depthcue.rb +101 -0
  53. data/examples/RedBook/dof.rb +206 -0
  54. data/examples/RedBook/double.rb +107 -0
  55. data/examples/RedBook/drawf.rb +93 -0
  56. data/examples/RedBook/feedback.rb +147 -0
  57. data/examples/RedBook/fog.rb +168 -0
  58. data/examples/RedBook/font.rb +153 -0
  59. data/examples/RedBook/hello.rb +81 -0
  60. data/examples/RedBook/image.rb +139 -0
  61. data/examples/RedBook/jitter.rb +207 -0
  62. data/examples/RedBook/light.rb +157 -0
  63. data/examples/RedBook/lines.rb +130 -0
  64. data/examples/RedBook/list.rb +113 -0
  65. data/examples/RedBook/material.rb +276 -0
  66. data/examples/RedBook/mipmap.rb +158 -0
  67. data/examples/RedBook/model.rb +115 -0
  68. data/examples/RedBook/movelight.rb +134 -0
  69. data/examples/RedBook/pickdepth.rb +181 -0
  70. data/examples/RedBook/planet.rb +110 -0
  71. data/examples/RedBook/quadric.rb +160 -0
  72. data/examples/RedBook/robot.rb +117 -0
  73. data/examples/RedBook/select.rb +198 -0
  74. data/examples/RedBook/smooth.rb +97 -0
  75. data/examples/RedBook/stencil.rb +165 -0
  76. data/examples/RedBook/stroke.rb +169 -0
  77. data/examples/RedBook/surface.rb +168 -0
  78. data/examples/RedBook/teaambient.rb +134 -0
  79. data/examples/RedBook/teapots.rb +184 -0
  80. data/examples/RedBook/tess.rb +185 -0
  81. data/examples/RedBook/texbind.rb +149 -0
  82. data/examples/RedBook/texgen.rb +171 -0
  83. data/examples/RedBook/texturesurf.rb +130 -0
  84. data/examples/RedBook/varray.rb +161 -0
  85. data/examples/RedBook/wrap.rb +150 -0
  86. data/examples/misc/OGLBench.rb +338 -0
  87. data/examples/misc/anisotropic.rb +196 -0
  88. data/examples/misc/fbo_test.rb +357 -0
  89. data/examples/misc/font-glut.rb +47 -0
  90. data/examples/misc/glfwtest.rb +30 -0
  91. data/examples/misc/plane.rb +161 -0
  92. data/examples/misc/readpixel.rb +66 -0
  93. data/examples/misc/sdltest.rb +36 -0
  94. data/examples/misc/trislam.rb +829 -0
  95. data/ext/opengl/GL/gl.h +2115 -0
  96. data/ext/opengl/GL/glext.h +11770 -0
  97. data/ext/opengl/common.h +372 -0
  98. data/ext/opengl/conv.h +247 -0
  99. data/ext/opengl/extconf.rb +19 -0
  100. data/ext/opengl/fptr_struct.h +912 -0
  101. data/ext/opengl/funcdef.h +279 -0
  102. data/ext/opengl/gl-1.0-1.1.c +3345 -0
  103. data/ext/opengl/gl-1.2.c +155 -0
  104. data/ext/opengl/gl-1.3.c +447 -0
  105. data/ext/opengl/gl-1.4.c +346 -0
  106. data/ext/opengl/gl-1.5.c +225 -0
  107. data/ext/opengl/gl-2.0.c +665 -0
  108. data/ext/opengl/gl-2.1.c +57 -0
  109. data/ext/opengl/gl-3.0.c +502 -0
  110. data/ext/opengl/gl-enums.c +4853 -0
  111. data/ext/opengl/gl-enums.h +14528 -0
  112. data/ext/opengl/gl-error.c +110 -0
  113. data/ext/opengl/gl-error.h +25 -0
  114. data/ext/opengl/gl-ext-3dfx.c +27 -0
  115. data/ext/opengl/gl-ext-arb.c +880 -0
  116. data/ext/opengl/gl-ext-ati.c +41 -0
  117. data/ext/opengl/gl-ext-ext.c +885 -0
  118. data/ext/opengl/gl-ext-gremedy.c +41 -0
  119. data/ext/opengl/gl-ext-nv.c +679 -0
  120. data/ext/opengl/gl.c +265 -0
  121. data/ext/opengl/gl_buffer.c +202 -0
  122. data/ext/opengl/glimpl.c +187 -0
  123. data/ext/opengl/glimpl.h +47 -0
  124. data/ext/opengl/opengl.c +9 -0
  125. data/lib/gl.rb +1 -0
  126. data/lib/opengl.rb +79 -0
  127. data/lib/opengl/bindings_version.rb +4 -0
  128. data/lib/opengl/implementation.rb +38 -0
  129. data/lib/opengl/test_case.rb +86 -0
  130. data/test/dummy.xorg.conf +140 -0
  131. data/test/test_gl.rb +45 -0
  132. data/test/test_gl_10_11.rb +1363 -0
  133. data/test/test_gl_12.rb +71 -0
  134. data/test/test_gl_13.rb +221 -0
  135. data/test/test_gl_14.rb +218 -0
  136. data/test/test_gl_15.rb +258 -0
  137. data/test/test_gl_20.rb +417 -0
  138. data/test/test_gl_21.rb +436 -0
  139. data/test/test_gl_ext_arb.rb +508 -0
  140. data/test/test_gl_ext_ati.rb +28 -0
  141. data/test/test_gl_ext_ext.rb +608 -0
  142. data/test/test_gl_ext_gremedy.rb +29 -0
  143. data/test/test_gl_ext_nv.rb +349 -0
  144. data/test/test_glimpl.rb +23 -0
  145. data/test/test_opengl_buffer.rb +122 -0
  146. data/utils/README +6 -0
  147. data/utils/enumgen.rb +108 -0
  148. data/utils/extlistgen.rb +90 -0
  149. metadata +326 -0
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'opengl'
4
+ require 'glu'
5
+ require 'glut'
6
+ require 'chunky_png'
7
+
8
+ class Lesson09
9
+ include Gl
10
+ include Glu
11
+ include Glut
12
+
13
+ Star = Struct.new :r, :g, :b, :distance, :angle
14
+
15
+ def initialize
16
+ @textures = nil
17
+ @filter = 0
18
+ @twinkle = false
19
+ @zoom = -15.0
20
+ @tilt = 90.0
21
+ @spin = 0.0
22
+ @fullscreen = false
23
+ @last_draw = Time.now
24
+
25
+ @star_count = 50
26
+ @stars = (0...@star_count).map do |i|
27
+ distance = i.to_f / @star_count * 5.0
28
+ angle = 0.0
29
+
30
+ Star.new rand(256), rand(256), rand(256), distance, angle
31
+ end
32
+
33
+ glutInit
34
+
35
+ glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
36
+ glutInitWindowSize 640, 480
37
+ glutInitWindowPosition 0, 0
38
+
39
+ @window = glutCreateWindow "NeHe Lesson 09 - ruby-opengl version"
40
+
41
+ glutDisplayFunc :draw_gl_scene
42
+ glutReshapeFunc :reshape
43
+ glutIdleFunc :idle
44
+ glutKeyboardFunc :keyboard
45
+
46
+ reshape 640, 480
47
+ load_texture
48
+ init_gl
49
+
50
+ glutMainLoop
51
+ end
52
+
53
+ def init_gl
54
+ glEnable GL_TEXTURE_2D
55
+ glShadeModel GL_SMOOTH
56
+ glClearColor 0.0, 0.0, 0.0, 0.5
57
+ glClearDepth 1.0
58
+ glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
59
+ glBlendFunc GL_SRC_ALPHA, GL_ONE
60
+ glEnable GL_BLEND
61
+
62
+ true
63
+ end
64
+
65
+ def reshape width, height
66
+ width = width.to_f
67
+ height = height.to_f
68
+ height = 1.0 if height.zero?
69
+
70
+ glViewport 0, 0, width, height
71
+
72
+ glMatrixMode GL_PROJECTION
73
+ glLoadIdentity
74
+
75
+ gluPerspective 45.0, width / height, 0.1, 100.0
76
+
77
+ glMatrixMode GL_MODELVIEW
78
+ glLoadIdentity
79
+
80
+ true
81
+ end
82
+
83
+ def draw_gl_scene
84
+ return if 0.01 > Time.now - @last_draw
85
+ @last_draw = Time.now
86
+
87
+ glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
88
+ glBindTexture GL_TEXTURE_2D, @textures[@filter]
89
+
90
+ @stars.each_with_index do |star, i|
91
+ glLoadIdentity
92
+ glTranslatef 0.0, 0.0, @zoom
93
+ glRotatef @tilt, 1.0, 0.0, 0.0
94
+ glRotatef star.angle, 0.0, 1.0, 0.0
95
+
96
+ glTranslatef star.distance, 0.0, 0.0
97
+
98
+ glRotatef star.angle, 0.0, -1.0, 0.0
99
+ glRotatef @tilt, -1.0, 0.0, 0.0
100
+
101
+ if @twinkle then
102
+ prev = @stars[i - 1]
103
+ glColor4ub prev.r, prev.g, prev.b, 255
104
+
105
+ glBegin GL_QUADS do
106
+ glTexCoord2f 0.0, 0.0; glVertex3f -1.0, -1.0, 0.0
107
+ glTexCoord2f 1.0, 0.0; glVertex3f 1.0, -1.0, 0.0
108
+ glTexCoord2f 1.0, 1.0; glVertex3f 1.0, 1.0, 0.0
109
+ glTexCoord2f 0.0, 1.0; glVertex3f -1.0, 1.0, 0.0
110
+ end
111
+ end
112
+
113
+ glRotatef @spin, 0.0, 0.0, 1.0
114
+ glColor4ub star.r, star.g, star.b, 255
115
+
116
+ glBegin GL_QUADS do
117
+ glTexCoord2f 0.0, 0.0; glVertex3f -1.0, -1.0, 0.0
118
+ glTexCoord2f 1.0, 0.0; glVertex3f 1.0, -1.0, 0.0
119
+ glTexCoord2f 1.0, 1.0; glVertex3f 1.0, 1.0, 0.0
120
+ glTexCoord2f 0.0, 1.0; glVertex3f -1.0, 1.0, 0.0
121
+ end
122
+
123
+ @spin += 0.01
124
+ star.angle += i.to_f / @star_count
125
+ star.distance -= 0.01
126
+
127
+ if star.distance < 0 then
128
+ star.distance += 5.0
129
+ star.r = rand 256
130
+ star.g = rand 256
131
+ star.b = rand 256
132
+ end
133
+ end
134
+
135
+ glutSwapBuffers
136
+ end
137
+
138
+ def idle
139
+ glutPostRedisplay
140
+ end
141
+
142
+ def keyboard key, x, y
143
+ case key
144
+ when ?\e then
145
+ glutDestroyWindow @window
146
+ exit 0
147
+ when 'F' then
148
+ @fullscreen = !@fullscreen
149
+
150
+ if @fullscreen then
151
+ glutFullScreen
152
+ else
153
+ glutPositionWindow 0, 0
154
+ end
155
+ when 'f' then
156
+ @filter += 1
157
+ @filter %= 3
158
+
159
+ puts "texture #{@filter}"
160
+ when 'W' then
161
+ @twinkle = !@twinkle
162
+
163
+ puts "twinkle #{@twinkle ? 'on' : 'off'}"
164
+ when 'T' then @tilt += 0.5
165
+ when 't' then @tilt -= 0.5
166
+ when 'Z' then @zoom += 0.2
167
+ when 'z' then @zoom -= 0.2
168
+ else
169
+ puts key
170
+ end
171
+
172
+ glutPostRedisplay
173
+ end
174
+
175
+ def load_texture
176
+
177
+ png = ChunkyPNG::Image.from_file(File.expand_path('../star.png', __FILE__))
178
+
179
+ height = png.height
180
+ width = png.width
181
+
182
+ image = png.to_rgba_stream
183
+
184
+ @textures = glGenTextures 3
185
+ glBindTexture GL_TEXTURE_2D, @textures[0]
186
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST
187
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST
188
+ glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
189
+
190
+ glBindTexture GL_TEXTURE_2D, @textures[1]
191
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
192
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
193
+ glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
194
+
195
+ glBindTexture GL_TEXTURE_2D, @textures[2]
196
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
197
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST
198
+ gluBuild2DMipmaps GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image
199
+ end
200
+
201
+ end
202
+
203
+ Lesson09.new
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'opengl'
4
+ require 'glu'
5
+ require 'glut'
6
+ require 'chunky_png'
7
+
8
+ class Lesson11
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
+
19
+ factor = 10.0 / 9 * Math::PI
20
+ @points = Array.new 45 do |x|
21
+ Array.new 45 do |y|
22
+ [x / 5.0 - 4.5, y / 5.0 - 4.5,
23
+ Math.sin(x / factor)]
24
+ end
25
+ end
26
+
27
+ @wiggle_count = 0
28
+
29
+ glutInit
30
+
31
+ glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
32
+ glutInitWindowSize 640, 480
33
+ glutInitWindowPosition 0, 0
34
+
35
+ @window = glutCreateWindow "NeHe Lesson 11 - ruby-opengl version"
36
+
37
+ glutDisplayFunc :draw_gl_scene
38
+ glutReshapeFunc :reshape
39
+ glutIdleFunc :idle
40
+ glutKeyboardFunc :keyboard
41
+
42
+ reshape 640, 480
43
+ load_texture
44
+ init_gl
45
+
46
+ glutMainLoop
47
+ end
48
+
49
+ def init_gl
50
+ glEnable GL_TEXTURE_2D
51
+ glShadeModel GL_SMOOTH
52
+ glClearColor 0.0, 0.0, 0.0, 0.5
53
+ glClearDepth 1.0
54
+ glEnable GL_DEPTH_TEST
55
+ glDepthFunc GL_LEQUAL
56
+ glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
57
+
58
+ glPolygonMode GL_BACK, GL_FILL
59
+ glPolygonMode GL_FRONT, GL_LINE
60
+
61
+ true
62
+ end
63
+
64
+ def reshape width, height
65
+ width = width.to_f
66
+ height = height.to_f
67
+ height = 1.0 if height.zero?
68
+
69
+ glViewport 0, 0, width, height
70
+
71
+ glMatrixMode GL_PROJECTION
72
+ glLoadIdentity
73
+
74
+ gluPerspective 45.0, width / height, 0.1, 100.0
75
+
76
+ glMatrixMode GL_MODELVIEW
77
+ glLoadIdentity
78
+
79
+ true
80
+ end
81
+
82
+ def draw_gl_scene
83
+ glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
84
+ glLoadIdentity
85
+
86
+ glTranslatef 0.0, 0.0, -12.0
87
+ glRotatef @xrot, 1.0, 0.0, 0.0
88
+ glRotatef @yrot, 0.0, 1.0, 0.0
89
+ glRotatef @zrot, 0.0, 0.0, 1.0
90
+
91
+ glBindTexture GL_TEXTURE_2D, @textures[0]
92
+
93
+ glBegin GL_QUADS do
94
+ (0...44).each do |x|
95
+ (0...44).each do |y|
96
+ tex_x = x / 44.0
97
+ tex_y = y / 44.0
98
+ tex_xb = (x + 1) / 44.0
99
+ tex_yb = (y + 1) / 44.0
100
+
101
+ glTexCoord2f tex_x, tex_y
102
+ glVertex3f @points[x][y][0], @points[x][y][1], @points[x][y][2]
103
+
104
+ glTexCoord2f tex_x, tex_yb
105
+ glVertex3f @points[x][y+1][0], @points[x][y+1][1], @points[x][y+1][2]
106
+
107
+ glTexCoord2f tex_xb, tex_yb
108
+ glVertex3f @points[x+1][y+1][0], @points[x+1][y+1][1], @points[x+1][y+1][2]
109
+
110
+ glTexCoord2f tex_xb, tex_y
111
+ glVertex3f @points[x+1][y][0], @points[x+1][y][1], @points[x+1][y][2]
112
+ end
113
+ end
114
+ end
115
+
116
+ if @wiggle_count == 4 then
117
+ (0...45).each do |y|
118
+ tmp = @points[0][y][2]
119
+ (0...44).each do |x|
120
+ @points[x][y][2] = @points[x+1][y][2]
121
+ end
122
+ @points[44][y][2] = tmp
123
+ end
124
+
125
+ @wiggle_count = 0
126
+ end
127
+
128
+ @wiggle_count += 1
129
+
130
+ @xrot += 0.03
131
+ @yrot += 0.02
132
+ @zrot += 0.04
133
+
134
+ glutSwapBuffers
135
+ end
136
+
137
+ def idle
138
+ glutPostRedisplay
139
+ end
140
+
141
+ def keyboard key, x, y
142
+ case key
143
+ when ?\e
144
+ glutDestroyWindow @window
145
+ exit 0
146
+ when 'F' then
147
+ @fullscreen = !@fullscreen
148
+
149
+ if @fullscreen then
150
+ glutFullScreen
151
+ else
152
+ glutPositionWindow 0, 0
153
+ end
154
+ end
155
+
156
+ glutPostRedisplay
157
+ end
158
+
159
+ def load_texture
160
+ png = ChunkyPNG::Image.from_file(File.expand_path('../tim.png', __FILE__))
161
+
162
+ height = png.height
163
+ width = png.width
164
+
165
+ image = png.to_rgba_stream.each_byte.to_a
166
+
167
+ @textures = glGenTextures 1
168
+ glBindTexture GL_TEXTURE_2D, @textures[0]
169
+ glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
170
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
171
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
172
+ end
173
+
174
+ end
175
+
176
+ Lesson11.new
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'opengl'
4
+ require 'glu'
5
+ require 'glut'
6
+ require 'chunky_png'
7
+
8
+ class Lesson12
9
+ include Gl
10
+ include Glu
11
+ include Glut
12
+
13
+ def initialize
14
+ @textures = nil
15
+ @box = nil
16
+ @top = nil
17
+ @xrot = 0
18
+ @yrot = 0
19
+
20
+ @box_color = [
21
+ [1.0, 0.0, 0.0],
22
+ [1.0, 0.5, 0.0],
23
+ [1.0, 1.0, 0.0],
24
+ [0.0, 1.0, 0.0],
25
+ [0.0, 1.0, 1.0],
26
+ ]
27
+
28
+ @top_color = [
29
+ [0.5, 0.0, 0.0],
30
+ [0.5, 0.25, 0.0],
31
+ [0.5, 0.5, 0.0],
32
+ [0.0, 0.5, 0.0],
33
+ [0.0, 0.5, 0.5],
34
+ ]
35
+
36
+ glutInit
37
+
38
+ glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
39
+ glutInitWindowSize 640, 480
40
+ glutInitWindowPosition 0, 0
41
+
42
+ @window = glutCreateWindow "NeHe Lesson 12 - ruby-opengl version"
43
+
44
+ glutDisplayFunc :draw_gl_scene
45
+ glutReshapeFunc :reshape
46
+ glutIdleFunc :idle
47
+ glutKeyboardFunc :keyboard
48
+
49
+ reshape 640, 480
50
+ load_texture
51
+ init_gl
52
+
53
+ glutMainLoop
54
+ end
55
+
56
+ def init_gl
57
+ build_lists
58
+
59
+ glEnable GL_TEXTURE_2D
60
+ glShadeModel GL_SMOOTH
61
+ glClearColor 0.0, 0.0, 0.0, 0.5
62
+ glClearDepth 1.0
63
+ glEnable GL_DEPTH_TEST
64
+ glDepthFunc GL_LEQUAL
65
+
66
+ glEnable GL_LIGHT0
67
+ glEnable GL_LIGHTING
68
+ glEnable GL_COLOR_MATERIAL
69
+
70
+ glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
71
+ end
72
+
73
+ def reshape width, height
74
+ width = width.to_f
75
+ height = height.to_f
76
+ height = 1.0 if height.zero?
77
+
78
+ glViewport 0, 0, width, height
79
+
80
+ glMatrixMode GL_PROJECTION
81
+ glLoadIdentity
82
+
83
+ gluPerspective 45.0, width / height, 0.1, 100.0
84
+
85
+ glMatrixMode GL_MODELVIEW
86
+ glLoadIdentity
87
+
88
+ true
89
+ end
90
+
91
+ def build_lists
92
+ @box = glGenLists 2
93
+ @top = @box + 1
94
+
95
+ glNewList @box, GL_COMPILE do
96
+ glBegin GL_QUADS do
97
+ # Bottom Face
98
+ glTexCoord2f(1, 1); glVertex3f(-1, -1, -1)
99
+ glTexCoord2f(0, 1); glVertex3f( 1, -1, -1)
100
+ glTexCoord2f(0, 0); glVertex3f( 1, -1, 1)
101
+ glTexCoord2f(1, 0); glVertex3f(-1, -1, 1)
102
+ # Front Face
103
+ glTexCoord2f(0, 0); glVertex3f(-1, -1, 1)
104
+ glTexCoord2f(1, 0); glVertex3f( 1, -1, 1)
105
+ glTexCoord2f(1, 1); glVertex3f( 1, 1, 1)
106
+ glTexCoord2f(0, 1); glVertex3f(-1, 1, 1)
107
+ # Back Face
108
+ glTexCoord2f(1, 0); glVertex3f(-1, -1, -1)
109
+ glTexCoord2f(1, 1); glVertex3f(-1, 1, -1)
110
+ glTexCoord2f(0, 1); glVertex3f( 1, 1, -1)
111
+ glTexCoord2f(0, 0); glVertex3f( 1, -1, -1)
112
+ # Right face
113
+ glTexCoord2f(1, 0); glVertex3f( 1, -1, -1)
114
+ glTexCoord2f(1, 1); glVertex3f( 1, 1, -1)
115
+ glTexCoord2f(0, 1); glVertex3f( 1, 1, 1)
116
+ glTexCoord2f(0, 0); glVertex3f( 1, -1, 1)
117
+ # Left Face
118
+ glTexCoord2f(0, 0); glVertex3f(-1, -1, -1)
119
+ glTexCoord2f(1, 0); glVertex3f(-1, -1, 1)
120
+ glTexCoord2f(1, 1); glVertex3f(-1, 1, 1)
121
+ glTexCoord2f(0, 1); glVertex3f(-1, 1, -1)
122
+ end
123
+ end
124
+
125
+ glNewList @top, GL_COMPILE do
126
+ glBegin GL_QUADS do
127
+ # Top Face
128
+ glTexCoord2f(0, 1); glVertex3f(-1, 1, -1)
129
+ glTexCoord2f(0, 0); glVertex3f(-1, 1, 1)
130
+ glTexCoord2f(1, 0); glVertex3f( 1, 1, 1)
131
+ glTexCoord2f(1, 1); glVertex3f( 1, 1, -1)
132
+ end
133
+ end
134
+ end
135
+
136
+ def draw_gl_scene
137
+ glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
138
+
139
+ glBindTexture GL_TEXTURE_2D, @textures[0]
140
+
141
+ (1..5).each do |y|
142
+ (0..y).each do |x|
143
+ glLoadIdentity
144
+ glTranslate 1.4 + x * 2.8 - y * 1.4, (6 - y) * 2.4 - 7, -20
145
+
146
+ glRotatef (45 - 2.0 * y) + @xrot, 1, 0, 0
147
+ glRotatef 45 + @yrot, 0, 1, 0
148
+
149
+ glColor3fv @box_color[y - 1]
150
+ glCallList @box
151
+
152
+ glColor3fv @top_color[y - 1]
153
+ glCallList @top
154
+ end
155
+ end
156
+
157
+ glutSwapBuffers
158
+ end
159
+
160
+ def idle
161
+ glutPostRedisplay
162
+ end
163
+
164
+ def keyboard key, x, y
165
+ case key
166
+ when ?\e
167
+ glutDestroyWindow @window
168
+ exit 0
169
+ when 'F' then
170
+ @fullscreen = !@fullscreen
171
+
172
+ if @fullscreen then
173
+ glutFullScreen
174
+ else
175
+ glutPositionWindow 0, 0
176
+ end
177
+ when 'X' then @xrot += 0.5
178
+ when 'x' then @xrot -= 0.5
179
+ when 'Y' then @yrot += 0.5
180
+ when 'y' then @yrot -= 0.5
181
+ end
182
+
183
+ glutPostRedisplay
184
+ end
185
+
186
+ def load_texture
187
+ png = ChunkyPNG::Image.from_file(File.expand_path('../crate.png', __FILE__))
188
+
189
+ height = png.height
190
+ width = png.width
191
+
192
+ image = png.to_rgba_stream.each_byte.to_a
193
+
194
+ @textures = glGenTextures 1
195
+ glBindTexture GL_TEXTURE_2D, @textures[0]
196
+ glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
197
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
198
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
199
+ end
200
+
201
+ end
202
+
203
+ Lesson12.new