I’ve been trying to implement the bloom effect, and it’s really, really easy to understand and implement. Except that I just can’t implement it, because there seems to be something hinky going on with my fragment shader. I’ve got multiple render targets. I’m rendering to GL_COLOR_ATTACHMENT0 and GL_COLOR_ATTACHMENT1. I’m using framebuffers to render to textures.
Before I explain my problem, here’s my fragment shader code:
#version 400 core
in vec4 passColour;
in vec3 passNormals;
in vec3 toLightVectors[16];
in vec3 passCamDir;
layout (location = 0) out vec4 outColour;
layout (location = 1) out vec4 brightColour;
void main(void){
vec3 surfaceNormal = normalize(passNormals);
float diffuse = 0;
float specular = 0;
for(int i = 0; i < 16; i++){
vec3 surfaceToLightVector = normalize(toLightVectors[i]);
diffuse += max(dot(surfaceNormal, surfaceToLightVector), 0.02);
float angle = dot(reflect(-surfaceToLightVector, surfaceNormal), passCamDir);
float spec = pow(max(angle, 0.02), 4);
specular += spec;
}
outColour = (diffuse + specular) * passColour;
float brightness = dot(outColour.rgb, vec3(0.2126, 0.7152, 0.0722));
if(brightness > 1.0){
brightColour = vec4(outColour.rgb, 1.0);
}
}
You can ignore the diffuse and specular lighting calculations at the top. Take a look at the last few lines, where I declare a variable called “brightness” and I use an if statement to determine if the brightness level of the fragment is exceeding 1.0.
GL_COLOR_ATTACHMENT0 is where I render the entire scene normally, with the lighting and everything. GL_COLOR_ATTACHMENT1 is where all the fragments that are brighter than 1.0 are rendered. brightColour is the colour output for GL_COLOR_ATTACHMENT1, but for some reason it’s rendering all of the fragments to GL_COLOR_ATTACHMENT1, even the ones that are definitely not brighter than 1.0
So my first thought was that there’s something up with the if statement. And there was.
Here are a list of conditions that I tried in the if statement:
brightness > 100.0 == true
brightness > 10000.0 == true
brightness < 0.0 && brightness > 1.0 == true (how is this even possible???)
So there’s definitely something wrong here. But I just can’t figure out what the heck’s going on. The most relevant answer that I got on the Internet is completely irrelevant (which means I can’t get any answers or clues). I tried several different ways to figure out what’s wrong, but I couldn’t figure anything out.
I can’t fix a problem if I don’t even know what the problem is, so I’d appreciate any help you guys can offer.
I also checked if 1 == 2, because I couldn’t think of anything else. 1 == 2 is still false, but I just don’t see how brightness < 0 && brightness > 1 can be true.
I’m not asking for an answer, I just need a push in the right direction.