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

@@ -74,9 +74,16 @@ class IMICFPS
glRotatef(@render_pitch,1,0,0)
glRotatef(@yaw,0,1,0)
glTranslatef(-@x, -@y, -@z)
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
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
def update

View File

@@ -4,6 +4,7 @@ class IMICFPS
# A game object is any renderable thing
class GameObject
include OpenGL
include GLU
include CommonMethods
attr_accessor :x, :y, :z, :scale
attr_accessor :visible, :renderable, :backface_culling
@@ -110,73 +111,159 @@ class IMICFPS
end
def render_bounding_box(box, color = @debug_color)
# TODO: Minimize number of calls in here
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)
glBegin(GL_TRIANGLES)
# TOP
glNormal3f(0,1,0)
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
glDisable(GL_LIGHTING)
glDrawArrays(GL_TRIANGLES, 0, _vertices_size/3)
glEnable(GL_LIGHTING)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
end
def handleGlError

View File

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

View File

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

View File

@@ -22,15 +22,15 @@ class IMICFPS
p model.faces.first
end
def height_at(vertex)
if vert = find_nearest_vertex(vertex)
def height_at(vertex, max_distance = Float::INFINITY)
if vert = find_nearest_vertex(vertex, max_distance)
return vert.y
else
-1
end
end
def find_nearest_vertex(vertex)
def find_nearest_vertex(vertex, max_distance)
nearest = nil
smaller_list = []
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|
next if vert.nil?
if nearest
if distance(vert, vertex) < distance(vert, nearest)
if distance(vert, vertex) < distance(vert, nearest) && distance(vert, vertex) <= max_distance
nearest = vert
end
end
nearest = vert unless nearest
nearest = vert unless nearest && distance(vert, vertex) > max_distance
end
return nearest