diff --git a/lib/objects/camera.rb b/lib/objects/camera.rb index 3afb73c..bcbc9bc 100644 --- a/lib/objects/camera.rb +++ b/lib/objects/camera.rb @@ -5,6 +5,7 @@ class IMICFPS include GLU 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) @x,@y,@z = x,y,z @vertical_angle = 0.0 @@ -17,6 +18,16 @@ class IMICFPS @true_mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2) @true_mouse_checked = 0 @mouse_sensitivity = 20.0 + + @bound_model = nil + end + + def bind_model(model) + @bound_model = model + end + + def unbind + @bound_model = nil end 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::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 def button_up(id) diff --git a/lib/objects/model.rb b/lib/objects/model.rb index 091239f..0db2e05 100644 --- a/lib/objects/model.rb +++ b/lib/objects/model.rb @@ -31,7 +31,7 @@ class IMICFPS unless load_model_from_cache case type 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 raise "Unsupported model type, supported models are: #{Model.supported_models.join(', ')}" end @@ -59,9 +59,7 @@ class IMICFPS 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) - glScalef(scale, scale, scale) - + glTranslatef(@x, @y, @z) glRotatef(@x_rotation,1.0, 0, 0) glRotatef(@y_rotation,0, 1.0, 0) glRotatef(@z_rotation,0, 0, 1.0) @@ -78,8 +76,10 @@ class IMICFPS ObjectManager.objects.each do |a| ObjectManager.objects.each do |b| next if a == b + next if b.name == "skydome" if a.intersect(a.model.bounding_box, b.model.bounding_box) if a.name == "tree" + puts "#{b.name} is touching me" a.y_rotation+=0.01 end end @@ -112,9 +112,9 @@ class IMICFPS 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 + return true else - false + return false end end diff --git a/lib/wavefront/model.rb b/lib/wavefront/model.rb index 077f621..73b9801 100644 --- a/lib/wavefront/model.rb +++ b/lib/wavefront/model.rb @@ -11,13 +11,14 @@ class IMICFPS include Parser attr_accessor :objects, :materials, :vertexes, :texures, :normals, :faces - attr_accessor :x, :y, :z + attr_accessor :x, :y, :z, :scale attr_reader :bounding_box - def initialize(object = "objects/cube.obj") - @x, @y, @z = 0, 0, 0 - @object_path = object - @file = File.open(object, 'r') + def initialize(file_path:, x: 0, y: 0, z: 0, scale: MODEL_METER_SCALE) + @x, @y, @z = x, y, z + @scale = scale + @file_path = file_path + @file = File.open(file_path, 'r') @material_file = nil @current_object = nil @current_material=nil @@ -33,7 +34,7 @@ class IMICFPS @bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil) start_time = Time.now 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 face_count = 0 diff --git a/lib/wavefront/object.rb b/lib/wavefront/object.rb index dd74395..94b4a12 100644 --- a/lib/wavefront/object.rb +++ b/lib/wavefront/object.rb @@ -1,10 +1,11 @@ class IMICFPS class Wavefront 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 - def initialize(name) + def initialize(parent, name) + @parent = parent @name = name @vertices = [] @textures = [] @@ -28,9 +29,9 @@ class IMICFPS @faces.each do |face| [face[0]].each do |v| next unless v - list << v.x - list << v.y - list << v.z + list << v.x*@parent.scale#+@parent.x + list << v.y*@parent.scale#+@parent.y + list << v.z*@parent.scale#+@parent.z list << v.weight end end diff --git a/lib/wavefront/parser.rb b/lib/wavefront/parser.rb index 6c4beae..737d2be 100644 --- a/lib/wavefront/parser.rb +++ b/lib/wavefront/parser.rb @@ -52,7 +52,7 @@ class IMICFPS end 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| array = line.strip.split(' ') case array.first @@ -78,7 +78,7 @@ class IMICFPS end def change_object(name) - @objects << Object.new(name) + @objects << Object.new(self, name) @current_object = @objects.last end diff --git a/lib/window.rb b/lib/window.rb index 92caa51..6abb634 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -19,14 +19,14 @@ class IMICFPS @number_of_faces = 0 @draw_skydome = true @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/biped.obj", x: 1, y: 0, z: 0) + @actor = 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", z: -5) # 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) @camera = Camera.new + @camera.bind_model(@actor) @crosshair_size = 10 @crosshair_thickness = 3 @@ -77,7 +77,8 @@ class IMICFPS OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~ ~ 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} ~ Mouse Sesitivity: #{@camera.mouse_sensitivity} ~ Faces: #{@number_of_faces} ~