2
\$\begingroup\$

I'm trying to implement bloom in the same manner as this tutorial, but I am having difficulties. I have the blur buffer blurring correctly, but I can't seem to combine the two images.

enter image description here


Below, you will find my code. The commented-out lines just render one or the other buffer to the screen, so I know these are correct. For now, I am just trying to combine them together; not fix them with HDR. I just add together both textures, but no matter how I change things around, all I get is the one on the left.

blurShader.use();
glUniform1i(glGetUniformLocation(blurShader.program, "horizontal"), true);
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[1]);

long blurCount = 5;

for(long i=0; i<blurCount; i++)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glActiveTexture(GL_TEXTURE0);

    // bind texture of other framebuffer (or scene if first iteration)
    glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[i%2]);

    RenderQuad();
    glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[i%2]);
}

glUniform1i(glGetUniformLocation(blurShader.program, "horizontal"), false);

for(long i=0; i<blurCount; i++)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glActiveTexture(GL_TEXTURE0);

    // bind texture of other framebuffer (or scene if first iteration)
    glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[(i+1)%2]);  

    RenderQuad();
    glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[(i+1)%2]);
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

//renderScreenQuad(colorBuffers[0]);
//renderScreenQuad(pingpongColorbuffers[1]);


// 2. Now render floating point color buffer to 2D quad and tonemap HDR colors 
// to default framebuffer's (clamped) color range
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
bloomShader.use(); 
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorBuffers[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[1]);
glUniform1i(glGetUniformLocation(bloomShader.program, "bloom"), bloom);
glUniform1f(glGetUniformLocation(bloomShader.program, "exposure"), exposure);
RenderQuad();

#version 330 core
out vec4 FragColor; in vec2 TexCoords;

uniform sampler2D colorTexture; uniform sampler2D bloomTexture;
uniform bool bloom; uniform float exposure;

void main() {             
    vec3 firstColor = texture(colorTexture, TexCoords).rgb;      
    vec3 secondColor = texture(bloomTexture, TexCoords).rgb;
    FragColor = vec4(firstColor+secondColor, 1.0f); 
}

#version 330 core
layout (location = 0) in vec3 position; layout (location = 1) in vec2 texCoords;

out vec2 TexCoords;

void main() {
    gl_Position = vec4(position, 1.0f);
    TexCoords = texCoords; 
}

Basically, what happens is that the first image bound goes through, and is accessible. Then, the second is the same as the first! If I change the order in which I bind the images, then the other image becomes the one sent in.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[1]);
glUniform1i(glGetUniformLocation(bloomShader.program, "bloom"), bloom);
glUniform1f(glGetUniformLocation(bloomShader.program, "exposure"), exposure);
RenderQuad();

The above code is the section I'm talking about. Each of the pingpongcolorbuffers is attached to its own FBO. I am trying to send those in. Is there some reason that would limit me to sending in only one?

Here is the combined result, before HDR: enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Even though you are binding the textures correctly with glActiveTexture and glBindTexture you are not telling GLSL which sampler uniform goes to which texture. What you should do is this:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorBuffers[0]);
glUniformi(glGetUniformLocation(bloomShader.program, "colorTexture"), colorBuffers[0]);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[1]);
glUniformi(glGetUniformLocation(bloomShader.program, "bloomTexture"), pingpongColorbuffers[1]));
\$\endgroup\$
1
  • \$\begingroup\$ THANK YOU. I changed the last bit to 0 or 1 for the unit and it worked. You have saved me so much time and confusion!!!! \$\endgroup\$
    – Yudrist
    Jul 23, 2016 at 17:10

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .