mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 08:02:36 +00:00
Refactored ModelCache into a module, misc. fixes due to change.
This commit is contained in:
@@ -7,7 +7,7 @@ class IMICFPS
|
|||||||
|
|
||||||
attr_accessor :visible, :renderable, :backface_culling
|
attr_accessor :visible, :renderable, :backface_culling
|
||||||
attr_accessor :position, :orientation, :scale, :velocity
|
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)
|
def initialize(manifest:, map_entity: nil, spawnpoint: nil, backface_culling: true, run_scripts: true)
|
||||||
@manifest = manifest
|
@manifest = manifest
|
||||||
@@ -16,7 +16,7 @@ class IMICFPS
|
|||||||
@position = map_entity.position.clone
|
@position = map_entity.position.clone
|
||||||
@orientation = map_entity.orientation.clone
|
@orientation = map_entity.orientation.clone
|
||||||
@scale = map_entity.scale.clone
|
@scale = map_entity.scale.clone
|
||||||
@bound_model = bind_model
|
bind_model
|
||||||
elsif spawnpoint
|
elsif spawnpoint
|
||||||
@position = spawnpoint.position.clone
|
@position = spawnpoint.position.clone
|
||||||
@orientation = spawnpoint.orientation.clone
|
@orientation = spawnpoint.orientation.clone
|
||||||
@@ -45,8 +45,7 @@ class IMICFPS
|
|||||||
|
|
||||||
setup
|
setup
|
||||||
|
|
||||||
if @bound_model
|
if @model
|
||||||
@bound_model.model.entity = self
|
|
||||||
@normalized_bounding_box = normalize_bounding_box_with_offset
|
@normalized_bounding_box = normalize_bounding_box_with_offset
|
||||||
|
|
||||||
normalize_bounding_box
|
normalize_bounding_box
|
||||||
@@ -68,22 +67,15 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def bind_model
|
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)
|
@model = model
|
||||||
@bound_model = model
|
|
||||||
@bound_model.model.entity = self
|
|
||||||
@bounding_box = normalize_bounding_box_with_offset
|
@bounding_box = normalize_bounding_box_with_offset
|
||||||
|
|
||||||
return model
|
|
||||||
end
|
|
||||||
|
|
||||||
def model
|
|
||||||
@bound_model.model if @bound_model
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def unbind_model
|
def unbind_model
|
||||||
@bound_model = nil
|
@model = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def attach_camera(camera)
|
def attach_camera(camera)
|
||||||
@@ -103,8 +95,6 @@ class IMICFPS
|
|||||||
|
|
||||||
|
|
||||||
def update
|
def update
|
||||||
model.update
|
|
||||||
|
|
||||||
unless at_same_position?
|
unless at_same_position?
|
||||||
Publisher.instance.publish(:entity_moved, nil, self)
|
Publisher.instance.publish(:entity_moved, nil, self)
|
||||||
@bounding_box = normalize_bounding_box_with_offset if model
|
@bounding_box = normalize_bounding_box_with_offset if model
|
||||||
@@ -122,11 +112,11 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def normalize_bounding_box_with_offset
|
def normalize_bounding_box_with_offset
|
||||||
@bound_model.model.bounding_box.normalize_with_offset(self)
|
@model.bounding_box.normalize_with_offset(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize_bounding_box
|
def normalize_bounding_box
|
||||||
@bound_model.model.bounding_box.normalize(self)
|
@model.bounding_box.normalize(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_matrix
|
def model_matrix
|
||||||
|
|||||||
11
lib/model.rb
11
lib/model.rb
@@ -3,17 +3,15 @@ class IMICFPS
|
|||||||
include CommonMethods
|
include CommonMethods
|
||||||
|
|
||||||
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones
|
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 :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 :positions_buffer_id, :colors_buffer_id, :normals_buffer_id, :uvs_buffer_id, :textures_buffer_id
|
||||||
attr_reader :vertex_array_id
|
attr_reader :vertex_array_id
|
||||||
attr_reader :aabb_tree
|
attr_reader :aabb_tree
|
||||||
|
|
||||||
def initialize(file_path:, entity: nil)
|
def initialize(file_path:)
|
||||||
@file_path = file_path
|
@file_path = file_path
|
||||||
@entity = entity
|
|
||||||
update if @entity
|
|
||||||
|
|
||||||
@material_file = nil
|
@material_file = nil
|
||||||
@current_object = nil
|
@current_object = nil
|
||||||
@@ -232,11 +230,6 @@ class IMICFPS
|
|||||||
puts @aabb_tree.inspect if window.config.get(:debug_options, :stats)
|
puts @aabb_tree.inspect if window.config.get(:debug_options, :stats)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
|
||||||
@position = @entity.position
|
|
||||||
@scale = @entity.scale
|
|
||||||
end
|
|
||||||
|
|
||||||
def has_texture?
|
def has_texture?
|
||||||
@has_texture
|
@has_texture
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,43 +1,36 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
class ModelCache
|
module ModelCache
|
||||||
CACHE = {}
|
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)
|
type = File.basename(model_file).split(".").last.to_sym
|
||||||
@name = manifest.name
|
|
||||||
@model_file = model_file = manifest.file_path + "/model/#{manifest.model}"
|
|
||||||
|
|
||||||
@type = File.basename(@model_file).split(".").last.to_sym
|
if model = load_model_from_cache(type, model_file)
|
||||||
@debug_color = Color.new(0.0, 1.0, 0.0)
|
return model
|
||||||
|
else
|
||||||
|
model = IMICFPS::Model.new(file_path: model_file)
|
||||||
|
cache_model(type, model_file, model)
|
||||||
|
|
||||||
@model = nil
|
return model
|
||||||
|
|
||||||
unless load_model_from_cache
|
|
||||||
@model = IMICFPS::Model.new(file_path: @model_file)
|
|
||||||
|
|
||||||
cache_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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return found
|
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
|
end
|
||||||
|
|
||||||
def cache_model
|
return false
|
||||||
CACHE[@type] = {} unless CACHE[@type].is_a?(Hash)
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -186,7 +186,6 @@ class IMICFPS
|
|||||||
|
|
||||||
def draw_mesh(model)
|
def draw_mesh(model)
|
||||||
model.objects.each_with_index do |o, i|
|
model.objects.each_with_index do |o, i|
|
||||||
glEnable(GL_CULL_FACE) if model.entity.backface_culling
|
|
||||||
glEnable(GL_COLOR_MATERIAL)
|
glEnable(GL_COLOR_MATERIAL)
|
||||||
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
||||||
glShadeModel(GL_FLAT) unless o.faces.first[4]
|
glShadeModel(GL_FLAT) unless o.faces.first[4]
|
||||||
@@ -238,7 +237,6 @@ class IMICFPS
|
|||||||
glDisable(GL_TEXTURE_2D)
|
glDisable(GL_TEXTURE_2D)
|
||||||
end
|
end
|
||||||
|
|
||||||
glDisable(GL_CULL_FACE) if model.entity.backface_culling
|
|
||||||
glDisable(GL_COLOR_MATERIAL)
|
glDisable(GL_COLOR_MATERIAL)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ class IMICFPS
|
|||||||
hash = @assets[@asset_index]
|
hash = @assets[@asset_index]
|
||||||
case hash[:type]
|
case hash[:type]
|
||||||
when :model
|
when :model
|
||||||
ModelCache.new(manifest: hash[:manifest], entity: @dummy_entity)
|
ModelCache.find_or_cache(manifest: hash[:manifest])
|
||||||
when :shader
|
when :shader
|
||||||
if window.config.get(:debug_options, :use_shaders)
|
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")
|
shader = Shader.new(name: hash[:name], includes_dir: "shaders/include", vertex: "shaders/vertex/#{hash[:name]}.glsl", fragment: "shaders/fragment/#{hash[:name]}.glsl")
|
||||||
|
|||||||
Reference in New Issue
Block a user