rubygl 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +13 -5
  2. data/.travis/push-rdoc-to-gh-pages.sh +22 -0
  3. data/.travis.yml +18 -0
  4. data/Gemfile +7 -0
  5. data/Gemfile.lock +12 -0
  6. data/LICENSE +21 -21
  7. data/README.md +40 -0
  8. data/Rakefile +98 -83
  9. data/bin/ffi_code_gen.rb +166 -166
  10. data/bin/gl_code_gen.rb +458 -458
  11. data/examples/faceted_example.rb +71 -64
  12. data/examples/instanced_example.rb +135 -127
  13. data/examples/phong_example.rb +80 -71
  14. data/ext/windows/RubyGL.so +0 -0
  15. data/lib/rubygl/event.rb +64 -0
  16. data/lib/{RubyGL → rubygl}/geometry.rb +216 -211
  17. data/lib/{RubyGL → rubygl}/math.rb +300 -300
  18. data/lib/{RubyGL → rubygl}/memory.rb +125 -121
  19. data/lib/rubygl/native/all_enums.rb +641 -0
  20. data/lib/{RubyGL/Native → rubygl/native}/glcontext.rb +23 -47
  21. data/lib/{RubyGL/Native → rubygl/native}/include/GLContext.h +36 -36
  22. data/lib/{RubyGL/Native → rubygl/native}/include/Input.h +15 -15
  23. data/lib/{RubyGL/Native → rubygl/native}/include/Window.h +27 -27
  24. data/lib/rubygl/native/input.rb +247 -0
  25. data/lib/{RubyGL/Native → rubygl/native}/opengl.rb +2808 -2032
  26. data/lib/{RubyGL/Native → rubygl/native}/src/GLContext.c +72 -72
  27. data/lib/{RubyGL/Native → rubygl/native}/src/Input.c +25 -25
  28. data/lib/{RubyGL/Native → rubygl/native}/src/Window.c +57 -57
  29. data/lib/{RubyGL/Native → rubygl/native}/window.rb +24 -24
  30. data/lib/{RubyGL → rubygl}/setup.rb +50 -50
  31. data/lib/{RubyGL → rubygl}/shader.rb +203 -203
  32. data/lib/{RubyGL → rubygl}/util.rb +77 -77
  33. data/lib/rubygl.rb +49 -48
  34. data/{RubyGL.gemspec → rubygl.gemspec} +20 -23
  35. data/test/test_util.rb +19 -0
  36. metadata +36 -33
  37. data/lib/RubyGL/Native/input.rb +0 -13
  38. data/lib/RubyGL/callback.rb +0 -10
@@ -1,301 +1,301 @@
1
-
2
- module RubyGL
3
-
4
- class Conversion
5
- @@PI_DIV = Math::PI / 180.0
6
- @@DIV_PI = 180.0 / Math::PI
7
-
8
- def self.deg_to_rad(degrees)
9
- degrees * @@PI_DIV
10
- end
11
-
12
- def self.rad_to_deg(radians)
13
- radians * @@DIV_PI
14
- end
15
- end
16
-
17
- class Vec3
18
- def initialize(array = nil)
19
- @data = Array.new(3, 0)
20
-
21
- if array then
22
- for i in 0...@data.size
23
- @data[i] = array[i]
24
- end
25
- end
26
- end
27
-
28
- def +(other_vector)
29
- new_vector = Vec3.new()
30
-
31
- for i in 0...@data.size
32
- new_vector[i] = @data[i] + other_vector[i]
33
- end
34
-
35
- new_vector
36
- end
37
-
38
- def -(other_vector)
39
- new_vector = Vec3.new()
40
-
41
- for i in 0...@data.size
42
- new_vector[i] = @data[i] - other_vector[i]
43
- end
44
-
45
- new_vector
46
- end
47
-
48
- def cross(other_vector)
49
- new_vector = Vec3.new()
50
-
51
- new_vector[0] = (@data[1] * other_vector[2]) - (@data[2] * other_vector[1])
52
- new_vector[1] = (@data[0] * other_vector[2]) - (@data[2] * other_vector[0])
53
- new_vector[2] = (@data[0] * other_vector[1]) - (@data[1] * other_vector[0])
54
-
55
- new_vector
56
- end
57
-
58
- def norm!()
59
- curr_len = self.len().abs()
60
-
61
- for i in 0...@data.size
62
- @data[i] /= curr_len
63
- end
64
- end
65
-
66
- def norm()
67
- new_vector = Vec2.new()
68
-
69
- for i in 0...@data.size
70
- new_vector[i] = @data[i]
71
- end
72
- new_vector.norm!
73
-
74
- new_vector
75
- end
76
-
77
- def len()
78
- sum = 0
79
-
80
- for i in 0...@data.size
81
- sum += @data[i] * @data[i]
82
- end
83
-
84
- Math::sqrt(sum)
85
- end
86
-
87
- def [](index)
88
- @data[index]
89
- end
90
-
91
- def []=(index, value)
92
- @data[index] = value
93
- end
94
-
95
- def to_ary()
96
- Array.new(@data)
97
- end
98
-
99
- def to_a()
100
- self.to_ary
101
- end
102
- end
103
-
104
- class Vec4
105
- def initialize()
106
- @data = Array.new(4, 0)
107
- end
108
-
109
- def +(other_vector)
110
- new_vector = Vec4.new()
111
-
112
- for i in 0..@data.size
113
- new_vector[i] = @data[i] + other_vector[i]
114
- end
115
-
116
- new_vector
117
- end
118
-
119
- def -(other_vector)
120
- new_vector = Vec4.new()
121
-
122
- for i in 0..@data.size
123
- new_vector[i] = @data[i] - other_vector[i]
124
- end
125
-
126
- new_vector
127
- end
128
-
129
- def norm!()
130
- curr_len = self.len()
131
-
132
- for i in 0...@data.size
133
- @data /= curr_len
134
- end
135
- end
136
-
137
- def norm()
138
- new_vector = Vec2.new()
139
-
140
- for i in 0...@data.size
141
- new_vector[i] = @data[i]
142
- end
143
- new_vector.norm!
144
-
145
- new_vector
146
- end
147
-
148
- def len()
149
- sum = 0
150
-
151
- for i in 0...@data.size
152
- sum += @data[i] * @data[i]
153
- end
154
-
155
- Math::sqrt(sum)
156
- end
157
-
158
- def [](index)
159
- @data[index]
160
- end
161
-
162
- def []=(index, value)
163
- @data[index] = value
164
- end
165
-
166
- def to_ary()
167
- Array.new(@data)
168
- end
169
-
170
- def to_a()
171
- self.to_ary
172
- end
173
- end
174
-
175
- # Stored In Column Order For Interoperability With OpenGL. Subscript Operator
176
- # Will Return A Vector That Represents A Column Within The Matrix. This Matrix
177
- # Class Only Supports Matrices Where The Number Of Rows Equals The Number Of
178
- # Columns; Operations Are Constrained As Such.
179
- class Mat4
180
- def initialize(diagonal = 1.0)
181
- @data = Array.new(4) { |index|
182
- column = Vec4.new()
183
- column[index] = diagonal
184
-
185
- column
186
- }
187
- end
188
-
189
- def self.translation(x, y, z)
190
- matrix = Mat4.new(1.0)
191
-
192
- matrix[3][0] = x
193
- matrix[3][1] = y
194
- matrix[3][2] = z
195
-
196
- matrix
197
- end
198
-
199
- # The axis value should be 0 if that axis should not be rotated around;
200
- # any other value indicates its priority (higher priority axis is rotated
201
- # around before the lower priority axis).
202
- def self.rotation(x_axis, y_axis, z_axis, theta)
203
- rad_angle = Conversion::deg_to_rad(theta)
204
- sin_angle = Math::sin(rad_angle)
205
- cos_angle = Math::cos(rad_angle)
206
-
207
- # Multiply Lower -> Higher To Get Ordering Correct
208
- axis_priority = [[:x, x_axis], [:y, y_axis], [:z, z_axis]]
209
- axis_priority.delete_if { |(_, val)|
210
- val == 0
211
- }.sort!.reverse!
212
-
213
- rot_matrix = Mat4.new(1.0)
214
- axis_priority.each { |(axis, _)|
215
- mat = Mat4.new(1.0)
216
-
217
- if axis == :x then
218
- mat[1][1] = mat[2][2] = cos_angle
219
- mat[1][2] = sin_angle
220
- mat[2][1] = -sin_angle
221
- elsif axis == :y then
222
- mat[0][0] = mat[2][2] = cos_angle
223
- mat[0][2] = -sin_angle
224
- mat[2][0] = sin_angle
225
- else
226
- mat[0][0] = mat[1][1] = cos_angle
227
- mat[0][1] = sin_angle
228
- mat[1][0] = -sin_angle
229
- end
230
-
231
- rot_matrix *= mat
232
- }
233
-
234
- rot_matrix
235
- end
236
-
237
- def self.perspective(fov, aspect, z_near, z_far)
238
- top = Math::tan(Conversion::deg_to_rad(fov) / 2) * z_near
239
- right = top * aspect
240
-
241
- mat = Mat4.new(1.0)
242
- mat[0][0] = z_near / right
243
- mat[1][1] = z_near / top
244
- mat[2][2] = -(z_far + z_near) / (z_far - z_near)
245
- mat[3][2] = -2.0 * z_far * z_near / (z_far - z_near)
246
- mat[2][3] = -1.0
247
-
248
- mat
249
- end
250
-
251
- def self.orthogonal(left, right, bottom, top, z_near = -1.0, z_far = 1.0)
252
- mat = Mat4.new(1.0)
253
- mat[0][0] = 2.0 / (right - left)
254
- mat[1][1] = 2.0 / (top - bottom)
255
- mat[2][2] = 2.0 / (z_near - z_far)
256
- mat[3][3] = 1.0
257
- mat[3][0] = -(right + left) / (right - left)
258
- mat[3][1] = -(top + bottom) / (top - bottom)
259
- mat[3][2] = -(z_far + z_near) / (z_far - z_near)
260
-
261
- mat
262
- end
263
-
264
- def *(other_matrix)
265
- new_matrix = Mat4.new(0)
266
-
267
- for i in 0...self.dim
268
- for j in 0...self.dim
269
- for k in 0...self.dim
270
- new_matrix[j][i] += self[k][i] * other_matrix[j][k]
271
- end
272
- end
273
- end
274
-
275
- new_matrix
276
- end
277
-
278
- def dim()
279
- @data.size()
280
- end
281
-
282
- def [](index)
283
- @data[index]
284
- end
285
-
286
- def []=(index, value)
287
- @data[index] = value
288
- end
289
-
290
- def to_ary()
291
- @data.collect { |vec|
292
- vec.to_ary
293
- }.flatten!
294
- end
295
-
296
- def to_a()
297
- self.to_ary
298
- end
299
- end
300
-
1
+
2
+ module RubyGL
3
+
4
+ class Conversion
5
+ @@PI_DIV = Math::PI / 180.0
6
+ @@DIV_PI = 180.0 / Math::PI
7
+
8
+ def self.deg_to_rad(degrees)
9
+ degrees * @@PI_DIV
10
+ end
11
+
12
+ def self.rad_to_deg(radians)
13
+ radians * @@DIV_PI
14
+ end
15
+ end
16
+
17
+ class Vec3
18
+ def initialize(array = nil)
19
+ @data = Array.new(3, 0)
20
+
21
+ if array then
22
+ for i in 0...@data.size
23
+ @data[i] = array[i]
24
+ end
25
+ end
26
+ end
27
+
28
+ def +(other_vector)
29
+ new_vector = Vec3.new()
30
+
31
+ for i in 0...@data.size
32
+ new_vector[i] = @data[i] + other_vector[i]
33
+ end
34
+
35
+ new_vector
36
+ end
37
+
38
+ def -(other_vector)
39
+ new_vector = Vec3.new()
40
+
41
+ for i in 0...@data.size
42
+ new_vector[i] = @data[i] - other_vector[i]
43
+ end
44
+
45
+ new_vector
46
+ end
47
+
48
+ def cross(other_vector)
49
+ new_vector = Vec3.new()
50
+
51
+ new_vector[0] = (@data[1] * other_vector[2]) - (@data[2] * other_vector[1])
52
+ new_vector[1] = (@data[0] * other_vector[2]) - (@data[2] * other_vector[0])
53
+ new_vector[2] = (@data[0] * other_vector[1]) - (@data[1] * other_vector[0])
54
+
55
+ new_vector
56
+ end
57
+
58
+ def norm!()
59
+ curr_len = self.len().abs()
60
+
61
+ for i in 0...@data.size
62
+ @data[i] /= curr_len
63
+ end
64
+ end
65
+
66
+ def norm()
67
+ new_vector = Vec2.new()
68
+
69
+ for i in 0...@data.size
70
+ new_vector[i] = @data[i]
71
+ end
72
+ new_vector.norm!
73
+
74
+ new_vector
75
+ end
76
+
77
+ def len()
78
+ sum = 0
79
+
80
+ for i in 0...@data.size
81
+ sum += @data[i] * @data[i]
82
+ end
83
+
84
+ Math::sqrt(sum)
85
+ end
86
+
87
+ def [](index)
88
+ @data[index]
89
+ end
90
+
91
+ def []=(index, value)
92
+ @data[index] = value
93
+ end
94
+
95
+ def to_ary()
96
+ Array.new(@data)
97
+ end
98
+
99
+ def to_a()
100
+ self.to_ary
101
+ end
102
+ end
103
+
104
+ class Vec4
105
+ def initialize()
106
+ @data = Array.new(4, 0)
107
+ end
108
+
109
+ def +(other_vector)
110
+ new_vector = Vec4.new()
111
+
112
+ for i in 0..@data.size
113
+ new_vector[i] = @data[i] + other_vector[i]
114
+ end
115
+
116
+ new_vector
117
+ end
118
+
119
+ def -(other_vector)
120
+ new_vector = Vec4.new()
121
+
122
+ for i in 0..@data.size
123
+ new_vector[i] = @data[i] - other_vector[i]
124
+ end
125
+
126
+ new_vector
127
+ end
128
+
129
+ def norm!()
130
+ curr_len = self.len()
131
+
132
+ for i in 0...@data.size
133
+ @data /= curr_len
134
+ end
135
+ end
136
+
137
+ def norm()
138
+ new_vector = Vec2.new()
139
+
140
+ for i in 0...@data.size
141
+ new_vector[i] = @data[i]
142
+ end
143
+ new_vector.norm!
144
+
145
+ new_vector
146
+ end
147
+
148
+ def len()
149
+ sum = 0
150
+
151
+ for i in 0...@data.size
152
+ sum += @data[i] * @data[i]
153
+ end
154
+
155
+ Math::sqrt(sum)
156
+ end
157
+
158
+ def [](index)
159
+ @data[index]
160
+ end
161
+
162
+ def []=(index, value)
163
+ @data[index] = value
164
+ end
165
+
166
+ def to_ary()
167
+ Array.new(@data)
168
+ end
169
+
170
+ def to_a()
171
+ self.to_ary
172
+ end
173
+ end
174
+
175
+ # Stored In Column Order For Interoperability With OpenGL. Subscript Operator
176
+ # Will Return A Vector That Represents A Column Within The Matrix. This Matrix
177
+ # Class Only Supports Matrices Where The Number Of Rows Equals The Number Of
178
+ # Columns; Operations Are Constrained As Such.
179
+ class Mat4
180
+ def initialize(diagonal = 1.0)
181
+ @data = Array.new(4) { |index|
182
+ column = Vec4.new()
183
+ column[index] = diagonal
184
+
185
+ column
186
+ }
187
+ end
188
+
189
+ def self.translation(x, y, z)
190
+ matrix = Mat4.new(1.0)
191
+
192
+ matrix[3][0] = x
193
+ matrix[3][1] = y
194
+ matrix[3][2] = z
195
+
196
+ matrix
197
+ end
198
+
199
+ # The axis value should be 0 if that axis should not be rotated around;
200
+ # any other value indicates its priority (higher priority axis is rotated
201
+ # around before the lower priority axis).
202
+ def self.rotation(x_axis, y_axis, z_axis, theta)
203
+ rad_angle = Conversion::deg_to_rad(theta)
204
+ sin_angle = Math::sin(rad_angle)
205
+ cos_angle = Math::cos(rad_angle)
206
+
207
+ # Multiply Lower -> Higher To Get Ordering Correct
208
+ axis_priority = [[:x, x_axis], [:y, y_axis], [:z, z_axis]]
209
+ axis_priority.delete_if { |(_, val)|
210
+ val == 0
211
+ }.sort!.reverse!
212
+
213
+ rot_matrix = Mat4.new(1.0)
214
+ axis_priority.each { |(axis, _)|
215
+ mat = Mat4.new(1.0)
216
+
217
+ if axis == :x then
218
+ mat[1][1] = mat[2][2] = cos_angle
219
+ mat[1][2] = sin_angle
220
+ mat[2][1] = -sin_angle
221
+ elsif axis == :y then
222
+ mat[0][0] = mat[2][2] = cos_angle
223
+ mat[0][2] = -sin_angle
224
+ mat[2][0] = sin_angle
225
+ else
226
+ mat[0][0] = mat[1][1] = cos_angle
227
+ mat[0][1] = sin_angle
228
+ mat[1][0] = -sin_angle
229
+ end
230
+
231
+ rot_matrix *= mat
232
+ }
233
+
234
+ rot_matrix
235
+ end
236
+
237
+ def self.perspective(fov, aspect, z_near, z_far)
238
+ top = Math::tan(Conversion::deg_to_rad(fov) / 2) * z_near
239
+ right = top * aspect
240
+
241
+ mat = Mat4.new(1.0)
242
+ mat[0][0] = z_near / right
243
+ mat[1][1] = z_near / top
244
+ mat[2][2] = -(z_far + z_near) / (z_far - z_near)
245
+ mat[3][2] = -2.0 * z_far * z_near / (z_far - z_near)
246
+ mat[2][3] = -1.0
247
+
248
+ mat
249
+ end
250
+
251
+ def self.orthogonal(left, right, bottom, top, z_near = -1.0, z_far = 1.0)
252
+ mat = Mat4.new(1.0)
253
+ mat[0][0] = 2.0 / (right - left)
254
+ mat[1][1] = 2.0 / (top - bottom)
255
+ mat[2][2] = 2.0 / (z_near - z_far)
256
+ mat[3][3] = 1.0
257
+ mat[3][0] = -(right + left) / (right - left)
258
+ mat[3][1] = -(top + bottom) / (top - bottom)
259
+ mat[3][2] = -(z_far + z_near) / (z_far - z_near)
260
+
261
+ mat
262
+ end
263
+
264
+ def *(other_matrix)
265
+ new_matrix = Mat4.new(0)
266
+
267
+ for i in 0...self.dim
268
+ for j in 0...self.dim
269
+ for k in 0...self.dim
270
+ new_matrix[j][i] += self[k][i] * other_matrix[j][k]
271
+ end
272
+ end
273
+ end
274
+
275
+ new_matrix
276
+ end
277
+
278
+ def dim()
279
+ @data.size()
280
+ end
281
+
282
+ def [](index)
283
+ @data[index]
284
+ end
285
+
286
+ def []=(index, value)
287
+ @data[index] = value
288
+ end
289
+
290
+ def to_ary()
291
+ @data.collect { |vec|
292
+ vec.to_ary
293
+ }.flatten!
294
+ end
295
+
296
+ def to_a()
297
+ self.to_ary
298
+ end
299
+ end
300
+
301
301
  end