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,211 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'opengl'
4
+ require 'glu'
5
+ require 'glut'
6
+ require 'chunky_png'
7
+
8
+ class Lesson16
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
+ @filter = 0
20
+
21
+ @fog_mode = [
22
+ GL_EXP,
23
+ GL_EXP2,
24
+ GL_LINEAR
25
+ ]
26
+
27
+ @fog_filter = 0
28
+ @fog_color = [0.5, 0.5, 0.5, 1.0]
29
+
30
+ glutInit
31
+
32
+ glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
33
+ glutInitWindowSize 640, 480
34
+ glutInitWindowPosition 0, 0
35
+
36
+ @window = glutCreateWindow "NeHe Lesson 16 - ruby-opengl version"
37
+
38
+ glutDisplayFunc :draw_gl_scene
39
+ glutReshapeFunc :reshape
40
+ glutIdleFunc :idle
41
+ glutKeyboardFunc :keyboard
42
+
43
+ reshape 640, 480
44
+ load_texture
45
+ init_gl
46
+
47
+ glutMainLoop
48
+ end
49
+
50
+ def init_gl
51
+ glEnable GL_TEXTURE_2D
52
+ glShadeModel GL_SMOOTH
53
+ glClearColor 0.5, 0.5, 0.5, 1.0
54
+
55
+ glFogi GL_FOG_MODE, @fog_mode[@fog_filter]
56
+ glFogfv GL_FOG_COLOR, @fog_color
57
+ glFogf GL_FOG_DENSITY, 0.35
58
+ glHint GL_FOG_HINT, GL_DONT_CARE
59
+ glFogf GL_FOG_START, 2.0
60
+ glFogf GL_FOG_END, 8.0
61
+ glEnable GL_FOG
62
+
63
+ glClearDepth 1.0
64
+ glEnable GL_DEPTH_TEST
65
+ glDepthFunc GL_LEQUAL
66
+ glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
67
+ end
68
+
69
+ def reshape width, height
70
+ width = width.to_f
71
+ height = height.to_f
72
+ height = 1.0 if height.zero?
73
+
74
+ glViewport 0, 0, width, height
75
+
76
+ glMatrixMode GL_PROJECTION
77
+ glLoadIdentity
78
+
79
+ gluPerspective 45.0, width / height, 0.1, 100.0
80
+
81
+ glMatrixMode GL_MODELVIEW
82
+ glLoadIdentity
83
+ end
84
+
85
+ def draw_gl_scene
86
+ glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
87
+ glMatrixMode GL_MODELVIEW
88
+ glLoadIdentity
89
+ glTranslatef 0.0, 0.0, -7.0
90
+
91
+ glRotatef @xrot, 1.0, 0.0, 0.0
92
+ glRotatef @yrot, 0.0, 1.0, 0.0
93
+ glRotatef @zrot, 0.0, 0.0, 1.0
94
+
95
+ glBindTexture GL_TEXTURE_2D, @textures[0]
96
+
97
+ glBegin GL_QUADS do
98
+ # front face
99
+ glTexCoord2f(0.0, 1.0)
100
+ glVertex3f(-1.0, -1.0, 1.0)
101
+ glTexCoord2f(1.0, 1.0)
102
+ glVertex3f(1.0, -1.0, 1.0)
103
+ glTexCoord2f(1.0, 0.0)
104
+ glVertex3f(1.0, 1.0, 1.0)
105
+ glTexCoord2f(0.0, 0.0)
106
+ glVertex3f(-1.0, 1.0, 1.0)
107
+
108
+ # back face
109
+ glTexCoord2f(0.0, 1.0)
110
+ glVertex3f(-1.0, -1.0, -1.0)
111
+ glTexCoord2f(1.0, 1.0)
112
+ glVertex3f(-1.0, 1.0, -1.0)
113
+ glTexCoord2f(1.0, 0.0)
114
+ glVertex3f(1.0, 1.0, -1.0)
115
+ glTexCoord2f(0.0, 0.0)
116
+ glVertex3f(1.0, -1.0, -1.0)
117
+
118
+ # top face
119
+ glTexCoord2f(0.0, 1.0)
120
+ glVertex3f(-1.0, 1.0, -1.0)
121
+ glTexCoord2f(1.0, 1.0)
122
+ glVertex3f(-1.0, 1.0, 1.0)
123
+ glTexCoord2f(1.0, 0.0)
124
+ glVertex3f(1.0, 1.0, 1.0)
125
+ glTexCoord2f(0.0, 0.0)
126
+ glVertex3f(1.0, 1.0, -1.0)
127
+
128
+ # bottom face
129
+ glTexCoord2f(1.0, 1.0)
130
+ glVertex3f(-1.0, -1.0, -1.0)
131
+ glTexCoord2f(1.0, 0.0)
132
+ glVertex3f(1.0, -1.0, -1.0)
133
+ glTexCoord2f(0.0, 0.0)
134
+ glVertex3f(1.0, -1.0, 1.0)
135
+ glTexCoord2f(0.0, 1.0)
136
+ glVertex3f(-1.0, -1.0, 1.0)
137
+
138
+ # right face
139
+ glTexCoord2f(1.0, 0.0)
140
+ glVertex3f(1.0, -1.0, -1.0)
141
+ glTexCoord2f(0.0, 0.0)
142
+ glVertex3f(1.0, 1.0, -1.0)
143
+ glTexCoord2f(0.0, 1.0)
144
+ glVertex3f(1.0, 1.0, 1.0)
145
+ glTexCoord2f(1.0, 1.0)
146
+ glVertex3f(1.0, -1.0, 1.0)
147
+
148
+ # left face
149
+ glTexCoord2f(0.0, 0.0)
150
+ glVertex3f(-1.0, -1.0, -1.0)
151
+ glTexCoord2f(0.0, 1.0)
152
+ glVertex3f(-1.0, -1.0, 1.0)
153
+ glTexCoord2f(1.0, 1.0)
154
+ glVertex3f(-1.0, 1.0, 1.0)
155
+ glTexCoord2f(1.0, 0.0)
156
+ glVertex3f(-1.0, 1.0, -1.0)
157
+ end
158
+
159
+ @xrot += 0.03
160
+ @yrot += 0.02
161
+ @zrot += 0.04
162
+
163
+ glutSwapBuffers
164
+ end
165
+
166
+ def idle
167
+ glutPostRedisplay
168
+ end
169
+
170
+ def keyboard key, x, y
171
+ case key
172
+ when ?\e
173
+ glutDestroyWindow @window
174
+ exit 0
175
+ when 'F' then
176
+ @fullscreen = !@fullscreen
177
+
178
+ if @fullscreen then
179
+ glutFullScreen
180
+ else
181
+ glutPositionWindow 0, 0
182
+ end
183
+ when 'f' then
184
+ @fog_filter += 1
185
+ @fog_filter %= 3
186
+
187
+ glFogi GL_FOG_MODE, @fog_mode[@fog_filter]
188
+ puts "fog #{@fog_filter}"
189
+ end
190
+
191
+ glutPostRedisplay
192
+ end
193
+
194
+ def load_texture
195
+ png = ChunkyPNG::Image.from_file(File.expand_path('../crate.png', __FILE__))
196
+
197
+ height = png.height
198
+ width = png.width
199
+
200
+ image = png.to_rgba_stream.each_byte.to_a
201
+
202
+ @textures = glGenTextures 1
203
+ glBindTexture GL_TEXTURE_2D, @textures[0]
204
+ glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
205
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
206
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
207
+ end
208
+
209
+ end
210
+
211
+ Lesson16.new
@@ -0,0 +1,209 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'opengl'
4
+ require 'glu'
5
+ require 'glut'
6
+ require 'chunky_png'
7
+
8
+ class Lesson19
9
+ include Gl
10
+ include Glu
11
+ include Glut
12
+
13
+ Particle = Struct.new(:active, :life, :fade,
14
+ :r, :g, :b,
15
+ :x, :y, :z,
16
+ :xi, :yi, :zi,
17
+ :xg, :yg, :zg)
18
+
19
+ def initialize
20
+ @fullscreen = true
21
+ @rainbow = true
22
+ @slowdown = 2.0
23
+ @x_speed = 2.0
24
+ @y_speed = 5.0
25
+ @zoom = -100.0
26
+ @textures = nil
27
+ @colors = [
28
+ [1.0, 0.5, 0.5],
29
+ [1.0, 0.75, 0.5],
30
+ [1.0, 1.0, 0.5],
31
+ [0.75, 1.0, 0.5],
32
+ [0.5, 1.0, 0.5],
33
+ [0.5, 1.0, 0.75],
34
+ [0.5, 1.0, 1.0],
35
+ [0.5, 0.75, 1.0],
36
+ [0.5, 0.5, 1.0],
37
+ [0.75, 0.5, 1.0],
38
+ [1.0, 0.5, 1.0],
39
+ [1.0, 0.5, 0.75],
40
+ ]
41
+ @color = 0
42
+
43
+ @max_particles = 1_000
44
+ @particles = (0...@max_particles).map do |i|
45
+ Particle.new(true, # active
46
+ 1.0, # life
47
+ rand(100) / 1000.0 + 0.003, # fade
48
+ @colors[i % 12][0], # red
49
+ @colors[i % 12][1], # green
50
+ @colors[i % 12][2], # blue
51
+ 0.0, 0.0, 0.0, # x, y, z
52
+ (rand * 2 - 1), # x speed
53
+ (rand * 2 - 1), # y speed
54
+ (rand * 2 - 1), # z speed
55
+ 0.0, # x gravity
56
+ -0.8, # y gravity
57
+ 0.0) # z gravity
58
+ end
59
+
60
+ glutInit
61
+
62
+ glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH
63
+ glutInitWindowSize 640, 480
64
+ glutInitWindowPosition 0, 0
65
+
66
+ @window = glutCreateWindow "NeHe Lesson 19 - ruby-opengl version"
67
+
68
+ glutDisplayFunc :draw_gl_scene
69
+ glutReshapeFunc :reshape
70
+ glutIdleFunc :idle
71
+ glutKeyboardFunc :keyboard
72
+
73
+ reshape 640, 480
74
+ load_texture
75
+ init_gl
76
+
77
+ glutMainLoop
78
+ end
79
+
80
+ def init_gl
81
+ glShadeModel GL_SMOOTH
82
+ glClearColor 0, 0, 0, 0
83
+
84
+ glClearDepth 1.0
85
+ glDisable GL_DEPTH_TEST
86
+ glEnable GL_BLEND
87
+ glBlendFunc GL_SRC_ALPHA, GL_ONE
88
+ glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
89
+ glHint GL_POINT_SMOOTH_HINT, GL_NICEST
90
+ glEnable GL_TEXTURE_2D
91
+ glBindTexture GL_TEXTURE_2D, @textures[0]
92
+ end
93
+
94
+ def reshape width, height
95
+ width = width.to_f
96
+ height = height.to_f
97
+ height = 1.0 if height.zero?
98
+
99
+ glViewport 0, 0, width, height
100
+
101
+ glMatrixMode GL_PROJECTION
102
+ glLoadIdentity
103
+
104
+ gluPerspective 45, width / height, 0.1, 200
105
+
106
+ glMatrixMode GL_MODELVIEW
107
+ glLoadIdentity
108
+ end
109
+
110
+ def draw_gl_scene
111
+ glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
112
+ glLoadIdentity
113
+
114
+ @particles.each do |particle|
115
+ next unless particle.active
116
+
117
+ x = particle.x
118
+ y = particle.y
119
+ z = particle.z + @zoom
120
+
121
+ glColor4f particle.r, particle.g, particle.b, particle.life
122
+
123
+ glBegin GL_TRIANGLE_STRIP do
124
+ glTexCoord2d 1, 1; glVertex3f x + 0.5, y + 0.5, z
125
+ glTexCoord2d 0, 1; glVertex3f x - 0.5, y + 0.5, z
126
+ glTexCoord2d 1, 0; glVertex3f x + 0.5, y - 0.5, z
127
+ glTexCoord2d 0, 0; glVertex3f x - 0.5, y - 0.5, z
128
+ end
129
+
130
+ particle.x += particle.xi / @slowdown
131
+ particle.y += particle.yi / @slowdown
132
+ particle.z += particle.zi / @slowdown
133
+
134
+ particle.xi += particle.xg
135
+ particle.yi += particle.yg
136
+ particle.zi += particle.zg
137
+
138
+ particle.life -= particle.fade
139
+
140
+ if particle.life < 0 then
141
+ particle.life = 1.0
142
+ particle.fade = rand(100) / 1000.0 + 0.003
143
+
144
+ particle.x = 0.0
145
+ particle.y = 0.0
146
+ particle.z = 0.0
147
+
148
+ particle.xi = @x_speed + rand * 2 - 1
149
+ particle.yi = @y_speed + rand * 2 - 1
150
+ particle.zi = rand * 2 - 1
151
+
152
+ particle.r = @colors[@color][0]
153
+ particle.g = @colors[@color][1]
154
+ particle.b = @colors[@color][2]
155
+
156
+ @color += 1
157
+ @color %= @colors.length - 1
158
+ end
159
+ end
160
+
161
+ glutSwapBuffers
162
+ end
163
+
164
+ def idle
165
+ glutPostRedisplay
166
+ end
167
+
168
+ def keyboard key, x, y
169
+ case key
170
+ when ?\e
171
+ glutDestroyWindow @window
172
+ exit 0
173
+ when 'F' then
174
+ @fullscreen = !@fullscreen
175
+
176
+ if @fullscreen then
177
+ glutFullScreen
178
+ else
179
+ glutPositionWindow 0, 0
180
+ end
181
+ when 'f' then
182
+ @fog_filter += 1
183
+ @fog_filter %= 3
184
+
185
+ glFogi GL_FOG_MODE, @fog_mode[@fog_filter]
186
+ puts "fog #{@fog_filter}"
187
+ end
188
+
189
+ glutPostRedisplay
190
+ end
191
+
192
+ def load_texture
193
+ png = ChunkyPNG::Image.from_file(File.expand_path('../particle.png', __FILE__))
194
+
195
+ height = png.height
196
+ width = png.width
197
+
198
+ image = png.to_rgba_stream.each_byte.to_a
199
+
200
+ @textures = glGenTextures 1
201
+ glBindTexture GL_TEXTURE_2D, @textures[0]
202
+ glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
203
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
204
+ glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
205
+ end
206
+
207
+ end
208
+
209
+ Lesson19.new
@@ -0,0 +1,308 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Nehe Lesson 36 Code
4
+ # modified from immediate mode to use vertex arrays for helix drawing
5
+ require 'opengl'
6
+ require 'glu'
7
+ require 'glut'
8
+
9
+ include Gl,Glu,Glut
10
+ include Math
11
+
12
+ def emptyTexture
13
+ # Create Storage Space For Texture Data (128x128x4)
14
+ data = ([0]*4*128*128).pack("f*")
15
+ txtnumber = glGenTextures(1) # Create 1 Texture
16
+ glBindTexture(GL_TEXTURE_2D, txtnumber[0]) # Bind The Texture
17
+ glTexImage2D(GL_TEXTURE_2D, 0, 4, 128, 128, 0,
18
+ GL_RGBA, GL_FLOAT, data) # Build Texture Using Information In data
19
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
20
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
21
+ return txtnumber[0] # Return The Texture ID
22
+ end
23
+
24
+ def init
25
+ global_ambient = [0.2, 0.2, 0.2, 1.0] # Set Ambient Lighting To Fairly Dark Light (No Color)
26
+ light0pos = [0.0, 5.0, 10.0, 1.0] # Set The Light Position
27
+ light0ambient = [0.2, 0.2, 0.2, 1.0] # More Ambient Light
28
+ light0diffuse = [0.3, 0.3, 0.3, 1.0] # Set The Diffuse Light A Bit Brighter
29
+ light0specular = [0.8, 0.8, 0.8, 1.0] # Fairly Bright Specular Lighting
30
+
31
+ lmodel_ambient = [0.2,0.2,0.2,1.0] # And More Ambient Light
32
+
33
+ $angle = 0.0 # Set Starting Angle To Zero
34
+
35
+ $lasttime = 0
36
+
37
+ $blurTexture = emptyTexture() # Create Our Empty Texture
38
+
39
+ $helix_v,$helix_n = createHelix()
40
+ glVertexPointer(3,GL_FLOAT,0,$helix_v.flatten.pack("f*"))
41
+ glNormalPointer(GL_FLOAT,0,$helix_n.flatten.pack("f*"))
42
+
43
+ glLoadIdentity() # Reset The Modelview Matrix
44
+
45
+ glEnable(GL_DEPTH_TEST) # Enable Depth Testing
46
+
47
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient) # Set The Ambient Light Model
48
+
49
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT,global_ambient) # Set The Global Ambient Light Model
50
+ glLightfv(GL_LIGHT0, GL_POSITION, light0pos) # Set The Lights Position
51
+ glLightfv(GL_LIGHT0, GL_AMBIENT, light0ambient) # Set The Ambient Light
52
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light0diffuse) # Set The Diffuse Light
53
+ glLightfv(GL_LIGHT0, GL_SPECULAR, light0specular) # Set Up Specular Lighting
54
+ glEnable(GL_LIGHTING) # Enable Lighting
55
+ glEnable(GL_LIGHT0) # Enable Light0
56
+
57
+ glShadeModel(GL_SMOOTH) # Select Smooth Shading
58
+
59
+ glMateriali(GL_FRONT, GL_SHININESS, 128)
60
+ glClearColor(0.0, 0.0, 0.0, 0.5) # Set The Clear Color To Black
61
+ end
62
+
63
+ # Keyboard handler to exit when ESC is typed
64
+ keyboard = lambda do |key, x, y|
65
+ case(key)
66
+ when ?\e
67
+ exit(0)
68
+ end
69
+ glutPostRedisplay
70
+ end
71
+
72
+ reshape = lambda do |w,h|
73
+ glMatrixMode(GL_PROJECTION)
74
+ glViewport(0,0,w,h)
75
+ glLoadIdentity()
76
+ width = 0.5
77
+ height = 0.5 * h/w;
78
+ glFrustum(-width,width,-height,height,1.0,2000.0)
79
+ glMatrixMode(GL_MODELVIEW)
80
+ glViewport(0,0,w,h)
81
+ end
82
+
83
+ def viewOrtho
84
+ glMatrixMode(GL_PROJECTION) # Select Projection
85
+ glPushMatrix() # Push The Matrix
86
+ glLoadIdentity() # Reset The Matrix
87
+ width = glutGet(GLUT_WINDOW_WIDTH)
88
+ height = glutGet(GLUT_WINDOW_HEIGHT)
89
+ glOrtho( 0, width , height , 0, -1, 1 ) # Select Ortho Mode (widthxheight)
90
+ glMatrixMode(GL_MODELVIEW) # Select Modelview Matrix
91
+ glPushMatrix() # Push The Matrix
92
+ glLoadIdentity() # Reset The Matrix
93
+ end
94
+
95
+ def viewPerspective # Set Up A Perspective View
96
+ glMatrixMode( GL_PROJECTION ) # Select Projection
97
+ glPopMatrix() # Pop The Matrix
98
+ glMatrixMode( GL_MODELVIEW ) # Select Modelview
99
+ glPopMatrix() # Pop The Matrix
100
+ end
101
+
102
+ def normalize(v)
103
+ len = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2])
104
+ return v if len==0
105
+ [ v[0] / len, v[1] / len, v[2] / len ]
106
+ end
107
+
108
+ def calcNormal(v) # Calculates Normal For A Quad Using 3 Points
109
+ # Finds The Vector Between 2 Points By Subtracting
110
+ # The x,y,z Coordinates From One Point To Another.
111
+ # Calculate The Vector From Point 1 To Point 0
112
+ v1, v2, out = [], [], []
113
+ x,y,z = 0,1,2
114
+
115
+ v1[x] = v[0][x] - v[1][x] # Vector 1.x=Vertex[0].x-Vertex[1].x
116
+ v1[y] = v[0][y] - v[1][y] # Vector 1.y=Vertex[0].y-Vertex[1].y
117
+ v1[z] = v[0][z] - v[1][z] # Vector 1.z=Vertex[0].y-Vertex[1].z
118
+ # Calculate The Vector From Point 2 To Point 1
119
+ v2[x] = v[1][x] - v[2][x] # Vector 2.x=Vertex[0].x-Vertex[1].x
120
+ v2[y] = v[1][y] - v[2][y] # Vector 2.y=Vertex[0].y-Vertex[1].y
121
+ v2[z] = v[1][z] - v[2][z] # Vector 2.z=Vertex[0].z-Vertex[1].z
122
+ # Compute The Cross Product To Give Us A Surface Normal
123
+ out[x] = v1[y]*v2[z] - v1[z]*v2[y] # Cross Product For Y - Z
124
+ out[y] = v1[z]*v2[x] - v1[x]*v2[z] # Cross Product For X - Z
125
+ out[z] = v1[x]*v2[y] - v1[y]*v2[x] # Cross Product For X - Y
126
+
127
+ normalize(out)
128
+ end
129
+
130
+ def createHelix() # creates helix VA
131
+ twists = 5
132
+ r = 1.5
133
+
134
+ helix_v = []
135
+ helix_n = []
136
+
137
+ 0.step(360,20) do |phi| # 360 Degrees In Steps Of 20
138
+ 0.step(360*twists,20) do |theta| # 360 Degrees * Number Of Twists In Steps Of 20
139
+ v= phi/180.0*PI # Calculate Angle Of First Point ( 0 )
140
+ u= theta/180.0*PI # Calculate Angle Of First Point ( 0 )
141
+
142
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (1st Point)
143
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (1st Point)
144
+ z=((u-(2.0*PI)) + sin(v))*r # Calculate z Position (1st Point)
145
+
146
+ v0 = [x,y,z]
147
+
148
+ v= phi/180.0*PI # Calculate Angle Of Second Point ( 0 )
149
+ u= (theta+20)/180.0*PI # Calculate Angle Of Second Point ( 20 )
150
+
151
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (2nd Point)
152
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (2nd Point)
153
+ z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (2nd Point)
154
+
155
+ v1 = [x,y,z]
156
+
157
+ v= (phi+20)/180.0*PI # Calculate Angle Of Third Point ( 20 )
158
+ u= (theta+20)/180.0*PI # Calculate Angle Of Third Point ( 20 )
159
+
160
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (3rd Point)
161
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (3rd Point)
162
+ z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (3rd Point)
163
+
164
+ v2 = [x,y,z]
165
+
166
+ v= (phi+20)/180.0*PI # Calculate Angle Of Fourth Point ( 20 )
167
+ u= (theta)/180.0*PI # Calculate Angle Of Fourth Point ( 0 )
168
+
169
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (4th Point)
170
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (4th Point)
171
+ z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (4th Point)
172
+
173
+ v3 = [x,y,z]
174
+
175
+ normal = calcNormal([v0,v1,v2,v3]) # Calculate The Quad Normal
176
+ helix_v << v0 << v1 << v2 << v3
177
+ helix_n << normal << normal << normal << normal
178
+ end
179
+ end
180
+ [helix_v,helix_n]
181
+ end
182
+
183
+ def processHelix() # Draws A Helix
184
+ glfMaterialColor = [0.4,0.2,0.8,1.0] # Set The Material Color
185
+ specular = [1.0,1.0,1.0,1.0] # Sets Up Specular Lighting
186
+
187
+ glLoadIdentity() # Reset The Modelview Matrix
188
+ gluLookAt(0, 5, 50, 0, 0, 0, 0, 1, 0) # Eye Position (0,5,50) Center Of Scene (0,0,0), Up On Y Axis
189
+
190
+ glPushMatrix() # Push The Modelview Matrix
191
+
192
+ glTranslatef(0,0,-50) # Translate 50 Units Into The Screen
193
+ glRotatef($angle/2.0,1,0,0) # Rotate By angle/2 On The X-Axis
194
+ glRotatef($angle/3.0,0,1,0) # Rotate By angle/3 On The Y-Axis
195
+
196
+ glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,glfMaterialColor)
197
+ glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular)
198
+
199
+ glEnableClientState(GL_VERTEX_ARRAY)
200
+ glEnableClientState(GL_NORMAL_ARRAY)
201
+ glDrawArrays(GL_QUADS,0,$helix_v.size)
202
+ glDisableClientState(GL_VERTEX_ARRAY)
203
+ glDisableClientState(GL_NORMAL_ARRAY)
204
+
205
+ glPopMatrix() # Pop The Matrix
206
+ end
207
+
208
+ def drawBlur(times,inc)
209
+ spost = 0.0 # Starting Texture Coordinate Offset
210
+ alphainc = 0.9 / times # Fade Speed For Alpha Blending
211
+ alpha = 0.2 # Starting Alpha Value
212
+
213
+ width = glutGet(GLUT_WINDOW_WIDTH)
214
+ height = glutGet(GLUT_WINDOW_HEIGHT)
215
+ # Disable AutoTexture Coordinates
216
+ glDisable(GL_TEXTURE_GEN_S)
217
+ glDisable(GL_TEXTURE_GEN_T)
218
+
219
+ glEnable(GL_TEXTURE_2D) # Enable 2D Texture Mapping
220
+ glDisable(GL_DEPTH_TEST) # Disable Depth Testing
221
+ glBlendFunc(GL_SRC_ALPHA,GL_ONE) # Set Blending Mode
222
+ glEnable(GL_BLEND) # Enable Blending
223
+ glBindTexture(GL_TEXTURE_2D,$blurTexture) # Bind To The Blur Texture
224
+ viewOrtho() # Switch To An Ortho View
225
+
226
+ alphainc = alpha / times # alphainc=0.2 / Times To Render Blur
227
+
228
+ glBegin(GL_QUADS) # Begin Drawing Quads
229
+ 0.upto(times-1) do |num| # Number Of Times To Render Blur
230
+ glColor4f(1.0, 1.0, 1.0, alpha) # Set The Alpha Value (Starts At 0.2)
231
+ glTexCoord2f(0+spost,1-spost) # Texture Coordinate ( 0, 1 )
232
+ glVertex2f(0,0) # First Vertex ( 0, 0 )
233
+
234
+ glTexCoord2f(0+spost,0+spost) # Texture Coordinate ( 0, 0 )
235
+ glVertex2f(0,height) # Second Vertex ( 0, height )
236
+
237
+ glTexCoord2f(1-spost,0+spost) # Texture Coordinate ( 1, 0 )
238
+ glVertex2f(width,height) # Third Vertex ( width, height )
239
+
240
+ glTexCoord2f(1-spost,1-spost) # Texture Coordinate ( 1, 1 )
241
+ glVertex2f(width,0) # Fourth Vertex ( width, 0 )
242
+
243
+ spost += inc # Gradually Increase spost (Zooming Closer To Texture Center)
244
+ alpha = alpha - alphainc # Gradually Decrease alpha (Gradually Fading Image Out)
245
+ end
246
+ glEnd() # Done Drawing Quads
247
+
248
+ viewPerspective() # Switch To A Perspective View
249
+
250
+ glEnable(GL_DEPTH_TEST) # Enable Depth Testing
251
+ glDisable(GL_TEXTURE_2D) # Disable 2D Texture Mapping
252
+ glDisable(GL_BLEND) # Disable Blending
253
+ glBindTexture(GL_TEXTURE_2D,0) # Unbind The Blur Texture
254
+ end
255
+
256
+
257
+ def renderToTexture
258
+ glViewport(0,0,128,128); # Set Our Viewport (Match Texture Size)
259
+
260
+ processHelix() # Render The Helix
261
+
262
+ glBindTexture(GL_TEXTURE_2D,$blurTexture) # Bind To The Blur Texture
263
+
264
+ # Copy Our ViewPort To The Blur Texture (From 0,0 To 128,128... No Border)
265
+ glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, 128, 128, 0)
266
+
267
+ glClearColor(0.0, 0.0, 0.5, 0.5) # Set The Clear Color To Medium Blue
268
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear The Screen And Depth Buffer
269
+ width = glutGet(GLUT_WINDOW_WIDTH)
270
+ height = glutGet(GLUT_WINDOW_HEIGHT)
271
+ glViewport(0 , 0,width,height) # Set Viewport (0,0 to widthxheight)
272
+ end
273
+
274
+ drawGLScene = lambda do # Draw The Scene
275
+ glClearColor(0.0, 0.0, 0.0, 0.5) # Set The Clear Color To Black
276
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear Screen And Depth Buffer
277
+ glLoadIdentity() # Reset The View
278
+ renderToTexture() # Render To A Texture
279
+ processHelix() # Draw Our Helix
280
+ drawBlur(25,0.02) # Draw The Blur Effect
281
+ glFlush() # Flush The GL Rendering Pipeline
282
+ glutSwapBuffers()
283
+ sleep(0.001) # don't hog all cpu time
284
+ end
285
+
286
+ idle = lambda do
287
+ now = glutGet(GLUT_ELAPSED_TIME)
288
+ elapsed = now - $lasttime
289
+ $angle += (elapsed * 0.03) # Update angle Based On The Clock
290
+ $lasttime = now
291
+
292
+ glutPostRedisplay()
293
+ end
294
+
295
+ # Main
296
+ glutInit()
297
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
298
+ glutInitWindowPosition(100,100)
299
+ glutInitWindowSize(640,480)
300
+ glutCreateWindow("NeHe's Lesson 36")
301
+ glutDisplayFunc(drawGLScene)
302
+ glutIdleFunc(idle)
303
+ glutReshapeFunc(reshape)
304
+ glutKeyboardFunc(keyboard)
305
+
306
+ init()
307
+
308
+ glutMainLoop()