mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 23:52:35 +00:00
Cleaning up Wavefront::Model and centralizing into Model, mouse_look now adapts to FOV better, bounding_boxes are correctly placed (not just moved from center)
This commit is contained in:
@@ -12,6 +12,7 @@ class IMICFPS
|
|||||||
attr_accessor :x, :y, :z, :scale
|
attr_accessor :x, :y, :z, :scale
|
||||||
attr_accessor :visible, :renderable
|
attr_accessor :visible, :renderable
|
||||||
attr_accessor :x_rotation, :y_rotation, :z_rotation
|
attr_accessor :x_rotation, :y_rotation, :z_rotation
|
||||||
|
attr_reader :model, :name, :debug_color
|
||||||
|
|
||||||
def initialize(type:, file_path:, x: 0, y: 0, z: 0, scale: MODEL_METER_SCALE, backface_culling: true)
|
def initialize(type:, file_path:, x: 0, y: 0, z: 0, scale: MODEL_METER_SCALE, backface_culling: true)
|
||||||
@type = type
|
@type = type
|
||||||
@@ -21,20 +22,23 @@ class IMICFPS
|
|||||||
@visible = true
|
@visible = true
|
||||||
@renderable = true
|
@renderable = true
|
||||||
@x_rotation,@y_rotation,@z_rotation = 0,0,0
|
@x_rotation,@y_rotation,@z_rotation = 0,0,0
|
||||||
|
@name = file_path.split("/").last.split(".").first
|
||||||
|
@debug_color = Color.new(rand(0.0..1.0), rand(0.0..1.0), rand(0.0..1.0))
|
||||||
|
@temp_bounding_box = BoundingBox.new(0,0,0, 0,0,0)
|
||||||
|
|
||||||
@model = nil
|
@model = nil
|
||||||
|
|
||||||
unless load_model_from_cache
|
unless load_model_from_cache
|
||||||
case type
|
case type
|
||||||
when :obj
|
when :obj
|
||||||
p file_path
|
|
||||||
@model = Wavefront::Model.new(@file_path)
|
@model = Wavefront::Model.new(@file_path)
|
||||||
else
|
else
|
||||||
raise "Unsupported model type, supported models are: #{Model.supported_models.join(', ')}"
|
raise "Unsupported model type, supported models are: #{Model.supported_models.join(', ')}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
cache_model
|
||||||
end
|
end
|
||||||
|
|
||||||
cache_model
|
|
||||||
|
|
||||||
ObjectManager.add_object(self)
|
ObjectManager.add_object(self)
|
||||||
|
|
||||||
@@ -51,8 +55,13 @@ class IMICFPS
|
|||||||
|
|
||||||
glEnable(GL_NORMALIZE)
|
glEnable(GL_NORMALIZE)
|
||||||
glPushMatrix
|
glPushMatrix
|
||||||
|
# Render bounding boxes before transformation is applied
|
||||||
|
render_bounding_box(@model.bounding_box) if $debug
|
||||||
|
@model.objects.each {|o| render_bounding_box(o.bounding_box, o.debug_color)} if $debug
|
||||||
|
|
||||||
glTranslatef(x,y,z)
|
glTranslatef(x,y,z)
|
||||||
glScalef(scale, scale, scale)
|
glScalef(scale, scale, scale)
|
||||||
|
|
||||||
glRotatef(@x_rotation,1.0, 0, 0)
|
glRotatef(@x_rotation,1.0, 0, 0)
|
||||||
glRotatef(@y_rotation,0, 1.0, 0)
|
glRotatef(@y_rotation,0, 1.0, 0)
|
||||||
glRotatef(@z_rotation,0, 0, 1.0)
|
glRotatef(@z_rotation,0, 0, 1.0)
|
||||||
@@ -66,6 +75,16 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
ObjectManager.objects.each do |a|
|
||||||
|
ObjectManager.objects.each do |b|
|
||||||
|
next if a == b
|
||||||
|
if a.intersect(a.model.bounding_box, b.model.bounding_box)
|
||||||
|
if a.name == "tree"
|
||||||
|
a.y_rotation+=0.1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_model_from_cache
|
def load_model_from_cache
|
||||||
@@ -86,6 +105,102 @@ class IMICFPS
|
|||||||
CACHE[@type][@file_path] = @model
|
CACHE[@type][@file_path] = @model
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Do two Axis Aligned Bounding Boxes intersect?
|
||||||
|
def intersect(a, b)
|
||||||
|
a = normalize_bounding_box(a)
|
||||||
|
b = normalize_bounding_box(b)
|
||||||
|
if (a.min_x <= b.max_x && a.max_x >= b.min_x) &&
|
||||||
|
(a.min_y <= b.max_y && a.max_y >= b.min_y) &&
|
||||||
|
(a.min_z <= b.max_z && a.max_z >= b.min_z)
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize_bounding_box(bounding_box)
|
||||||
|
@temp_bounding_box.min_x = bounding_box.min_x*scale+x
|
||||||
|
@temp_bounding_box.min_y = bounding_box.min_y*scale+y
|
||||||
|
@temp_bounding_box.min_z = bounding_box.min_z*scale+z
|
||||||
|
|
||||||
|
@temp_bounding_box.max_x = bounding_box.max_x*scale+x
|
||||||
|
@temp_bounding_box.max_y = bounding_box.max_y*scale+y
|
||||||
|
@temp_bounding_box.max_z = bounding_box.max_z*scale+z
|
||||||
|
return @temp_bounding_box
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_bounding_box(bounding_box, color = @debug_color)
|
||||||
|
# TODO: Minimize number of calls in here
|
||||||
|
bounding_box = normalize_bounding_box(bounding_box)
|
||||||
|
puts bounding_box
|
||||||
|
puts
|
||||||
|
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
||||||
|
glBegin(GL_TRIANGLES)
|
||||||
|
# TOP
|
||||||
|
glNormal3f(0,1,0)
|
||||||
|
glColor3f(color.red, color.green, color.blue)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
|
||||||
|
# BOTTOM
|
||||||
|
glNormal3f(0,-1,0)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
|
||||||
|
# RIGHT SIDE
|
||||||
|
glNormal3f(0,0,1)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
|
||||||
|
# LEFT SIDE
|
||||||
|
glNormal3f(1,0,0)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
|
||||||
|
# FRONT
|
||||||
|
glNormal3f(-1,0,0)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
||||||
|
|
||||||
|
# BACK
|
||||||
|
glNormal3f(-1,0,0)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
||||||
|
glEnd
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
||||||
|
end
|
||||||
|
|
||||||
def handleGlError
|
def handleGlError
|
||||||
e = glGetError()
|
e = glGetError()
|
||||||
if e != GL_NO_ERROR
|
if e != GL_NO_ERROR
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class IMICFPS
|
|||||||
|
|
||||||
attr_accessor :objects, :materials, :vertexes, :texures, :normals, :faces
|
attr_accessor :objects, :materials, :vertexes, :texures, :normals, :faces
|
||||||
attr_accessor :x, :y, :z
|
attr_accessor :x, :y, :z
|
||||||
|
attr_reader :bounding_box
|
||||||
|
|
||||||
def initialize(object = "objects/cube.obj")
|
def initialize(object = "objects/cube.obj")
|
||||||
@x, @y, @z = 0, 0, 0
|
@x, @y, @z = 0, 0, 0
|
||||||
@@ -30,7 +31,6 @@ class IMICFPS
|
|||||||
@smoothing= 0
|
@smoothing= 0
|
||||||
|
|
||||||
@bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil)
|
@bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil)
|
||||||
@debug_color = Color.new(rand(0.0..1.0), rand(0.0..1.0), rand(0.0..1.0))
|
|
||||||
start_time = Time.now
|
start_time = Time.now
|
||||||
parse
|
parse
|
||||||
puts "#{object.split('/').last} took #{(Time.now-start_time).round(2)} seconds to parse"
|
puts "#{object.split('/').last} took #{(Time.now-start_time).round(2)} seconds to parse"
|
||||||
@@ -81,83 +81,11 @@ class IMICFPS
|
|||||||
# glBindTexture(GL_TEXTURE_2D, 0)
|
# glBindTexture(GL_TEXTURE_2D, 0)
|
||||||
glDisable(GL_TEXTURE_2D)
|
glDisable(GL_TEXTURE_2D)
|
||||||
end
|
end
|
||||||
render_bounding_box(o.bounding_box, o.debug_color) if $debug
|
|
||||||
glDisable(GL_CULL_FACE) if back_face_culling
|
glDisable(GL_CULL_FACE) if back_face_culling
|
||||||
glDisable(GL_COLOR_MATERIAL)
|
glDisable(GL_COLOR_MATERIAL)
|
||||||
end
|
end
|
||||||
render_bounding_box(@bounding_box) if $debug
|
|
||||||
|
|
||||||
|
|
||||||
$window.number_of_faces+=self.faces.size
|
$window.number_of_faces+=self.faces.size
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_bounding_box(bounding_box, color = @debug_color)
|
|
||||||
# TODO: Minimize number of calls in here
|
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
|
||||||
glBegin(GL_TRIANGLES)
|
|
||||||
# TOP
|
|
||||||
glNormal3f(0,1,0)
|
|
||||||
glColor3f(color.red, color.green, color.blue)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
|
|
||||||
# BOTTOM
|
|
||||||
glNormal3f(0,-1,0)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
|
|
||||||
# RIGHT SIDE
|
|
||||||
glNormal3f(0,0,1)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
|
|
||||||
# LEFT SIDE
|
|
||||||
glNormal3f(1,0,0)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
|
|
||||||
# FRONT
|
|
||||||
glNormal3f(-1,0,0)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.max_z)
|
|
||||||
|
|
||||||
# BACK
|
|
||||||
glNormal3f(-1,0,0)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.min_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.min_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
glVertex3f(bounding_box.max_x, bounding_box.max_y, bounding_box.min_z)
|
|
||||||
glEnd
|
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ class IMICFPS
|
|||||||
@delta_time = Gosu.milliseconds
|
@delta_time = Gosu.milliseconds
|
||||||
@number_of_faces = 0
|
@number_of_faces = 0
|
||||||
@draw_skydome = true
|
@draw_skydome = true
|
||||||
Model.new(type: :obj, file_path: "objects/skydome.obj", x: 0, y: 0,z: 0, scale: 1, backface_culling: false)
|
@skydome = Model.new(type: :obj, file_path: "objects/skydome.obj", x: 0, y: 0,z: 0, scale: 1, backface_culling: false)
|
||||||
Model.new(type: :obj, file_path: "objects/cube.obj", x: 0,y: 1,z: -2, scale: 0.0005)
|
Model.new(type: :obj, file_path: "objects/cube.obj", x: 0,y: 1,z: -2, scale: 0.0005)
|
||||||
Model.new(type: :obj, file_path: "objects/biped.obj", x: 1, y: 0, z: 0)
|
Model.new(type: :obj, file_path: "objects/biped.obj", x: 1, y: 0, z: 0)
|
||||||
Model.new(type: :obj, file_path: "objects/tree.obj", x: 3)
|
Model.new(type: :obj, file_path: "objects/tree.obj", x: 3)
|
||||||
Model.new(type: :obj, file_path: "objects/tree.obj", z: -5)
|
# Model.new(type: :obj, file_path: "objects/tree.obj", z: -5)
|
||||||
Model.new(type: :obj, file_path: "objects/tree.obj", x: -2, z: -6)
|
# Model.new(type: :obj, file_path: "objects/tree.obj", x: -2, z: -6)
|
||||||
Model.new(type: :obj, file_path: "objects/sponza.obj", scale: 1, y: -0.2)
|
# Model.new(type: :obj, file_path: "objects/sponza.obj", scale: 1, y: -0.2)
|
||||||
|
|
||||||
@camera = Vertex.new(0,-1,0)
|
@camera = Vertex.new(0,-1,0)
|
||||||
@camera_target = Vertex.new(0,-1,0)
|
@camera_target = Vertex.new(0,-1,0)
|
||||||
@@ -63,10 +63,7 @@ class IMICFPS
|
|||||||
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
|
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
render
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
gl do
|
gl do
|
||||||
glClearColor(0,0.2,0.5,1) # skyish blue
|
glClearColor(0,0.2,0.5,1) # skyish blue
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen and the depth buffer
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen and the depth buffer
|
||||||
@@ -124,8 +121,8 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
if @true_mouse_checked > 2
|
if @true_mouse_checked > 2
|
||||||
@horizontal_angle-=Float(@true_mouse.x-self.mouse_x)/@mouse_sesitivity
|
@horizontal_angle-=Float(@true_mouse.x-self.mouse_x)/(@mouse_sesitivity*@initial_fov)*70
|
||||||
@vertical_angle-=Float(@true_mouse.y-self.mouse_y)/@mouse_sesitivity
|
@vertical_angle-=Float(@true_mouse.y-self.mouse_y)/(@mouse_sesitivity*@initial_fov)*70
|
||||||
@horizontal_angle %= 360.0
|
@horizontal_angle %= 360.0
|
||||||
@vertical_angle = @vertical_angle.clamp(-90.0, 90.0)
|
@vertical_angle = @vertical_angle.clamp(-90.0, 90.0)
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user