只有3个组件时OpenGL无法读取帧缓冲区附件,但有4个组件时可以读取

tv6aics1  于 2023-02-08  发布在  其他
关注(0)|答案(1)|浏览(142)

我目前正在OpenGL中进行延迟渲染,并且尝试将位置和法线数据从几何通道传递到照明通道。
然而,任何非GL_RGBA类型的帧缓冲区附件在作为纹理读取时会给予空值。我已经设法将其归结为一个事实,即任何没有4个组件的帧缓冲区附件都无法工作。
我该怎么补救?我错过了什么?
C++代码:

// Geometry Buffer
gbuffer::gbuffer(uint32_t _width, uint32_t _height) {
    // Create GL buffer.
    glCreateFramebuffers(1, &handle_);

    // Colour attachments.
    colour_attachments_.resize(num_gbuffer_attachments);
    colour_attachments_[gbuffer_position] = std::make_shared<texture_2d>("position", _width, _height, GL_RGB16F); // Only 3 components, does not work.
    colour_attachments_[gbuffer_normal] = std::make_shared<texture_2d>("normal", _width, _height, GL_RGB16F); // Only 3 components, does not work.
    colour_attachments_[gbuffer_albedo] = std::make_shared<texture_2d>("albedo_colour", _width, _height, GL_RGBA8); // 4 components, no problem.
    colour_attachments_[gbuffer_specular] = std::make_shared<texture_2d>("specular_colour", _width, _height, GL_RGBA8);  // 4 components, no problem.
    colour_attachments_[gbuffer_gloss] = std::make_shared<texture_2d>("gloss", _width, _height, GL_R32F); // Only 1 component, does not work.
    for (auto i = 0; i < colour_attachments_.size(); ++i) {
        glNamedFramebufferTexture(handle_, GL_COLOR_ATTACHMENT0 + i, colour_attachments_[i]->handle(), 0);
    }

    // Depth-Stencil attachments.
    depth_stencil_attachment_ = std::make_shared<texture_2d>("depth_stencil", _width, _height, sized_format::depth24_stencil8);
    glNamedFramebufferTexture(handle_, GL_DEPTH_STENCIL_ATTACHMENT, depth_stencil_attachment_->handle(), 0);
}

GLSL代码:

uniform sampler2D u_texture_gbuffer_position;
uniform sampler2D u_texture_gbuffer_normal;
uniform sampler2D u_texture_gbuffer_albedo;
uniform sampler2D u_texture_gbuffer_specular;
uniform sampler2D u_texture_gbuffer_gloss;

const vec3 position = texture(u_texture_gbuffer_position, io_tex_coord.xy).rgb; // All zeros.
const vec3 normal = texture(u_texture_gbuffer_normal, io_tex_coord.xy).rgb; // All zeroes.
const vec4 albedo = texture(u_texture_gbuffer_albedo, io_tex_coord.xy); // Has correct values.
const vec4 specular = texture(u_texture_gbuffer_specular, io_tex_coord.xy); // Has correct values.
const float gloss = texture(u_texture_gbuffer_gloss, io_tex_coord.xy).r; // All zeroes.
cgh8pdjw

cgh8pdjw1#

我修正了这个问题。GL_BLEND被启用,这意味着任何alpha值为0的片段都将被丢弃。
因此,即使我正在写入一个只有1或3个组件的纹理,因为默认情况下,它们的alpha值是0.0f,甚至它们的RGB值也因为某种原因而被丢弃。
因此,解决方案是在我的几何体过程中禁用GL_BLEND,以确保所有纹理都被正确写入,而不会丢弃它们的值。

相关问题