Added post-process effects : blur, sharpen, grayscale, sepia, sobel edge detection

This commit is contained in:
Hugo Willaume 2019-04-15 22:00:50 +09:00
parent f404b6ae9b
commit c4776edba1
2 changed files with 99 additions and 7 deletions

View File

@ -180,11 +180,10 @@ void MyGlWindow::setup()
//meshes["Cube"]->assignTexture(_scnctx.textures["MossTex"]);
//meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0));
//meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"]));
meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1));
meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"]));
//meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
//meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1));
//moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7));
@ -226,7 +225,7 @@ void MyGlWindow::draw()
_scnctx.viewMatrix = view;
_scnctx.projectionMatrix = projection;
_multipassManager.enableFrameBufferTexture("depth_tex");
_multipassManager.enableFrameBufferTexture("render_tex");
glClearColor(_scnctx.bg.r, _scnctx.bg.g, _scnctx.bg.b, _scnctx.bg.a);
glViewport(0, 0, _scnctx.width, _scnctx.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -236,7 +235,7 @@ void MyGlWindow::draw()
for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it).second->draw(_scnctx);
_multipassManager.shader.addUniform("depth", true);
_multipassManager.shader.addUniform("sobel", true);
_multipassManager.drawResultToScreen(_scnctx);
//skybox.draw(shaders["Skybox"], _scnctx);
}

View File

@ -6,7 +6,31 @@ out vec4 final_color;
uniform sampler2D tex;
uniform bool depth;
uniform bool blur;
uniform bool sharpen;
uniform bool sepia;
uniform bool grayscale;
uniform bool sobel;
const float f = 1.0 / 300.0;
vec2 offsets[9] = vec2[](
vec2(-f, f), vec2(0.0, f), vec2(f, f),
vec2(-f, 0.0), vec2(0.0, 0.0), vec2(f, 0.0),
vec2(-f, -f), vec2(0.0, -f), vec2(f, -f)
);
float blur_kernel[9] = float[](
1.0 / 16, 2.0 / 16, 1.0 / 16,
2.0 / 16, 4.0 / 16, 2.0 / 16,
1.0 / 16, 2.0 / 16, 1.0 / 16
);
float sharp_kernel[9] = float[](
-1, -1, -1,
-1, 9, -1,
-1, -1, -1
);
float LinearizeDepth(in vec2 uv)
{
@ -16,6 +40,50 @@ float LinearizeDepth(in vec2 uv)
return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
}
vec3 blurring_func(in vec2 uv)
{
vec3 texblur = vec3(0,0,0);
for (int i = 0; i < 9; i++)
texblur += vec3(texture(tex, uv + offsets[i]).rgb * blur_kernel[i]);
return texblur;
}
vec4 sharpening_func(in vec2 uv)
{
vec4 texsharp = vec4(0,0,0,0);
for (uint i = 0; i < 9; i++)
texsharp += texture(tex, uv + offsets[i]).rgba * sharp_kernel[i];
return texsharp;
}
vec3 sobel_filter(vec3 color)
{
float sobel_x[9] = float[](
-1, 0, 1,
-2, 0, 2,
-1, 0, 1
);
float sobel_y[9] = float[](
-1, -2, -1,
0, 0, 0,
1, 2, 1
);
vec3 vec_x = vec3(0);
vec3 vec_y = vec3(0);
for (int i = 0; i < 9; i++)
{
vec_x += texture(tex, uv + offsets[i]).rgb * sobel_x[i];
vec_y += texture(tex, uv + offsets[i]).rgb * sobel_y[i];
}
return sqrt(vec_x * vec_x + vec_y * vec_y);
}
void main()
{
//rrra because depth textures are not usual textures, they have only one channel
@ -25,5 +93,30 @@ void main()
float d;
d = LinearizeDepth(uv);
final_color = vec4(d,d,d,1.0);
}
}
else if (blur)
{
final_color = vec4(blurring_func(uv), 1);
}
else if (sharpen)
{
final_color = vec4(sharpening_func(uv));
}
else if (sepia)
{
final_color = vec4(final_color.r * .393 + final_color.g * .769 + final_color.b * .189,
final_color.r * .349 + final_color.g * .686 + final_color.b * .168,
final_color.r * .272 + final_color.g * .534 + final_color.b * .131, 1.0);
}
else if (grayscale)
{
float average = final_color.r * 0.2126 + final_color.g * 0.7152 + final_color.b * 0.0722;
final_color = vec4(average, average, average, 1.0);
}
else if (sobel)
{
vec3 sobeled = sobel_filter(final_color.rgb);
float average = sobeled.r * 0.2126 + sobeled.g * 0.7152 + sobeled.b * 0.0722;
final_color = vec4(average, average, average, 1.0);
}
}