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

Why can't access the G-Buffer from my lighting shader?

本文关键字:访问 G-Buffer 我的 为什么      更新时间:2023-10-16

我在我的引擎中实现了一个新的渲染管道,现在渲染被破坏了。当我直接在屏幕上绘制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;
}

Fragment Shader for lighting.

#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);
    }
}

照明通道的目标和采样器。纹理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

照明通道的绘制函数。

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);
}

我发现了错误,这是一个愚蠢的错误。旧的渲染管道在调用该通道的draw函数之前绑定了正确的framebuffer。但新版本没有,所以每个绘制函数必须自己完成。因此我想更新所有的绘制函数,但是我错过了照明通道的绘制函数。

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

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