I am trying to model Christmas lights. The models render but I do not see the bloom effect. I have even made a temporary change to the blend shader so that it outputs the image that is supposed to be blended, and everything is black.
This is my code:
unsigned int hdrFBO;
glGenFramebuffers(1, &hdrFBO);
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
unsigned int colorBuffers[2];
glGenTextures(2, colorBuffers);
for (unsigned int i = 0; i < 2; i++)
{
glBindTexture(GL_TEXTURE_2D, colorBuffers[i]);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA16F, gWindowWidth, gWindowHeight, 0, GL_RGBA, GL_FLOAT, NULL
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// attach texture to framebuffer
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, colorBuffers[i], 0
);
}
GLuint rboDepth;
glGenRenderbuffers(1, &rboDepth);
glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24,
gWindowWidth, gWindowHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, rboDepth);
unsigned int attachments[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, attachments);
unsigned int pingpongFBO[2];
unsigned int pingpongBuffer[2];
glGenFramebuffers(2, pingpongFBO);
glGenTextures(2, pingpongBuffer);
for (unsigned int i = 0; i < 2; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[i]);
glBindTexture(GL_TEXTURE_2D, pingpongBuffer[i]);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA16F, gWindowWidth, gWindowHeight, 0, GL_RGBA, GL_FLOAT, NULL
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pingpongBuffer[i], 0
);
}
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 1.7777777777f, 0.5f, 200.0f);
double lastTime = glfwGetTime();
double deltaTime;
while (!glfwWindowShouldClose(gWindow))
{
double currentTime = glfwGetTime();
deltaTime = currentTime - lastTime;
lastTime = currentTime;
glfwPollEvents();
update(deltaTime);
glm::mat4 view = MyCam.getViewMatrix();
glm::mat4 model = glm::rotate(glm::mat4(1), glm::radians(270.0f), glm::vec3(1, 0, 0));
glm::mat4 mvp = projection * view * model;
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
glViewport(0, 0, gWindowWidth, gWindowHeight);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
treeShader.use();
treeShader.setUniform("mvp", mvp);
//tree.Draw();
lightShader.use();
lightShader.setUniform("mvp", mvp);
lights.Draw();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
bool horizontal = true;
bool first_iteration = true;
int amount = 10;
shaderBlur.use();
for (int i = 0; i < amount; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[horizontal]);
shaderBlur.setUniformI("horizontal", horizontal);
glActiveTexture(GL_TEXTURE0);
glBindTexture(
GL_TEXTURE_2D,
first_iteration ? colorBuffers[1] : pingpongBuffer[!horizontal]
);
shaderBlur.setUniformSampler("image", 0);
glBindVertexArray(lVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
horizontal = !horizontal;
if (first_iteration)
first_iteration = false;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
blendShader.use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorBuffers[0]); // scene
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pingpongBuffer[!horizontal]); // bloom
blendShader.setUniformSampler("scene", 0);
blendShader.setUniformSampler("bloomBlur", 1);
glBindVertexArray(lVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glfwSwapBuffers(gWindow);
These are my fragment shaders:
#version 430 core //lights
layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;
void main()
{
vec3 emissiveColor = vec3(1, 1, 1);
float emissiveStrength = .75;
vec3 color = vec3(20, 20, 20);
vec3 emissive = emissiveColor * emissiveStrength;
color += emissive;
FragColor = vec4(color, 1.0);
float brightness = dot(emissive, vec3(0.2126, 0.7152, 0.0722));
if (brightness > 1.0)
BrightColor = vec4(emissive, 1.0);
else
BrightColor = vec4(0.0);
}
#version 430 core //blur
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D image;
uniform bool horizontal;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
void main()
{
vec2 tex_offset = 1.0 / textureSize(image, 0); // gets size of single texel
vec3 result = texture(image, TexCoords).rgb * weight[0]; // current fragment's contribution
if(horizontal)
{
for(int i = 1; i < 5; ++i)
{
result += texture(image, TexCoords + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(image, TexCoords - vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
}
}
else
{
for(int i = 1; i < 5; ++i)
{
result += texture(image, TexCoords + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(image, TexCoords - vec2(0.0, tex_offset.y * i)).rgb * weight[i];
}
}
FragColor = vec4(result, 1.0);
}
#version 430 core //blend
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D scene;
uniform sampler2D bloomBlur;
void main()
{
float exposure = .5;
const float gamma = 2.2;
vec3 hdrColor = texture(scene, TexCoords).rgb;
vec3 bloomColor = texture(bloomBlur, TexCoords).rgb;
hdrColor += bloomColor; // additive blending
// tone mapping
vec3 result = vec3(1.0) - exp(-hdrColor * exposure);
// also gamma correct while we're at it
result = pow(result, vec3(1.0 / gamma));
//FragColor = vec4(result, 1.0);
FragColor = vec4(bloomColor, 1);
}