mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-18 09:02:35 +00:00
Work In Progress
This commit is contained in:
@@ -5,7 +5,7 @@ class IMICFPS
|
||||
attr_reader :screen_vbo, :vertices, :uvs
|
||||
def initialize
|
||||
@framebuffer = nil
|
||||
@buffers = [:position, :diffuse, :normal, :texcoord]
|
||||
@buffers = [:position, :diffuse, :normal, :texcoord, :scene]
|
||||
@textures = {}
|
||||
@screen_vbo = nil
|
||||
@ready = false
|
||||
@@ -86,9 +86,9 @@ class IMICFPS
|
||||
@textures[@buffers[i]] = texture_id
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, nil)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, nil)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texture_id, 0)
|
||||
end
|
||||
|
||||
@@ -101,7 +101,7 @@ class IMICFPS
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nil)
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0)
|
||||
|
||||
draw_buffers = [ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 ]
|
||||
draw_buffers = @buffers.each_with_index.map { |b ,i| Object.const_get("GL_COLOR_ATTACHMENT#{i}") }
|
||||
glDrawBuffers(draw_buffers.size, draw_buffers.pack("I*"))
|
||||
end
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def render(camera, lights, entities)
|
||||
if Shader.available?("default") && Shader.available?("render_screen")
|
||||
if Shader.available?("default") && Shader.available?("render_screen") && Shader.available?("lighting")
|
||||
@g_buffer.bind_for_writing
|
||||
gl_error?
|
||||
|
||||
@@ -24,19 +24,6 @@ class IMICFPS
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
||||
|
||||
Shader.use("default") do |shader|
|
||||
lights.each_with_index do |light, i|
|
||||
shader.uniform_float("lights[#{i}.end", -1.0);
|
||||
shader.uniform_float("lights[#{i}.type", light.type);
|
||||
shader.uniform_vec3("lights[#{i}].position", light.position)
|
||||
shader.uniform_vec3("lights[#{i}].ambient", light.ambient)
|
||||
shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse)
|
||||
shader.uniform_vec3("lights[#{i}].specular", light.specular)
|
||||
end
|
||||
gl_error?
|
||||
|
||||
|
||||
shader.uniform_integer("totalLights", lights.size)
|
||||
|
||||
entities.each do |entity|
|
||||
next unless entity.visible && entity.renderable
|
||||
|
||||
@@ -52,6 +39,9 @@ class IMICFPS
|
||||
end
|
||||
end
|
||||
|
||||
lighting(lights)
|
||||
post_processing
|
||||
|
||||
@g_buffer.unbind_framebuffer
|
||||
gl_error?
|
||||
|
||||
@@ -59,8 +49,6 @@ class IMICFPS
|
||||
@g_buffer.bind_for_reading
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
||||
|
||||
# lighting(lights)
|
||||
post_processing
|
||||
render_framebuffer
|
||||
|
||||
@g_buffer.unbind_framebuffer
|
||||
@@ -96,25 +84,32 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def lighting(lights)
|
||||
@g_buffer.set_read_buffer(:position)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
0, 0, @g_buffer.width / 2, @g_buffer.height / 2,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
if Shader.available?("lighting")
|
||||
Shader.use("lighting") do |shader|
|
||||
lights.each do |light|
|
||||
shader.uniform_integer("inLightType", light.type);
|
||||
shader.uniform_vec3("inLightPosition", light.position)
|
||||
shader.uniform_vec3("inLightAmbient", light.ambient)
|
||||
shader.uniform_vec3("inLightDiffuse", light.diffuse)
|
||||
shader.uniform_vec3("inLightSpecular", light.specular)
|
||||
|
||||
@g_buffer.set_read_buffer(:diffuse)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
0, @g_buffer.height / 2, @g_buffer.width / 2, @g_buffer.height,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
glBindVertexArray(@g_buffer.screen_vbo)
|
||||
glEnableVertexAttribArray(0)
|
||||
glEnableVertexAttribArray(1)
|
||||
|
||||
@g_buffer.set_read_buffer(:normal)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
@g_buffer.width / 2, @g_buffer.height / 2, @g_buffer.width, @g_buffer.height,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
glDisable(GL_DEPTH_TEST)
|
||||
glEnable(GL_BLEND)
|
||||
|
||||
@g_buffer.set_read_buffer(:texcoord)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
@g_buffer.width / 2, 0, @g_buffer.width, @g_buffer.height / 2,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||
|
||||
glDisableVertexAttribArray(1)
|
||||
glDisableVertexAttribArray(0)
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
|
||||
gl_error?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def post_processing
|
||||
@@ -131,7 +126,7 @@ class IMICFPS
|
||||
glEnable(GL_BLEND)
|
||||
|
||||
glActiveTexture(GL_TEXTURE0)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:scene))
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def preload_default_shaders
|
||||
shaders = ["default", "render_screen"]
|
||||
shaders = ["default", "render_screen", "lighting"]
|
||||
shaders.each do |shader|
|
||||
Shader.new(
|
||||
name: shader,
|
||||
|
||||
Reference in New Issue
Block a user