简体   繁体   中英

OpenGL Additive blending causes white screen

I'm working on a deferred shading project and I've got a problem with blending all the lights into the final render.

Basically I'm just looping over each light and then rendering the fullscreen quad with my shader that does lighting calculations but the final result is just a pure white screen. If I disable blending, I can see the scene fine but it will be lit by one light.

void Render()
{
    FirstPass();
    SecondPass();
}

void FirstPass()
{
    glDisable(GL_BLEND);
    glEnable(GL_DEPTH);
    glDepthMask(GL_TRUE);

    renderTarget->BindFrameBuffer();
    gbufferShader->Bind();

    glViewport(0, 0, renderTarget->Width(), renderTarget->Height());
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    for (int i = 0; i < meshes.size(); ++i)
    {
        // set uniforms and render mesh
    }
    renderTarget->UnbindFrameBuffer();
}

EDIT: I'm not rendering light volumes/geometry, i'm just calculating final pixel colours based on the lights (point/spot/directional).

void SecondPass()
{
    glDepthMask(GL_FALSE);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);

    renderTarget->BindTextures();

    pointLightShader->Bind();

    glViewport(0, 0, renderTarget->Width(), renderTarget->Height());
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    for (int i = 0; i < lights.size(); ++i)
    {
        // set uniforms
        // shader does lighting calculations 
        screenQuad->Render();
    }
    renderTarget->UnbindTextures();
}

I can't imagine there being anything special to do in the shader other than output a vec4 for the final frag colour each time?

This is the main part of the pointLight fragment shader:

out vec4 FragColour;

void main() 
{
    vec4 posData = texture(positionTexture, TexCoord);
    vec4 normData = texture(normalTexture, TexCoord);
    vec4 diffData = texture(diffuseTexture, TexCoord);

    vec3 pos = vec3(posData);
    vec3 norm = vec3(normData);
    vec3 diff = vec3(diffData);

    float a = posData.w;
    float b = normData.w;
    float c = diffData.w;

    FragColour = vec4(shadePixel(pos, norm, diff), 1.0);
}

But yeah, basically if I use this blend the whole screen is just white.

Well I fixed it, and I feel like an idiot now :)

My opengl was set to

glClearColor(1.0, 1.0, 1.0, 1.0); 

which (obviously) is pure white.

I just changed it to black background

glClearColor(0.0, 0.0, 0.0, 1.0);

And now I see everything fine. I guess it was additively blending with the white background, which obviously would be white.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM