Bounding boxes are now drawn using Vertex arrays

This commit is contained in:
2018-07-30 12:52:35 -05:00
parent ded012a9e0
commit 67b06b0001
7 changed files with 195 additions and 78 deletions

View File

@@ -1,8 +1,8 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
glu (8.3.0) glu (8.2.2)
glu (8.3.0-x86-mingw32) glu (8.2.2-x86-mingw32)
gosu (0.13.3) gosu (0.13.3)
gosu (0.13.3-x86-mingw32) gosu (0.13.3-x86-mingw32)
opengl-bindings (1.6.6) opengl-bindings (1.6.6)

View File

@@ -74,9 +74,16 @@ class IMICFPS
glRotatef(@render_pitch,1,0,0) glRotatef(@render_pitch,1,0,0)
glRotatef(@yaw,0,1,0) glRotatef(@yaw,0,1,0)
glTranslatef(-@x, -@y, -@z) glTranslatef(-@x, -@y, -@z)
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored. glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
glLoadIdentity glLoadIdentity
if $debug && @game_object
glBegin(GL_LINES)
glColor3f(1,0,0)
glVertex3f(@x, @y, @z)
glVertex3f(@game_object.x, @game_object.y, @game_object.z)
glEnd
end
end end
def update def update

View File

@@ -4,6 +4,7 @@ class IMICFPS
# A game object is any renderable thing # A game object is any renderable thing
class GameObject class GameObject
include OpenGL include OpenGL
include GLU
include CommonMethods include CommonMethods
attr_accessor :x, :y, :z, :scale attr_accessor :x, :y, :z, :scale
attr_accessor :visible, :renderable, :backface_culling attr_accessor :visible, :renderable, :backface_culling
@@ -110,73 +111,159 @@ class IMICFPS
end end
def render_bounding_box(box, color = @debug_color) def render_bounding_box(box, color = @debug_color)
# TODO: Minimize number of calls in here
box = normalize_bounding_box(box) box = normalize_bounding_box(box)
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
_normals = [
0,1,0,
0,1,0,
0,1,0,
0,1,0,
0,1,0,
0,1,0,
0,-1,0,
0,-1,0,
0,-1,0,
0,-1,0,
0,-1,0,
0,-1,0,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
1,0,0,
1,0,0,
1,0,0,
1,0,0,
1,0,0,
1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0
].pack("f*")
_colors = [
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue,
color.red, color.green, color.blue
].pack("f*")
_vertices = [
box.min_x, box.max_y, box.max_z,
box.min_x, box.max_y, box.min_z,
box.max_x, box.max_y, box.min_z,
box.min_x, box.max_y, box.max_z,
box.max_x, box.max_y, box.max_z,
box.max_x, box.max_y, box.min_z,
box.max_x, box.min_y, box.min_z,
box.max_x, box.min_y, box.max_z,
box.min_x, box.min_y, box.max_z,
box.max_x, box.min_y, box.min_z,
box.min_x, box.min_y, box.min_z,
box.min_x, box.min_y, box.max_z,
box.min_x, box.max_y, box.max_z,
box.min_x, box.max_y, box.min_z,
box.min_x, box.min_y, box.min_z,
box.min_x, box.min_y, box.max_z,
box.min_x, box.min_y, box.min_z,
box.min_x, box.max_y, box.max_z,
box.max_x, box.max_y, box.max_z,
box.max_x, box.max_y, box.min_z,
box.max_x, box.min_y, box.min_z,
box.max_x, box.min_y, box.max_z,
box.max_x, box.min_y, box.min_z,
box.max_x, box.max_y, box.max_z,
box.min_x, box.max_y, box.max_z,
box.max_x, box.max_y, box.max_z,
box.max_x, box.min_y, box.max_z,
box.min_x, box.max_y, box.max_z,
box.max_x, box.min_y, box.max_z,
box.min_x, box.min_y, box.max_z,
box.max_x, box.min_y, box.min_z,
box.min_x, box.min_y, box.min_z,
box.min_x, box.max_y, box.min_z,
box.max_x, box.min_y, box.min_z,
box.min_x, box.max_y, box.min_z,
box.max_x, box.max_y, box.min_z
]
_vertices_size = _vertices.size
_vertices = _vertices.pack("f*")
glVertexPointer(3, GL_FLOAT, 0, _vertices)
glColorPointer(3, GL_FLOAT, 0, _colors)
glNormalPointer(GL_FLOAT, 0, _normals)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glBegin(GL_TRIANGLES) glDisable(GL_LIGHTING)
# TOP glDrawArrays(GL_TRIANGLES, 0, _vertices_size/3)
glNormal3f(0,1,0) glEnable(GL_LIGHTING)
glColor3f(color.red, color.green, color.blue)
glVertex3f(box.min_x, box.max_y, box.max_z)
glVertex3f(box.min_x, box.max_y, box.min_z)
glVertex3f(box.max_x, box.max_y, box.min_z)
glVertex3f(box.min_x, box.max_y, box.max_z)
glVertex3f(box.max_x, box.max_y, box.max_z)
glVertex3f(box.max_x, box.max_y, box.min_z)
# BOTTOM
glNormal3f(0,-1,0)
glVertex3f(box.max_x, box.min_y, box.min_z)
glVertex3f(box.max_x, box.min_y, box.max_z)
glVertex3f(box.min_x, box.min_y, box.max_z)
glVertex3f(box.max_x, box.min_y, box.min_z)
glVertex3f(box.min_x, box.min_y, box.min_z)
glVertex3f(box.min_x, box.min_y, box.max_z)
# RIGHT SIDE
glNormal3f(0,0,1)
glVertex3f(box.min_x, box.max_y, box.max_z)
glVertex3f(box.min_x, box.max_y, box.min_z)
glVertex3f(box.min_x, box.min_y, box.min_z)
glVertex3f(box.min_x, box.min_y, box.max_z)
glVertex3f(box.min_x, box.min_y, box.min_z)
glVertex3f(box.min_x, box.max_y, box.max_z)
# LEFT SIDE
glNormal3f(1,0,0)
glVertex3f(box.max_x, box.max_y, box.max_z)
glVertex3f(box.max_x, box.max_y, box.min_z)
glVertex3f(box.max_x, box.min_y, box.min_z)
glVertex3f(box.max_x, box.min_y, box.max_z)
glVertex3f(box.max_x, box.min_y, box.min_z)
glVertex3f(box.max_x, box.max_y, box.max_z)
# FRONT
glNormal3f(-1,0,0)
glVertex3f(box.min_x, box.max_y, box.max_z)
glVertex3f(box.max_x, box.max_y, box.max_z)
glVertex3f(box.max_x, box.min_y, box.max_z)
glVertex3f(box.min_x, box.max_y, box.max_z)
glVertex3f(box.max_x, box.min_y, box.max_z)
glVertex3f(box.min_x, box.min_y, box.max_z)
# BACK
glNormal3f(-1,0,0)
glVertex3f(box.max_x, box.min_y, box.min_z)
glVertex3f(box.min_x, box.min_y, box.min_z)
glVertex3f(box.min_x, box.max_y, box.min_z)
glVertex3f(box.max_x, box.min_y, box.min_z)
glVertex3f(box.min_x, box.max_y, box.min_z)
glVertex3f(box.max_x, box.max_y, box.min_z)
glEnd
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
end end
def handleGlError def handleGlError

View File

@@ -45,7 +45,6 @@ class MultiLineText
def y=(int) def y=(int)
@y = int @y = int
puts "Hi, #{int}"
@texts.each_with_index {|t, i| t.y=int+(i*t.size)} @texts.each_with_index {|t, i| t.y=int+(i*t.size)}
end end

View File

@@ -48,7 +48,7 @@ class IMICFPS
glEnable(GL_BLEND) glEnable(GL_BLEND)
glBindTexture(GL_TEXTURE_2D, @name_texture_id) glBindTexture(GL_TEXTURE_2D, @name_texture_id)
glBegin(GL_TRIANGLES) glBegin(GL_TRIANGLES)
# glColor3f(0.0,0.0,0.0) glColor3f(1.0,1.0,1.0)
# TOP LEFT # TOP LEFT
glTexCoord2f(0, 0) glTexCoord2f(0, 0)
glVertex3f(_x-_width,_y+_height,@z) glVertex3f(_x-_width,_y+_height,@z)
@@ -81,15 +81,15 @@ class IMICFPS
def draw def draw
if !@first_person_view if !@first_person_view
draw_nameplate
super super
draw_nameplate
end end
end end
def update def update
super super
@floor = @terrain.height_at(self) @floor = @terrain.height_at(self, 4.5)
relative_speed = @speed relative_speed = @speed
if button_down?(Gosu::KbLeftControl) if button_down?(Gosu::KbLeftControl)
@@ -141,7 +141,7 @@ class IMICFPS
end end
if @jumping && !@falling if @jumping && !@falling
if button_down?(Gosu::KbSpace) if button_down?(Gosu::KbSpace)
@y_velocity+=(2*5)*delta_time @y_velocity+=(2*15)*delta_time
@falling = true if @y_velocity >= 2 @falling = true if @y_velocity >= 2
end end
end end

View File

@@ -22,15 +22,15 @@ class IMICFPS
p model.faces.first p model.faces.first
end end
def height_at(vertex) def height_at(vertex, max_distance = Float::INFINITY)
if vert = find_nearest_vertex(vertex) if vert = find_nearest_vertex(vertex, max_distance)
return vert.y return vert.y
else else
-1 -1
end end
end end
def find_nearest_vertex(vertex) def find_nearest_vertex(vertex, max_distance)
nearest = nil nearest = nil
smaller_list = [] smaller_list = []
smaller_list << @nearest_vertex_lookup.dig(vertex.x.round-1, vertex.y.round-1) smaller_list << @nearest_vertex_lookup.dig(vertex.x.round-1, vertex.y.round-1)
@@ -41,12 +41,12 @@ class IMICFPS
smaller_list.each do |vert| smaller_list.each do |vert|
next if vert.nil? next if vert.nil?
if nearest if nearest
if distance(vert, vertex) < distance(vert, nearest) if distance(vert, vertex) < distance(vert, nearest) && distance(vert, vertex) <= max_distance
nearest = vert nearest = vert
end end
end end
nearest = vert unless nearest nearest = vert unless nearest && distance(vert, vertex) > max_distance
end end
return nearest return nearest

View File

@@ -31,7 +31,7 @@ class IMICFPS
# Tree.new(x: 1, z: -5, terrain: @terrain) # Tree.new(x: 1, z: -5, terrain: @terrain)
# Tree.new(x: 5, z: 5, terrain: @terrain) # Tree.new(x: 5, z: 5, terrain: @terrain)
# TestObject.new(scale: 1) # TestObject.new(scale: 1)
p ObjectManager.objects.map {|o| o.name} # p ObjectManager.objects.map {|o| o.name}
# 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)
@@ -51,16 +51,23 @@ class IMICFPS
Light.new(x: 0, y: 100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1)) Light.new(x: 0, y: 100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1))
end end
def draw def glError?
e = glGetError() e = glGetError()
if e != GL_NO_ERROR if e != GL_NO_ERROR
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n" $stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
exit exit
end end
end
def draw
glError?
gl do gl do
glError?
glClearColor(0,0.2,0.5,1) # skyish blue glClearColor(0,0.2,0.5,1) # skyish blue
glError?
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
glError?
LightManager.lights.each(&:draw) LightManager.lights.each(&:draw)
@@ -82,6 +89,7 @@ class IMICFPS
def update def update
@last_frame_time = Gosu.milliseconds @last_frame_time = Gosu.milliseconds
begin
string = <<-eos string = <<-eos
OpenGL Vendor: #{glGetString(GL_VENDOR)} OpenGL Vendor: #{glGetString(GL_VENDOR)}
OpenGL Renderer: #{glGetString(GL_RENDERER)} OpenGL Renderer: #{glGetString(GL_RENDERER)}
@@ -99,6 +107,22 @@ Last Frame: #{delta_time*1000.0}ms (#{Gosu.fps} fps)
Draw Skydome: #{@draw_skydome} Draw Skydome: #{@draw_skydome}
Debug mode: <c=992200>#{$debug}</c> Debug mode: <c=992200>#{$debug}</c>
eos eos
rescue ArgumentError
string = <<-eos
Unable to call glGetString!
Camera pitch: #{@camera.pitch.round(2)} Yaw: #{@camera.yaw.round(2)} Roll #{@camera.roll.round(2)}
Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)}
#{if @camera.game_object then "Actor X:#{@camera.game_object.x.round(2)} Y:#{@camera.game_object.y.round(2)} Z:#{@camera.game_object.z.round(2)}";end}
Field Of View: #{@camera.field_of_view}
Mouse Sesitivity: #{@camera.mouse_sensitivity}
Faces: #{@number_of_faces}
Last Frame: #{delta_time*1000.0}ms (#{Gosu.fps} fps)
Draw Skydome: #{@draw_skydome}
Debug mode: <c=992200>#{$debug}</c>
eos
end
@text.text = string @text.text = string
# ObjectManager.objects.each do |object| # ObjectManager.objects.each do |object|