Added support for renderering multiple textures per Model

This commit is contained in:
2020-05-11 22:47:14 -05:00
parent d03c3ffcd8
commit f72a8d4c35
6 changed files with 35 additions and 18 deletions

View File

@@ -47,7 +47,6 @@ class IMICFPS
@materials.each do |key, material| @materials.each do |key, material|
if material.texture_id if material.texture_id
@has_texture = true @has_texture = true
@textured_material = key
end end
end end

View File

@@ -1,16 +1,17 @@
class IMICFPS class IMICFPS
class Model class Model
class ModelObject class ModelObject
attr_reader :id, :name, :vertices, :textures, :normals, :bounding_box, :debug_color attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
attr_accessor :faces, :scale attr_accessor :faces, :scale
def initialize(id, name) def initialize(id, name)
@id = id @id = id
@name = name @name = name
@vertices = [] @vertices = []
@textures = [] @uvs = []
@normals = [] @normals = []
@faces = [] @faces = []
@materials = []
@bounding_box = BoundingBox.new @bounding_box = BoundingBox.new
@debug_color = Color.new(1.0,1.0,1.0) @debug_color = Color.new(1.0,1.0,1.0)
@@ -23,13 +24,17 @@ class IMICFPS
# material = index[3] # material = index[3]
end end
def has_texture?
@materials.find { |mat| mat.texture_id } ? true : false
end
def reflatten def reflatten
@vertices_list = nil @vertices_list = nil
@textures_list = nil @uvs_list = nil
@normals_list = nil @normals_list = nil
flattened_vertices flattened_vertices
flattened_textures flattened_uvs
flattened_normals flattened_normals
end end
@@ -59,8 +64,8 @@ class IMICFPS
@vertices_list_size @vertices_list_size
end end
def flattened_textures def flattened_uvs
unless @textures_list unless @uvs_list
list = [] list = []
@faces.each do |face| @faces.each do |face|
face.uvs.each do |v| face.uvs.each do |v|
@@ -71,11 +76,11 @@ class IMICFPS
end end
end end
@textures_list_size = list.size @uvs_list_size = list.size
@textures_list = list.pack("f*") @uvs_list = list.pack("f*")
end end
return @textures_list return @uvs_list
end end
def flattened_normals def flattened_normals

View File

@@ -55,6 +55,7 @@ class IMICFPS
def set_material(name) def set_material(name)
@model.current_material = name @model.current_material = name
@model.current_object.materials << current_material
end end
def add_material(name, material) def add_material(name, material)

View File

@@ -150,8 +150,8 @@ class IMICFPS
else else
raise raise
end end
@model.current_object.textures << texture
@model.uvs << texture @model.uvs << texture
@model.current_object.uvs << texture
end end
end end
end end

View File

@@ -148,8 +148,6 @@ class IMICFPS
glEnableVertexAttribArray(1) glEnableVertexAttribArray(1)
glEnableVertexAttribArray(2) glEnableVertexAttribArray(2)
if model.has_texture? if model.has_texture?
glBindTexture(GL_TEXTURE_2D, model.materials[model.textured_material].texture_id)
glEnableVertexAttribArray(3) glEnableVertexAttribArray(3)
glEnableVertexAttribArray(4) glEnableVertexAttribArray(4)
end end
@@ -167,7 +165,15 @@ class IMICFPS
glLineWidth(1) glLineWidth(1)
end end
glDrawArrays(GL_TRIANGLES, 0, model.faces.count * 3) offset = 0
model.objects.each do |object|
if object.has_texture?
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)
end
glDrawArrays(GL_TRIANGLES, offset, object.faces.count * 3)
offset += object.faces.count * 3
end
window.number_of_vertices += model.faces.count * 3 window.number_of_vertices += model.faces.count * 3
if model.has_texture? if model.has_texture?
@@ -194,11 +200,11 @@ class IMICFPS
glEnableClientState(GL_COLOR_ARRAY) glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY) glEnableClientState(GL_NORMAL_ARRAY)
if model.has_texture? if o.has_texture?
glEnable(GL_TEXTURE_2D) glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, model.materials[model.textured_material].texture_id) glBindTexture(GL_TEXTURE_2D, o.materials.find { |mat| mat.texture_id }.texture_id)
glEnableClientState(GL_TEXTURE_COORD_ARRAY) glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_textures) glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_uvs)
end end
glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices) glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices)
@@ -232,7 +238,7 @@ class IMICFPS
glDisableClientState(GL_COLOR_ARRAY) glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY) glDisableClientState(GL_NORMAL_ARRAY)
if model.has_texture? if o.has_texture?
glDisableClientState(GL_TEXTURE_COORD_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glDisable(GL_TEXTURE_2D) glDisable(GL_TEXTURE_2D)
end end

View File

@@ -22,12 +22,18 @@ class IMICFPS
if path.is_a?(Array) if path.is_a?(Array)
if path.size > 1 if path.size > 1
# Try loading from models textures folder
path = "#{GAME_ROOT_PATH}/assets/#{path.join("/")}" path = "#{GAME_ROOT_PATH}/assets/#{path.join("/")}"
else else
path = path.first path = path.first
end end
end end
# Try searching shared textures folder
unless File.exist?(path)
path = "#{IMICFPS.assets_path}/base/shared/textures/#{path.split("/").last}"
end
@path = path @path = path
if @path if @path