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|
if material.texture_id
@has_texture = true
@textured_material = key
end
end

View File

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

View File

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

View File

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

View File

@@ -148,8 +148,6 @@ class IMICFPS
glEnableVertexAttribArray(1)
glEnableVertexAttribArray(2)
if model.has_texture?
glBindTexture(GL_TEXTURE_2D, model.materials[model.textured_material].texture_id)
glEnableVertexAttribArray(3)
glEnableVertexAttribArray(4)
end
@@ -167,7 +165,15 @@ class IMICFPS
glLineWidth(1)
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
if model.has_texture?
@@ -194,11 +200,11 @@ class IMICFPS
glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
if model.has_texture?
if o.has_texture?
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)
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_textures)
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_uvs)
end
glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices)
@@ -232,7 +238,7 @@ class IMICFPS
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
if model.has_texture?
if o.has_texture?
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glDisable(GL_TEXTURE_2D)
end

View File

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