Patched bounding box renderer to work again- in immediate mode renderer, more work on lighting rework for modern renderer

This commit is contained in:
2020-05-14 09:52:51 -05:00
parent dae950c72a
commit fbdea30015
6 changed files with 109 additions and 69 deletions

View File

@@ -1,29 +1,43 @@
class IMICFPS
class BoundingBoxRenderer
attr_reader :bounding_boxes, :vertex_count
def initialize(map:)
@map = map
def initialize
@bounding_boxes = {}
@vertex_count = 0
end
def create_bounding_box(object, box, color = nil, mesh_object_id)
def render(entities)
entities.each do |entity|
create_bounding_box(entity,color = nil)
draw_bounding_boxes
end
color ||= object.debug_color
(@bounding_boxes.keys - entities.map { |e| e.object_id }).each do |key|
@bounding_boxes.delete(key)
end
end
if @bounding_boxes[mesh_object_id]
if @bounding_boxes[mesh_object_id][:color] != color
@bounding_boxes[mesh_object_id][:colors] = mesh_colors(color).pack("f*")
@bounding_boxes[mesh_object_id][:color] = color
def create_bounding_box(entity, color = nil)
color ||= entity.debug_color
entity_id = entity.object_id
if @bounding_boxes[entity_id]
if @bounding_boxes[entity_id][:color] != color
@bounding_boxes[entity_id][:colors] = mesh_colors(color).pack("f*")
@bounding_boxes[entity_id][:color] = color
return
else
return
end
end
@bounding_boxes[mesh_object_id] = {object: object, box: box, color: color, objects: []}
box = object.normalize_bounding_box
@bounding_boxes[entity_id] = {
entity: entity,
color: color,
objects: []
}
box = entity.normalize_bounding_box
normals = mesh_normals
colors = mesh_colors(color)
@@ -31,14 +45,14 @@ class IMICFPS
@vertex_count+=vertices.size
@bounding_boxes[mesh_object_id][:vertices_size] = vertices.size
@bounding_boxes[mesh_object_id][:vertices] = vertices.pack("f*")
@bounding_boxes[mesh_object_id][:normals] = normals.pack("f*")
@bounding_boxes[mesh_object_id][:colors] = colors.pack("f*")
@bounding_boxes[entity_id][:vertices_size] = vertices.size
@bounding_boxes[entity_id][:vertices] = vertices.pack("f*")
@bounding_boxes[entity_id][:normals] = normals.pack("f*")
@bounding_boxes[entity_id][:colors] = colors.pack("f*")
object.model.objects.each do |mesh|
entity.model.objects.each do |mesh|
data = {}
box = mesh.bounding_box.normalize(object)
box = mesh.bounding_box.normalize(entity)
normals = mesh_normals
colors = mesh_colors(mesh.debug_color)
@@ -51,7 +65,7 @@ class IMICFPS
data[:normals] = normals.pack("f*")
data[:colors] = colors.pack("f*")
@bounding_boxes[mesh_object_id][:objects] << data
@bounding_boxes[entity_id][:objects] << data
end
end
@@ -199,19 +213,15 @@ class IMICFPS
@bounding_boxes.each do |key, bounding_box|
glPushMatrix
glTranslatef(bounding_box[:object].position.x, bounding_box[:object].position.y, bounding_box[:object].position.z)
glTranslatef(
bounding_box[:entity].position.x,
bounding_box[:entity].position.y,
bounding_box[:entity].position.z
)
draw_bounding_box(bounding_box)
@bounding_boxes[key][:objects].each {|o| draw_bounding_box(o)}
glPopMatrix
found = @map.entities.detect { |o| o == bounding_box[:object] }
unless found
@vertex_count -= @bounding_boxes[key][:vertices_size]
@bounding_boxes[key][:objects].each {|o| @vertex_count -= o[:vertices_size]}
@bounding_boxes.delete(key)
end
end
end
@@ -226,7 +236,7 @@ class IMICFPS
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glDisable(GL_LIGHTING)
glDrawArrays(GL_TRIANGLES, 0, bounding_box[:vertices_size]/3)
glDrawArrays(GL_TRIANGLES, 0, bounding_box[:vertices_size] / 3)
glEnable(GL_LIGHTING)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

View File

@@ -145,6 +145,10 @@ class IMICFPS
glReadBuffer(GL_COLOR_ATTACHMENT0 + @textures.keys.index(buffer))
end
def set_read_buffer_depth
glReadBuffer(GL_DEPTH_ATTACHMENT)
end
def unbind_framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0)
end

View File

@@ -16,7 +16,7 @@ class IMICFPS
end
def render(camera, lights, entities)
if window.config.get(:debug_options, :use_shaders) && Shader.available?("g_buffer") && Shader.available?("deferred_lighting")
if window.config.get(:debug_options, :use_shaders) && Shader.available?("g_buffer") && Shader.available?("lighting")
@g_buffer.bind_for_writing
gl_error?
@@ -46,9 +46,14 @@ class IMICFPS
@g_buffer.bind_for_reading
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
# lighting(lights)
lighting(lights)
gl_error?
post_processing
render_framebuffer
gl_error?
# render_framebuffer
gl_error?
@g_buffer.unbind_framebuffer
gl_error?
@@ -105,39 +110,44 @@ class IMICFPS
end
def lighting(lights)
Shader.use("deferred_lighting") do |shader|
Shader.use("lighting") do |shader|
glBindVertexArray(@g_buffer.screen_vbo)
glDisable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
shader.uniform_integer("diffuse", 0)
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:position))
shader.uniform_integer("position", 1)
glActiveTexture(GL_TEXTURE2)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:texcoord))
shader.uniform_integer("texcoord", 2)
glActiveTexture(GL_TEXTURE3)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:normal))
shader.uniform_integer("normal", 3)
glActiveTexture(GL_TEXTURE4)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
shader.uniform_integer("depth", 4)
lights.each_with_index do |light, i|
shader.uniform_float("light.type", light.type);
shader.uniform_vec3("light.direction", light.direction)
shader.uniform_vec3("light.position", light.position)
shader.uniform_vec3("light.ambient", light.ambient)
shader.uniform_vec3("light.diffuse", light.diffuse)
shader.uniform_vec3("light.specular", light.specular)
glBindVertexArray(@g_buffer.screen_vbo)
glDisable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:position))
glActiveTexture(GL_TEXTURE2)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:texcoord))
glActiveTexture(GL_TEXTURE3)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:normal))
glActiveTexture(GL_TEXTURE4)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
shader.uniform_integer("light[0].type", light.type);
shader.uniform_vec3("light[0].direction", light.direction)
shader.uniform_vec3("light[0].position", light.position)
shader.uniform_vec3("light[0].diffuse", light.diffuse)
shader.uniform_vec3("light[0].ambient", light.ambient)
shader.uniform_vec3("light[0].specular", light.specular)
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
glBindVertexArray(0)
end
glBindVertexArray(0)
end
end
@@ -145,8 +155,8 @@ class IMICFPS
end
def render_framebuffer
if Shader.available?("deferred_lighting")
Shader.use("deferred_lighting") do |shader|
if Shader.available?("lighting")
Shader.use("lighting") do |shader|
glBindVertexArray(@g_buffer.screen_vbo)
glDisable(GL_DEPTH_TEST)
@@ -154,6 +164,7 @@ class IMICFPS
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
shader.uniform_integer("diffuse_texture", 0)
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)

View File

@@ -5,12 +5,12 @@ class IMICFPS
attr_reader :opengl_renderer, :bounding_box_renderer
def initialize
# @bounding_box_renderer = BoundingBoxRenderer.new(map: map)
@bounding_box_renderer = BoundingBoxRenderer.new
@opengl_renderer = OpenGLRenderer.new
end
def preload_default_shaders
shaders = ["g_buffer", "deferred_lighting"]
shaders = ["g_buffer", "lighting"]
shaders.each do |shader|
Shader.new(
name: shader,
@@ -26,6 +26,7 @@ class IMICFPS
glEnable(GL_DEPTH_TEST)
@opengl_renderer.render(camera, lights, entities)
@bounding_box_renderer.render(entities) if window.config.get(:debug_options, :boundingboxes)
end
def canvas_size_changed

View File

@@ -7,12 +7,20 @@ const int POINT = 1;
const int SPOT = 2;
in vec2 outTexCoords;
flat in Light outLight;
flat in Light outLight[1];
uniform sampler2D diffuse, position, texcoord, normal, depth;
vec4 directionalLight(Light light) {
return vec4(0,0,0,0);
return vec4(0,0,0,1);
}
vec4 pointLight(Light light) {
return vec4(0.25, 0.25, 0.25, 1);
}
vec4 spotLight(Light light) {
return vec4(0.5, 0.5, 0.5, 1);
}
vec4 calculateLighting(Light light) {
@@ -22,6 +30,12 @@ vec4 calculateLighting(Light light) {
case DIRECTIONAL: {
result = directionalLight(light);
}
case POINT: {
result = pointLight(light);
}
case SPOT: {
result = spotLight(light);
}
default: {
result = vec4(1,1,1,1);
}
@@ -31,5 +45,5 @@ vec4 calculateLighting(Light light) {
}
void main() {
FragColor = texture(diffuse, outTexCoords) * calculateLighting(outLight);
FragColor = texture(diffuse, outTexCoords) * calculateLighting(outLight[0]);
}

View File

@@ -5,10 +5,10 @@ layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inTexCoords;
uniform sampler2D diffuse, position, texcoord, normal, depth;
uniform Light light;
uniform Light light[1];
out vec2 outTexCoords;
flat out Light outLight;
flat out Light outLight[1];
void main() {
gl_Position = vec4(inPosition.x, inPosition.y, inPosition.z, 1.0);