简体   繁体   English

帧缓冲区不能存储浮点数

[英]Framebuffers can't store floating point numbers

I've tried to implement deferred shading but the gbuffer can't store any floating point numbers. 我尝试实现延迟着色,但是gbuffer无法存储任何浮点数。 The edges from the lighting is VERY rough since the framebuffer can't store negative components for the normals eventhough i passed in the correct internal format and data type. 照明的边缘非常粗糙,因为即使我以正确的内部格式和数据类型传递,帧缓冲区也无法存储法线的负分量。

Example picture of the rough edges 粗糙边缘的示例图片

Texture generation: 纹理生成:

    public TextureResource(int width, int height, int filter, int internalFormat, int format, boolean clamp, int dataType, ByteBuffer data) {
    id = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, id);
    glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, dataType, data);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
    if(clamp) {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

Framebuffer generation: 帧缓冲区生成:

this.width = width;
    this.height = height;
    this.attachments = attachments;
    boolean hasDepth = false;
    IntBuffer drawBuffers = Util.createIntBuffer(attachments.length);
    assert(attachments.length <= MAX_ATTACHMENTS);
    framebuffer = glGenFramebuffers();
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    for(int i = 0; i < attachments.length; i++) {
        if(attachments[i].getAttachmentType() == GL_DEPTH_ATTACHMENT) {
            drawBuffers.put(GL_NONE);
            hasDepth = true;
        } else {
            drawBuffers.put(attachments[i].getAttachmentType());
        }
        if(attachments[i].getAttachmentType() == GL_NONE) {
            continue;
        }
        attachments[i].createTexture(width, height);
        glFramebufferTexture2D(GL_FRAMEBUFFER, attachments[i].getAttachmentType(), GL_TEXTURE_2D, attachments[i].getTexture().getId(), 0);
    }
    if(!hasDepth) {
        renderbuffer = glGenRenderbuffers();
        glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
    }
    drawBuffers.flip();
    glDrawBuffers(drawBuffers);
    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        System.err.println("Framebuffer creation failed!");
        System.exit(1);
    }

GBuffer: GBuffer:

super(width, height, new FramebufferAttachment(GL_COLOR_ATTACHMENT1, GL_NEAREST, GL_RGB32F, GL_RGB, false, GL_FLOAT),
                         new FramebufferAttachment(GL_COLOR_ATTACHMENT0, GL_NEAREST, GL_RGB32F, GL_RGB, false, GL_FLOAT),
                         new FramebufferAttachment(GL_COLOR_ATTACHMENT2, GL_NEAREST, GL_RGB, GL_RGB, false, GL_UNSIGNED_BYTE));

Geometry pass fragment shader: 几何图形通过片段着色器:

    #version 330 core

in vec3 worldPos0;
in vec2 texCoord0;
in vec3 normal0;

layout (location = 0) out vec3 gWorldPos;
layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec3 gColor;

uniform sampler2D diffuse;

void main() {
    gWorldPos = worldPos0;
    gNormal = normalize(normal0);
    gColor = texture(diffuse, texCoord0).xyz;
}

It's because it is 3.3 core. 这是因为它是3.3核心。 OpenGL standard somewhat contradicts itself on the matter of what format are renderable, some implementations actually support RGBxF formats. OpenGL标准在可渲染的格式方面有些矛盾,某些实现实际上支持RGBxF格式。 OpenGL 4.4 specs declare them renderable. OpenGL 4.4规范声明它们可渲染。

Ifit is really needed, I think is it possible to map integer values to floating point. 如果确实需要,我认为可以将整数值映射到浮点数。 Ofc you still have limited amount of values, but you can implement negative and fraction values, while having limited range Ofc值的数量仍然有限,但是您可以在范围有限的情况下实现负值和分数值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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