mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 08:02:36 +00:00
Refactored Model to make faces first class objects for use in AABBTree, renamed BoundingBox.intersect to intersect?
This commit is contained in:
@@ -54,7 +54,7 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
broadphase.each do |entity, _collisions|
|
broadphase.each do |entity, _collisions|
|
||||||
_collisions.reject! {|ent| !entity.bounding_box.intersect(ent.bounding_box)}
|
_collisions.reject! {|ent| !entity.bounding_box.intersect?(ent.bounding_box)}
|
||||||
# TODO: mesh aabb tree vs other mesh aabb tree check
|
# TODO: mesh aabb tree vs other mesh aabb tree check
|
||||||
# TODO: triangle vs other triangle check
|
# TODO: triangle vs other triangle check
|
||||||
_collisions.each do |ent|
|
_collisions.each do |ent|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ class IMICFPS
|
|||||||
TextureCoordinate = Struct.new(:u, :v, :weight)
|
TextureCoordinate = Struct.new(:u, :v, :weight)
|
||||||
Point = Struct.new(:x, :y)
|
Point = Struct.new(:x, :y)
|
||||||
Color = Struct.new(:red, :green, :blue, :alpha)
|
Color = Struct.new(:red, :green, :blue, :alpha)
|
||||||
|
Face = Struct.new(:vertices, :uvs, :normals, :material, :smoothing)
|
||||||
|
|
||||||
module EntityManager # Get included into GameState context
|
module EntityManager # Get included into GameState context
|
||||||
def add_entity(entity)
|
def add_entity(entity)
|
||||||
|
|||||||
@@ -26,13 +26,6 @@ class IMICFPS
|
|||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns whether both bounding boxes intersect
|
|
||||||
def intersect(other)
|
|
||||||
(@min.x <= other.max.x && @max.x >= other.min.x) &&
|
|
||||||
(@min.y <= other.max.y && @max.y >= other.min.y) &&
|
|
||||||
(@min.z <= other.max.z && @max.z >= other.min.z)
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns the difference between both bounding boxes
|
# returns the difference between both bounding boxes
|
||||||
def difference(other)
|
def difference(other)
|
||||||
temp = BoundingBox.new
|
temp = BoundingBox.new
|
||||||
@@ -42,6 +35,13 @@ class IMICFPS
|
|||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# returns whether both bounding boxes intersect
|
||||||
|
def intersect?(other)
|
||||||
|
(@min.x <= other.max.x && @max.x >= other.min.x) &&
|
||||||
|
(@min.y <= other.max.y && @max.y >= other.min.y) &&
|
||||||
|
(@min.z <= other.max.z && @max.z >= other.min.z)
|
||||||
|
end
|
||||||
|
|
||||||
# does this bounding box envelop other bounding box? (inclusive of border)
|
# does this bounding box envelop other bounding box? (inclusive of border)
|
||||||
def contains?(other)
|
def contains?(other)
|
||||||
other.min.x >= min.x && other.min.y >= min.y && other.min.z >= min.z &&
|
other.min.x >= min.x && other.min.y >= min.y && other.min.z >= min.z &&
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def search_subtree(bounding_box, items = [])
|
def search_subtree(bounding_box, items = [])
|
||||||
if @bounding_box.intersect(bounding_box)
|
if @bounding_box.intersect?(bounding_box)
|
||||||
if leaf?
|
if leaf?
|
||||||
items << self
|
items << self
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -58,6 +58,23 @@ class IMICFPS
|
|||||||
@textured_material = key
|
@textured_material = key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@aabb_tree = AABBTree.new
|
||||||
|
@faces.each do |face|
|
||||||
|
box = BoundingBox.new
|
||||||
|
box.min = face.vertices.first.dup
|
||||||
|
box.max = face.vertices.first.dup
|
||||||
|
|
||||||
|
face.vertices.each do |vertex|
|
||||||
|
if vertex.sum < box.min.sum
|
||||||
|
box.min = vertex.dup
|
||||||
|
elsif vertex.sum > box.max.sum
|
||||||
|
box.max = vertex.dup
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@aabb_tree.insert(face, box)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def allocate_gl_objects
|
def allocate_gl_objects
|
||||||
|
|||||||
@@ -34,11 +34,11 @@ class IMICFPS
|
|||||||
|
|
||||||
def flattened_vertices
|
def flattened_vertices
|
||||||
unless @vertices_list
|
unless @vertices_list
|
||||||
@debug_color = @faces.first[3].diffuse
|
@debug_color = @faces.first.material.diffuse
|
||||||
|
|
||||||
list = []
|
list = []
|
||||||
@faces.each do |face|
|
@faces.each do |face|
|
||||||
[face[0]].each do |v|
|
face.vertices.each do |v|
|
||||||
next unless v
|
next unless v
|
||||||
list << v.x*@scale
|
list << v.x*@scale
|
||||||
list << v.y*@scale
|
list << v.y*@scale
|
||||||
@@ -61,8 +61,8 @@ class IMICFPS
|
|||||||
def flattened_textures
|
def flattened_textures
|
||||||
unless @textures_list
|
unless @textures_list
|
||||||
list = []
|
list = []
|
||||||
@faces.each_with_index do |face, i|
|
@faces.each do |face|
|
||||||
[face[1]].each do |v|
|
face.uvs.each do |v|
|
||||||
next unless v
|
next unless v
|
||||||
list << v.x
|
list << v.x
|
||||||
list << v.y
|
list << v.y
|
||||||
@@ -81,12 +81,11 @@ class IMICFPS
|
|||||||
unless @normals_list
|
unless @normals_list
|
||||||
list = []
|
list = []
|
||||||
@faces.each do |face|
|
@faces.each do |face|
|
||||||
[face[2]].each do |v|
|
face.normals.each do |n|
|
||||||
next unless v
|
next unless n
|
||||||
list << v.x
|
list << n.x
|
||||||
list << v.y
|
list << n.y
|
||||||
list << v.z
|
list << n.z
|
||||||
# list << v.weight
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -101,15 +100,14 @@ class IMICFPS
|
|||||||
unless @materials_list
|
unless @materials_list
|
||||||
list = []
|
list = []
|
||||||
@faces.each do |face|
|
@faces.each do |face|
|
||||||
# p face
|
material = face.material
|
||||||
[face[3]].each do |v|
|
next unless material
|
||||||
next unless v
|
face.vertices.each do # Add material to each vertex
|
||||||
# p v
|
|
||||||
# exit
|
list << material.diffuse.red
|
||||||
list << v.diffuse.red
|
list << material.diffuse.green
|
||||||
list << v.diffuse.green
|
list << material.diffuse.blue
|
||||||
list << v.diffuse.blue
|
# list << material.alpha
|
||||||
# list << v.alpha
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -38,17 +38,29 @@ class IMICFPS
|
|||||||
norms << f.split("/")[2]
|
norms << f.split("/")[2]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
face = Face.new
|
||||||
|
face.vertices = []
|
||||||
|
face.uvs = []
|
||||||
|
face.normals = []
|
||||||
|
face.material = material
|
||||||
|
face.smoothing= @smoothing
|
||||||
|
|
||||||
verts.each_with_index do |v, index|
|
verts.each_with_index do |v, index|
|
||||||
if uvs.first != ""
|
if uvs.first != ""
|
||||||
face = [@vertices[Integer(v)-1], @uvs[Integer(uvs[index])-1], @normals[Integer(norms[index])-1], material, @smoothing]
|
face.vertices << @vertices[Integer(v)-1]
|
||||||
|
face.uvs << @uvs[Integer(uvs[index])-1]
|
||||||
|
face.normals << @normals[Integer(norms[index])-1]
|
||||||
else
|
else
|
||||||
face = [@vertices[Integer(v)-1], nil, @normals[Integer(norms[index])-1], material, @smoothing]
|
face.vertices << @vertices[Integer(v)-1]
|
||||||
|
face.uvs << nil
|
||||||
|
face.normals << @normals[Integer(norms[index])-1]
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@current_object.faces << face
|
@current_object.faces << face
|
||||||
@faces << face
|
@faces << face
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
puts "Total Lines: #{lines}" if $debug
|
puts "Total Lines: #{lines}" if $debug
|
||||||
calculate_bounding_box(@vertices, @bounding_box)
|
calculate_bounding_box(@vertices, @bounding_box)
|
||||||
|
|||||||
Reference in New Issue
Block a user