rubygl 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.travis/push-rdoc-to-gh-pages.sh +22 -0
- data/.travis.yml +18 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +12 -0
- data/LICENSE +21 -21
- data/README.md +40 -0
- data/Rakefile +98 -83
- data/bin/ffi_code_gen.rb +166 -166
- data/bin/gl_code_gen.rb +458 -458
- data/examples/faceted_example.rb +71 -64
- data/examples/instanced_example.rb +135 -127
- data/examples/phong_example.rb +80 -71
- data/ext/windows/RubyGL.so +0 -0
- data/lib/rubygl/event.rb +64 -0
- data/lib/{RubyGL → rubygl}/geometry.rb +216 -211
- data/lib/{RubyGL → rubygl}/math.rb +300 -300
- data/lib/{RubyGL → rubygl}/memory.rb +125 -121
- data/lib/rubygl/native/all_enums.rb +641 -0
- data/lib/{RubyGL/Native → rubygl/native}/glcontext.rb +23 -47
- data/lib/{RubyGL/Native → rubygl/native}/include/GLContext.h +36 -36
- data/lib/{RubyGL/Native → rubygl/native}/include/Input.h +15 -15
- data/lib/{RubyGL/Native → rubygl/native}/include/Window.h +27 -27
- data/lib/rubygl/native/input.rb +247 -0
- data/lib/{RubyGL/Native → rubygl/native}/opengl.rb +2808 -2032
- data/lib/{RubyGL/Native → rubygl/native}/src/GLContext.c +72 -72
- data/lib/{RubyGL/Native → rubygl/native}/src/Input.c +25 -25
- data/lib/{RubyGL/Native → rubygl/native}/src/Window.c +57 -57
- data/lib/{RubyGL/Native → rubygl/native}/window.rb +24 -24
- data/lib/{RubyGL → rubygl}/setup.rb +50 -50
- data/lib/{RubyGL → rubygl}/shader.rb +203 -203
- data/lib/{RubyGL → rubygl}/util.rb +77 -77
- data/lib/rubygl.rb +49 -48
- data/{RubyGL.gemspec → rubygl.gemspec} +20 -23
- data/test/test_util.rb +19 -0
- metadata +36 -33
- data/lib/RubyGL/Native/input.rb +0 -13
- data/lib/RubyGL/callback.rb +0 -10
@@ -1,203 +1,203 @@
|
|
1
|
-
require_relative './native/opengl'
|
2
|
-
|
3
|
-
module RubyGL
|
4
|
-
|
5
|
-
class Shader
|
6
|
-
def initialize(vert_src, frag_src)
|
7
|
-
@v_shader_id = Native.glCreateShader(Native::GL_VERTEX_SHADER)
|
8
|
-
@f_shader_id = Native.glCreateShader(Native::GL_FRAGMENT_SHADER)
|
9
|
-
@attrib_locs, @uniform_locs = {}, {}
|
10
|
-
|
11
|
-
# Pointer Used To Point To The String Of Our Source Code
|
12
|
-
shader_src_ptr = FFI::MemoryPointer.new(:pointer)
|
13
|
-
shader_src_len = FFI::MemoryPointer.new(:int)
|
14
|
-
|
15
|
-
v_shader_ptr = FFI::MemoryPointer.from_string(vert_src)
|
16
|
-
f_shader_ptr = FFI::MemoryPointer.from_string(frag_src)
|
17
|
-
|
18
|
-
shader_src_ptr.write_pointer(v_shader_ptr.address)
|
19
|
-
shader_src_len.write_int(vert_src.size)
|
20
|
-
Native.glShaderSource(@v_shader_id, 1, shader_src_ptr, shader_src_len)
|
21
|
-
|
22
|
-
shader_src_ptr.write_pointer(f_shader_ptr.address)
|
23
|
-
shader_src_len.write_int(frag_src.size)
|
24
|
-
Native.glShaderSource(@f_shader_id, 1, shader_src_ptr, shader_src_len)
|
25
|
-
|
26
|
-
Native.glCompileShader(@v_shader_id)
|
27
|
-
Native.glCompileShader(@f_shader_id)
|
28
|
-
|
29
|
-
# Throw Exception With Compile Error If Compilation Failed
|
30
|
-
Shader.compile_status(@v_shader_id)
|
31
|
-
Shader.compile_status(@f_shader_id)
|
32
|
-
|
33
|
-
@program_id = Native.glCreateProgram()
|
34
|
-
|
35
|
-
Native.glAttachShader(@program_id, @v_shader_id)
|
36
|
-
Native.glAttachShader(@program_id, @f_shader_id)
|
37
|
-
|
38
|
-
Native.glLinkProgram(@program_id)
|
39
|
-
|
40
|
-
Shader.link_status(@program_id)
|
41
|
-
end
|
42
|
-
|
43
|
-
def use_program()
|
44
|
-
Native.glUseProgram(@program_id)
|
45
|
-
end
|
46
|
-
|
47
|
-
# Splat Operator Aggregates Incoming Parameters And Expands Outgoing Parameters
|
48
|
-
# If method_sym Does Not Accept A Pointer To The Data, Pass nil For data And
|
49
|
-
# Pass In The Normal Parameters Required For The Specific Method.
|
50
|
-
def send_uniform(method_sym, var_name, data = nil, *args)
|
51
|
-
loc = @uniform_locs[var_name] ||= uniform_location(var_name)
|
52
|
-
|
53
|
-
if data != nil then
|
54
|
-
data_ptr = FFI::MemoryPointer.new(:float, data.size)
|
55
|
-
data_ptr.write_array_of_float(data)
|
56
|
-
|
57
|
-
RubyGL::Native.send(method_sym, loc, *args, data_ptr)
|
58
|
-
else
|
59
|
-
RubyGL::Native.send(method_sym, loc, *args)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def attrib_location(var_name)
|
64
|
-
Native.glGetAttribLocation(@program_id, var_name)
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
68
|
-
def uniform_location(var_name)
|
69
|
-
Native.glGetUniformLocation(@program_id, var_name)
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.compile_status(shader_id)
|
73
|
-
result = FFI::MemoryPointer.new(:int)
|
74
|
-
Native.glGetShaderiv(shader_id, Native::GL_COMPILE_STATUS, result)
|
75
|
-
|
76
|
-
if (result.get_int(0) != Native::GL_TRUE) then
|
77
|
-
Native.glGetShaderiv(shader_id, Native::GL_INFO_LOG_LENGTH, result)
|
78
|
-
|
79
|
-
compile_log = FFI::MemoryPointer.new(:char, result.get_int(0))
|
80
|
-
|
81
|
-
Native.glGetShaderInfoLog(shader_id, compile_log.total, FFI::MemoryPointer::NULL, compile_log)
|
82
|
-
|
83
|
-
raise compile_log.read_string_to_null()
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def self.link_status(program_id)
|
88
|
-
result = FFI::MemoryPointer.new(:int)
|
89
|
-
Native.glGetProgramiv(program_id, Native::GL_LINK_STATUS, result)
|
90
|
-
|
91
|
-
if (result.get_int(0) != Native::GL_TRUE) then
|
92
|
-
Native.glGetProgramiv(program_id, Native::GL_INFO_LOG_LENGTH, result)
|
93
|
-
|
94
|
-
link_log = FFI::MemoryPointer.new(:char, result.get_int(0))
|
95
|
-
|
96
|
-
Native.glGetShaderInfoLog(program_id, link_log.total, FFI::MemoryPointer::NULL, link_log)
|
97
|
-
|
98
|
-
raise link_log.read_string_to_null()
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
class ShaderGenerator
|
104
|
-
def self.faceted_shader()
|
105
|
-
Shader.new('''
|
106
|
-
#version 130
|
107
|
-
in vec3 position;
|
108
|
-
out vec3 vPosition;
|
109
|
-
uniform mat4 modelview;
|
110
|
-
uniform mat4 perspective;
|
111
|
-
|
112
|
-
void main() {
|
113
|
-
vec4 hPosition = modelview * vec4(position, 1);
|
114
|
-
vPosition = hPosition.xyz;
|
115
|
-
|
116
|
-
gl_Position = perspective * hPosition;
|
117
|
-
}
|
118
|
-
''','''
|
119
|
-
#version 130
|
120
|
-
in vec3 vPosition;
|
121
|
-
out vec4 fColor;
|
122
|
-
uniform vec4 color;
|
123
|
-
uniform vec3 light;
|
124
|
-
|
125
|
-
void main() {
|
126
|
-
vec3 dx = dFdy(vPosition);
|
127
|
-
vec3 dy = dFdx(vPosition);
|
128
|
-
vec3 triangle_norm = normalize(cross(dx, dy));
|
129
|
-
float factor = clamp(dot(triangle_norm, light), 0, 1);
|
130
|
-
|
131
|
-
fColor = vec4(color.xyz * factor, color.w);
|
132
|
-
}
|
133
|
-
''')
|
134
|
-
end
|
135
|
-
|
136
|
-
def self.color_shader()
|
137
|
-
Shader.new('''
|
138
|
-
#version 130
|
139
|
-
in vec3 position;
|
140
|
-
uniform mat4 modelview;
|
141
|
-
uniform mat4 perspective;
|
142
|
-
|
143
|
-
void main() {
|
144
|
-
gl_Position = perspective * modelView * vec4(position, 1);
|
145
|
-
}
|
146
|
-
''','''
|
147
|
-
#version 130
|
148
|
-
out vec4 fColor;
|
149
|
-
uniform vec4 color;
|
150
|
-
|
151
|
-
void main() {
|
152
|
-
fColor = color;
|
153
|
-
}
|
154
|
-
''')
|
155
|
-
end
|
156
|
-
|
157
|
-
def self.phong_shader()
|
158
|
-
Shader.new('''
|
159
|
-
#version 130
|
160
|
-
in vec3 position;
|
161
|
-
in vec3 normal;
|
162
|
-
out vec3 vPosition;
|
163
|
-
out vec3 vNormal;
|
164
|
-
uniform mat4 modelview;
|
165
|
-
uniform mat4 perspective;
|
166
|
-
|
167
|
-
void main() {
|
168
|
-
vec4 hPosition = modelview * vec4(position, 1);
|
169
|
-
vPosition = hPosition.xyz;
|
170
|
-
vNormal = (modelview * vec4(normal, 1)).xyz;
|
171
|
-
|
172
|
-
gl_Position = perspective * hPosition;
|
173
|
-
}
|
174
|
-
''','''
|
175
|
-
#version 130
|
176
|
-
in vec3 vPosition;
|
177
|
-
in vec3 vNormal;
|
178
|
-
out vec4 fColor;
|
179
|
-
uniform vec4 color;
|
180
|
-
uniform vec3 light;
|
181
|
-
|
182
|
-
float PhongIntensity(vec3 pos, vec3 norm) {
|
183
|
-
vec3 N = normalize(norm);
|
184
|
-
vec3 L = normalize(light - pos);
|
185
|
-
vec3 E = normalize(pos);
|
186
|
-
vec3 R = reflect(L, N);
|
187
|
-
|
188
|
-
float diffuse = abs(dot(N, L));
|
189
|
-
float specular = abs(dot(R, E));
|
190
|
-
|
191
|
-
return clamp(pow(specular, 10) + diffuse, 0, 1);
|
192
|
-
}
|
193
|
-
|
194
|
-
void main() {
|
195
|
-
float intensity = PhongIntensity(vPosition, vNormal);
|
196
|
-
|
197
|
-
fColor = vec4(intensity * color.xyz, color.w);
|
198
|
-
}
|
199
|
-
''')
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
end
|
1
|
+
require_relative './native/opengl'
|
2
|
+
|
3
|
+
module RubyGL
|
4
|
+
|
5
|
+
class Shader
|
6
|
+
def initialize(vert_src, frag_src)
|
7
|
+
@v_shader_id = Native.glCreateShader(Native::GL_VERTEX_SHADER)
|
8
|
+
@f_shader_id = Native.glCreateShader(Native::GL_FRAGMENT_SHADER)
|
9
|
+
@attrib_locs, @uniform_locs = {}, {}
|
10
|
+
|
11
|
+
# Pointer Used To Point To The String Of Our Source Code
|
12
|
+
shader_src_ptr = FFI::MemoryPointer.new(:pointer)
|
13
|
+
shader_src_len = FFI::MemoryPointer.new(:int)
|
14
|
+
|
15
|
+
v_shader_ptr = FFI::MemoryPointer.from_string(vert_src)
|
16
|
+
f_shader_ptr = FFI::MemoryPointer.from_string(frag_src)
|
17
|
+
|
18
|
+
shader_src_ptr.write_pointer(v_shader_ptr.address)
|
19
|
+
shader_src_len.write_int(vert_src.size)
|
20
|
+
Native.glShaderSource(@v_shader_id, 1, shader_src_ptr, shader_src_len)
|
21
|
+
|
22
|
+
shader_src_ptr.write_pointer(f_shader_ptr.address)
|
23
|
+
shader_src_len.write_int(frag_src.size)
|
24
|
+
Native.glShaderSource(@f_shader_id, 1, shader_src_ptr, shader_src_len)
|
25
|
+
|
26
|
+
Native.glCompileShader(@v_shader_id)
|
27
|
+
Native.glCompileShader(@f_shader_id)
|
28
|
+
|
29
|
+
# Throw Exception With Compile Error If Compilation Failed
|
30
|
+
Shader.compile_status(@v_shader_id)
|
31
|
+
Shader.compile_status(@f_shader_id)
|
32
|
+
|
33
|
+
@program_id = Native.glCreateProgram()
|
34
|
+
|
35
|
+
Native.glAttachShader(@program_id, @v_shader_id)
|
36
|
+
Native.glAttachShader(@program_id, @f_shader_id)
|
37
|
+
|
38
|
+
Native.glLinkProgram(@program_id)
|
39
|
+
|
40
|
+
Shader.link_status(@program_id)
|
41
|
+
end
|
42
|
+
|
43
|
+
def use_program()
|
44
|
+
Native.glUseProgram(@program_id)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Splat Operator Aggregates Incoming Parameters And Expands Outgoing Parameters
|
48
|
+
# If method_sym Does Not Accept A Pointer To The Data, Pass nil For data And
|
49
|
+
# Pass In The Normal Parameters Required For The Specific Method.
|
50
|
+
def send_uniform(method_sym, var_name, data = nil, *args)
|
51
|
+
loc = @uniform_locs[var_name] ||= uniform_location(var_name)
|
52
|
+
|
53
|
+
if data != nil then
|
54
|
+
data_ptr = FFI::MemoryPointer.new(:float, data.size)
|
55
|
+
data_ptr.write_array_of_float(data)
|
56
|
+
|
57
|
+
RubyGL::Native.send(method_sym, loc, *args, data_ptr)
|
58
|
+
else
|
59
|
+
RubyGL::Native.send(method_sym, loc, *args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def attrib_location(var_name)
|
64
|
+
Native.glGetAttribLocation(@program_id, var_name)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def uniform_location(var_name)
|
69
|
+
Native.glGetUniformLocation(@program_id, var_name)
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.compile_status(shader_id)
|
73
|
+
result = FFI::MemoryPointer.new(:int)
|
74
|
+
Native.glGetShaderiv(shader_id, Native::GL_COMPILE_STATUS, result)
|
75
|
+
|
76
|
+
if (result.get_int(0) != Native::GL_TRUE) then
|
77
|
+
Native.glGetShaderiv(shader_id, Native::GL_INFO_LOG_LENGTH, result)
|
78
|
+
|
79
|
+
compile_log = FFI::MemoryPointer.new(:char, result.get_int(0))
|
80
|
+
|
81
|
+
Native.glGetShaderInfoLog(shader_id, compile_log.total, FFI::MemoryPointer::NULL, compile_log)
|
82
|
+
|
83
|
+
raise compile_log.read_string_to_null()
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.link_status(program_id)
|
88
|
+
result = FFI::MemoryPointer.new(:int)
|
89
|
+
Native.glGetProgramiv(program_id, Native::GL_LINK_STATUS, result)
|
90
|
+
|
91
|
+
if (result.get_int(0) != Native::GL_TRUE) then
|
92
|
+
Native.glGetProgramiv(program_id, Native::GL_INFO_LOG_LENGTH, result)
|
93
|
+
|
94
|
+
link_log = FFI::MemoryPointer.new(:char, result.get_int(0))
|
95
|
+
|
96
|
+
Native.glGetShaderInfoLog(program_id, link_log.total, FFI::MemoryPointer::NULL, link_log)
|
97
|
+
|
98
|
+
raise link_log.read_string_to_null()
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class ShaderGenerator
|
104
|
+
def self.faceted_shader()
|
105
|
+
Shader.new('''
|
106
|
+
#version 130
|
107
|
+
in vec3 position;
|
108
|
+
out vec3 vPosition;
|
109
|
+
uniform mat4 modelview;
|
110
|
+
uniform mat4 perspective;
|
111
|
+
|
112
|
+
void main() {
|
113
|
+
vec4 hPosition = modelview * vec4(position, 1);
|
114
|
+
vPosition = hPosition.xyz;
|
115
|
+
|
116
|
+
gl_Position = perspective * hPosition;
|
117
|
+
}
|
118
|
+
''','''
|
119
|
+
#version 130
|
120
|
+
in vec3 vPosition;
|
121
|
+
out vec4 fColor;
|
122
|
+
uniform vec4 color;
|
123
|
+
uniform vec3 light;
|
124
|
+
|
125
|
+
void main() {
|
126
|
+
vec3 dx = dFdy(vPosition);
|
127
|
+
vec3 dy = dFdx(vPosition);
|
128
|
+
vec3 triangle_norm = normalize(cross(dx, dy));
|
129
|
+
float factor = clamp(dot(triangle_norm, light), 0, 1);
|
130
|
+
|
131
|
+
fColor = vec4(color.xyz * factor, color.w);
|
132
|
+
}
|
133
|
+
''')
|
134
|
+
end
|
135
|
+
|
136
|
+
def self.color_shader()
|
137
|
+
Shader.new('''
|
138
|
+
#version 130
|
139
|
+
in vec3 position;
|
140
|
+
uniform mat4 modelview;
|
141
|
+
uniform mat4 perspective;
|
142
|
+
|
143
|
+
void main() {
|
144
|
+
gl_Position = perspective * modelView * vec4(position, 1);
|
145
|
+
}
|
146
|
+
''','''
|
147
|
+
#version 130
|
148
|
+
out vec4 fColor;
|
149
|
+
uniform vec4 color;
|
150
|
+
|
151
|
+
void main() {
|
152
|
+
fColor = color;
|
153
|
+
}
|
154
|
+
''')
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.phong_shader()
|
158
|
+
Shader.new('''
|
159
|
+
#version 130
|
160
|
+
in vec3 position;
|
161
|
+
in vec3 normal;
|
162
|
+
out vec3 vPosition;
|
163
|
+
out vec3 vNormal;
|
164
|
+
uniform mat4 modelview;
|
165
|
+
uniform mat4 perspective;
|
166
|
+
|
167
|
+
void main() {
|
168
|
+
vec4 hPosition = modelview * vec4(position, 1);
|
169
|
+
vPosition = hPosition.xyz;
|
170
|
+
vNormal = (modelview * vec4(normal, 1)).xyz;
|
171
|
+
|
172
|
+
gl_Position = perspective * hPosition;
|
173
|
+
}
|
174
|
+
''','''
|
175
|
+
#version 130
|
176
|
+
in vec3 vPosition;
|
177
|
+
in vec3 vNormal;
|
178
|
+
out vec4 fColor;
|
179
|
+
uniform vec4 color;
|
180
|
+
uniform vec3 light;
|
181
|
+
|
182
|
+
float PhongIntensity(vec3 pos, vec3 norm) {
|
183
|
+
vec3 N = normalize(norm);
|
184
|
+
vec3 L = normalize(light - pos);
|
185
|
+
vec3 E = normalize(pos);
|
186
|
+
vec3 R = reflect(L, N);
|
187
|
+
|
188
|
+
float diffuse = abs(dot(N, L));
|
189
|
+
float specular = abs(dot(R, E));
|
190
|
+
|
191
|
+
return clamp(pow(specular, 10) + diffuse, 0, 1);
|
192
|
+
}
|
193
|
+
|
194
|
+
void main() {
|
195
|
+
float intensity = PhongIntensity(vPosition, vNormal);
|
196
|
+
|
197
|
+
fColor = vec4(intensity * color.xyz, color.w);
|
198
|
+
}
|
199
|
+
''')
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
@@ -1,77 +1,77 @@
|
|
1
|
-
|
2
|
-
module RubyGL
|
3
|
-
|
4
|
-
class Util
|
5
|
-
# Returns the value of the array at the overflow wrapped index value.
|
6
|
-
def self.overflow_wrap(array, index)
|
7
|
-
array[index % array.size]
|
8
|
-
end
|
9
|
-
|
10
|
-
# Returns [vertices, indices] where vertices is an array of unique vertices
|
11
|
-
# corresponding to the vertices in vertex_array and indices is the zero based
|
12
|
-
# index array that references the unqiue vertices stored in vertices.
|
13
|
-
def self.gen_index_arrays(vertex_array, components)
|
14
|
-
unique_points, point_indices, index_hash = [], [], {}
|
15
|
-
|
16
|
-
curr_index = 0
|
17
|
-
vertex_array.each_slice(components) { |point|
|
18
|
-
index = index_hash[point]
|
19
|
-
|
20
|
-
if index then
|
21
|
-
point_indices.push(index)
|
22
|
-
else
|
23
|
-
unique_points.push(point)
|
24
|
-
point_indices.push(curr_index)
|
25
|
-
|
26
|
-
index_hash[point] = curr_index
|
27
|
-
curr_index += 1
|
28
|
-
end
|
29
|
-
}
|
30
|
-
unique_points.flatten!
|
31
|
-
|
32
|
-
[unique_points, point_indices]
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
# Returns an array of vertex normals which is indexable by the indices array.
|
37
|
-
# The components parameter should be the number of components per vertex for
|
38
|
-
# each triangle.
|
39
|
-
#
|
40
|
-
# The vertices parameters must contain 3 component vertices or this function
|
41
|
-
# will not be able to compute appropriate normals.
|
42
|
-
def self.gen_vertex_normals(indices, vertices)
|
43
|
-
normals = Array.new(vertices.size, Vec3.new)
|
44
|
-
|
45
|
-
indices.each_slice(3) { |i1, i2, i3|
|
46
|
-
# 3 Components Per Vertex (Index References 3 Components)
|
47
|
-
adj_i1, adj_i2, adj_i3 = i1 * 3, i2 * 3, i3 * 3
|
48
|
-
|
49
|
-
# Construct Triangle Vertices
|
50
|
-
v1 = Vec3.new(vertices[adj_i1..adj_i1 + 2])
|
51
|
-
v2 = Vec3.new(vertices[adj_i2..adj_i2 + 2])
|
52
|
-
v3 = Vec3.new(vertices[adj_i3..adj_i3 + 2])
|
53
|
-
|
54
|
-
d1 = v2 - v1
|
55
|
-
d2 = v3 - v2
|
56
|
-
|
57
|
-
normal = d1.cross(d2)
|
58
|
-
normal.norm!
|
59
|
-
|
60
|
-
normals[i1] += normal
|
61
|
-
normals[i2] += normal
|
62
|
-
normals[i3] += normal
|
63
|
-
}
|
64
|
-
|
65
|
-
# Normalize All Vectors And Convert From Vec3 To Flat Array
|
66
|
-
normals.map! { |normal_vector|
|
67
|
-
normal_vector.norm!
|
68
|
-
|
69
|
-
normal_vector.to_a
|
70
|
-
}
|
71
|
-
normals.flatten!
|
72
|
-
|
73
|
-
normals
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
1
|
+
|
2
|
+
module RubyGL
|
3
|
+
|
4
|
+
class Util
|
5
|
+
# Returns the value of the array at the overflow wrapped index value.
|
6
|
+
def self.overflow_wrap(array, index)
|
7
|
+
array[index % array.size]
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns [vertices, indices] where vertices is an array of unique vertices
|
11
|
+
# corresponding to the vertices in vertex_array and indices is the zero based
|
12
|
+
# index array that references the unqiue vertices stored in vertices.
|
13
|
+
def self.gen_index_arrays(vertex_array, components)
|
14
|
+
unique_points, point_indices, index_hash = [], [], {}
|
15
|
+
|
16
|
+
curr_index = 0
|
17
|
+
vertex_array.each_slice(components) { |point|
|
18
|
+
index = index_hash[point]
|
19
|
+
|
20
|
+
if index then
|
21
|
+
point_indices.push(index)
|
22
|
+
else
|
23
|
+
unique_points.push(point)
|
24
|
+
point_indices.push(curr_index)
|
25
|
+
|
26
|
+
index_hash[point] = curr_index
|
27
|
+
curr_index += 1
|
28
|
+
end
|
29
|
+
}
|
30
|
+
unique_points.flatten!
|
31
|
+
|
32
|
+
[unique_points, point_indices]
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# Returns an array of vertex normals which is indexable by the indices array.
|
37
|
+
# The components parameter should be the number of components per vertex for
|
38
|
+
# each triangle.
|
39
|
+
#
|
40
|
+
# The vertices parameters must contain 3 component vertices or this function
|
41
|
+
# will not be able to compute appropriate normals.
|
42
|
+
def self.gen_vertex_normals(indices, vertices)
|
43
|
+
normals = Array.new(vertices.size, Vec3.new)
|
44
|
+
|
45
|
+
indices.each_slice(3) { |i1, i2, i3|
|
46
|
+
# 3 Components Per Vertex (Index References 3 Components)
|
47
|
+
adj_i1, adj_i2, adj_i3 = i1 * 3, i2 * 3, i3 * 3
|
48
|
+
|
49
|
+
# Construct Triangle Vertices
|
50
|
+
v1 = Vec3.new(vertices[adj_i1..adj_i1 + 2])
|
51
|
+
v2 = Vec3.new(vertices[adj_i2..adj_i2 + 2])
|
52
|
+
v3 = Vec3.new(vertices[adj_i3..adj_i3 + 2])
|
53
|
+
|
54
|
+
d1 = v2 - v1
|
55
|
+
d2 = v3 - v2
|
56
|
+
|
57
|
+
normal = d1.cross(d2)
|
58
|
+
normal.norm!
|
59
|
+
|
60
|
+
normals[i1] += normal
|
61
|
+
normals[i2] += normal
|
62
|
+
normals[i3] += normal
|
63
|
+
}
|
64
|
+
|
65
|
+
# Normalize All Vectors And Convert From Vec3 To Flat Array
|
66
|
+
normals.map! { |normal_vector|
|
67
|
+
normal_vector.norm!
|
68
|
+
|
69
|
+
normal_vector.to_a
|
70
|
+
}
|
71
|
+
normals.flatten!
|
72
|
+
|
73
|
+
normals
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/lib/rubygl.rb
CHANGED
@@ -1,48 +1,49 @@
|
|
1
|
-
require 'ffi'
|
2
|
-
require 'rbconfig'
|
3
|
-
|
4
|
-
# OS Detection
|
5
|
-
def os
|
6
|
-
@os ||= (
|
7
|
-
host_os = RbConfig::CONFIG['host_os']
|
8
|
-
|
9
|
-
case host_os
|
10
|
-
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
|
11
|
-
:windows
|
12
|
-
when /darwin|mac os/
|
13
|
-
:macosx
|
14
|
-
when /linux/
|
15
|
-
:linux
|
16
|
-
when /solaris|bsd/
|
17
|
-
:unix
|
18
|
-
else
|
19
|
-
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
|
20
|
-
end
|
21
|
-
)
|
22
|
-
end
|
23
|
-
|
24
|
-
def relative_path(resource_path)
|
25
|
-
File.join(File.dirname(File.expand_path(__FILE__)), resource_path)
|
26
|
-
end
|
27
|
-
|
28
|
-
# Initialize All Modules
|
29
|
-
module RubyGL
|
30
|
-
module Native
|
31
|
-
extend FFI::Library
|
32
|
-
ffi_lib relative_path("../ext/#{os.to_s}/SDL2.dll")
|
33
|
-
ffi_lib relative_path("../ext/#{os.to_s}/RubyGL.so")
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# Load Module Code
|
38
|
-
require_relative './rubygl/
|
39
|
-
require_relative './rubygl/
|
40
|
-
require_relative './rubygl/
|
41
|
-
require_relative './rubygl/
|
42
|
-
require_relative './rubygl/
|
43
|
-
require_relative './rubygl/
|
44
|
-
|
45
|
-
|
46
|
-
require_relative './rubygl/native/
|
47
|
-
require_relative './rubygl/native/
|
48
|
-
require_relative './rubygl/native/
|
1
|
+
require 'ffi'
|
2
|
+
require 'rbconfig'
|
3
|
+
|
4
|
+
# OS Detection
|
5
|
+
def os
|
6
|
+
@os ||= (
|
7
|
+
host_os = RbConfig::CONFIG['host_os']
|
8
|
+
|
9
|
+
case host_os
|
10
|
+
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
|
11
|
+
:windows
|
12
|
+
when /darwin|mac os/
|
13
|
+
:macosx
|
14
|
+
when /linux/
|
15
|
+
:linux
|
16
|
+
when /solaris|bsd/
|
17
|
+
:unix
|
18
|
+
else
|
19
|
+
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
|
20
|
+
end
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def relative_path(resource_path)
|
25
|
+
File.join(File.dirname(File.expand_path(__FILE__)), resource_path)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Initialize All Modules
|
29
|
+
module RubyGL
|
30
|
+
module Native
|
31
|
+
extend FFI::Library
|
32
|
+
ffi_lib relative_path("../ext/#{os.to_s}/SDL2.dll")
|
33
|
+
ffi_lib relative_path("../ext/#{os.to_s}/RubyGL.so")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Load Module Code
|
38
|
+
require_relative './rubygl/event'
|
39
|
+
require_relative './rubygl/geometry'
|
40
|
+
require_relative './rubygl/math'
|
41
|
+
require_relative './rubygl/memory'
|
42
|
+
require_relative './rubygl/setup'
|
43
|
+
require_relative './rubygl/shader'
|
44
|
+
require_relative './rubygl/util'
|
45
|
+
|
46
|
+
require_relative './rubygl/native/window'
|
47
|
+
require_relative './rubygl/native/glcontext'
|
48
|
+
require_relative './rubygl/native/input'
|
49
|
+
require_relative './rubygl/native/opengl'
|