Add depth of field to post shader

This commit is contained in:
Peder Bergebakken Sundt 2019-04-01 13:06:50 +02:00
parent 8a48b59c0b
commit c695d96158
2 changed files with 14 additions and 1 deletions

View File

@ -17,5 +17,14 @@ void main() {
float z = pow(texture(depthbuffer, UV).r , 0x800);
z = abs(z*2-1)*1.2;
color_out = vec4(vec3(texture(depthbuffer, UV).r), 1.0);
int radius = int(5*z);
vec3 color = vec3(0);
for (int x = -radius; x <= radius; x++)
for (int y = -radius; y <= radius; y++){
vec2 p = UV + x*dx + y*dy;
color += texture(framebuffer, p);
}
color /= pow(2*radius+1, 2);
color_out = vec4(color, 1.0);
}

View File

@ -87,12 +87,16 @@ void initRenderer(GLFWwindow* window, int windowWidth, int windowHeight) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowWidth, windowHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
if (first) glGenTextures(1, &framebufferDepthTextureID);
glBindTexture(GL_TEXTURE_2D, framebufferDepthTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, windowWidth, windowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
if (first) glGenRenderbuffers(1, &framebufferDepthBufferID);
glBindRenderbuffer(GL_RENDERBUFFER, framebufferDepthBufferID);