cyberarm_engine 0.19.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -8
- data/.rubocop.yml +7 -7
- data/.travis.yml +5 -5
- data/Gemfile +6 -6
- data/LICENSE.txt +21 -21
- data/README.md +74 -74
- data/Rakefile +10 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/cyberarm_engine.gemspec +39 -39
- data/lib/cyberarm_engine/animator.rb +219 -219
- data/lib/cyberarm_engine/background.rb +179 -179
- data/lib/cyberarm_engine/background_nine_slice.rb +142 -142
- data/lib/cyberarm_engine/bounding_box.rb +150 -150
- data/lib/cyberarm_engine/builtin/intro_state.rb +130 -130
- data/lib/cyberarm_engine/cache/download_manager.rb +121 -121
- data/lib/cyberarm_engine/cache.rb +4 -4
- data/lib/cyberarm_engine/common.rb +113 -113
- data/lib/cyberarm_engine/config_file.rb +46 -46
- data/lib/cyberarm_engine/console/command.rb +157 -157
- data/lib/cyberarm_engine/console/commands/help_command.rb +43 -43
- data/lib/cyberarm_engine/console/subcommand.rb +99 -99
- data/lib/cyberarm_engine/console.rb +248 -248
- data/lib/cyberarm_engine/game_object.rb +248 -248
- data/lib/cyberarm_engine/game_state.rb +97 -97
- data/lib/cyberarm_engine/model/material.rb +21 -21
- data/lib/cyberarm_engine/model/model_object.rb +131 -131
- data/lib/cyberarm_engine/model/parser.rb +74 -74
- data/lib/cyberarm_engine/model/parsers/collada_parser.rb +138 -138
- data/lib/cyberarm_engine/model/parsers/wavefront_parser.rb +154 -154
- data/lib/cyberarm_engine/model.rb +212 -212
- data/lib/cyberarm_engine/model_cache.rb +31 -31
- data/lib/cyberarm_engine/opengl/light.rb +50 -50
- data/lib/cyberarm_engine/opengl/orthographic_camera.rb +46 -46
- data/lib/cyberarm_engine/opengl/perspective_camera.rb +38 -38
- data/lib/cyberarm_engine/opengl/renderer/bounding_box_renderer.rb +249 -249
- data/lib/cyberarm_engine/opengl/renderer/g_buffer.rb +164 -164
- data/lib/cyberarm_engine/opengl/renderer/opengl_renderer.rb +298 -298
- data/lib/cyberarm_engine/opengl/renderer/renderer.rb +22 -22
- data/lib/cyberarm_engine/opengl/shader.rb +406 -406
- data/lib/cyberarm_engine/opengl/texture.rb +69 -69
- data/lib/cyberarm_engine/opengl.rb +28 -28
- data/lib/cyberarm_engine/ray.rb +56 -56
- data/lib/cyberarm_engine/stats.rb +21 -21
- data/lib/cyberarm_engine/text.rb +197 -197
- data/lib/cyberarm_engine/timer.rb +23 -23
- data/lib/cyberarm_engine/transform.rb +296 -296
- data/lib/cyberarm_engine/ui/border_canvas.rb +102 -102
- data/lib/cyberarm_engine/ui/dsl.rb +139 -139
- data/lib/cyberarm_engine/ui/element.rb +488 -488
- data/lib/cyberarm_engine/ui/elements/button.rb +97 -97
- data/lib/cyberarm_engine/ui/elements/check_box.rb +54 -54
- data/lib/cyberarm_engine/ui/elements/container.rb +256 -256
- data/lib/cyberarm_engine/ui/elements/edit_box.rb +179 -179
- data/lib/cyberarm_engine/ui/elements/edit_line.rb +263 -263
- data/lib/cyberarm_engine/ui/elements/flow.rb +15 -15
- data/lib/cyberarm_engine/ui/elements/image.rb +72 -72
- data/lib/cyberarm_engine/ui/elements/list_box.rb +88 -82
- data/lib/cyberarm_engine/ui/elements/progress.rb +51 -51
- data/lib/cyberarm_engine/ui/elements/radio.rb +6 -6
- data/lib/cyberarm_engine/ui/elements/slider.rb +104 -104
- data/lib/cyberarm_engine/ui/elements/stack.rb +11 -11
- data/lib/cyberarm_engine/ui/elements/text_block.rb +162 -162
- data/lib/cyberarm_engine/ui/elements/toggle_button.rb +65 -65
- data/lib/cyberarm_engine/ui/event.rb +54 -54
- data/lib/cyberarm_engine/ui/gui_state.rb +256 -256
- data/lib/cyberarm_engine/ui/style.rb +49 -49
- data/lib/cyberarm_engine/ui/theme.rb +207 -207
- data/lib/cyberarm_engine/vector.rb +293 -293
- data/lib/cyberarm_engine/version.rb +4 -4
- data/lib/cyberarm_engine/window.rb +120 -120
- data/lib/cyberarm_engine.rb +71 -71
- metadata +3 -3
@@ -1,138 +1,138 @@
|
|
1
|
-
module CyberarmEngine
|
2
|
-
class ColladaParser < Model::Parser
|
3
|
-
def self.handles
|
4
|
-
[:dae]
|
5
|
-
end
|
6
|
-
|
7
|
-
def parse
|
8
|
-
@collada = Nokogiri::XML(File.read(@model.file_path))
|
9
|
-
|
10
|
-
@collada.css("library_materials material").each do |material|
|
11
|
-
parse_material(material)
|
12
|
-
end
|
13
|
-
|
14
|
-
@collada.css("library_geometries geometry").each do |geometry|
|
15
|
-
parse_geometry(geometry)
|
16
|
-
end
|
17
|
-
|
18
|
-
@model.calculate_bounding_box(@model.vertices, @model.bounding_box)
|
19
|
-
@model.objects.each do |o|
|
20
|
-
@model.calculate_bounding_box(o.vertices, o.bounding_box)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def parse_material(material)
|
25
|
-
name = material.attributes["id"].value
|
26
|
-
effect_id = material.at_css("instance_effect").attributes["url"].value
|
27
|
-
|
28
|
-
mat = Model::Material.new(name)
|
29
|
-
effect = @collada.at_css("[id=\"#{effect_id.sub('#', '')}\"]")
|
30
|
-
|
31
|
-
emission = effect.at_css("emission color")
|
32
|
-
diffuse = effect.at_css("diffuse color").children.first.to_s.split(" ").map { |c| Float(c) }
|
33
|
-
|
34
|
-
mat.diffuse = Color.new(*diffuse[0..2])
|
35
|
-
|
36
|
-
add_material(name, mat)
|
37
|
-
end
|
38
|
-
|
39
|
-
def parse_geometry(geometry)
|
40
|
-
geometry_id = geometry.attributes["id"].value
|
41
|
-
geometry_name = geometry.attributes["name"].value
|
42
|
-
|
43
|
-
change_object(geometry_id, geometry_name)
|
44
|
-
|
45
|
-
mesh = geometry.at_css("mesh")
|
46
|
-
|
47
|
-
get_positions(geometry_id, mesh)
|
48
|
-
get_normals(geometry_id, mesh)
|
49
|
-
get_texture_coordinates(geometry_id, mesh)
|
50
|
-
|
51
|
-
project_node(geometry_name)
|
52
|
-
build_faces(geometry_id, mesh)
|
53
|
-
end
|
54
|
-
|
55
|
-
def get_positions(id, mesh)
|
56
|
-
positions = mesh.at_css("[id=\"#{id}-positions\"]")
|
57
|
-
array = positions.at_css("[id=\"#{id}-positions-array\"]")
|
58
|
-
|
59
|
-
stride = Integer(positions.at_css("[source=\"##{id}-positions-array\"]").attributes["stride"].value)
|
60
|
-
list = array.children.first.to_s.split(" ").map { |f| Float(f) }.each_slice(stride).each do |slice|
|
61
|
-
position = Vector.new(*slice)
|
62
|
-
@model.current_object.vertices << position
|
63
|
-
@model.vertices << position
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def get_normals(id, mesh)
|
68
|
-
normals = mesh.at_css("[id=\"#{id}-normals\"]")
|
69
|
-
array = normals.at_css("[id=\"#{id}-normals-array\"]")
|
70
|
-
|
71
|
-
stride = Integer(normals.at_css("[source=\"##{id}-normals-array\"]").attributes["stride"].value)
|
72
|
-
list = array.children.first.to_s.split(" ").map { |f| Float(f) }.each_slice(stride).each do |slice|
|
73
|
-
normal = Vector.new(*slice)
|
74
|
-
@model.current_object.normals << normal
|
75
|
-
@model.normals << normal
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def get_texture_coordinates(id, mesh)
|
80
|
-
end
|
81
|
-
|
82
|
-
def project_node(name)
|
83
|
-
@collada.css("library_visual_scenes visual_scene node").each do |node|
|
84
|
-
next unless node.attributes["name"].value == name
|
85
|
-
|
86
|
-
transform = Transform.new(node.at_css("matrix").children.first.to_s.split(" ").map { |f| Float(f) })
|
87
|
-
|
88
|
-
@model.current_object.vertices.each do |vert|
|
89
|
-
v = vert.multiply_transform(transform)
|
90
|
-
vert.x = v.x
|
91
|
-
vert.y = v.y
|
92
|
-
vert.z = v.z
|
93
|
-
vert.w = v.w
|
94
|
-
end
|
95
|
-
|
96
|
-
break
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def build_faces(_id, mesh)
|
101
|
-
material_name = mesh.at_css("triangles").attributes["material"].value
|
102
|
-
set_material(material_name)
|
103
|
-
|
104
|
-
positions_index = []
|
105
|
-
normals_index = []
|
106
|
-
uvs_index = []
|
107
|
-
|
108
|
-
mesh.at_css("triangles p").children.first.to_s.split(" ").map { |i| Integer(i) }.each_slice(3).each do |slice|
|
109
|
-
positions_index << slice[0]
|
110
|
-
normals_index << slice[1]
|
111
|
-
uvs_index << slice[2]
|
112
|
-
end
|
113
|
-
|
114
|
-
norm_index = 0
|
115
|
-
positions_index.each_slice(3) do |slice|
|
116
|
-
face = Face.new
|
117
|
-
face.vertices = []
|
118
|
-
face.uvs = []
|
119
|
-
face.normals = []
|
120
|
-
face.colors = []
|
121
|
-
face.material = current_material
|
122
|
-
face.smoothing = @model.smoothing
|
123
|
-
|
124
|
-
slice.each do |index|
|
125
|
-
face.vertices << @model.vertices[index]
|
126
|
-
# face.uvs << @model.uvs[index]
|
127
|
-
face.normals << @model.normals[normals_index[norm_index]]
|
128
|
-
face.colors << current_material.diffuse
|
129
|
-
|
130
|
-
norm_index += 1
|
131
|
-
end
|
132
|
-
|
133
|
-
@model.current_object.faces << face
|
134
|
-
@model.faces << face
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
1
|
+
module CyberarmEngine
|
2
|
+
class ColladaParser < Model::Parser
|
3
|
+
def self.handles
|
4
|
+
[:dae]
|
5
|
+
end
|
6
|
+
|
7
|
+
def parse
|
8
|
+
@collada = Nokogiri::XML(File.read(@model.file_path))
|
9
|
+
|
10
|
+
@collada.css("library_materials material").each do |material|
|
11
|
+
parse_material(material)
|
12
|
+
end
|
13
|
+
|
14
|
+
@collada.css("library_geometries geometry").each do |geometry|
|
15
|
+
parse_geometry(geometry)
|
16
|
+
end
|
17
|
+
|
18
|
+
@model.calculate_bounding_box(@model.vertices, @model.bounding_box)
|
19
|
+
@model.objects.each do |o|
|
20
|
+
@model.calculate_bounding_box(o.vertices, o.bounding_box)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_material(material)
|
25
|
+
name = material.attributes["id"].value
|
26
|
+
effect_id = material.at_css("instance_effect").attributes["url"].value
|
27
|
+
|
28
|
+
mat = Model::Material.new(name)
|
29
|
+
effect = @collada.at_css("[id=\"#{effect_id.sub('#', '')}\"]")
|
30
|
+
|
31
|
+
emission = effect.at_css("emission color")
|
32
|
+
diffuse = effect.at_css("diffuse color").children.first.to_s.split(" ").map { |c| Float(c) }
|
33
|
+
|
34
|
+
mat.diffuse = Color.new(*diffuse[0..2])
|
35
|
+
|
36
|
+
add_material(name, mat)
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_geometry(geometry)
|
40
|
+
geometry_id = geometry.attributes["id"].value
|
41
|
+
geometry_name = geometry.attributes["name"].value
|
42
|
+
|
43
|
+
change_object(geometry_id, geometry_name)
|
44
|
+
|
45
|
+
mesh = geometry.at_css("mesh")
|
46
|
+
|
47
|
+
get_positions(geometry_id, mesh)
|
48
|
+
get_normals(geometry_id, mesh)
|
49
|
+
get_texture_coordinates(geometry_id, mesh)
|
50
|
+
|
51
|
+
project_node(geometry_name)
|
52
|
+
build_faces(geometry_id, mesh)
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_positions(id, mesh)
|
56
|
+
positions = mesh.at_css("[id=\"#{id}-positions\"]")
|
57
|
+
array = positions.at_css("[id=\"#{id}-positions-array\"]")
|
58
|
+
|
59
|
+
stride = Integer(positions.at_css("[source=\"##{id}-positions-array\"]").attributes["stride"].value)
|
60
|
+
list = array.children.first.to_s.split(" ").map { |f| Float(f) }.each_slice(stride).each do |slice|
|
61
|
+
position = Vector.new(*slice)
|
62
|
+
@model.current_object.vertices << position
|
63
|
+
@model.vertices << position
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_normals(id, mesh)
|
68
|
+
normals = mesh.at_css("[id=\"#{id}-normals\"]")
|
69
|
+
array = normals.at_css("[id=\"#{id}-normals-array\"]")
|
70
|
+
|
71
|
+
stride = Integer(normals.at_css("[source=\"##{id}-normals-array\"]").attributes["stride"].value)
|
72
|
+
list = array.children.first.to_s.split(" ").map { |f| Float(f) }.each_slice(stride).each do |slice|
|
73
|
+
normal = Vector.new(*slice)
|
74
|
+
@model.current_object.normals << normal
|
75
|
+
@model.normals << normal
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_texture_coordinates(id, mesh)
|
80
|
+
end
|
81
|
+
|
82
|
+
def project_node(name)
|
83
|
+
@collada.css("library_visual_scenes visual_scene node").each do |node|
|
84
|
+
next unless node.attributes["name"].value == name
|
85
|
+
|
86
|
+
transform = Transform.new(node.at_css("matrix").children.first.to_s.split(" ").map { |f| Float(f) })
|
87
|
+
|
88
|
+
@model.current_object.vertices.each do |vert|
|
89
|
+
v = vert.multiply_transform(transform)
|
90
|
+
vert.x = v.x
|
91
|
+
vert.y = v.y
|
92
|
+
vert.z = v.z
|
93
|
+
vert.w = v.w
|
94
|
+
end
|
95
|
+
|
96
|
+
break
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def build_faces(_id, mesh)
|
101
|
+
material_name = mesh.at_css("triangles").attributes["material"].value
|
102
|
+
set_material(material_name)
|
103
|
+
|
104
|
+
positions_index = []
|
105
|
+
normals_index = []
|
106
|
+
uvs_index = []
|
107
|
+
|
108
|
+
mesh.at_css("triangles p").children.first.to_s.split(" ").map { |i| Integer(i) }.each_slice(3).each do |slice|
|
109
|
+
positions_index << slice[0]
|
110
|
+
normals_index << slice[1]
|
111
|
+
uvs_index << slice[2]
|
112
|
+
end
|
113
|
+
|
114
|
+
norm_index = 0
|
115
|
+
positions_index.each_slice(3) do |slice|
|
116
|
+
face = Face.new
|
117
|
+
face.vertices = []
|
118
|
+
face.uvs = []
|
119
|
+
face.normals = []
|
120
|
+
face.colors = []
|
121
|
+
face.material = current_material
|
122
|
+
face.smoothing = @model.smoothing
|
123
|
+
|
124
|
+
slice.each do |index|
|
125
|
+
face.vertices << @model.vertices[index]
|
126
|
+
# face.uvs << @model.uvs[index]
|
127
|
+
face.normals << @model.normals[normals_index[norm_index]]
|
128
|
+
face.colors << current_material.diffuse
|
129
|
+
|
130
|
+
norm_index += 1
|
131
|
+
end
|
132
|
+
|
133
|
+
@model.current_object.faces << face
|
134
|
+
@model.faces << face
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -1,154 +1,154 @@
|
|
1
|
-
module CyberarmEngine
|
2
|
-
class WavefrontParser < Model::Parser
|
3
|
-
def self.handles
|
4
|
-
[:obj]
|
5
|
-
end
|
6
|
-
|
7
|
-
def parse
|
8
|
-
lines = 0
|
9
|
-
list = File.read(@model.file_path).split("\n")
|
10
|
-
list.each do |line|
|
11
|
-
lines += 1
|
12
|
-
line = line.strip
|
13
|
-
|
14
|
-
array = line.split(" ")
|
15
|
-
case array[0]
|
16
|
-
when "mtllib"
|
17
|
-
@model.material_file = array[1]
|
18
|
-
parse_mtllib
|
19
|
-
when "usemtl"
|
20
|
-
set_material(array[1])
|
21
|
-
when "o"
|
22
|
-
change_object(nil, array[1])
|
23
|
-
when "s"
|
24
|
-
set_smoothing(array[1])
|
25
|
-
when "v"
|
26
|
-
add_vertex(array)
|
27
|
-
when "vt"
|
28
|
-
add_texture_coordinate(array)
|
29
|
-
|
30
|
-
when "vn"
|
31
|
-
add_normal(array)
|
32
|
-
|
33
|
-
when "f"
|
34
|
-
verts = []
|
35
|
-
uvs = []
|
36
|
-
norms = []
|
37
|
-
array[1..3].each do |f|
|
38
|
-
verts << f.split("/")[0]
|
39
|
-
uvs << f.split("/")[1]
|
40
|
-
norms << f.split("/")[2]
|
41
|
-
end
|
42
|
-
|
43
|
-
face = Face.new
|
44
|
-
face.vertices = []
|
45
|
-
face.uvs = []
|
46
|
-
face.normals = []
|
47
|
-
face.colors = []
|
48
|
-
face.material = current_material
|
49
|
-
face.smoothing = @model.smoothing
|
50
|
-
|
51
|
-
mat = face.material.diffuse
|
52
|
-
color = mat
|
53
|
-
|
54
|
-
verts.each_with_index do |v, index|
|
55
|
-
if uvs.first != ""
|
56
|
-
face.vertices << @model.vertices[Integer(v) - 1]
|
57
|
-
face.uvs << @model.uvs[Integer(uvs[index]) - 1]
|
58
|
-
face.normals << @model.normals[Integer(norms[index]) - 1]
|
59
|
-
face.colors << color
|
60
|
-
else
|
61
|
-
face.vertices << @model.vertices[Integer(v) - 1]
|
62
|
-
face.uvs << nil
|
63
|
-
face.normals << @model.normals[Integer(norms[index]) - 1]
|
64
|
-
face.colors << color
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
@model.current_object.faces << face
|
69
|
-
@model.faces << face
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
@model.calculate_bounding_box(@model.vertices, @model.bounding_box)
|
74
|
-
@model.objects.each do |o|
|
75
|
-
@model.calculate_bounding_box(o.vertices, o.bounding_box)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def parse_mtllib
|
80
|
-
file = File.open(@model.file_path.sub(File.basename(@model.file_path), "") + @model.material_file, "r")
|
81
|
-
file.readlines.each do |line|
|
82
|
-
array = line.strip.split(" ")
|
83
|
-
case array.first
|
84
|
-
when "newmtl"
|
85
|
-
material = Model::Material.new(array.last)
|
86
|
-
@model.current_material = array.last
|
87
|
-
@model.materials[array.last] = material
|
88
|
-
when "Ns" # Specular Exponent
|
89
|
-
when "Ka" # Ambient color
|
90
|
-
@model.materials[@model.current_material].ambient = Color.new(Float(array[1]), Float(array[2]),
|
91
|
-
Float(array[3]))
|
92
|
-
when "Kd" # Diffuse color
|
93
|
-
@model.materials[@model.current_material].diffuse = Color.new(Float(array[1]), Float(array[2]),
|
94
|
-
Float(array[3]))
|
95
|
-
when "Ks" # Specular color
|
96
|
-
@model.materials[@model.current_material].specular = Color.new(Float(array[1]), Float(array[2]),
|
97
|
-
Float(array[3]))
|
98
|
-
when "Ke" # Emissive
|
99
|
-
when "Ni" # Unknown (Blender Specific?)
|
100
|
-
when "d" # Dissolved (Transparency)
|
101
|
-
when "illum" # Illumination model
|
102
|
-
when "map_Kd" # Diffuse texture
|
103
|
-
texture = File.basename(array[1])
|
104
|
-
texture_path = "#{File.expand_path('../../', @model.file_path)}/textures/#{texture}"
|
105
|
-
@model.materials[@model.current_material].set_texture(texture_path)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
def set_smoothing(value)
|
111
|
-
@model.smoothing = value == "1"
|
112
|
-
end
|
113
|
-
|
114
|
-
def add_vertex(array)
|
115
|
-
@model.vertex_count += 1
|
116
|
-
vert = nil
|
117
|
-
if array.size == 5
|
118
|
-
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
119
|
-
elsif array.size == 4
|
120
|
-
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
121
|
-
else
|
122
|
-
raise
|
123
|
-
end
|
124
|
-
@model.current_object.vertices << vert
|
125
|
-
@model.vertices << vert
|
126
|
-
end
|
127
|
-
|
128
|
-
def add_normal(array)
|
129
|
-
vert = nil
|
130
|
-
if array.size == 5
|
131
|
-
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
132
|
-
elsif array.size == 4
|
133
|
-
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
134
|
-
else
|
135
|
-
raise
|
136
|
-
end
|
137
|
-
@model.current_object.normals << vert
|
138
|
-
@model.normals << vert
|
139
|
-
end
|
140
|
-
|
141
|
-
def add_texture_coordinate(array)
|
142
|
-
texture = nil
|
143
|
-
if array.size == 4
|
144
|
-
texture = Vector.new(Float(array[1]), 1 - Float(array[2]), Float(array[3]))
|
145
|
-
elsif array.size == 3
|
146
|
-
texture = Vector.new(Float(array[1]), 1 - Float(array[2]), 1.0)
|
147
|
-
else
|
148
|
-
raise
|
149
|
-
end
|
150
|
-
@model.uvs << texture
|
151
|
-
@model.current_object.uvs << texture
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
1
|
+
module CyberarmEngine
|
2
|
+
class WavefrontParser < Model::Parser
|
3
|
+
def self.handles
|
4
|
+
[:obj]
|
5
|
+
end
|
6
|
+
|
7
|
+
def parse
|
8
|
+
lines = 0
|
9
|
+
list = File.read(@model.file_path).split("\n")
|
10
|
+
list.each do |line|
|
11
|
+
lines += 1
|
12
|
+
line = line.strip
|
13
|
+
|
14
|
+
array = line.split(" ")
|
15
|
+
case array[0]
|
16
|
+
when "mtllib"
|
17
|
+
@model.material_file = array[1]
|
18
|
+
parse_mtllib
|
19
|
+
when "usemtl"
|
20
|
+
set_material(array[1])
|
21
|
+
when "o"
|
22
|
+
change_object(nil, array[1])
|
23
|
+
when "s"
|
24
|
+
set_smoothing(array[1])
|
25
|
+
when "v"
|
26
|
+
add_vertex(array)
|
27
|
+
when "vt"
|
28
|
+
add_texture_coordinate(array)
|
29
|
+
|
30
|
+
when "vn"
|
31
|
+
add_normal(array)
|
32
|
+
|
33
|
+
when "f"
|
34
|
+
verts = []
|
35
|
+
uvs = []
|
36
|
+
norms = []
|
37
|
+
array[1..3].each do |f|
|
38
|
+
verts << f.split("/")[0]
|
39
|
+
uvs << f.split("/")[1]
|
40
|
+
norms << f.split("/")[2]
|
41
|
+
end
|
42
|
+
|
43
|
+
face = Face.new
|
44
|
+
face.vertices = []
|
45
|
+
face.uvs = []
|
46
|
+
face.normals = []
|
47
|
+
face.colors = []
|
48
|
+
face.material = current_material
|
49
|
+
face.smoothing = @model.smoothing
|
50
|
+
|
51
|
+
mat = face.material.diffuse
|
52
|
+
color = mat
|
53
|
+
|
54
|
+
verts.each_with_index do |v, index|
|
55
|
+
if uvs.first != ""
|
56
|
+
face.vertices << @model.vertices[Integer(v) - 1]
|
57
|
+
face.uvs << @model.uvs[Integer(uvs[index]) - 1]
|
58
|
+
face.normals << @model.normals[Integer(norms[index]) - 1]
|
59
|
+
face.colors << color
|
60
|
+
else
|
61
|
+
face.vertices << @model.vertices[Integer(v) - 1]
|
62
|
+
face.uvs << nil
|
63
|
+
face.normals << @model.normals[Integer(norms[index]) - 1]
|
64
|
+
face.colors << color
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
@model.current_object.faces << face
|
69
|
+
@model.faces << face
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
@model.calculate_bounding_box(@model.vertices, @model.bounding_box)
|
74
|
+
@model.objects.each do |o|
|
75
|
+
@model.calculate_bounding_box(o.vertices, o.bounding_box)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def parse_mtllib
|
80
|
+
file = File.open(@model.file_path.sub(File.basename(@model.file_path), "") + @model.material_file, "r")
|
81
|
+
file.readlines.each do |line|
|
82
|
+
array = line.strip.split(" ")
|
83
|
+
case array.first
|
84
|
+
when "newmtl"
|
85
|
+
material = Model::Material.new(array.last)
|
86
|
+
@model.current_material = array.last
|
87
|
+
@model.materials[array.last] = material
|
88
|
+
when "Ns" # Specular Exponent
|
89
|
+
when "Ka" # Ambient color
|
90
|
+
@model.materials[@model.current_material].ambient = Color.new(Float(array[1]), Float(array[2]),
|
91
|
+
Float(array[3]))
|
92
|
+
when "Kd" # Diffuse color
|
93
|
+
@model.materials[@model.current_material].diffuse = Color.new(Float(array[1]), Float(array[2]),
|
94
|
+
Float(array[3]))
|
95
|
+
when "Ks" # Specular color
|
96
|
+
@model.materials[@model.current_material].specular = Color.new(Float(array[1]), Float(array[2]),
|
97
|
+
Float(array[3]))
|
98
|
+
when "Ke" # Emissive
|
99
|
+
when "Ni" # Unknown (Blender Specific?)
|
100
|
+
when "d" # Dissolved (Transparency)
|
101
|
+
when "illum" # Illumination model
|
102
|
+
when "map_Kd" # Diffuse texture
|
103
|
+
texture = File.basename(array[1])
|
104
|
+
texture_path = "#{File.expand_path('../../', @model.file_path)}/textures/#{texture}"
|
105
|
+
@model.materials[@model.current_material].set_texture(texture_path)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def set_smoothing(value)
|
111
|
+
@model.smoothing = value == "1"
|
112
|
+
end
|
113
|
+
|
114
|
+
def add_vertex(array)
|
115
|
+
@model.vertex_count += 1
|
116
|
+
vert = nil
|
117
|
+
if array.size == 5
|
118
|
+
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
119
|
+
elsif array.size == 4
|
120
|
+
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
121
|
+
else
|
122
|
+
raise
|
123
|
+
end
|
124
|
+
@model.current_object.vertices << vert
|
125
|
+
@model.vertices << vert
|
126
|
+
end
|
127
|
+
|
128
|
+
def add_normal(array)
|
129
|
+
vert = nil
|
130
|
+
if array.size == 5
|
131
|
+
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
132
|
+
elsif array.size == 4
|
133
|
+
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
134
|
+
else
|
135
|
+
raise
|
136
|
+
end
|
137
|
+
@model.current_object.normals << vert
|
138
|
+
@model.normals << vert
|
139
|
+
end
|
140
|
+
|
141
|
+
def add_texture_coordinate(array)
|
142
|
+
texture = nil
|
143
|
+
if array.size == 4
|
144
|
+
texture = Vector.new(Float(array[1]), 1 - Float(array[2]), Float(array[3]))
|
145
|
+
elsif array.size == 3
|
146
|
+
texture = Vector.new(Float(array[1]), 1 - Float(array[2]), 1.0)
|
147
|
+
else
|
148
|
+
raise
|
149
|
+
end
|
150
|
+
@model.uvs << texture
|
151
|
+
@model.current_object.uvs << texture
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|