From 632830503b62bfd3f63804086efb482f6d753772 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Thu, 22 Mar 2018 15:15:20 -0500 Subject: [PATCH] Everything is broken and stuff. --- lib/objects/model.rb | 68 +++++++++++++++++++++++++++++++---------- lib/objects/terrain.rb | 14 ++++----- lib/wavefront/model.rb | 5 ++- lib/wavefront/object.rb | 20 +++++++++--- lib/wavefront/parser.rb | 14 ++++----- lib/window.rb | 15 ++++----- 6 files changed, 94 insertions(+), 42 deletions(-) diff --git a/lib/objects/model.rb b/lib/objects/model.rb index 7e57b98..0b16776 100644 --- a/lib/objects/model.rb +++ b/lib/objects/model.rb @@ -56,12 +56,12 @@ class IMICFPS 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 + # @model.objects.each {|o| render_bounding_box(o.bounding_box, o.debug_color)} if $debug - 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) + # 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) handleGlError @model.draw(@x, @y, @z, @scale, @backface_culling) @@ -79,7 +79,12 @@ class IMICFPS if self.intersect(self.model.bounding_box, b.model.bounding_box) self.y_rotation+=0.02 - puts "#{b.name} is touching me" + puts "#{b.name} is touching #{self.name}" + a_box = normalize_bounding_box(self.model.bounding_box).to_a.map {|q| q.round(2)} + puts "(#{self.name}): (#{a_box[0..2].join(',')}) and (#{a_box[3..5].join(',')})" + + b_box = normalize_bounding_box(b.model.bounding_box).to_a.map {|q| q.round(2)} + puts "(#{b.name}): (#{b_box[0..2].join(',')}) and (#{b_box[3..5].join(',')})" else # puts "!=! No Collision" end @@ -110,15 +115,46 @@ class IMICFPS b = normalize_bounding_box(b) puts "bounding boxes match!" if a == b - # maxx1 > minx2 && minx1 < maxx2 # && maxy1 > miny1 && miny1 < maxy2 + # p to_abs(a),to_abs(b) + # exit + # puts "MAX_X" + # return false if a.max_x <= b.min_x + # puts "MIN_X" + # return false if a.min_x >= b.max_x + # + # puts "MAX_Y" + # return false if a.max_y <= b.min_y + # puts "MIN_Y" + # return false if a.min_y >= b.max_y + # + # puts "MAX_Z" + # return false if a.max_z <= b.min_z + # puts "MIN_Z" + # return false if a.min_z >= b.max_z + # puts "END" + # return true + # if (((a.min_x <= b.min_x && b.max_x <= a.max_x) || (b.min_x <= a.min_x && a.min_x <= b.max_x)) && + # ((a.min_y <= b.min_y && b.max_y <= a.max_y) || (b.min_y <= a.min_y && a.min_y <= b.max_y)) && + # ((a.min_z <= b.min_z && b.max_z <= a.max_z) || (b.min_z <= a.min_z && a.min_z <= b.max_z))) if (a.max_x >= b.max_x && a.min_x <= b.max_x) && (a.max_y >= b.min_y && a.min_y <= b.max_y) && (a.max_z >= b.min_z && a.min_z <= b.max_z) - # puts a - # puts b - # exit return true + # elsif (b.max_x >= a.max_x && b.min_x <= a.max_x) && (b.max_y >= a.min_y && b.min_y <= a.max_y) && (b.max_z >= a.min_z && b.min_z <= a.max_z) + # return true end end + def to_abs(box) + temp = BoundingBox.new + temp.min_x = box.min_x.abs + temp.min_y = box.min_y.abs + temp.min_z = box.min_z.abs + + temp.max_x = box.max_x.abs + temp.max_y = box.max_y.abs + temp.max_z = box.max_z.abs + return temp + end + def normalize_bounding_box(box) temp = BoundingBox.new temp.min_x = box.min_x.to_f*scale+x @@ -138,12 +174,12 @@ class IMICFPS box = normalize_bounding_box(box) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) - glBegin(GL_LINES) - glColor3f(0,0,1.0) - glVertex3f(box.min_x, box.min_y, box.min_z) - glColor3f(1.0,0,0) - glVertex3f(box.max_x, box.max_y, box.max_z) - glEnd + # glBegin(GL_LINES) + # glColor3f(0,0,1.0) + # glVertex3f(box.min_x, box.min_y, box.min_z) + # glColor3f(1.0,0,0) + # glVertex3f(box.max_x, box.max_y, box.max_z) + # glEnd glBegin(GL_TRIANGLES) # TOP glNormal3f(0,1,0) diff --git a/lib/objects/terrain.rb b/lib/objects/terrain.rb index db03020..31aeb86 100644 --- a/lib/objects/terrain.rb +++ b/lib/objects/terrain.rb @@ -2,7 +2,7 @@ class IMICFPS class Terrain include OpenGL def initialize(size:, heightmap: nil) - @size = size + @size = size.to_f @heightmap = heightmap end @@ -13,14 +13,14 @@ class IMICFPS glBegin(GL_TRIANGLES) glNormal3f(0,1,0) glColor3f(1, 0.5, 0.0) - glVertex3f(-@size,height,-@size) - glVertex3f(-@size,height,@size) - glVertex3f(@size,height,@size) + glVertex3f(-@size/2,height,-@size/2) + glVertex3f(-@size/2,height,@size/2) + glVertex3f(@size/2,height,@size/2) glColor3f(0, 0.5, 0.0) - glVertex3f(@size,height,@size) - glVertex3f(@size,height,-@size) - glVertex3f(-@size,height,-@size) + glVertex3f(@size/2,height,@size/2) + glVertex3f(@size/2,height,-@size/2) + glVertex3f(-@size/2,height,-@size/2) glEnd glDisable(GL_COLOR_MATERIAL) diff --git a/lib/wavefront/model.rb b/lib/wavefront/model.rb index 55ac1be..1182cb7 100644 --- a/lib/wavefront/model.rb +++ b/lib/wavefront/model.rb @@ -34,8 +34,11 @@ class IMICFPS @bounding_box = BoundingBox.new(0,0,0, 0,0,0) start_time = Time.now parse + # point = rand(1.0..10.0) + # @bounding_box = BoundingBox.new(point.to_f/2, point.to_f/2, point.to_f/2, point, point, point) + # puts "!!!!!!!!!!!!!!" + # puts @bounding_box puts "#{@file_path.split('/').last} took #{(Time.now-start_time).round(2)} seconds to parse" - p @bounding_box face_count = 0 @objects.each {|o| face_count+=o.faces.size} diff --git a/lib/wavefront/object.rb b/lib/wavefront/object.rb index a3ed2a7..b7f544d 100644 --- a/lib/wavefront/object.rb +++ b/lib/wavefront/object.rb @@ -13,6 +13,7 @@ class IMICFPS @faces = [] @bounding_box = BoundingBox.new(0,0,0, 0,0,0) @debug_color = Color.new(1.0,0.0,0.0) + @x,@y,@z = 0,0,0 # Faces array packs everything: # vertex = index[0] @@ -21,17 +22,28 @@ class IMICFPS # material = index[3] end + def at_same_position? + if @x == @parent.x + if @x == @parent.x + if @x == @parent.x + true + end + end + end + end + def flattened_vertices - unless @vertices_list + unless @vertices_list && at_same_position? @debug_color = @faces.first[3].diffuse + @x,@y,@z = @parent.x,@parent.y,@parent.z list = [] @faces.each do |face| [face[0]].each do |v| next unless v - list << v.x*@parent.scale#+@parent.x - list << v.y*@parent.scale#+@parent.y - list << v.z*@parent.scale#+@parent.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 5617e76..32ae5ba 100644 --- a/lib/wavefront/parser.rb +++ b/lib/wavefront/parser.rb @@ -150,7 +150,7 @@ class IMICFPS def calculate_bounding_box(vertices, bounding_box) unless bounding_box.min_x.is_a?(Float) - vertex = vertices.first + vertex = vertices.last bounding_box.min_x = vertex.x bounding_box.min_y = vertex.y bounding_box.min_z = vertex.z @@ -161,13 +161,13 @@ class IMICFPS end vertices.each do |vertex| - bounding_box.min_x = vertex.x if vertex.x < bounding_box.min_x - bounding_box.min_y = vertex.y if vertex.y < bounding_box.min_y - bounding_box.min_z = vertex.z if vertex.z < bounding_box.min_z + bounding_box.min_x = vertex.x if vertex.x <= bounding_box.min_x + bounding_box.min_y = vertex.y if vertex.y <= bounding_box.min_y + bounding_box.min_z = vertex.z if vertex.z <= bounding_box.min_z - bounding_box.max_x = vertex.x if vertex.x > bounding_box.max_x - bounding_box.max_y = vertex.y if vertex.y > bounding_box.max_y - bounding_box.max_z = vertex.z if vertex.z > bounding_box.max_z + bounding_box.max_x = vertex.x if vertex.x >= bounding_box.max_x + bounding_box.max_y = vertex.y if vertex.y >= bounding_box.max_y + bounding_box.max_z = vertex.z if vertex.z >= bounding_box.max_z end end end diff --git a/lib/window.rb b/lib/window.rb index 3830424..e60dd8d 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -5,6 +5,7 @@ class IMICFPS # include GLUT attr_accessor :number_of_faces, :needs_cursor + attr_reader :camera def initialize(window_width = 1280, window_height = 800, fullscreen = false) if ARGV.join.include?("--native") @@ -17,17 +18,17 @@ class IMICFPS @delta_time = Gosu.milliseconds @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, auto_manage: false) - @actor = Model.new(type: :obj, file_path: "objects/biped.obj", x: 0, y: 0, z: -2) - Model.new(type: :obj, file_path: "objects/tree.obj", x: 0, y: 0, z: -10) + @draw_skydome = false + @skydome = Model.new(type: :obj, file_path: "objects/skydome.obj", x: 0, y: 0,z: 0, scale: 0.8, backface_culling: false, auto_manage: false) + @actor = Model.new(type: :obj, file_path: "objects/biped.obj", x: 1, y: 0, z: 8) + Model.new(type: :obj, file_path: "objects/tree.obj", x: 10, y: 0, z: 10) # 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) - @terrain = Terrain.new(size: 100) + # @terrain = Terrain.new(size: 20) @camera = Camera.new(x: 0, y: -2, z: 1) - @camera.bind_model(@actor) + # @camera.bind_model(@actor) @crosshair_size = 10 @crosshair_thickness = 3 @@ -59,7 +60,7 @@ class IMICFPS ObjectManager.objects.each do |object| object.draw if object.visible && object.renderable end - @terrain.draw + @terrain.draw if @terrain end # Draw crosshair