From f72a8d4c35e84a7485c7ac06033bdca3513fec4d Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 11 May 2020 22:47:14 -0500 Subject: [PATCH] Added support for renderering multiple textures per Model --- lib/model.rb | 1 - lib/model/model_object.rb | 23 ++++++++++++++--------- lib/model/parser.rb | 1 + lib/model/parsers/wavefront_parser.rb | 2 +- lib/renderer/opengl_renderer.rb | 20 +++++++++++++------- lib/texture.rb | 6 ++++++ 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/model.rb b/lib/model.rb index 9cdf9c1..ee2507f 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -47,7 +47,6 @@ class IMICFPS @materials.each do |key, material| if material.texture_id @has_texture = true - @textured_material = key end end diff --git a/lib/model/model_object.rb b/lib/model/model_object.rb index 18e3be9..3df6619 100644 --- a/lib/model/model_object.rb +++ b/lib/model/model_object.rb @@ -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 diff --git a/lib/model/parser.rb b/lib/model/parser.rb index 751b000..bc86ebd 100644 --- a/lib/model/parser.rb +++ b/lib/model/parser.rb @@ -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) diff --git a/lib/model/parsers/wavefront_parser.rb b/lib/model/parsers/wavefront_parser.rb index eff6be7..80c2552 100644 --- a/lib/model/parsers/wavefront_parser.rb +++ b/lib/model/parsers/wavefront_parser.rb @@ -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 diff --git a/lib/renderer/opengl_renderer.rb b/lib/renderer/opengl_renderer.rb index 71ffdb0..6f1577c 100644 --- a/lib/renderer/opengl_renderer.rb +++ b/lib/renderer/opengl_renderer.rb @@ -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 diff --git a/lib/texture.rb b/lib/texture.rb index b92471e..ede4863 100644 --- a/lib/texture.rb +++ b/lib/texture.rb @@ -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