mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 08:02:36 +00:00
Patched bounding box renderer to work again- in immediate mode renderer, more work on lighting rework for modern renderer
This commit is contained in:
@@ -1,29 +1,43 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
class BoundingBoxRenderer
|
class BoundingBoxRenderer
|
||||||
attr_reader :bounding_boxes, :vertex_count
|
attr_reader :bounding_boxes, :vertex_count
|
||||||
def initialize(map:)
|
def initialize
|
||||||
@map = map
|
|
||||||
|
|
||||||
@bounding_boxes = {}
|
@bounding_boxes = {}
|
||||||
@vertex_count = 0
|
@vertex_count = 0
|
||||||
end
|
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]
|
def create_bounding_box(entity, color = nil)
|
||||||
if @bounding_boxes[mesh_object_id][:color] != color
|
color ||= entity.debug_color
|
||||||
@bounding_boxes[mesh_object_id][:colors] = mesh_colors(color).pack("f*")
|
entity_id = entity.object_id
|
||||||
@bounding_boxes[mesh_object_id][:color] = color
|
|
||||||
|
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
|
return
|
||||||
else
|
else
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
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
|
normals = mesh_normals
|
||||||
colors = mesh_colors(color)
|
colors = mesh_colors(color)
|
||||||
@@ -31,14 +45,14 @@ class IMICFPS
|
|||||||
|
|
||||||
@vertex_count+=vertices.size
|
@vertex_count+=vertices.size
|
||||||
|
|
||||||
@bounding_boxes[mesh_object_id][:vertices_size] = vertices.size
|
@bounding_boxes[entity_id][:vertices_size] = vertices.size
|
||||||
@bounding_boxes[mesh_object_id][:vertices] = vertices.pack("f*")
|
@bounding_boxes[entity_id][:vertices] = vertices.pack("f*")
|
||||||
@bounding_boxes[mesh_object_id][:normals] = normals.pack("f*")
|
@bounding_boxes[entity_id][:normals] = normals.pack("f*")
|
||||||
@bounding_boxes[mesh_object_id][:colors] = colors.pack("f*")
|
@bounding_boxes[entity_id][:colors] = colors.pack("f*")
|
||||||
|
|
||||||
object.model.objects.each do |mesh|
|
entity.model.objects.each do |mesh|
|
||||||
data = {}
|
data = {}
|
||||||
box = mesh.bounding_box.normalize(object)
|
box = mesh.bounding_box.normalize(entity)
|
||||||
|
|
||||||
normals = mesh_normals
|
normals = mesh_normals
|
||||||
colors = mesh_colors(mesh.debug_color)
|
colors = mesh_colors(mesh.debug_color)
|
||||||
@@ -51,7 +65,7 @@ class IMICFPS
|
|||||||
data[:normals] = normals.pack("f*")
|
data[:normals] = normals.pack("f*")
|
||||||
data[:colors] = colors.pack("f*")
|
data[:colors] = colors.pack("f*")
|
||||||
|
|
||||||
@bounding_boxes[mesh_object_id][:objects] << data
|
@bounding_boxes[entity_id][:objects] << data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -199,19 +213,15 @@ class IMICFPS
|
|||||||
@bounding_boxes.each do |key, bounding_box|
|
@bounding_boxes.each do |key, bounding_box|
|
||||||
glPushMatrix
|
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)
|
draw_bounding_box(bounding_box)
|
||||||
@bounding_boxes[key][:objects].each {|o| draw_bounding_box(o)}
|
@bounding_boxes[key][:objects].each {|o| draw_bounding_box(o)}
|
||||||
|
|
||||||
glPopMatrix
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -226,7 +236,7 @@ class IMICFPS
|
|||||||
|
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
||||||
glDisable(GL_LIGHTING)
|
glDisable(GL_LIGHTING)
|
||||||
glDrawArrays(GL_TRIANGLES, 0, bounding_box[:vertices_size]/3)
|
glDrawArrays(GL_TRIANGLES, 0, bounding_box[:vertices_size] / 3)
|
||||||
glEnable(GL_LIGHTING)
|
glEnable(GL_LIGHTING)
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
||||||
|
|
||||||
|
|||||||
@@ -145,6 +145,10 @@ class IMICFPS
|
|||||||
glReadBuffer(GL_COLOR_ATTACHMENT0 + @textures.keys.index(buffer))
|
glReadBuffer(GL_COLOR_ATTACHMENT0 + @textures.keys.index(buffer))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_read_buffer_depth
|
||||||
|
glReadBuffer(GL_DEPTH_ATTACHMENT)
|
||||||
|
end
|
||||||
|
|
||||||
def unbind_framebuffer
|
def unbind_framebuffer
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0)
|
glBindFramebuffer(GL_FRAMEBUFFER, 0)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render(camera, lights, entities)
|
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
|
@g_buffer.bind_for_writing
|
||||||
gl_error?
|
gl_error?
|
||||||
|
|
||||||
@@ -46,9 +46,14 @@ class IMICFPS
|
|||||||
@g_buffer.bind_for_reading
|
@g_buffer.bind_for_reading
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
||||||
|
|
||||||
# lighting(lights)
|
lighting(lights)
|
||||||
|
gl_error?
|
||||||
|
|
||||||
post_processing
|
post_processing
|
||||||
render_framebuffer
|
gl_error?
|
||||||
|
|
||||||
|
# render_framebuffer
|
||||||
|
gl_error?
|
||||||
|
|
||||||
@g_buffer.unbind_framebuffer
|
@g_buffer.unbind_framebuffer
|
||||||
gl_error?
|
gl_error?
|
||||||
@@ -105,39 +110,44 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def lighting(lights)
|
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|
|
lights.each_with_index do |light, i|
|
||||||
shader.uniform_float("light.type", light.type);
|
shader.uniform_integer("light[0].type", light.type);
|
||||||
shader.uniform_vec3("light.direction", light.direction)
|
shader.uniform_vec3("light[0].direction", light.direction)
|
||||||
shader.uniform_vec3("light.position", light.position)
|
shader.uniform_vec3("light[0].position", light.position)
|
||||||
shader.uniform_vec3("light.ambient", light.ambient)
|
shader.uniform_vec3("light[0].diffuse", light.diffuse)
|
||||||
shader.uniform_vec3("light.diffuse", light.diffuse)
|
shader.uniform_vec3("light[0].ambient", light.ambient)
|
||||||
shader.uniform_vec3("light.specular", light.specular)
|
shader.uniform_vec3("light[0].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))
|
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||||
|
|
||||||
glBindVertexArray(0)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
glBindVertexArray(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -145,8 +155,8 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_framebuffer
|
def render_framebuffer
|
||||||
if Shader.available?("deferred_lighting")
|
if Shader.available?("lighting")
|
||||||
Shader.use("deferred_lighting") do |shader|
|
Shader.use("lighting") do |shader|
|
||||||
glBindVertexArray(@g_buffer.screen_vbo)
|
glBindVertexArray(@g_buffer.screen_vbo)
|
||||||
|
|
||||||
glDisable(GL_DEPTH_TEST)
|
glDisable(GL_DEPTH_TEST)
|
||||||
@@ -154,6 +164,7 @@ class IMICFPS
|
|||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0)
|
glActiveTexture(GL_TEXTURE0)
|
||||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
||||||
|
shader.uniform_integer("diffuse_texture", 0)
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ class IMICFPS
|
|||||||
attr_reader :opengl_renderer, :bounding_box_renderer
|
attr_reader :opengl_renderer, :bounding_box_renderer
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
# @bounding_box_renderer = BoundingBoxRenderer.new(map: map)
|
@bounding_box_renderer = BoundingBoxRenderer.new
|
||||||
@opengl_renderer = OpenGLRenderer.new
|
@opengl_renderer = OpenGLRenderer.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def preload_default_shaders
|
def preload_default_shaders
|
||||||
shaders = ["g_buffer", "deferred_lighting"]
|
shaders = ["g_buffer", "lighting"]
|
||||||
shaders.each do |shader|
|
shaders.each do |shader|
|
||||||
Shader.new(
|
Shader.new(
|
||||||
name: shader,
|
name: shader,
|
||||||
@@ -26,6 +26,7 @@ class IMICFPS
|
|||||||
glEnable(GL_DEPTH_TEST)
|
glEnable(GL_DEPTH_TEST)
|
||||||
|
|
||||||
@opengl_renderer.render(camera, lights, entities)
|
@opengl_renderer.render(camera, lights, entities)
|
||||||
|
@bounding_box_renderer.render(entities) if window.config.get(:debug_options, :boundingboxes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def canvas_size_changed
|
def canvas_size_changed
|
||||||
|
|||||||
@@ -7,12 +7,20 @@ const int POINT = 1;
|
|||||||
const int SPOT = 2;
|
const int SPOT = 2;
|
||||||
|
|
||||||
in vec2 outTexCoords;
|
in vec2 outTexCoords;
|
||||||
flat in Light outLight;
|
flat in Light outLight[1];
|
||||||
|
|
||||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
|
||||||
vec4 directionalLight(Light light) {
|
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) {
|
vec4 calculateLighting(Light light) {
|
||||||
@@ -22,6 +30,12 @@ vec4 calculateLighting(Light light) {
|
|||||||
case DIRECTIONAL: {
|
case DIRECTIONAL: {
|
||||||
result = directionalLight(light);
|
result = directionalLight(light);
|
||||||
}
|
}
|
||||||
|
case POINT: {
|
||||||
|
result = pointLight(light);
|
||||||
|
}
|
||||||
|
case SPOT: {
|
||||||
|
result = spotLight(light);
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
result = vec4(1,1,1,1);
|
result = vec4(1,1,1,1);
|
||||||
}
|
}
|
||||||
@@ -31,5 +45,5 @@ vec4 calculateLighting(Light light) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
FragColor = texture(diffuse, outTexCoords) * calculateLighting(outLight);
|
FragColor = texture(diffuse, outTexCoords) * calculateLighting(outLight[0]);
|
||||||
}
|
}
|
||||||
@@ -5,10 +5,10 @@ layout (location = 0) in vec3 inPosition;
|
|||||||
layout (location = 1) in vec2 inTexCoords;
|
layout (location = 1) in vec2 inTexCoords;
|
||||||
|
|
||||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
uniform Light light;
|
uniform Light light[1];
|
||||||
|
|
||||||
out vec2 outTexCoords;
|
out vec2 outTexCoords;
|
||||||
flat out Light outLight;
|
flat out Light outLight[1];
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(inPosition.x, inPosition.y, inPosition.z, 1.0);
|
gl_Position = vec4(inPosition.x, inPosition.y, inPosition.z, 1.0);
|
||||||
Reference in New Issue
Block a user