Moved OpenGL renderering code into its own class for future optimizations

This commit is contained in:
2018-09-20 09:07:39 -05:00
parent aa4a905bd8
commit 35b5ee948a
9 changed files with 294 additions and 241 deletions

View File

@@ -60,26 +60,8 @@ class IMICFPS
def setup
end
# Not advisable to put OpenGL code here, instead put it in Renderer.
def draw
handleGlError
glEnable(GL_NORMALIZE)
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
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)
handleGlError
glPopMatrix
handleGlError
end
def update
@@ -117,162 +99,6 @@ class IMICFPS
return temp
end
def render_bounding_box(box, color = @debug_color)
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)
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
e = glGetError()
if e != GL_NO_ERROR

View File

@@ -38,8 +38,7 @@ class IMICFPS
def draw_nameplate
_height = (@name_image.height/@devisor)
_width = (@name_image.width/@devisor)/2
_x = @x
_y = normalize_bounding_box(model.bounding_box).max_y+0.05
_y = 2#normalize_bounding_box(model.bounding_box).max_y+0.05
glPushMatrix
glDisable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
@@ -51,27 +50,27 @@ class IMICFPS
glColor3f(1.0,1.0,1.0)
# TOP LEFT
glTexCoord2f(0, 0)
glVertex3f(_x-_width,_y+_height,@z)
glVertex3f(0-_width,_y+_height,0)
# TOP RIGHT
glTexCoord2f(1, 0)
glVertex3f(_x+_width, _y+_height,@z)
glVertex3f(0+_width, _y+_height,0)
# BOTTOM LEFT
glTexCoord2f(0, 1)
glVertex3f(_x-_width,_y,@z)
glVertex3f(0-_width,_y,0)
# BOTTOM LEFT
glTexCoord2f(0, 1)
glVertex3f(_x-_width,_y,@z)
glVertex3f(0-_width,_y,0)
# BOTTOM RIGHT
glTexCoord2f(1, 1)
glVertex3f(_x+_width, _y,@z)
glVertex3f(0+_width, _y,0)
# TOP RIGHT
glTexCoord2f(1, 0)
glVertex3f(_x+_width,_y+_height,@z)
glVertex3f(0+_width,_y+_height,0)
glEnd
# glDisable(GL_BLEND)
glDisable(GL_TEXTURE_2D)

View File

@@ -2,6 +2,7 @@ class IMICFPS
class Terrain < GameObject
def setup
bind_model(ModelLoader.new(type: :obj, file_path: "objects/randomish_terrain.obj", game_object: self))
# bind_model(ModelLoader.new(type: :obj, file_path: "/home/cyberarm/Documents/blends/untitled.obj", game_object: self))
self.scale = 1
@nearest_vertex_lookup = {}

View File

@@ -3,9 +3,11 @@ class IMICFPS
def setup
bind_model(ModelLoader.new(type: :obj, file_path: "objects/tree.obj", game_object: self))
vert = @terrain.find_nearest_vertex(self, 4.5)
self.x = vert.x
self.y = vert.y
self.z = vert.z
if vert
self.x = vert.x
self.y = vert.y
self.z = vert.z
end
end
# def update