Commit 35539c0a authored by Johannes Braun's avatar Johannes Braun
Browse files

SSAO! But found out there are quite big light-buffer issues (duplicating lights)

parent 26c26a64
#version 430
#extension GL_ARB_shading_language_include : require
#extension GL_ARB_bindless_texture : require
sampler2DMS rgb_color_texture;
uniform uint u_sample_count;
uniform uint axis;
const float weights[4] = {2.0, 3.0, 2.0, 1.0};
layout(location=0) out vec4 output_color;
vec2 axisVector(uint a)
{
vec2 retval = vec2(0, 0);
retval[a] = 1.f;
return retval;
}
void main() {
ivec2 pixel = ivec2(gl_FragCoord.xy);
output_color = vec4(0);
for(int s=0; s<u_sample_count; ++s)
{
vec4 color = vec4(0);
vec2 diff = axisVector(axis);
for(int i=0; i<4; ++i)
{
color += weights[i] * texelFetch(rgb_color_texture, ivec2(pixel + i*diff), s);
color += weights[i] * texelFetch(rgb_color_texture, ivec2(pixel - i*diff), s);
}
output_color += color/16.f;
}
output_color /= u_sample_count;
}
......@@ -9,6 +9,7 @@
//in vec2 out_texcoord;
uniform sampler2D u_ssao_texture;
uniform samplerCube environment;
uniform vec3 camera_position;
uniform mat4 u_view;
......@@ -85,6 +86,8 @@ void main()
}
// Add resulting color to the finished image, and compensate for multisampling
out_final_color += final_color_part / u_samples;
out_final_color += final_color_part;
}
out_final_color /= u_samples;
out_final_color *= vec4(texelFetch(u_ssao_texture, pixel, 0).rgb, 1);
}
......@@ -81,7 +81,8 @@ vec4 phong(const in Material material, const in LightShadow light_and_map, sampl
float k_t = (1-k_s) * material.transmission.value;
float k_d = 1 - k_s - k_t;
return vec4(k_d * (phong_diffuse + material.base.value * textureLod(environment, normal, 16).rgb) + k_t * environment_refract + k_s * (environment_reflect + phong_specular), 1);
return vec4(phong_diffuse, 1);
//return vec4(k_d * (phong_diffuse + material.base.value * textureLod(environment, normal, 16).rgb) + k_t * environment_refract + k_s * (environment_reflect + phong_specular), 1);
}
#endif // !__GBUFFER_LIGHTS_GLH
#version 430
#extension GL_ARB_shading_language_include : require
#extension GL_ARB_bindless_texture : require
#include <util/math/geometry.glsl>
uniform mat4 projection_matrix;
uniform float radius = 0.15f;
uniform float quality = 8;
sampler2DMS screen_space_positions;
sampler2DMS screen_space_normals;
uniform uint u_sample_count;
uniform vec2 screen_size;
layout(location=0) out vec4 output_color;
float ssao_rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
vec3 ssao_rand3(int i, vec3 v, vec2 v2){
float x = ssao_rand(vec2(v.x + i*3+0, v.x + ((i*3+0)*1531)%113) + v2*19789)*2 - 1.0;
float y = ssao_rand(vec2(v.y + i*3+1, v.y + ((i*3+1)*1531)%113) + v2*19789)*2 - 1.0;
float z = ssao_rand(vec2(v.z + i*3+2, v.z + ((i*3+2)*1531)%113) + v2*19789)*2 - 1.0;
return vec3(x,y,z);
}
vec3 ssao_samples[16] = {
vec3(0.53812504, 0.18565957, -0.43192),
vec3(0.13790712, 0.24864247, 0.44301823),
vec3(0.33715037, 0.56794053, -0.005789503),
vec3(-0.6999805, -0.04511441, -0.0019965635),
vec3(0.06896307, -0.15983082, -0.85477847),
vec3(0.056099437, 0.006954967, -0.1843352),
vec3(-0.014653638, 0.14027752, 0.0762037),
vec3(0.010019933, -0.1924225, -0.034443386),
vec3(-0.35775623, -0.5301969, -0.43581226),
vec3(-0.3169221, 0.106360726, 0.015860917),
vec3(0.010350345, -0.58698344, 0.0046293875),
vec3(-0.08972908, -0.49408212, 0.3287904),
vec3(0.7119986, -0.0154690035, -0.09183723),
vec3(-0.053382345, 0.059675813, -0.5411899),
vec3(0.035267662, -0.063188605, 0.54602677),
vec3(-0.47761092, 0.2847911, -0.0271716)
};
void main()
{
ivec2 pixel = ivec2(gl_FragCoord.xy);
for(int s=0; s<u_sample_count; ++s)
{
vec4 position = texelFetch(screen_space_positions, pixel, s);
vec3 normal = texelFetch(screen_space_normals, pixel, s).xyz;
//if(position.w < 0.5f) discard;
float occlude = 0.f;
vec3 color = vec3(0);
for(int i = 0; i < quality; ++i)
{
vec3 vector = ssao_rand3(i, ssao_samples[i%16], vec2(pixel) / vec2(screen_size));
///vector += normal;
//vector.z = abs(vector.z);
//vector = sign(dot(vector, normal)) * localToWorld(vector, normal);
float ray_sign = 1;//sign(dot(vector, normal));
vec3 offset = normalize(vector + 1.05f * normal) * radius;
vector = position.xyz + offset;
vec4 diff = projection_matrix * vec4(vector, 1);
diff = (diff/diff.w) * 0.5f + 0.5f;
vec4 offset_position = texelFetch(screen_space_positions, ivec2(diff.xy * vec2(screen_size)), s);
if(offset_position.w < 0.5f) continue;
float range_check = smoothstep(0.0f, 1.0f, radius / abs(position.z - offset_position.z ));
occlude += (offset_position.z >= vector.z ? 1.0f : 0.0f) * range_check;
}
output_color += vec4(vec3(1-(occlude / quality)), 1.f);
}
output_color /= float(u_sample_count);
}
......@@ -10,6 +10,7 @@ in vec4 out_world_position;
in vec4 out_view_position;
in vec4 out_position;
in vec4 out_normal;
in vec4 out_normal_screen;
in vec4 out_tangent;
in mat3 out_tangent_space;
in vec3 out_camera_position;
......@@ -31,6 +32,7 @@ layout(location = 1) out vec4 output_rough_metallic_transmit_emit;
layout(location = 2) out vec4 output_normal;
layout(location = 3) out vec4 output_modelview_position;
layout(location = 4) out vec4 output_world_position;
layout(location = 5) out vec4 output_normal_screen;
#define texOrVec4(b, sampler, vec) (b ? texture(sampler, parallax_texcoord) : vec)
......@@ -47,6 +49,8 @@ void main()
parallax_texcoord = out_texcoord + (to_eye.xy * v);
}
output_normal_screen = out_normal_screen;
output_base_ior.rgb = u_material.getBase(parallax_texcoord);
output_base_ior.a = u_material.ior;
......@@ -62,4 +66,6 @@ void main()
output_normal = u_material.has_normal_map ?
vec4(out_tangent_space * normalize((texture(u_material.normal_map, parallax_texcoord).xyz)*2.0 - 1.0), 0) :
vec4(out_normal.rgb, 0);
output_normal_screen = globals.camera.view_matrix * output_normal;
}
......@@ -14,6 +14,7 @@
#include <components/FramerateCounter.h>
#include <components/PlayerController.h>
#include "core/res/collada.h"
using namespace glare;
......@@ -113,7 +114,8 @@ void loadScene(const fs::path& path, float scale)
core::state::graph_root->clearChildren();
//Initialize some scene graph from an obj file
if (auto scene = core::global_resources::scenes.get(path, scale))
// Don't use resources here, because of duplicating lights.
if (auto scene = core::Collada().load(path, scale))
{
core::state::graph_root->attach(scene);
}
......
......@@ -33,8 +33,14 @@ namespace glare::core
m_gbuffer_framebuffer->attach(gl::Attachment::eColor2);
m_gbuffer_framebuffer->attach(gl::Attachment::eColor3);
m_gbuffer_framebuffer->attach(gl::Attachment::eColor4);
m_gbuffer_framebuffer->attach(gl::Attachment::eColor5);
m_gbuffer_framebuffer->attach(gl::Attachment::eDepth);
m_temporary_framebuffer_01 = std::make_shared<Framebuffer<Texture>>(static_cast<unsigned>(mode->width), static_cast<unsigned>(mode->height));
m_temporary_framebuffer_01->attach(gl::Attachment::eColor0);
m_temporary_framebuffer_02 = std::make_shared<Framebuffer<Texture>>(static_cast<unsigned>(mode->width), static_cast<unsigned>(mode->height));
m_temporary_framebuffer_02->attach(gl::Attachment::eColor0);
m_lights_buffer = std::make_unique<Buffer<gl::BufferType::eShaderStorage>>();
//Update all uniforms that won't change anymore...
......@@ -146,6 +152,30 @@ namespace glare::core
void GBuffer::draw() const
{
m_temporary_framebuffer_01->activate();
static auto ssao_renderer = DefaultTextureRenderers::makeRenderer(files::shader("screenshader/ssao/ssao.frag"));
ssao_renderer->shader().uniform("projection_matrix", state::camera->projectionMatrix());
ssao_renderer->shader().uniform("screen_space_positions", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor3).makeTextureResident());
ssao_renderer->shader().uniform("screen_space_normals", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor5).makeTextureResident());
ssao_renderer->shader().uniform("u_gbuffer.depth", m_gbuffer_framebuffer->depthAttachment().makeTextureResident());
ssao_renderer->shader().uniform("screen_size", glm::vec2(m_width, m_height));
ssao_renderer->draw();
m_temporary_framebuffer_01->deactivate();
m_temporary_framebuffer_02->activate();
static auto blur_1d = DefaultTextureRenderers::makeRenderer(files::shader("screenshader/blur/blur_1d.frag"));
blur_1d->shader().uniform("rgb_color_texture", m_temporary_framebuffer_01->colorAttachment(gl::Attachment::eColor0).makeTextureResident());
blur_1d->shader().uniform("axis", 0u);
blur_1d->draw();
m_temporary_framebuffer_02->deactivate();
m_temporary_framebuffer_01->activate();
blur_1d->shader().uniform("rgb_color_texture", m_temporary_framebuffer_02->colorAttachment(gl::Attachment::eColor0).makeTextureResident());
blur_1d->shader().uniform("axis", 1u);
blur_1d->draw();
m_temporary_framebuffer_01->deactivate();
deactivate();
gl::setEnabled(gl::EnableParameter::eDepthTest, false);
m_texture_renderer->shader().use();
......@@ -155,6 +185,7 @@ namespace glare::core
if (m_skybox->isInitialized())
m_texture_renderer->shader().uniform("environment", m_skybox->makeResident());
m_texture_renderer->shader().uniform("u_ssao_texture", m_temporary_framebuffer_01->colorAttachment(gl::Attachment::eColor0).makeTextureResident());
m_texture_renderer->shader().uniform("u_view", state::camera->viewMatrix());
m_texture_renderer->shader().uniform("u_projection", state::camera->projectionMatrix());
m_texture_renderer->shader().uniform("camera_position", glm::vec3(state::camera->owner()->worldTransform().position));
......
......@@ -52,6 +52,8 @@ namespace glare::core
std::vector<LightShadow> m_lights;
std::shared_ptr<Skybox> m_skybox;
std::shared_ptr<Framebuffer<TextureMultisampled>> m_gbuffer_framebuffer;
std::shared_ptr<Framebuffer<Texture>> m_temporary_framebuffer_01;
std::shared_ptr<Framebuffer<Texture>> m_temporary_framebuffer_02;
std::shared_ptr<TextureRenderer> m_texture_renderer;
};
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment