mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 23:52:35 +00:00
Restructured Model loading to enable easier implementation of other parsers
This commit is contained in:
36
lib/model/material.rb
Normal file
36
lib/model/material.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
class IMICFPS
|
||||
class Model
|
||||
class Material
|
||||
attr_accessor :name, :ambient, :diffuse, :specular
|
||||
attr_reader :texture_id
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@ambient = Color.new(1, 1, 1, 1)
|
||||
@diffuse = Color.new(1, 1, 1, 1)
|
||||
@specular= Color.new(1, 1, 1, 1)
|
||||
@texture = nil
|
||||
@texture_id = nil
|
||||
end
|
||||
|
||||
def set_texture(texture_path)
|
||||
# puts "#{name} texture #{texture_path}"
|
||||
@texture = Gosu::Image.new(texture_path, retro: false)
|
||||
array_of_pixels = @texture.to_blob
|
||||
|
||||
tex_names_buf = ' ' * 8
|
||||
glGenTextures(1, tex_names_buf)
|
||||
@texture_id = tex_names_buf.unpack('L2').first
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, @texture_id)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, @texture.width, @texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
|
||||
glGenerateMipmap(GL_TEXTURE_2D)
|
||||
|
||||
@texture = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
122
lib/model/model_object.rb
Normal file
122
lib/model/model_object.rb
Normal file
@@ -0,0 +1,122 @@
|
||||
class IMICFPS
|
||||
class Model
|
||||
class ModelObject
|
||||
attr_reader :name, :vertices, :textures, :normals, :bounding_box, :debug_color
|
||||
attr_accessor :faces, :scale
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@vertices = []
|
||||
@textures = []
|
||||
@normals = []
|
||||
@faces = []
|
||||
@bounding_box = BoundingBox.new
|
||||
@debug_color = Color.new(1.0,1.0,1.0)
|
||||
|
||||
@scale = 1.0
|
||||
|
||||
# Faces array packs everything:
|
||||
# vertex = index[0]
|
||||
# uv = index[1]
|
||||
# normal = index[2]
|
||||
# material = index[3]
|
||||
end
|
||||
|
||||
def reflatten
|
||||
@vertices_list = nil
|
||||
@textures_list = nil
|
||||
@normals_list = nil
|
||||
|
||||
flattened_vertices
|
||||
flattened_textures
|
||||
flattened_normals
|
||||
end
|
||||
|
||||
def flattened_vertices
|
||||
unless @vertices_list
|
||||
@debug_color = @faces.first.material.diffuse
|
||||
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
face.vertices.each do |v|
|
||||
next unless v
|
||||
list << v.x*@scale
|
||||
list << v.y*@scale
|
||||
list << v.z*@scale
|
||||
list << v.weight
|
||||
end
|
||||
end
|
||||
|
||||
@vertices_list_size = list.size
|
||||
@vertices_list = list.pack("f*")
|
||||
end
|
||||
|
||||
return @vertices_list
|
||||
end
|
||||
|
||||
def flattened_vertices_size
|
||||
@vertices_list_size
|
||||
end
|
||||
|
||||
def flattened_textures
|
||||
unless @textures_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
face.uvs.each do |v|
|
||||
next unless v
|
||||
list << v.x
|
||||
list << v.y
|
||||
list << v.z
|
||||
end
|
||||
end
|
||||
|
||||
@textures_list_size = list.size
|
||||
@textures_list = list.pack("f*")
|
||||
end
|
||||
|
||||
return @textures_list
|
||||
end
|
||||
|
||||
def flattened_normals
|
||||
unless @normals_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
face.normals.each do |n|
|
||||
next unless n
|
||||
list << n.x
|
||||
list << n.y
|
||||
list << n.z
|
||||
end
|
||||
end
|
||||
|
||||
@normals_list_size = list.size
|
||||
@normals_list = list.pack("f*")
|
||||
end
|
||||
|
||||
return @normals_list
|
||||
end
|
||||
|
||||
def flattened_materials
|
||||
unless @materials_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
material = face.material
|
||||
next unless material
|
||||
face.vertices.each do # Add material to each vertex
|
||||
|
||||
list << material.diffuse.red
|
||||
list << material.diffuse.green
|
||||
list << material.diffuse.blue
|
||||
# list << material.alpha
|
||||
end
|
||||
end
|
||||
|
||||
@materials_list_size = list.size
|
||||
@materials_list = list.pack("f*")
|
||||
end
|
||||
|
||||
return @materials_list
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
30
lib/model/parser.rb
Normal file
30
lib/model/parser.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
class IMICFPS
|
||||
class Model
|
||||
class Parser
|
||||
@@parsers = []
|
||||
|
||||
def self.handles
|
||||
raise NotImplementedError, "Model::Parser#handles must return an array of file extensions that this parser supports"
|
||||
end
|
||||
|
||||
def self.inherited(parser)
|
||||
@@parsers << parser
|
||||
end
|
||||
|
||||
def self.find(file_type)
|
||||
found_parser = @@parsers.find do |parser|
|
||||
parser.handles.include?(file_type)
|
||||
end
|
||||
|
||||
return found_parser
|
||||
end
|
||||
|
||||
def initialize(model)
|
||||
@model = model
|
||||
end
|
||||
|
||||
def parse
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
180
lib/model/parsers/wavefront_parser.rb
Normal file
180
lib/model/parsers/wavefront_parser.rb
Normal file
@@ -0,0 +1,180 @@
|
||||
class IMICFPS
|
||||
class WavefrontParser < Model::Parser
|
||||
def self.handles
|
||||
[:obj]
|
||||
end
|
||||
|
||||
def initialize(model)
|
||||
@model = model
|
||||
end
|
||||
|
||||
def parse
|
||||
lines = 0
|
||||
list = File.read(@model.file_path).split("\n")
|
||||
list.each do |line|
|
||||
lines+=1
|
||||
line = line.strip
|
||||
|
||||
array = line.split(' ')
|
||||
case array[0]
|
||||
when 'mtllib'
|
||||
@model.material_file = array[1]
|
||||
parse_mtllib
|
||||
when 'usemtl'
|
||||
set_material(array[1])
|
||||
when 'o'
|
||||
change_object(array[1])
|
||||
when 's'
|
||||
set_smoothing(array[1])
|
||||
when 'v'
|
||||
add_vertex(array)
|
||||
when 'vt'
|
||||
add_texture_coordinate(array)
|
||||
|
||||
when 'vn'
|
||||
add_normal(array)
|
||||
|
||||
when 'f'
|
||||
verts = []
|
||||
uvs = []
|
||||
norms = []
|
||||
array[1..3].each do |f|
|
||||
verts << f.split("/")[0]
|
||||
uvs << f.split("/")[1]
|
||||
norms << f.split("/")[2]
|
||||
end
|
||||
|
||||
face = Face.new
|
||||
face.vertices = []
|
||||
face.uvs = []
|
||||
face.normals = []
|
||||
face.colors = []
|
||||
face.material = material
|
||||
face.smoothing= @model.smoothing
|
||||
|
||||
mat = face.material.diffuse
|
||||
color = Vector.new(mat.red, mat.green, mat.blue)
|
||||
|
||||
verts.each_with_index do |v, index|
|
||||
|
||||
if uvs.first != ""
|
||||
face.vertices << @model.vertices[Integer(v)-1]
|
||||
face.uvs << @model.uvs[Integer(uvs[index])-1]
|
||||
face.normals << @model.normals[Integer(norms[index])-1]
|
||||
face.colors << color
|
||||
else
|
||||
face.vertices << @model.vertices[Integer(v)-1]
|
||||
face.uvs << nil
|
||||
face.normals << @model.normals[Integer(norms[index])-1]
|
||||
face.colors << color
|
||||
end
|
||||
end
|
||||
|
||||
@model.current_object.faces << face
|
||||
@model.faces << face
|
||||
end
|
||||
end
|
||||
|
||||
puts "Total Lines: #{lines}" if $window.config.get(:debug_options, :stats)
|
||||
@model.calculate_bounding_box(@model.vertices, @model.bounding_box)
|
||||
@model.objects.each do |o|
|
||||
@model.calculate_bounding_box(o.vertices, o.bounding_box)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_mtllib
|
||||
file = File.open(@model.file_path.sub(File.basename(@model.file_path), '')+@model.material_file, 'r')
|
||||
file.readlines.each do |line|
|
||||
array = line.strip.split(' ')
|
||||
case array.first
|
||||
when 'newmtl'
|
||||
material = Model::Material.new(array.last)
|
||||
@model.current_material = array.last
|
||||
@model.materials[array.last] = material
|
||||
when 'Ns' # Specular Exponent
|
||||
when 'Ka' # Ambient color
|
||||
@model.materials[@model.current_material].ambient = Color.new(Float(array[1]), Float(array[2]), Float(array[3]))
|
||||
when 'Kd' # Diffuse color
|
||||
@model.materials[@model.current_material].diffuse = Color.new(Float(array[1]), Float(array[2]), Float(array[3]))
|
||||
when 'Ks' # Specular color
|
||||
@model.materials[@model.current_material].specular = Color.new(Float(array[1]), Float(array[2]), Float(array[3]))
|
||||
when 'Ke' # Emissive
|
||||
when 'Ni' # Unknown (Blender Specific?)
|
||||
when 'd' # Dissolved (Transparency)
|
||||
when 'illum' # Illumination model
|
||||
when 'map_Kd' # Diffuse texture
|
||||
texture = File.basename(array[1])
|
||||
texture_path = "#{File.expand_path("../../", @model.file_path)}/textures/#{texture}"
|
||||
@model.materials[@model.current_material].set_texture(texture_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def change_object(name)
|
||||
@model.objects << Model::ModelObject.new(name)
|
||||
@model.current_object = @model.objects.last
|
||||
end
|
||||
|
||||
def set_smoothing(value)
|
||||
if value == "1"
|
||||
@model.smoothing = true
|
||||
else
|
||||
@model.smoothing = false
|
||||
end
|
||||
end
|
||||
|
||||
def set_material(name)
|
||||
@model.current_material = name
|
||||
end
|
||||
|
||||
def material
|
||||
@model.materials[@model.current_material]
|
||||
end
|
||||
|
||||
def faces_count
|
||||
count = 0
|
||||
@model.objects.each {|o| count+=o.faces.count}
|
||||
return count
|
||||
end
|
||||
|
||||
def add_vertex(array)
|
||||
@model.vertex_count+=1
|
||||
vert = nil
|
||||
if array.size == 5
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
||||
elsif array.size == 4
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
||||
else
|
||||
raise
|
||||
end
|
||||
@model.current_object.vertices << vert
|
||||
@model.vertices << vert
|
||||
end
|
||||
|
||||
def add_normal(array)
|
||||
vert = nil
|
||||
if array.size == 5
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
||||
elsif array.size == 4
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
||||
else
|
||||
raise
|
||||
end
|
||||
@model.current_object.normals << vert
|
||||
@model.normals << vert
|
||||
end
|
||||
|
||||
def add_texture_coordinate(array)
|
||||
texture = nil
|
||||
if array.size == 4
|
||||
texture = Vector.new(Float(array[1]), 1-Float(array[2]), Float(array[3]))
|
||||
elsif array.size == 3
|
||||
texture = Vector.new(Float(array[1]), 1-Float(array[2]), 1.0)
|
||||
else
|
||||
raise
|
||||
end
|
||||
@model.current_object.textures << texture
|
||||
@model.uvs << texture
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user