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.
- 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
data/bin/ffi_code_gen.rb
CHANGED
@@ -1,167 +1,167 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
|
3
|
-
def resolve_typedef(type, type_defs)
|
4
|
-
while (type_defs[type]) do
|
5
|
-
type = type_defs[type]
|
6
|
-
end
|
7
|
-
|
8
|
-
return type
|
9
|
-
end
|
10
|
-
|
11
|
-
def get_type(type, hash)
|
12
|
-
hash[type]
|
13
|
-
end
|
14
|
-
|
15
|
-
def match_type(type, regex_hash)
|
16
|
-
regex_hash.select { |key,val|
|
17
|
-
type =~ key
|
18
|
-
}.values[0]
|
19
|
-
end
|
20
|
-
|
21
|
-
def write_file(name, contents, mode = 'w+')
|
22
|
-
File.open(name, mode) { |file|
|
23
|
-
file << contents
|
24
|
-
}
|
25
|
-
end
|
26
|
-
|
27
|
-
/--------------------------------START SCRIPT---------------------------------/
|
28
|
-
# ARGV: [Object File] [Header File] [Ruby Source Name] [Library Name]
|
29
|
-
|
30
|
-
SOURCE_INDENTS = 4
|
31
|
-
FFI_TYPES = { 'char' => 'char', 'signed char' => 'char',
|
32
|
-
'unsigned char' => 'uchar', 'int8_t' => 'int8', 'uint8_t' => 'uint8',
|
33
|
-
'short' => 'short', 'unsigned short' => 'ushort', 'int16_t' => 'int16',
|
34
|
-
'uint16_t' => 'uint16', 'int' => 'int', 'unsigned int' => 'uint',
|
35
|
-
'int32_t' => 'int32', 'uint32_t' => 'uint32', 'long int' => 'long',
|
36
|
-
'unsigned long int' => 'ulong', 'int64_t' => 'int64', 'uint64_t' => 'uint64',
|
37
|
-
'long long int' => 'long_long', 'unsigned long long int' => 'ulong_long',
|
38
|
-
'float' => 'float', 'double' => 'double' }
|
39
|
-
FFI_PTR_TYPES = { /\Aconst +(?:unsigned +)?char *\*\Z/ => 'string', /\*/ => 'pointer' }
|
40
|
-
|
41
|
-
# Open Pre-Processor(ed) Source File
|
42
|
-
source_code = ''
|
43
|
-
File.open(ARGV[0]) { |file|
|
44
|
-
source_code = file.read
|
45
|
-
}
|
46
|
-
|
47
|
-
# Associate Type Definitions With Their Actual Types
|
48
|
-
typedef_hash = {}
|
49
|
-
source_code.scan(/^typedef (.*?;)$/).flatten.each { |segment|
|
50
|
-
segment.gsub!(/\*/, '* ')
|
51
|
-
|
52
|
-
if (segment =~ /\(.*?\*.*?\)/) then # Function Pointer
|
53
|
-
key = segment[/\* *(.*?) *\)/, 1]
|
54
|
-
|
55
|
-
typedef_hash[key] = segment[0..-2] # Remove Semi-Colon
|
56
|
-
else # Other Type
|
57
|
-
key = segment[/([^ ]+);/, 1]
|
58
|
-
|
59
|
-
typedef_hash[key] = segment[/(.*?) +[^ ]+;/, 1]
|
60
|
-
end
|
61
|
-
}
|
62
|
-
|
63
|
-
# Open Header File
|
64
|
-
source_code = ''
|
65
|
-
File.open(ARGV[1]) { |file|
|
66
|
-
source_code = file.read
|
67
|
-
}
|
68
|
-
|
69
|
-
# Associate Macros With Their Literal Types (OpenGL Enumerations)
|
70
|
-
macro_tuples = []
|
71
|
-
source_code.scan(/^#define GL_.+?$/i).each { |macro|
|
72
|
-
token = macro[/^.+ +(.+) +.+$/, 1]
|
73
|
-
value = macro[/^.+ +.+ +(.+)$/, 1]
|
74
|
-
macro_tuples.push([token, value])
|
75
|
-
}
|
76
|
-
|
77
|
-
# Pull Out OpenGL Function Names From Header
|
78
|
-
gl_functions = source_code.scan(/^.+?gl.+?\(.*?\);$/)
|
79
|
-
|
80
|
-
ffi_template = "require 'ffi'
|
81
|
-
|
82
|
-
module RubyGL::Native
|
83
|
-
FUNCTIONS_GO_HERE
|
84
|
-
|
85
|
-
CONSTANTS_GO_HERE
|
86
|
-
end
|
87
|
-
"
|
88
|
-
|
89
|
-
# Append Functions (Param Type -> Typedefs -> Ruby Type)
|
90
|
-
buffer = ''
|
91
|
-
gl_functions.each { |signature|
|
92
|
-
buffer << ' ' * SOURCE_INDENTS + 'attach_function :' +
|
93
|
-
signature[/(gl.*?)\(.*?\);/, 1] + ', ['
|
94
|
-
|
95
|
-
# Get Type List
|
96
|
-
param_type_list = signature[/\((.*?)\);/, 1].split(', ').map { |full_param|
|
97
|
-
full_param[/(.+) [[:alnum:]]+/, 1]
|
98
|
-
}
|
99
|
-
|
100
|
-
# Type Definition -> Primitive (Actual) Type
|
101
|
-
param_type_list.map! { |param_type|
|
102
|
-
# Parameter Type Could Use Multiple Type Definitions
|
103
|
-
|
104
|
-
simple_param_types = param_type.gsub(/(?:\*|const)/, ' ').split(' ')
|
105
|
-
# Translate Each Type And Put Back Into param_type
|
106
|
-
simple_param_types.each { |simple_param_type|
|
107
|
-
primitive_type = resolve_typedef(simple_param_type, typedef_hash)
|
108
|
-
|
109
|
-
param_type.sub!(/#{simple_param_type}/, primitive_type)
|
110
|
-
}
|
111
|
-
|
112
|
-
param_type
|
113
|
-
}
|
114
|
-
|
115
|
-
# Primitive Type -> Ruby FFI Type
|
116
|
-
param_type_list.each { |param_type|
|
117
|
-
mutable_type = param_type.gsub(/const/, '').strip
|
118
|
-
|
119
|
-
if (get_type(mutable_type, FFI_TYPES)) then
|
120
|
-
param_type = get_type(mutable_type, FFI_TYPES)
|
121
|
-
else
|
122
|
-
param_type = match_type(param_type, FFI_PTR_TYPES)
|
123
|
-
end
|
124
|
-
|
125
|
-
# Write Out FFI Type
|
126
|
-
buffer << ':' + param_type + ', '
|
127
|
-
}
|
128
|
-
if (buffer[-1] == '[') then # No Parameters
|
129
|
-
buffer << '],'
|
130
|
-
else # At Least One Parameter
|
131
|
-
buffer[-2..-1] = '],'
|
132
|
-
end
|
133
|
-
|
134
|
-
# Get Return Type
|
135
|
-
# We Do Not Care About const If We Can Match Our Return Type With A
|
136
|
-
# Primitive Type. Otherwise, We Might Be Able To Match Against A
|
137
|
-
# const char* Or const unsigned char* In Which Case We Prefer To Use A
|
138
|
-
# string Type.
|
139
|
-
return_type = signature[/(.*?) gl[^ ]+\(/, 1]
|
140
|
-
mutable_return_type = return_type.gsub(/ *const */, '')
|
141
|
-
|
142
|
-
mutable_return_type.gsub(/\*/, ' ').split(' ').each { |type|
|
143
|
-
resolved_type = resolve_typedef(type, typedef_hash)
|
144
|
-
|
145
|
-
mutable_return_type.sub!(/#{type}/, resolved_type)
|
146
|
-
return_type.sub!(/#{type}/, resolved_type)
|
147
|
-
}
|
148
|
-
|
149
|
-
if (get_type(mutable_return_type, FFI_TYPES)) then
|
150
|
-
return_type = get_type(mutable_return_type, FFI_TYPES)
|
151
|
-
elsif (match_type(return_type, FFI_PTR_TYPES))
|
152
|
-
return_type = match_type(return_type, FFI_PTR_TYPES)
|
153
|
-
end
|
154
|
-
|
155
|
-
buffer << ' :' + return_type + "\n"
|
156
|
-
}
|
157
|
-
ffi_template.sub!(/FUNCTIONS_GO_HERE/, buffer)
|
158
|
-
|
159
|
-
# Append OpenGL Enumerations (Macros)
|
160
|
-
buffer = ''
|
161
|
-
macro_tuples.each { |name,val|
|
162
|
-
buffer << ' ' * SOURCE_INDENTS + "#{name} = #{val}\n"
|
163
|
-
}
|
164
|
-
ffi_template.sub!(/CONSTANTS_GO_HERE/, buffer)
|
165
|
-
|
166
|
-
write_file(ARGV[2], ffi_template)
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
def resolve_typedef(type, type_defs)
|
4
|
+
while (type_defs[type]) do
|
5
|
+
type = type_defs[type]
|
6
|
+
end
|
7
|
+
|
8
|
+
return type
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_type(type, hash)
|
12
|
+
hash[type]
|
13
|
+
end
|
14
|
+
|
15
|
+
def match_type(type, regex_hash)
|
16
|
+
regex_hash.select { |key,val|
|
17
|
+
type =~ key
|
18
|
+
}.values[0]
|
19
|
+
end
|
20
|
+
|
21
|
+
def write_file(name, contents, mode = 'w+')
|
22
|
+
File.open(name, mode) { |file|
|
23
|
+
file << contents
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
/--------------------------------START SCRIPT---------------------------------/
|
28
|
+
# ARGV: [Object File] [Header File] [Ruby Source Name] [Library Name]
|
29
|
+
|
30
|
+
SOURCE_INDENTS = 4
|
31
|
+
FFI_TYPES = { 'char' => 'char', 'signed char' => 'char',
|
32
|
+
'unsigned char' => 'uchar', 'int8_t' => 'int8', 'uint8_t' => 'uint8',
|
33
|
+
'short' => 'short', 'unsigned short' => 'ushort', 'int16_t' => 'int16',
|
34
|
+
'uint16_t' => 'uint16', 'int' => 'int', 'unsigned int' => 'uint',
|
35
|
+
'int32_t' => 'int32', 'uint32_t' => 'uint32', 'long int' => 'long',
|
36
|
+
'unsigned long int' => 'ulong', 'int64_t' => 'int64', 'uint64_t' => 'uint64',
|
37
|
+
'long long int' => 'long_long', 'unsigned long long int' => 'ulong_long',
|
38
|
+
'float' => 'float', 'double' => 'double' }
|
39
|
+
FFI_PTR_TYPES = { /\Aconst +(?:unsigned +)?char *\*\Z/ => 'string', /\*/ => 'pointer' }
|
40
|
+
|
41
|
+
# Open Pre-Processor(ed) Source File
|
42
|
+
source_code = ''
|
43
|
+
File.open(ARGV[0]) { |file|
|
44
|
+
source_code = file.read
|
45
|
+
}
|
46
|
+
|
47
|
+
# Associate Type Definitions With Their Actual Types
|
48
|
+
typedef_hash = {}
|
49
|
+
source_code.scan(/^typedef (.*?;)$/).flatten.each { |segment|
|
50
|
+
segment.gsub!(/\*/, '* ')
|
51
|
+
|
52
|
+
if (segment =~ /\(.*?\*.*?\)/) then # Function Pointer
|
53
|
+
key = segment[/\* *(.*?) *\)/, 1]
|
54
|
+
|
55
|
+
typedef_hash[key] = segment[0..-2] # Remove Semi-Colon
|
56
|
+
else # Other Type
|
57
|
+
key = segment[/([^ ]+);/, 1]
|
58
|
+
|
59
|
+
typedef_hash[key] = segment[/(.*?) +[^ ]+;/, 1]
|
60
|
+
end
|
61
|
+
}
|
62
|
+
|
63
|
+
# Open Header File
|
64
|
+
source_code = ''
|
65
|
+
File.open(ARGV[1]) { |file|
|
66
|
+
source_code = file.read
|
67
|
+
}
|
68
|
+
|
69
|
+
# Associate Macros With Their Literal Types (OpenGL Enumerations)
|
70
|
+
macro_tuples = []
|
71
|
+
source_code.scan(/^#define GL_.+?$/i).each { |macro|
|
72
|
+
token = macro[/^.+ +(.+) +.+$/, 1]
|
73
|
+
value = macro[/^.+ +.+ +(.+)$/, 1]
|
74
|
+
macro_tuples.push([token, value])
|
75
|
+
}
|
76
|
+
|
77
|
+
# Pull Out OpenGL Function Names From Header
|
78
|
+
gl_functions = source_code.scan(/^.+?gl.+?\(.*?\);$/)
|
79
|
+
|
80
|
+
ffi_template = "require 'ffi'
|
81
|
+
|
82
|
+
module RubyGL::Native
|
83
|
+
FUNCTIONS_GO_HERE
|
84
|
+
|
85
|
+
CONSTANTS_GO_HERE
|
86
|
+
end
|
87
|
+
"
|
88
|
+
|
89
|
+
# Append Functions (Param Type -> Typedefs -> Ruby Type)
|
90
|
+
buffer = ''
|
91
|
+
gl_functions.each { |signature|
|
92
|
+
buffer << ' ' * SOURCE_INDENTS + 'attach_function :' +
|
93
|
+
signature[/(gl.*?)\(.*?\);/, 1] + ', ['
|
94
|
+
|
95
|
+
# Get Type List
|
96
|
+
param_type_list = signature[/\((.*?)\);/, 1].split(', ').map { |full_param|
|
97
|
+
full_param[/(.+) [[:alnum:]]+/, 1]
|
98
|
+
}
|
99
|
+
|
100
|
+
# Type Definition -> Primitive (Actual) Type
|
101
|
+
param_type_list.map! { |param_type|
|
102
|
+
# Parameter Type Could Use Multiple Type Definitions
|
103
|
+
|
104
|
+
simple_param_types = param_type.gsub(/(?:\*|const)/, ' ').split(' ')
|
105
|
+
# Translate Each Type And Put Back Into param_type
|
106
|
+
simple_param_types.each { |simple_param_type|
|
107
|
+
primitive_type = resolve_typedef(simple_param_type, typedef_hash)
|
108
|
+
|
109
|
+
param_type.sub!(/#{simple_param_type}/, primitive_type)
|
110
|
+
}
|
111
|
+
|
112
|
+
param_type
|
113
|
+
}
|
114
|
+
|
115
|
+
# Primitive Type -> Ruby FFI Type
|
116
|
+
param_type_list.each { |param_type|
|
117
|
+
mutable_type = param_type.gsub(/const/, '').strip
|
118
|
+
|
119
|
+
if (get_type(mutable_type, FFI_TYPES)) then
|
120
|
+
param_type = get_type(mutable_type, FFI_TYPES)
|
121
|
+
else
|
122
|
+
param_type = match_type(param_type, FFI_PTR_TYPES)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Write Out FFI Type
|
126
|
+
buffer << ':' + param_type + ', '
|
127
|
+
}
|
128
|
+
if (buffer[-1] == '[') then # No Parameters
|
129
|
+
buffer << '],'
|
130
|
+
else # At Least One Parameter
|
131
|
+
buffer[-2..-1] = '],'
|
132
|
+
end
|
133
|
+
|
134
|
+
# Get Return Type
|
135
|
+
# We Do Not Care About const If We Can Match Our Return Type With A
|
136
|
+
# Primitive Type. Otherwise, We Might Be Able To Match Against A
|
137
|
+
# const char* Or const unsigned char* In Which Case We Prefer To Use A
|
138
|
+
# string Type.
|
139
|
+
return_type = signature[/(.*?) gl[^ ]+\(/, 1]
|
140
|
+
mutable_return_type = return_type.gsub(/ *const */, '')
|
141
|
+
|
142
|
+
mutable_return_type.gsub(/\*/, ' ').split(' ').each { |type|
|
143
|
+
resolved_type = resolve_typedef(type, typedef_hash)
|
144
|
+
|
145
|
+
mutable_return_type.sub!(/#{type}/, resolved_type)
|
146
|
+
return_type.sub!(/#{type}/, resolved_type)
|
147
|
+
}
|
148
|
+
|
149
|
+
if (get_type(mutable_return_type, FFI_TYPES)) then
|
150
|
+
return_type = get_type(mutable_return_type, FFI_TYPES)
|
151
|
+
elsif (match_type(return_type, FFI_PTR_TYPES))
|
152
|
+
return_type = match_type(return_type, FFI_PTR_TYPES)
|
153
|
+
end
|
154
|
+
|
155
|
+
buffer << ' :' + return_type + "\n"
|
156
|
+
}
|
157
|
+
ffi_template.sub!(/FUNCTIONS_GO_HERE/, buffer)
|
158
|
+
|
159
|
+
# Append OpenGL Enumerations (Macros)
|
160
|
+
buffer = ''
|
161
|
+
macro_tuples.each { |name,val|
|
162
|
+
buffer << ' ' * SOURCE_INDENTS + "#{name} = #{val}\n"
|
163
|
+
}
|
164
|
+
ffi_template.sub!(/CONSTANTS_GO_HERE/, buffer)
|
165
|
+
|
166
|
+
write_file(ARGV[2], ffi_template)
|
167
167
|
/---------------------------------END SCRIPT----------------------------------/
|