为什么无法从我的灯光着色器访问G-Buffer?

dan*_*jar 5 c++ opengl rendering framebuffer

我在我的引擎中实现了一个新的渲染管道,渲染现在已经破坏了.当我直接将G-Buffer的纹理绘制到屏幕时,它会正确显示.所以G-Buffer很好.但不知何故,照明通道会造成麻烦.即使我没有使用它的结果纹理,但尝试在光照通过后从G-Buffer显示反照率,它显示纯灰色.

我无法解释这种行为,奇怪的是,任何时候都没有OpenGL错误.

顶点着色器绘制全屏四边形.

#version 330

in vec4 vertex;

out vec2 coord;

void main()
{
    coord = vertex.xy;
    gl_Position = vertex * 2.0 - 1.0;
}
Run Code Online (Sandbox Code Playgroud)

用于照明的片段着色器.

#version 330

in vec2 coord;
out vec3 image;

uniform int type = 0;
uniform sampler2D positions;
uniform sampler2D normals;
uniform vec3 light;
uniform vec3 color;
uniform float radius;
uniform float intensity = 1.0;

void main()
{
    if(type == 0) // directional light
    {
        vec3 normal = texture2D(normals, coord).xyz;
        float fraction = max(dot(normalize(light), normal) / 2.0 + 0.5, 0);
        image = intensity * color * fraction;
    }
    else if(type == 1) // point light
    {
        vec3 pixel = texture2D(positions, coord).xyz;
        vec3 normal = texture2D(normals, coord).xyz;
        float dist = max(distance(pixel, light), 1);
        float magnitude = 1 / pow(dist / radius + 1, 2);
        float cutoff = 0.4;
        float attenuation = clamp((magnitude - cutoff) / (1 - cutoff), 0, 1);
        float fraction = clamp(dot(normalize(light - pixel), normal), -1, 1);
        image = intensity * color * attenuation * max(fraction, 0.2);
    }
}
Run Code Online (Sandbox Code Playgroud)

照明通道的目标和采样器.纹理ID分别映射到附件着色器位置.

unordered_map<GLenum, GLuint> targets;
targets.insert(make_pair(GL_COLOR_ATTACHMENT2, ...)); // light
targets.insert(make_pair(GL_DEPTH_STENCIL_ATTACHMENT, ...)); // depth and stencil

unordered_map<string, GLuint> samplers; 
samplers.insert(make_pair("positions", ...)); // positions from G-Buffer
samplers.insert(make_pair("normals", ...)); // normals from G-Buffer
Run Code Online (Sandbox Code Playgroud)

绘制照明通道的功能.

void DrawLights(unordered_map<string, GLuint> Samplers, GLuint Program)
{
    auto lis = Entity->Get<Light>();

    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);

    glUseProgram(Program);
    int n = 0; for(auto i : Samplers)
    {
        glActiveTexture(GL_TEXTURE0 + n);
        glBindTexture(GL_TEXTURE_2D, i.second);
        glUniform1i(glGetUniformLocation(Program, i.first.c_str()), n);
        n++;
    }

    mat4 view = Entity->Get<Camera>(*Global->Get<unsigned int>("camera"))->View;

    for(auto i : lis)
    {
        int type = i.second->Type == Light::DIRECTIONAL ? 0 : 1;
        vec3 pos = vec3(view * vec4(Entity->Get<Form>(i.first)->Position(), !type ? 0 : 1));
        glUniform1i(glGetUniformLocation(Program, "type"),      type);
        glUniform3f(glGetUniformLocation(Program, "light"),     pos.x, pos.y, pos.z);
        glUniform3f(glGetUniformLocation(Program, "color"),     i.second->Color.x, i.second->Color.y, i.second->Color.z);
        glUniform1f(glGetUniformLocation(Program, "radius"),    i.second->Radius);
        glUniform1f(glGetUniformLocation(Program, "intensity"), i.second->Intensity);

        glBegin(GL_QUADS);
            glVertex2i(0, 0);
            glVertex2i(1, 0);
            glVertex2i(1, 1);
            glVertex2i(0, 1);
        glEnd();
    }

    glDisable(GL_BLEND);
    glActiveTexture(GL_TEXTURE0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindTexture(GL_TEXTURE_2D, 0);
}
Run Code Online (Sandbox Code Playgroud)

dan*_*jar 0

我发现了这个错误,而且这是一个非常愚蠢的错误。旧的渲染管道在调用该通道的绘制函数之前绑定了正确的帧缓冲区。但新的函数没有这样做,因此每个绘制函数都必须自己完成此操作。因此我想更新所有的绘制函数,但是我错过了光照通道的绘制函数。

因此,G-Buffer 的帧缓冲区仍然被绑定,并且光照通道改变了它的目标。

感谢你们,你们没有任何改变来发现这个错误,因为我没有发布我完整的管道系统。