From bc695df4a14eaeebeb3e722209f8dee51f969575 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Thu, 7 May 2020 21:45:03 -0500 Subject: [PATCH] Refactored ModelCache into a module, misc. fixes due to change. --- lib/game_objects/entity.rb | 28 +++++---------- lib/model.rb | 11 ++---- lib/model_cache.rb | 45 +++++++++++-------------- lib/renderer/opengl_renderer.rb | 2 -- lib/states/game_states/loading_state.rb | 2 +- 5 files changed, 31 insertions(+), 57 deletions(-) diff --git a/lib/game_objects/entity.rb b/lib/game_objects/entity.rb index d821d90..51324cc 100644 --- a/lib/game_objects/entity.rb +++ b/lib/game_objects/entity.rb @@ -7,7 +7,7 @@ class IMICFPS attr_accessor :visible, :renderable, :backface_culling attr_accessor :position, :orientation, :scale, :velocity - attr_reader :name, :debug_color, :bounding_box, :drag, :camera, :manifest + attr_reader :name, :debug_color, :bounding_box, :drag, :camera, :manifest, :model def initialize(manifest:, map_entity: nil, spawnpoint: nil, backface_culling: true, run_scripts: true) @manifest = manifest @@ -16,7 +16,7 @@ class IMICFPS @position = map_entity.position.clone @orientation = map_entity.orientation.clone @scale = map_entity.scale.clone - @bound_model = bind_model + bind_model elsif spawnpoint @position = spawnpoint.position.clone @orientation = spawnpoint.orientation.clone @@ -45,8 +45,7 @@ class IMICFPS setup - if @bound_model - @bound_model.model.entity = self + if @model @normalized_bounding_box = normalize_bounding_box_with_offset normalize_bounding_box @@ -68,22 +67,15 @@ class IMICFPS end def bind_model - model = ModelCache.new(manifest: @manifest) + model = ModelCache.find_or_cache(manifest: @manifest) + raise "model isn't a model!" unless model.is_a?(Model) - raise "model isn't a model!" unless model.is_a?(ModelCache) - @bound_model = model - @bound_model.model.entity = self + @model = model @bounding_box = normalize_bounding_box_with_offset - - return model - end - - def model - @bound_model.model if @bound_model end def unbind_model - @bound_model = nil + @model = nil end def attach_camera(camera) @@ -103,8 +95,6 @@ class IMICFPS def update - model.update - unless at_same_position? Publisher.instance.publish(:entity_moved, nil, self) @bounding_box = normalize_bounding_box_with_offset if model @@ -122,11 +112,11 @@ class IMICFPS end def normalize_bounding_box_with_offset - @bound_model.model.bounding_box.normalize_with_offset(self) + @model.bounding_box.normalize_with_offset(self) end def normalize_bounding_box - @bound_model.model.bounding_box.normalize(self) + @model.bounding_box.normalize(self) end def model_matrix diff --git a/lib/model.rb b/lib/model.rb index 118bbcd..9cdf9c1 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -3,17 +3,15 @@ class IMICFPS include CommonMethods attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones - attr_accessor :scale, :entity, :material_file, :current_material, :current_object, :vertex_count, :smoothing + attr_accessor :material_file, :current_material, :current_object, :vertex_count, :smoothing attr_reader :position, :bounding_box, :textured_material, :file_path attr_reader :positions_buffer_id, :colors_buffer_id, :normals_buffer_id, :uvs_buffer_id, :textures_buffer_id attr_reader :vertex_array_id attr_reader :aabb_tree - def initialize(file_path:, entity: nil) + def initialize(file_path:) @file_path = file_path - @entity = entity - update if @entity @material_file = nil @current_object = nil @@ -232,11 +230,6 @@ class IMICFPS puts @aabb_tree.inspect if window.config.get(:debug_options, :stats) end - def update - @position = @entity.position - @scale = @entity.scale - end - def has_texture? @has_texture end diff --git a/lib/model_cache.rb b/lib/model_cache.rb index 4757105..dd71f58 100644 --- a/lib/model_cache.rb +++ b/lib/model_cache.rb @@ -1,43 +1,36 @@ class IMICFPS - class ModelCache + module ModelCache CACHE = {} - attr_reader :model, :name, :debug_color + def self.find_or_cache(manifest:) + model_file = manifest.file_path + "/model/#{manifest.model}" - def initialize(manifest:, entity: nil) - @name = manifest.name - @model_file = model_file = manifest.file_path + "/model/#{manifest.model}" + type = File.basename(model_file).split(".").last.to_sym - @type = File.basename(@model_file).split(".").last.to_sym - @debug_color = Color.new(0.0, 1.0, 0.0) + if model = load_model_from_cache(type, model_file) + return model + else + model = IMICFPS::Model.new(file_path: model_file) + cache_model(type, model_file, model) - @model = nil - - unless load_model_from_cache - @model = IMICFPS::Model.new(file_path: @model_file) - - cache_model + return model end - - return self end - def load_model_from_cache - found = false - if CACHE[@type].is_a?(Hash) - if CACHE[@type][@model_file] - @model = CACHE[@type][@model_file]#.dup # Don't know why, but adding .dup improves performance with Sponza (1 fps -> 20 fps) - puts "Used cached model for: #{@model_file.split('/').last}" if $window.config.get(:debug_options, :stats) - found = true + def self.load_model_from_cache(type, model_file) + if CACHE[type].is_a?(Hash) + if CACHE[type][model_file] + puts "Used cached model for: #{model_file.split('/').last}" if $window.config.get(:debug_options, :stats) + return CACHE[type][model_file] end end - return found + return false end - def cache_model - CACHE[@type] = {} unless CACHE[@type].is_a?(Hash) - CACHE[@type][@model_file] = @model + def self.cache_model(type, model_file, model) + CACHE[type] = {} unless CACHE[type].is_a?(Hash) + CACHE[type][model_file] = model end end end diff --git a/lib/renderer/opengl_renderer.rb b/lib/renderer/opengl_renderer.rb index 48a2401..71ffdb0 100644 --- a/lib/renderer/opengl_renderer.rb +++ b/lib/renderer/opengl_renderer.rb @@ -186,7 +186,6 @@ class IMICFPS def draw_mesh(model) model.objects.each_with_index do |o, i| - glEnable(GL_CULL_FACE) if model.entity.backface_culling glEnable(GL_COLOR_MATERIAL) glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) glShadeModel(GL_FLAT) unless o.faces.first[4] @@ -238,7 +237,6 @@ class IMICFPS glDisable(GL_TEXTURE_2D) end - glDisable(GL_CULL_FACE) if model.entity.backface_culling glDisable(GL_COLOR_MATERIAL) end end diff --git a/lib/states/game_states/loading_state.rb b/lib/states/game_states/loading_state.rb index 4fe7036..6eb1472 100644 --- a/lib/states/game_states/loading_state.rb +++ b/lib/states/game_states/loading_state.rb @@ -60,7 +60,7 @@ class IMICFPS hash = @assets[@asset_index] case hash[:type] when :model - ModelCache.new(manifest: hash[:manifest], entity: @dummy_entity) + ModelCache.find_or_cache(manifest: hash[:manifest]) when :shader if window.config.get(:debug_options, :use_shaders) shader = Shader.new(name: hash[:name], includes_dir: "shaders/include", vertex: "shaders/vertex/#{hash[:name]}.glsl", fragment: "shaders/fragment/#{hash[:name]}.glsl")