Camera can bind to model

This commit is contained in:
2018-03-20 22:48:01 -05:00
parent d390d3d728
commit 2de1c0ffdf
6 changed files with 47 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ class IMICFPS
include GLU include GLU
attr_accessor :x,:y,:z, :field_of_view, :vertical_angle, :horizontal_angle, :mouse_sensitivity attr_accessor :x,:y,:z, :field_of_view, :vertical_angle, :horizontal_angle, :mouse_sensitivity
attr_reader :bound_model
def initialize(x: 0, y: 0, z: 0, fov: 70.0) def initialize(x: 0, y: 0, z: 0, fov: 70.0)
@x,@y,@z = x,y,z @x,@y,@z = x,y,z
@vertical_angle = 0.0 @vertical_angle = 0.0
@@ -17,6 +18,16 @@ class IMICFPS
@true_mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2) @true_mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2)
@true_mouse_checked = 0 @true_mouse_checked = 0
@mouse_sensitivity = 20.0 @mouse_sensitivity = 20.0
@bound_model = nil
end
def bind_model(model)
@bound_model = model
end
def unbind
@bound_model = nil
end end
def draw def draw
@@ -86,6 +97,17 @@ class IMICFPS
@y+=relative_speed if button_down?(Gosu::KbC) || button_down?(Gosu::KbLeftShift) @y+=relative_speed if button_down?(Gosu::KbC) || button_down?(Gosu::KbLeftShift)
@y-=relative_speed if button_down?(Gosu::KbSpace) @y-=relative_speed if button_down?(Gosu::KbSpace)
if @bound_model
distance = 2.0
x_offset = distance * Math.cos(@bound_model.y_rotation)
z_offset = distance * Math.sin(@bound_model.y_rotation)
@bound_model.x = @x*-1+x_offset
@bound_model.y = @y*-1-2
@bound_model.z = @z*-1-z_offset
@bound_model.y_rotation = (@horizontal_angle*-1)+180
end
end end
def button_up(id) def button_up(id)

View File

@@ -31,7 +31,7 @@ class IMICFPS
unless load_model_from_cache unless load_model_from_cache
case type case type
when :obj when :obj
@model = Wavefront::Model.new(@file_path) @model = Wavefront::Model.new(file_path: @file_path, x: x, y: y, z: z, scale: scale)
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
@@ -59,9 +59,7 @@ class IMICFPS
render_bounding_box(@model.bounding_box) if $debug render_bounding_box(@model.bounding_box) if $debug
@model.objects.each {|o| render_bounding_box(o.bounding_box, o.debug_color)} 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)
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)
@@ -78,8 +76,10 @@ class IMICFPS
ObjectManager.objects.each do |a| ObjectManager.objects.each do |a|
ObjectManager.objects.each do |b| ObjectManager.objects.each do |b|
next if a == b next if a == b
next if b.name == "skydome"
if a.intersect(a.model.bounding_box, b.model.bounding_box) if a.intersect(a.model.bounding_box, b.model.bounding_box)
if a.name == "tree" if a.name == "tree"
puts "#{b.name} is touching me"
a.y_rotation+=0.01 a.y_rotation+=0.01
end end
end end
@@ -112,9 +112,9 @@ class IMICFPS
if (a.min_x <= b.max_x && a.max_x >= b.min_x) && 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_y <= b.max_y && a.max_y >= b.min_y) &&
(a.min_z <= b.max_z && a.max_z >= b.min_z) (a.min_z <= b.max_z && a.max_z >= b.min_z)
true return true
else else
false return false
end end
end end

View File

@@ -11,13 +11,14 @@ class IMICFPS
include Parser include Parser
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, :scale
attr_reader :bounding_box attr_reader :bounding_box
def initialize(object = "objects/cube.obj") def initialize(file_path:, x: 0, y: 0, z: 0, scale: MODEL_METER_SCALE)
@x, @y, @z = 0, 0, 0 @x, @y, @z = x, y, z
@object_path = object @scale = scale
@file = File.open(object, 'r') @file_path = file_path
@file = File.open(file_path, 'r')
@material_file = nil @material_file = nil
@current_object = nil @current_object = nil
@current_material=nil @current_material=nil
@@ -33,7 +34,7 @@ class IMICFPS
@bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil) @bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil)
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 "#{@file_path.split('/').last} took #{(Time.now-start_time).round(2)} seconds to parse"
p @bounding_box p @bounding_box
face_count = 0 face_count = 0

View File

@@ -1,10 +1,11 @@
class IMICFPS class IMICFPS
class Wavefront class Wavefront
class Object class Object
attr_reader :name, :vertices, :textures, :normals, :bounding_box, :debug_color attr_reader :parent, :name, :vertices, :textures, :normals, :bounding_box, :debug_color
attr_accessor :faces attr_accessor :faces
def initialize(name) def initialize(parent, name)
@parent = parent
@name = name @name = name
@vertices = [] @vertices = []
@textures = [] @textures = []
@@ -28,9 +29,9 @@ class IMICFPS
@faces.each do |face| @faces.each do |face|
[face[0]].each do |v| [face[0]].each do |v|
next unless v next unless v
list << v.x list << v.x*@parent.scale#+@parent.x
list << v.y list << v.y*@parent.scale#+@parent.y
list << v.z list << v.z*@parent.scale#+@parent.z
list << v.weight list << v.weight
end end
end end

View File

@@ -52,7 +52,7 @@ class IMICFPS
end end
def parse_mtllib def parse_mtllib
file = File.open(@object_path.sub(File.basename(@object_path), '')+@material_file, 'r') file = File.open(@file_path.sub(File.basename(@file_path), '')+@material_file, 'r')
file.readlines.each do |line| file.readlines.each do |line|
array = line.strip.split(' ') array = line.strip.split(' ')
case array.first case array.first
@@ -78,7 +78,7 @@ class IMICFPS
end end
def change_object(name) def change_object(name)
@objects << Object.new(name) @objects << Object.new(self, name)
@current_object = @objects.last @current_object = @objects.last
end end

View File

@@ -19,14 +19,14 @@ class IMICFPS
@number_of_faces = 0 @number_of_faces = 0
@draw_skydome = true @draw_skydome = true
@skydome = 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) @actor = 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 = Camera.new @camera = Camera.new
@camera.bind_model(@actor)
@crosshair_size = 10 @crosshair_size = 10
@crosshair_thickness = 3 @crosshair_thickness = 3
@@ -77,7 +77,8 @@ class IMICFPS
OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~ OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~
~ ~
Vertical Angle: #{@camera.vertical_angle.round(2)} Horizontal Angle: #{@camera.horizontal_angle.round(2)} ~ Vertical Angle: #{@camera.vertical_angle.round(2)} Horizontal Angle: #{@camera.horizontal_angle.round(2)} ~
X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} ~ Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} ~
Actor X:#{@camera.bound_model.x.round(2)} Y:#{@camera.bound_model.y.round(2)} Z:#{@camera.bound_model.z.round(2)} ~
FOV: #{@camera.field_of_view} ~ FOV: #{@camera.field_of_view} ~
Mouse Sesitivity: #{@camera.mouse_sensitivity} ~ Mouse Sesitivity: #{@camera.mouse_sensitivity} ~
Faces: #{@number_of_faces} ~ Faces: #{@number_of_faces} ~