I have implemented a simple gaussian blur and combined it with deferred shading, and it works. When I'm trying to add another object to the scene the object is also affected by the blur, which is not what I'm looking for. Is it possible to make the blur only affect the image of the target object or is it supposed to affect the whole scene?
For the blur I'm using 2 vertex shaders, one which is calculating the horizontal texture coordinates and another which computes the vertical texture coordinates. Both of the vertex shaders is using the same fragment shader which just computes the gaussian-weighted average of the color of the texels on these pre-computed texture coordinates.
Fragment shader for the blur object:
vec3 color;
color += texture(gColorSpec, v_blurTexCoords[ 0]).rgb *0.0044299121055113265;
color += texture(gColorSpec, v_blurTexCoords[ 1]).rgb*0.00895781211794;
color += texture(gColorSpec, v_blurTexCoords[ 2]).rgb*0.0215963866053;
color += texture(gColorSpec, v_blurTexCoords[ 3]).rgb*0.0443683338718;
color += texture(gColorSpec, v_blurTexCoords[ 4]).rgb*0.0776744219933;
color += texture(gColorSpec, v_blurTexCoords[ 5]).rgb*0.115876621105;
color += texture(gColorSpec, v_blurTexCoords[ 6]).rgb*0.147308056121;
color += texture(gColorSpec, v_texCoord ).rgb*0.159576912161;
color += texture(gColorSpec, v_blurTexCoords[ 7]).rgb*0.147308056121;
color += texture(gColorSpec, v_blurTexCoords[ 8]).rgb*0.115876621105;
color += texture(gColorSpec, v_blurTexCoords[ 9]).rgb*0.0776744219933;
color += texture(gColorSpec, v_blurTexCoords[10]).rgb*0.0443683338718;
color += texture(gColorSpec, v_blurTexCoords[11]).rgb*0.0215963866053;
color += texture(gColorSpec, v_blurTexCoords[12]).rgb*0.00895781211794;
color += texture(gColorSpec, v_blurTexCoords[13]).rgb*0.0044299121055113265;
...
In this case gColorSpec
is the color sample (image) for the deferred shading.
In the lightning pass of deferred shading I'm activating the first and second vertex shader.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//object without blur
ShadersWithoutBlur.Use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gPosition);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gNormal);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, gColorSpec);
// Also send light relevant uniforms
glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "lightPos"), 1, &lightPos[0]);
glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "lightColor"), 1, &lightColor[0]);
glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "viewPos"), 1, &camera.Position[0]);
glUniform3fv(glGetUniformLocation(ShadersWithoutBlur.shaderProgram, "kd"), 1, &kd[0]);
//Blur object
FirstLightningPass.Use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gPosition);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gNormal);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, gColorSpec);
// Also send light relevant uniforms
glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "lightPos"), 1, &lightPos[0]);
glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "lightColor"), 1, &lightColor[0]);
glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "viewPos"), 1, &camera.Position[0]);
glUniform3fv(glGetUniformLocation(FirstLightningPass.shaderProgram, "kd"), 1, &kd[0]);
SecondLightningPass.Use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gPosition);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gNormal);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, gColorSpec);
glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "lightPos"), 1, &lightPos[0]);
glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "lightColor"), 1, &lightColor[0]);
glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "viewPos"), 1, &camera.Position[0]);
glUniform3fv(glGetUniformLocation(SecondLightningPass.shaderProgram, "kd"), 1, &kd[0]);
// Finally render quad
RenderQuad();
Already tried: As I'm using deferred shading I have tried to make another frame buffer to render the geometry into, which is then only used for the blur image object. This doesn't work though as I seem to only need one framebuffer which adds up the whole scene geometry. I have also tried adding another attachment which would replace the gColorSpec
, simply to check if it had a connection with the other gColorSpec
for the other image object.