Framebuffer rendering on quad is now working! 😂

This commit is contained in:
2020-03-25 13:06:40 -05:00
parent 87ae77a9cb
commit 15e5d0a5e1
6 changed files with 60 additions and 21 deletions

View File

@@ -169,10 +169,13 @@ class IMICFPS
def configure_vao
glBindVertexArray(@vertex_array_id)
gl_error?
# index, size, type, normalized, stride, pointer
# vertices (positions)
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
gl_error?
# inPosition
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
gl_error?

View File

@@ -14,20 +14,20 @@ class IMICFPS
-1.0, -1.0, 0,
1.0, -1.0, 0,
-1.0, 1.0, 0,
-1.0, 1.0, 0,
1.0, -1.0, 0,
1.0, 1.0, 0,
].freeze
@uvs = [
0, 0,
1, 0,
0, 1,
1, 1,
0, 0,
0, 0,
1, 1,
1, 0
0, 1,
1, 0,
1, 1
].freeze
create_framebuffer
@@ -43,7 +43,7 @@ class IMICFPS
end
def create_framebuffer
buffer = ' ' * 8
buffer = ' ' * 4
glGenFramebuffers(1, buffer)
@framebuffer = buffer.unpack('L2').first
@@ -80,17 +80,19 @@ class IMICFPS
def create_textures
@buffers.size.times do |i|
buffer = ' ' * 8
buffer = ' ' * 4
glGenTextures(1, buffer)
texture_id = buffer.unpack('L2').first
@textures[@buffers[i]] = texture_id
glBindTexture(GL_TEXTURE_2D, texture_id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, nil)
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)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texture_id, 0)
end
buffer = ' ' * 8
buffer = ' ' * 4
glGenTextures(1, buffer)
texture_id = buffer.unpack('L2').first
@textures[:depth] = texture_id
@@ -104,7 +106,7 @@ class IMICFPS
end
def create_screen_vbo
buffer = ' ' * 8
buffer = ' ' * 4
glGenVertexArrays(1, buffer)
@screen_vbo = buffer.unpack('L2').first
@@ -125,6 +127,7 @@ class IMICFPS
glBufferData(GL_ARRAY_BUFFER, @uvs.size * Fiddle::SIZEOF_FLOAT, @uvs.pack("f*"), GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nil)
glBindVertexArray(0)
end
def bind_for_writing

View File

@@ -13,6 +13,7 @@ class IMICFPS
@g_buffer.bind_for_writing
gl_error?
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Shader.use("default") do |shader|
@@ -47,9 +48,15 @@ class IMICFPS
@g_buffer.unbind_framebuffer
gl_error?
lighting(lights)
@g_buffer.bind_for_reading
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
# lighting(lights)
post_processing
render_framebuffer
@g_buffer.unbind_framebuffer
gl_error?
else
puts "Shader 'default' failed to compile, using immediate mode for rendering..." unless @@immediate_mode_warning
@@ -82,11 +89,24 @@ class IMICFPS
end
def lighting(lights)
@g_buffer.bind_for_reading
@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)
@g_buffer.set_read_buffer(:diffuse)
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
0, 0, @g_buffer.width / 2, @g_buffer.height / 2,
0, @g_buffer.height / 2, @g_buffer.width / 2, @g_buffer.height,
GL_COLOR_BUFFER_BIT, GL_LINEAR)
@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)
@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)
end
@@ -99,10 +119,18 @@ class IMICFPS
glBindVertexArray(@g_buffer.screen_vbo)
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
glDisable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
glDisableVertexAttribArray(1)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
end
end
end

View File

@@ -44,7 +44,7 @@ class IMICFPS
array_of_pixels = texture.to_blob
tex_names_buf = ' ' * 8
tex_names_buf = ' ' * 4
glGenTextures(1, tex_names_buf)
texture_id = tex_names_buf.unpack('L2').first

View File

@@ -2,8 +2,10 @@
@include "light_struct"
// layout(location = 0) out vec4 fragColor;
layout (location = 1) out vec3 fragColor;
layout(location = 0) out vec3 fragPosition;
layout (location = 1) out vec4 fragColor;
layout (location = 2) out vec3 fragNormal;
layout (location = 3) out vec3 fragUV;
in vec3 outPosition;
in vec3 outColor;
@@ -99,5 +101,8 @@ void main() {
}
}
fragColor = vec3(1,1,1);//result;
fragPosition = outPosition;
fragColor = vec4(result, 1.0);
fragNormal = outNormal;
fragUV = outUV;
}

View File

@@ -1,10 +1,10 @@
#version 330 core
out vec4 FragColor;
in vec2 outTexCoords;
uniform sampler2D screenTexture;
void main() {
void main() {
FragColor = texture(screenTexture, outTexCoords);
}