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,212 +1,217 @@
1
- require_relative './math'
2
- require_relative './util'
3
-
4
- module RubyGL
5
-
6
- # All Point Operations Return New Points
7
- class Point
8
- attr_accessor :x, :y, :z
9
-
10
- def initialize(x, y, z = 0)
11
- @x = x
12
- @y = y
13
- @z = z
14
- end
15
-
16
- def +(point)
17
- Point.new(@x + point.x, @y + point.y, @z + point.z)
18
- end
19
-
20
- def -(point)
21
- Point.new(@x - point.x, @y - point.y, @z - point.z)
22
- end
23
-
24
- def scale(value)
25
- Point.new(@x * value, @y * value, @z * value)
26
- end
27
-
28
- def distance(point)
29
- temp = self - point
30
-
31
- Math::sqrt(temp.x * temp.x + temp.y * temp.y + temp.z * temp.z)
32
- end
33
-
34
- def midpoint(point)
35
- Point.new((@x + point.x) * 0.5, (@y + point.y) * 0.5, (@z + point.z) * 0.5)
36
- end
37
-
38
- def to_ary
39
- [@x, @y, @z]
40
- end
41
-
42
- def to_a
43
- self.to_ary
44
- end
45
- end
46
-
47
- # Class For Generating 2-D Shapes
48
- class SimpleShape
49
-
50
- end
51
-
52
- # Class For Generating 3-D Shapes
53
- class ComplexShape
54
- # Generates an array of 3 component vertices in counter-clockwise triangle
55
- # configuration. This is the vertex data for a 3 dimensional sphere. The
56
- # num_rings parameter should always be 0 or a positive even number.
57
- def self.gen_sphere(radius, num_rings)
58
- points_per_ring = num_rings * 2 + 4
59
- rings_per_half = num_rings / 2 + 1 # Count The Center Ring For Both Sides
60
-
61
- # Generate Points From Bottom Of Sphere To The Top
62
- points = []
63
- for i in 1..num_rings + 1
64
- ring_y_factor = (i - rings_per_half).to_f / rings_per_half
65
- ring_y_value = ring_y_factor * radius
66
- ring_radius = Math::cos(Math::asin(ring_y_value / radius)) * radius
67
-
68
- for i in 0..points_per_ring
69
- radians = Conversion.deg_to_rad(i.to_f / points_per_ring * 360)
70
-
71
- x = Math::cos(radians) * ring_radius
72
- z = Math::sin(radians) * ring_radius
73
-
74
- points.push(Point.new(x, ring_y_value, z))
75
- end
76
- end
77
-
78
- vertex_array, previous_points = [], []
79
-
80
- # Build Bottom End-Cap
81
- bottom_point = [0, -radius, 0]
82
- for i in 0...points_per_ring
83
- curr_vert = points[i]
84
- next_vert = Util.overflow_wrap(points, i + 1)
85
-
86
- vertex_array.push([next_vert.to_a, curr_vert.to_a, bottom_point])
87
- previous_points.push(curr_vert)
88
- end
89
-
90
- # Build Intermediate Mesh
91
- for i in points_per_ring...points.size
92
- curr_vert = points[i]
93
- next_vert = Util.overflow_wrap(points, i + 1)
94
-
95
- last_curr_vert = previous_points[i % points_per_ring]
96
- last_next_vert = Util.overflow_wrap(previous_points, (i % points_per_ring) + 1)
97
-
98
- vertex_array.push([next_vert.to_a, curr_vert.to_a, last_curr_vert.to_a])
99
- vertex_array.push([last_next_vert.to_a, next_vert.to_a, last_curr_vert.to_a])
100
- previous_points[i % points_per_ring] = curr_vert
101
- end
102
-
103
- # Build Top End-Cap
104
- top_point = [0, radius, 0]
105
- for i in 0...previous_points.size()
106
- curr_vert = previous_points[i]
107
- next_vert = Util.overflow_wrap(previous_points, i + 1)
108
-
109
- vertex_array.push([curr_vert.to_a, next_vert.to_a, top_point.to_a])
110
- end
111
-
112
- vertex_array.flatten!
113
-
114
- vertex_array
115
- end
116
-
117
- # Generates an array of 3 component vertices in counter-clockwise triangle
118
- # configuration. This is the vertex data for a 3 dimensional diamond. The
119
- # girdle_facets parameters should always be an even number.
120
- def self.gen_diamond(diamond_height, girdle_radius, girdle_facets)
121
- # Table Y Value Is Equal To diamond_height
122
- girdle_y_value = diamond_height * 2.5 / 3.0 # Girdle Upper Ring
123
- crown_y_midpoint = 2.0 / 3.0
124
-
125
- girdle_thickness = diamond_height / 10.0
126
-
127
- table_radius = girdle_radius / 1.8
128
- table_facets = girdle_facets / 2.0
129
-
130
- girdle_points = []
131
- girdle_point_factor = 360.0 / girdle_facets # Multiply With Point Index To Get Degree
132
- # Generate x And z Values For Diamond Girdle
133
- for i in 0...girdle_facets
134
- curr_degree = i.to_f * girdle_point_factor
135
- curr_rad = Conversion::deg_to_rad(curr_degree)
136
-
137
- x = Math::cos(curr_rad) * girdle_radius
138
- z = Math::sin(curr_rad) * girdle_radius
139
-
140
- girdle_points.push(Point.new(x, girdle_y_value, z))
141
- end
142
-
143
- table_points = []
144
- table_point_factor = 360.0 / table_facets # Multiply With Point Index To Get Degree
145
- # Generate x And z Values For Diamond Table
146
- for i in 0...table_facets
147
- curr_degree = i.to_f * table_point_factor
148
- curr_rad = Conversion::deg_to_rad(curr_degree)
149
-
150
- x = Math::cos(curr_rad) * table_radius
151
- z = Math::sin(curr_rad) * table_radius
152
-
153
- table_points.push(Point.new(x, diamond_height, z))
154
- end
155
-
156
- vertex_array = []
157
- # Traverse Every Other Point On The Girdle
158
- for i in 0...girdle_points.size / 2
159
- center_girdle_index = (i * 2) + 1
160
- left_girdle_index = center_girdle_index - 1
161
- right_girdle_index = center_girdle_index + 1
162
-
163
- gird_center = girdle_points[center_girdle_index]
164
- gird_left = girdle_points[left_girdle_index]
165
- gird_right = Util.overflow_wrap(girdle_points, right_girdle_index)
166
-
167
- tab_left = table_points[left_girdle_index / 2]
168
- tab_right = Util.overflow_wrap(table_points, right_girdle_index / 2)
169
-
170
- # Crown Triangles
171
- # Using 3D Equation Of A Line To Get Central Connection Point For Crown
172
- slope = tab_left.midpoint(tab_right) - gird_center
173
- crown_midpoint = slope.scale(crown_y_midpoint) + gird_center
174
-
175
- vertex_array.push([crown_midpoint.to_a, gird_left.to_a, gird_center.to_a])
176
- vertex_array.push([crown_midpoint.to_a, gird_center.to_a, gird_right.to_a])
177
-
178
- vertex_array.push([crown_midpoint.to_a, tab_left.to_a, gird_left.to_a])
179
- vertex_array.push([crown_midpoint.to_a, gird_right.to_a, tab_right.to_a])
180
-
181
- vertex_array.push([crown_midpoint.to_a, tab_right.to_a, tab_left.to_a])
182
-
183
- # Girdle Triangles
184
- lower_gird_center = Point.new(gird_center.x, gird_center.y - girdle_thickness, gird_center.z)
185
- lower_gird_left = Point.new(gird_left.x, gird_left.y - girdle_thickness, gird_left.z)
186
- lower_gird_right = Point.new(gird_right.x, gird_right.y - girdle_thickness, gird_right.z)
187
-
188
- vertex_array.push([gird_center.to_a, gird_left.to_a, lower_gird_left.to_a])
189
- vertex_array.push([gird_center.to_a, lower_gird_left.to_a, lower_gird_center.to_a])
190
-
191
- vertex_array.push([gird_center.to_a, lower_gird_center.to_a, gird_right.to_a])
192
- vertex_array.push([gird_right.to_a, lower_gird_center.to_a, lower_gird_right.to_a])
193
-
194
- # Pavilion Triangles
195
- pavil_bottom = Point.new(0, 0, 0)
196
-
197
- vertex_array.push([lower_gird_center.to_a, lower_gird_left.to_a, pavil_bottom.to_a])
198
- vertex_array.push([lower_gird_center.to_a, pavil_bottom.to_a, lower_gird_right.to_a])
199
-
200
- # Table Triangles
201
- table_mid = Point.new(0, diamond_height, 0)
202
-
203
- vertex_array.push([table_mid.to_a, tab_left.to_a, tab_right.to_a])
204
- end
205
-
206
- vertex_array.flatten!
207
-
208
- vertex_array
209
- end
210
- end
211
-
1
+ require_relative './math'
2
+ require_relative './util'
3
+
4
+ module RubyGL
5
+
6
+ # All Point Operations Return New Points
7
+ class Point
8
+ attr_accessor :x, :y, :z
9
+
10
+ def initialize(x, y, z = 0)
11
+ @x = x
12
+ @y = y
13
+ @z = z
14
+ end
15
+
16
+ def +(point)
17
+ Point.new(@x + point.x, @y + point.y, @z + point.z)
18
+ end
19
+
20
+ def -(point)
21
+ Point.new(@x - point.x, @y - point.y, @z - point.z)
22
+ end
23
+
24
+ def *(point)
25
+ Point.new(@x * point.x, @y * point.y, @z * point.z)
26
+ end
27
+
28
+ def scale(value)
29
+ Point.new(@x * value, @y * value, @z * value)
30
+ end
31
+
32
+ def distance(point)
33
+ temp = self - point
34
+
35
+ Math::sqrt(temp.x * temp.x + temp.y * temp.y + temp.z * temp.z)
36
+ end
37
+
38
+ def midpoint(point)
39
+ Point.new((@x + point.x) * 0.5, (@y + point.y) * 0.5, (@z + point.z) * 0.5)
40
+ end
41
+
42
+ def to_ary
43
+ [@x, @y, @z]
44
+ end
45
+
46
+ def to_a
47
+ self.to_ary
48
+ end
49
+ end
50
+
51
+ # Class For Generating 2-D Shapes
52
+ class SimpleShape
53
+
54
+ end
55
+
56
+ # Class For Generating 3-D Shapes
57
+ class ComplexShape
58
+ # Generates an array of 3 component vertices in counter-clockwise triangle
59
+ # configuration. This is the vertex data for a 3 dimensional sphere. The
60
+ # num_rings parameter should always be 0 or a positive even number.
61
+ def self.gen_sphere(radius, num_rings)
62
+ points_per_ring = num_rings * 2 + 4
63
+ rings_per_half = num_rings / 2 + 1 # Count The Center Ring For Both Sides
64
+
65
+ # Generate Points From Bottom Of Sphere To The Top
66
+ points = []
67
+ for i in 1..num_rings + 1
68
+ ring_y_factor = (i - rings_per_half).to_f / rings_per_half
69
+ ring_y_value = ring_y_factor * radius
70
+ ring_radius = Math::cos(Math::asin(ring_y_value / radius)) * radius
71
+
72
+ for i in 0..points_per_ring
73
+ radians = Conversion.deg_to_rad(i.to_f / points_per_ring * 360)
74
+
75
+ x = Math::cos(radians) * ring_radius
76
+ z = Math::sin(radians) * ring_radius
77
+
78
+ points.push(Point.new(x, ring_y_value, z))
79
+ end
80
+ end
81
+
82
+ vertex_array, previous_points = [], []
83
+
84
+ # Build Bottom End-Cap
85
+ bottom_point = [0, -radius, 0]
86
+ for i in 0...points_per_ring
87
+ curr_vert = points[i]
88
+ next_vert = Util.overflow_wrap(points, i + 1)
89
+
90
+ vertex_array.push([curr_vert.to_a, next_vert.to_a, bottom_point])
91
+ previous_points.push(curr_vert)
92
+ end
93
+
94
+ # Build Intermediate Mesh
95
+ for i in points_per_ring...points.size
96
+ curr_vert = points[i]
97
+ next_vert = Util.overflow_wrap(points, i + 1)
98
+
99
+ last_curr_vert = previous_points[i % points_per_ring]
100
+ last_next_vert = Util.overflow_wrap(previous_points, (i % points_per_ring) + 1)
101
+
102
+ vertex_array.push([curr_vert.to_a, next_vert.to_a, last_curr_vert.to_a])
103
+ vertex_array.push([next_vert.to_a, last_next_vert.to_a, last_curr_vert.to_a])
104
+
105
+ previous_points[i % points_per_ring] = curr_vert
106
+ end
107
+
108
+ # Build Top End-Cap
109
+ top_point = [0, radius, 0]
110
+ for i in 0...previous_points.size()
111
+ curr_vert = previous_points[i]
112
+ next_vert = Util.overflow_wrap(previous_points, i + 1)
113
+
114
+ vertex_array.push([next_vert.to_a, curr_vert.to_a, top_point.to_a])
115
+ end
116
+
117
+ vertex_array.flatten!
118
+
119
+ vertex_array
120
+ end
121
+
122
+ # Generates an array of 3 component vertices in counter-clockwise triangle
123
+ # configuration. This is the vertex data for a 3 dimensional diamond. The
124
+ # girdle_facets parameters should always be an even number.
125
+ def self.gen_diamond(diamond_height, girdle_radius, girdle_facets)
126
+ # Table Y Value Is Equal To diamond_height
127
+ girdle_y_value = diamond_height * 2.5 / 3.0 # Girdle Upper Ring
128
+ crown_y_midpoint = 2.0 / 3.0
129
+
130
+ girdle_thickness = diamond_height / 10.0
131
+
132
+ table_radius = girdle_radius / 1.8
133
+ table_facets = girdle_facets / 2.0
134
+
135
+ girdle_points = []
136
+ girdle_point_factor = 360.0 / girdle_facets # Multiply With Point Index To Get Degree
137
+ # Generate x And z Values For Diamond Girdle
138
+ for i in 0...girdle_facets
139
+ curr_degree = i.to_f * girdle_point_factor
140
+ curr_rad = Conversion::deg_to_rad(curr_degree)
141
+
142
+ x = Math::cos(curr_rad) * girdle_radius
143
+ z = Math::sin(curr_rad) * girdle_radius
144
+
145
+ girdle_points.push(Point.new(x, girdle_y_value, z))
146
+ end
147
+
148
+ table_points = []
149
+ table_point_factor = 360.0 / table_facets # Multiply With Point Index To Get Degree
150
+ # Generate x And z Values For Diamond Table
151
+ for i in 0...table_facets
152
+ curr_degree = i.to_f * table_point_factor
153
+ curr_rad = Conversion::deg_to_rad(curr_degree)
154
+
155
+ x = Math::cos(curr_rad) * table_radius
156
+ z = Math::sin(curr_rad) * table_radius
157
+
158
+ table_points.push(Point.new(x, diamond_height, z))
159
+ end
160
+
161
+ vertex_array = []
162
+ # Traverse Every Other Point On The Girdle
163
+ for i in 0...girdle_points.size / 2
164
+ center_girdle_index = (i * 2) + 1
165
+ left_girdle_index = center_girdle_index - 1
166
+ right_girdle_index = center_girdle_index + 1
167
+
168
+ gird_center = girdle_points[center_girdle_index]
169
+ gird_left = girdle_points[left_girdle_index]
170
+ gird_right = Util.overflow_wrap(girdle_points, right_girdle_index)
171
+
172
+ tab_left = table_points[left_girdle_index / 2]
173
+ tab_right = Util.overflow_wrap(table_points, right_girdle_index / 2)
174
+
175
+ # Crown Triangles
176
+ # Using 3D Equation Of A Line To Get Central Connection Point For Crown
177
+ slope = tab_left.midpoint(tab_right) - gird_center
178
+ crown_midpoint = slope.scale(crown_y_midpoint) + gird_center
179
+
180
+ vertex_array.push([gird_left.to_a, crown_midpoint.to_a, gird_center.to_a])
181
+ vertex_array.push([gird_center.to_a, crown_midpoint.to_a, gird_right.to_a])
182
+
183
+ vertex_array.push([tab_left.to_a, crown_midpoint.to_a, gird_left.to_a])
184
+ vertex_array.push([gird_right.to_a, crown_midpoint.to_a, tab_right.to_a])
185
+
186
+ vertex_array.push([tab_right.to_a, crown_midpoint.to_a, tab_left.to_a])
187
+
188
+ # Girdle Triangles
189
+ lower_gird_center = Point.new(gird_center.x, gird_center.y - girdle_thickness, gird_center.z)
190
+ lower_gird_left = Point.new(gird_left.x, gird_left.y - girdle_thickness, gird_left.z)
191
+ lower_gird_right = Point.new(gird_right.x, gird_right.y - girdle_thickness, gird_right.z)
192
+
193
+ vertex_array.push([gird_left.to_a, gird_center.to_a, lower_gird_left.to_a])
194
+ vertex_array.push([lower_gird_left.to_a, gird_center.to_a, lower_gird_center.to_a])
195
+
196
+ vertex_array.push([lower_gird_center.to_a, gird_center.to_a, gird_right.to_a])
197
+ vertex_array.push([lower_gird_center.to_a, gird_right.to_a, lower_gird_right.to_a])
198
+
199
+ # Pavilion Triangles
200
+ pavil_bottom = Point.new(0, 0, 0)
201
+
202
+ vertex_array.push([lower_gird_left.to_a, lower_gird_center.to_a, pavil_bottom.to_a])
203
+ vertex_array.push([pavil_bottom.to_a, lower_gird_center.to_a, lower_gird_right.to_a])
204
+
205
+ # Table Triangles
206
+ table_mid = Point.new(0, diamond_height, 0)
207
+
208
+ vertex_array.push([tab_left.to_a, table_mid.to_a, tab_right.to_a])
209
+ end
210
+
211
+ vertex_array.flatten!
212
+
213
+ vertex_array
214
+ end
215
+ end
216
+
212
217
  end