Renamed lighting shader to default, shoehorned in glsl 3.30 support for Intel on Linux, removed duplicate handleGlError methods, added OpenGL and GLU to Object namespace, removed redundant includes for OpenGL and GLU, VBO and VAO now render (all be it incorrectly)

This commit is contained in:
2019-08-11 14:53:15 -05:00
parent 73478b7e37
commit f11b091fe0
17 changed files with 142 additions and 89 deletions

View File

@@ -21,6 +21,12 @@ when :OPENGL_PLATFORM_MACOSX
OpenGL.load_lib("libGL.dylib", "/System/Library/Frameworks/OpenGL.framework/Libraries") OpenGL.load_lib("libGL.dylib", "/System/Library/Frameworks/OpenGL.framework/Libraries")
GLU.load_lib("libGLU.dylib", "/System/Library/Frameworks/OpenGL.framework/Libraries") GLU.load_lib("libGLU.dylib", "/System/Library/Frameworks/OpenGL.framework/Libraries")
when :OPENGL_PLATFORM_LINUX when :OPENGL_PLATFORM_LINUX
# Black magic to get GLSL 3.30 support on older Intel hardware
if `glxinfo | egrep "OpenGL vendor|OpenGL renderer"`.include?("Intel")
ENV["MESA_GL_VERSION_OVERRIDE"] = "3.3"
ENV["MESA_GLSL_VERSION_OVERRIDE"] = "330"
end
gl_library_path = nil gl_library_path = nil
if File.exist?("/usr/lib/x86_64-linux-gnu/libGL.so") # Ubuntu (Debian) if File.exist?("/usr/lib/x86_64-linux-gnu/libGL.so") # Ubuntu (Debian)
@@ -63,6 +69,9 @@ if RUBY_VERSION < "2.5.0"
end end
include CyberarmEngine include CyberarmEngine
include OpenGL
include GLU
require_relative "lib/version" require_relative "lib/version"
require_relative "lib/constants" require_relative "lib/constants"
require_relative "lib/common_methods" require_relative "lib/common_methods"
@@ -111,9 +120,6 @@ require_relative "lib/wavefront/model"
require_relative "lib/window" require_relative "lib/window"
MODEL_METER_SCALE = 1.0 # Objects exported from blender using the default or meter object scale will be close to 1 GL unit
if ARGV.join.include?("--profile") if ARGV.join.include?("--profile")
begin begin
require "ruby-prof" require "ruby-prof"

View File

@@ -42,5 +42,14 @@ class IMICFPS
def fill(color = Gosu::Color::WHITE) def fill(color = Gosu::Color::WHITE)
draw_rect(0, 0, window.width, window.height, color) draw_rect(0, 0, window.width, window.height, color)
end end
def handleGlError
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error detected by handler at: #{caller[0]}"
$stderr.puts " #{gluErrorString(e)} (#{e})\n"
exit
end
end
end end
end end

View File

@@ -5,4 +5,7 @@ class IMICFPS
Point = Struct.new(:x, :y) Point = Struct.new(:x, :y)
Color = Struct.new(:red, :green, :blue, :alpha) Color = Struct.new(:red, :green, :blue, :alpha)
Face = Struct.new(:vertices, :uvs, :normals, :colors, :material, :smoothing) Face = Struct.new(:vertices, :uvs, :normals, :colors, :material, :smoothing)
# Objects exported from blender using the default or meter object scale will be close to 1 GL unit
MODEL_METER_SCALE = 1.0
end end

View File

@@ -1,8 +1,6 @@
class IMICFPS class IMICFPS
class Camera class Camera
include CommonMethods include CommonMethods
include OpenGL
include GLU
attr_accessor :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity attr_accessor :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity
attr_reader :entity, :position attr_reader :entity, :position

View File

@@ -3,8 +3,6 @@ class IMICFPS
# A game object is any renderable thing # A game object is any renderable thing
class Entity class Entity
include OpenGL
include GLU
include CommonMethods include CommonMethods
attr_accessor :scale, :visible, :renderable, :backface_culling attr_accessor :scale, :visible, :renderable, :backface_culling
@@ -104,13 +102,5 @@ class IMICFPS
def normalize_bounding_box def normalize_bounding_box
@bound_model.model.bounding_box.normalize(self) @bound_model.model.bounding_box.normalize(self)
end end
def handleGlError
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
exit
end
end
end end
end end

View File

@@ -1,6 +1,5 @@
class IMICFPS class IMICFPS
class Light class Light
include OpenGL
attr_reader :ambient, :diffuse, :specular, :position, :light_id attr_reader :ambient, :diffuse, :specular, :position, :light_id
attr_accessor :x, :y, :z, :intensity attr_accessor :x, :y, :z, :intensity
def initialize(x:,y:,z:, game_state:, def initialize(x:,y:,z:, game_state:,

View File

@@ -1,8 +1,5 @@
class IMICFPS class IMICFPS
class BoundingBoxRenderer class BoundingBoxRenderer
include OpenGL
include GLU
attr_reader :bounding_boxes, :vertex_count attr_reader :bounding_boxes, :vertex_count
def initialize(game_state:) def initialize(game_state:)
@game_state = game_state @game_state = game_state
@@ -11,14 +8,6 @@ class IMICFPS
@vertex_count = 0 @vertex_count = 0
end end
def handleGlError
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
exit
end
end
def create_bounding_box(object, box, color = nil, mesh_object_id) def create_bounding_box(object, box, color = nil, mesh_object_id)
color ||= object.debug_color color ||= object.debug_color

View File

@@ -1,19 +1,6 @@
class IMICFPS class IMICFPS
class OpenGLRenderer class OpenGLRenderer
include CommonMethods include CommonMethods
include OpenGL
include GLU
def initialize
end
def handleGlError
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
exit
end
end
def draw_object(object) def draw_object(object)
handleGlError handleGlError
@@ -28,12 +15,12 @@ class IMICFPS
handleGlError handleGlError
if Shader.available?("lighting") if Shader.available?("default")
Shader.use("lighting") do |shader| Shader.use("default") do |shader|
glUniform3f(shader.attribute_location("SunLight"), 1.0, 1.0, 1.0) glUniform3f(shader.attribute_location("worldPosition"), object.position.x, object.position.y, object.position.z)
handleGlError handleGlError
draw_mesh(object.model) draw_model(object.model)
object.draw object.draw
end end
else else
@@ -47,6 +34,28 @@ class IMICFPS
handleGlError handleGlError
end end
def draw_model(model)
glBindVertexArray(model.vertex_array_id)
glBindBuffer(GL_ARRAY_BUFFER, model.vertices_buffer_id)
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
glEnableVertexAttribArray(2)
glEnableVertexAttribArray(3)
glEnableVertexAttribArray(4)
glDrawArrays(GL_TRIANGLES, 0, model.vertices.count)
window.number_of_vertices+=model.vertices.size
glDisableVertexAttribArray(4)
glDisableVertexAttribArray(3)
glDisableVertexAttribArray(2)
glDisableVertexAttribArray(1)
glDisableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
end
def draw_mesh(model) def draw_mesh(model)
model.objects.each_with_index do |o, i| model.objects.each_with_index do |o, i|
glEnable(GL_CULL_FACE) if model.entity.backface_culling glEnable(GL_CULL_FACE) if model.entity.backface_culling

View File

@@ -1,8 +1,6 @@
class IMICFPS class IMICFPS
class Renderer class Renderer
include CommonMethods include CommonMethods
include OpenGL
include GLU
attr_reader :opengl_renderer, :bounding_box_renderer attr_reader :opengl_renderer, :bounding_box_renderer
@@ -28,14 +26,6 @@ class IMICFPS
# @bounding_box_renderer.bounding_boxes.clear # @bounding_box_renderer.bounding_boxes.clear
end end
def handleGlError
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
exit
end
end
def finalize # cleanup def finalize # cleanup
end end
end end

View File

@@ -1,8 +1,5 @@
class IMICFPS class IMICFPS
class Game < GameState class Game < GameState
include OpenGL
include GLU
def setup def setup
@collision_manager = CollisionManager.new(game_state: self) @collision_manager = CollisionManager.new(game_state: self)
@renderer = Renderer.new(game_state: self) @renderer = Renderer.new(game_state: self)

View File

@@ -15,7 +15,7 @@ class IMICFPS
add_asset(:model, "base", "tree") add_asset(:model, "base", "tree")
add_asset(:model, "base", "biped") add_asset(:model, "base", "biped")
add_asset(:shader, nil, "lighting") add_asset(:shader, nil, "default")
@act = false @act = false
@cycled = false @cycled = false

View File

@@ -1,7 +1,6 @@
class IMICFPS class IMICFPS
class Wavefront class Wavefront
class Material class Material
include OpenGL
attr_accessor :name, :ambient, :diffuse, :specular attr_accessor :name, :ambient, :diffuse, :specular
attr_reader :texture_id attr_reader :texture_id
def initialize(name) def initialize(name)

View File

@@ -5,7 +5,6 @@ require_relative "material"
class IMICFPS class IMICFPS
class Wavefront class Wavefront
class Model class Model
include OpenGL
include CommonMethods include CommonMethods
include Parser include Parser
@@ -14,8 +13,9 @@ class IMICFPS
attr_accessor :scale, :entity attr_accessor :scale, :entity
attr_reader :position, :bounding_box, :textured_material attr_reader :position, :bounding_box, :textured_material
attr_reader :vertices_buffer attr_reader :vertices_buffer_id
attr_reader :vertices_buffer_data attr_reader :vertices_buffer_data
attr_reader :vertices_buffer_size
attr_reader :vertex_array_id attr_reader :vertex_array_id
attr_reader :aabb_tree attr_reader :aabb_tree
@@ -45,8 +45,8 @@ class IMICFPS
puts "#{@file_path.split('/').last} took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds to parse" if $debug.get(:stats) puts "#{@file_path.split('/').last} took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds to parse" if $debug.get(:stats)
allocate_gl_objects allocate_gl_objects
populate_buffers populate_vertex_buffer
# populate_arrays configure_vao
@objects.each {|o| @vertex_count+=o.vertices.size} @objects.each {|o| @vertex_count+=o.vertices.size}
@objects.each_with_index do |o, i| @objects.each_with_index do |o, i|
@@ -61,7 +61,9 @@ class IMICFPS
end end
end end
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
build_collision_tree build_collision_tree
puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds" if $debug.get(:stats)
end end
def allocate_gl_objects def allocate_gl_objects
@@ -73,15 +75,17 @@ class IMICFPS
@vertex_array_id = buffer.unpack('L2').first @vertex_array_id = buffer.unpack('L2').first
# Allocate buffers for future use # Allocate buffers for future use
@vertices_buffer = nil @vertices_buffer_id = nil
buffer = " " * 4 buffer = " " * 4
glGenBuffers(1, buffer) glGenBuffers(1, buffer)
@vertices_buffer = buffer.unpack('L2').first @vertices_buffer_id = buffer.unpack('L2').first
end end
def populate_buffers def populate_vertex_buffer
@vertices_buffer_size = 0
@vertices_buffer_data = [] @vertices_buffer_data = []
model_has_texture = @materials.any? { |id, mat| mat.texture_id != nil }
verts = [] verts = []
colors = [] colors = []
@@ -93,8 +97,11 @@ class IMICFPS
verts << face.vertices.map { |vert| [vert.x, vert.y, vert.z] } verts << face.vertices.map { |vert| [vert.x, vert.y, vert.z] }
colors << face.colors.map { |vert| [vert.x, vert.y, vert.z] } colors << face.colors.map { |vert| [vert.x, vert.y, vert.z] }
norms << face.normals.map { |vert| [vert.x, vert.y, vert.z, vert.weight] } norms << face.normals.map { |vert| [vert.x, vert.y, vert.z, vert.weight] }
uvs << face.uvs.map { |vert| [vert.x, vert.y, vert.z] } if face.material.texture_id
tex_ids << face.material.texture_id if face.material.texture_id if model_has_texture
uvs << face.uvs.map { |vert| [vert.x, vert.y, vert.z] }
tex_ids << face.material.texture_id ? face.material.texture_id : -1
end
end end
verts.each_with_index do |vert, i| verts.each_with_index do |vert, i|
@@ -105,18 +112,57 @@ class IMICFPS
@vertices_buffer_data << tex_ids[i] if tex_ids.size > 0 @vertices_buffer_data << tex_ids[i] if tex_ids.size > 0
end end
data_size = 0
data_size += Fiddle::SIZEOF_FLOAT * 3 * verts.size
data_size += Fiddle::SIZEOF_FLOAT * 3 * colors.size
data_size += Fiddle::SIZEOF_FLOAT * 4 * norms.size
data_size += Fiddle::SIZEOF_FLOAT * 3 * uvs.size
data_size += Fiddle::SIZEOF_INT * 1 * tex_ids.size
@vertices_buffer_size = data_size
data = @vertices_buffer_data.flatten.pack("f*") data = @vertices_buffer_data.flatten.pack("f*")
glBindBuffer(GL_ARRAY_BUFFER, @vertices_buffer) glBindBuffer(GL_ARRAY_BUFFER, @vertices_buffer_id)
glBufferData(GL_ARRAY_BUFFER, Fiddle::SIZEOF_FLOAT * @vertices_buffer_data.size, data, GL_STATIC_DRAW) glBufferData(GL_ARRAY_BUFFER, @vertices_buffer_size, data, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0)
end end
def populate_arrays def configure_vao
glBindBuffer(GL_ARRAY_BUFFER, @vertices_buffer_id)
glBindVertexArray(@vertex_array_id) glBindVertexArray(@vertex_array_id)
glBindBuffer(GL_ARRAY_BUFFER, @vertices_buffer)
glBindVertexArray(0) glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
glEnableVertexAttribArray(2)
glEnableVertexAttribArray(3)
glEnableVertexAttribArray(4)
# index, size, type, normalized, stride, pointer
# vertices (positions)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, Fiddle::SIZEOF_FLOAT * 3, nil)
handleGlError
# colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, Fiddle::SIZEOF_FLOAT * (3 + 3), nil)
handleGlError
# normals
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, Fiddle::SIZEOF_FLOAT * (4 + 3 + 3), nil)
handleGlError
# uvs
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, Fiddle::SIZEOF_FLOAT * (3 + 4 + 3 + 3), nil)
handleGlError
# texture ids
glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, Fiddle::SIZEOF_FLOAT + (Fiddle::SIZEOF_FLOAT * (3 + 4 + 3 + 3)), nil)
handleGlError
glDisableVertexAttribArray(4)
glDisableVertexAttribArray(3)
glDisableVertexAttribArray(2)
glDisableVertexAttribArray(1)
glDisableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
end end
def build_collision_tree def build_collision_tree

View File

@@ -0,0 +1,10 @@
# version 330
in vec3 outColor;
in vec4 outNormal;
in vec3 outUV;
in float outTextureID;
void main() {
gl_FragColor = vec4(outColor, 1.0);
}

View File

@@ -1,7 +0,0 @@
# version 150
out vec4 frag_colour;
void main() {
frag_colour = vec4(0.5, 0.0, 0.5, 1.0);
}

View File

@@ -0,0 +1,24 @@
# version 330
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec4 inNormal;
layout(location = 3) in vec3 inUV;
layout(location = 4) in float inTextureID;
out vec3 outColor;
out vec4 outNormal;
out vec3 outUV;
out float outTextureID;
uniform vec3 worldPosition;
void main() {
// projection * view * model *
outColor = inColor;
outNormal= inNormal;
outUV = inUV;
outTextureID = inTextureID;
gl_Position = vec4(worldPosition + inPosition, 1.0);
}

View File

@@ -1,9 +0,0 @@
# version 330
layout(location = 0) in vec3 vert;
uniform vec3 position;
void main() {
// projection * view * model *
gl_Position = vec4(vert+position, 1.0);
}