当前位置: 首页 > 知识库问答 >
问题:

OpenGL帧缓冲区-渲染到纹理

高建本
2023-03-14

我做了一个在上面渲染天空盒和粒子的应用程序。我想添加一些效果,我需要使用帧缓冲区来渲染天空盒、粒子颜色、深度和位置以分离纹理。然后我想使用简单的着色器来使用这些纹理中的值并以适当的方式混合它们。我为纹理、帧缓冲区和屏幕四边形(渲染简单的矩形)编写了辅助类,但不幸的是——当我尝试使用它时没有任何渲染。

当绑定帧缓冲区被注释掉时,我的场景如下所示:

修改着色器表明深度和位置值计算正确。因此问题在于纹理和帧缓冲区的使用方式。一些代码的时间:

帧缓冲区帮助器类(仅重要方法):

void Framebuffer::init(){
    // unbind all textures from openGL
    glBindTexture(GL_TEXTURE_2D, 0);
    glGenFramebuffers(1, &framebuffer);
}

void Framebuffer::bind(){
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}

void Framebuffer::unbind(){
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void Framebuffer::attachTexture(GLuint texture, GLenum attachmentType){
    glBindTexture(GL_TEXTURE_2D, texture);    
    glFramebufferTexture(GL_FRAMEBUFFER, attachmentType, texture, 0);
}

void Framebuffer::drawBuffers(GLsizei n, const GLenum *buffers){
    glDrawBuffers(n, buffers);
}

纹理助手类:

void Texture::init(GLuint windowWidth, GLuint windowHeight, GLint internalFormat, GLenum format, GLenum type){
    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &texture);

    glBindTexture(GL_TEXTURE_2D, texture);  

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexImage2D( GL_TEXTURE_2D, 0, internalFormat , windowWidth, windowHeight, 0, format, type, 0);

    glBindTexture(GL_TEXTURE_2D, 0);
}

void Texture::bind(){
    glBindTexture(GL_TEXTURE_2D, texture);  
}

void Texture::unbind(){
    glBindTexture(GL_TEXTURE_2D, 0);    
}

GLuint Texture::getId(){
    return texture; 
}

ScreenQuad类:

void ScreenQuad::init(void){
    vao.createVAO();
    vao.bindVAO();

    vbo.createVBO();
    vbo.addData(vertices, 8*sizeof(GLfloat));

    vbo.bindVBO(GL_ARRAY_BUFFER);
    vbo.uploadDataToGPU(GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)3, 2, GL_FLOAT, GL_FALSE, 0, NULL);

    loadShaders("shaders/basicPostShader.vp", "shaders/basicPostShader.fp");
}

void ScreenQuad::loadShaders(string vsPath, string fsPath){
    shaderProgram.createProgram();
    shaderProgram.loadVertexShader(vsPath);
    shaderProgram.loadFragmentShader(fsPath);

    glBindAttribLocation(shaderProgram.getProgramID(), 3, "v_coord");

    shaderProgram.linkProgram();
}

void ScreenQuad::draw(GLuint depthTexture, GLuint colorTexture, GLuint positionTexture, GLuint backgroundTexture){
    shaderProgram.bindProgram();
    glEnable(GL_TEXTURE_2D);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, depthTexture);
    shaderProgram.setUniform("u_depthtex", 0);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, colorTexture);
    shaderProgram.setUniform("u_colortex", 1);
    glActiveTexture(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, positionTexture);
    shaderProgram.setUniform("u_positiontex", 2);
    glActiveTexture(GL_TEXTURE3);
    glBindTexture(GL_TEXTURE_2D, backgroundTexture);
    shaderProgram.setUniform("u_backgroundtex", 3);

    glEnableVertexAttribArray(3);
    vbo.bindVBO();

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    vbo.unbindVBO();
    glDisableVertexAttribArray(3);

    shaderProgram.unbindProgram();
}

以及初始化和渲染场景的方法:

void OpenGLContext::setupScene(void) {
    glClearColor(0.4f, 0.6f, 0.9f, 1.0f);

    //FRAMEBUFFERS:

    skyboxFramebuffer.init();
    skyboxTexture.init(windowWidth, windowHeight, GL_RGBA32F, GL_RGBA, GL_FLOAT);
    skyboxFramebuffer.bind();
    skyboxFramebuffer.attachTexture(skyboxTexture.getId(), GL_COLOR_ATTACHMENT0);
    const GLenum skyboxDrawBuffers[1] = { GL_COLOR_ATTACHMENT0};
    skyboxFramebuffer.drawBuffers(1, skyboxDrawBuffers);
    skyboxFramebuffer.validate();
    skyboxFramebuffer.unbind();

    mainFramebuffer.init();
    mainColorTexture.init(windowWidth, windowHeight, GL_RGBA32F, GL_RGBA, GL_FLOAT);
    mainPositionTexture.init(windowWidth, windowHeight, GL_RGBA32F, GL_RGBA, GL_FLOAT);
    mainDepthTexture.init(windowWidth, windowHeight, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT);
    mainFramebuffer.bind();
    mainFramebuffer.attachTexture(mainColorTexture.getId(), GL_COLOR_ATTACHMENT0);
    mainFramebuffer.attachTexture(mainPositionTexture.getId(), GL_COLOR_ATTACHMENT1);
    mainFramebuffer.attachTexture(mainDepthTexture.getId(), GL_DEPTH_ATTACHMENT);
    const GLenum mainDrawBuffers[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
    mainFramebuffer.drawBuffers(2, mainDrawBuffers);
    mainFramebuffer.validate();
    mainFramebuffer.unbind();

    //SKYBOX:

    skybox->init("resources/skybox/default/",
        "pos_x.tga",
        "neg_x.tga",
        "pos_y.tga",
        "neg_y.tga",
        "pos_z.tga",
        "neg_z.tga");

    //PARTICLES:

    particles->init(scene);

    //SCREENQUAD:

    screenQuad.init();
}

void OpenGLContext::renderScene() {
    glfwGetFramebufferSize(window, &windowWidth, &windowHeight);

    glViewport(0, 0, windowWidth, windowHeight);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    fpsCounter->calcFPS(1.0, windowName);
    if(mode==INPUT_ENABLED_MODE){
        updateInputs();
    }

    projectionMatrix = controls->getProjectionMatrix();
    viewMatrix = controls->getViewMatrix();
    modelMatrix = glm::mat4(1.0f);

    glm::mat4 mvpMatrix = projectionMatrix*viewMatrix*modelMatrix;

    //SKYBOX:
    skyboxFramebuffer.bind();
    skybox->render(mvpMatrix);
    skyboxFramebuffer.unbind();
    //PARTICLES:

    if(scene->tryLockScene()){
        if(scene->isSceneUpdated()){
            particles->updateParticlesPosition(scene);
            scene->setSceneUpdated(false);
        }
        scene->unlockScene();
    }
    mainFramebuffer.bind();
    particles->draw(modelMatrix, viewMatrix, projectionMatrix);
    mainFramebuffer.unbind();
    //SCREENQUAD:

    screenQuad.draw(mainDepthTexture.getId(), mainColorTexture.getId(), mainPositionTexture.getId(), skyboxTexture.getId());

    glfwSwapBuffers(window);
    glfwPollEvents();
}

加屏幕四着色器:顶点:

#version 430

layout (location = 3) in vec2 v_coord;
layout (binding = 0) uniform sampler2D u_depthtex;
layout (binding = 1) uniform sampler2D u_colortex;
layout (binding = 2) uniform sampler2D u_positiontex;
layout (binding = 3) uniform sampler2D u_backgroundtex;
out vec2 fs_texcoord;

void main(void) {
  gl_Position = vec4(v_coord, 0.0, 1.0);
  fs_texcoord = (v_coord + 1.0) / 2.0;
}

和片段:

#version 430

layout (binding = 0) uniform sampler2D u_depthtex;
layout (binding = 1) uniform sampler2D u_colortex;
layout (binding = 2) uniform sampler2D u_positiontex;
layout (binding = 3) uniform sampler2D u_backgroundtex;
layout (location = 0) out vec4 out_Color;
in vec2 fs_texcoord;

void main(void) {

  float exp_depth = texture(u_depthtex,fs_texcoord).r;

  if(exp_depth>0.99f){
    out_Color = vec4(texture(u_backgroundtex,fs_texcoord).xyz,1.0f);
    return;
  }

  out_Color = vec4(texture(u_colortex,fs_texcoord).xyz, 1.0f);
}

着色器助手类、vao和vbo助手类肯定没问题。日志中不会发生错误。

更新:粒子顶点着色器:

#version 430

uniform mat4x4 modelViewMatrix;
uniform mat4x4 projectionMatrix;

uniform float pointRadius;  // point size in world space
uniform float pointScale;   // scale to calculate size in pixels

layout (location = 0) in vec3 in_Position;
layout (location = 1) in vec4 in_Color;

out vec3 fs_PosEye;
out vec4 fs_Position;
out vec4 fs_Color;

void main(void) {

    vec3 posEye = (modelViewMatrix *  vec4(in_Position.xyz, 1.0f)).xyz;
    float dist = length(posEye);
    gl_PointSize = pointRadius * (pointScale/dist);

    fs_PosEye = posEye;
    fs_Position = modelViewMatrix *  vec4(in_Position.xyz, 1.0f);
    fs_Color = in_Color;
    gl_Position = projectionMatrix * modelViewMatrix  *  vec4(in_Position.xyz, 1.0f);

}

片段着色器:

#version 430

uniform mat4x4 modelViewMatrix;
uniform mat4x4 projectionMatrix;

uniform float pointRadius;  // point size in world space
uniform float pointScale;   // scale to calculate size in pixels

in vec4 fs_Position;
in vec3 fs_PosEye;
in vec4 fs_Color;

layout (location = 0) out vec4 out_Color;
layout (location = 1) out vec4 out_Position;

void main(void)
{
    // calculate normal from texture coordinates
    vec3 normal;
    normal.xy = gl_PointCoord.xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
    float r = dot(normal.xy, normal.xy);

    if(r>1.0) 
        discard;

    normal.z = sqrt(1.0-r);

    //calculate depth

    vec4 pixelPos = vec4(fs_PosEye + normalize(normal)*pointRadius,1.0f);
    vec4 clipSpacePos = projectionMatrix * pixelPos;
    gl_FragDepth = (clipSpacePos.z / clipSpacePos.w);

    out_Color = fs_Color;
    out_Position = pixelPos;
}

还有粒子。draw()方法:

void CParticles::draw(glm::mat4 modelMatrix, glm::mat4 viewMatrix, glm::mat4 projectionMatrix){
    shaderProgram.bindProgram();

    glm::mat4 modelViewMatrix = viewMatrix*modelMatrix;

    shaderProgram.setUniform("projectionMatrix", &projectionMatrix);
    shaderProgram.setUniform("modelViewMatrix", &modelViewMatrix);

    shaderProgram.setUniform("pointRadius", &pointRadius);
    shaderProgram.setUniform("pointScale", &pointScale);

    glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
    glEnable(GL_POINT_SPRITE);
    glEnable(GL_PROGRAM_POINT_SIZE);

    glDepthMask(GL_TRUE);
    glEnable(GL_DEPTH_TEST);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_POINTS, 0, n);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    glDisable(GL_PROGRAM_POINT_SIZE);
    glDisable(GL_POINT_SPRITE);

    shaderProgram.unbindProgram();
}

更新2:

问题是,当我尝试在screenQuad着色器中从粒子着色器填充的纹理中采样数据时,它们是空的。每个深度、位置和颜色纹理采样器返回零。我使用与skybox相同的类和方法,但skybox纹理效果很好。

更新3:随机代码的变化告诉我,如果我在评论行中将深度纹理附加到帧缓冲区,粒子颜色最终会传递到一个纹理,我可以在屏幕上看到它(但没有任何深度测试。红色粒子(最后绘制)总是在前面)。

我想将粒子着色器与深度纹理连接起来可能有问题。但我还是找不到确切的虫子。我希望我的建议会有帮助。

共有2个答案

贝阳泽
2023-03-14

问题是当我调用glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER|BIT | GL_STENCIL_BUFFER_BIT | GL_STENCIL_BUFFER|BIT |

需要启用它才能使glClear(GL_DEPTH_BUFFER_BIT)生效。

另外,我还需要以适当的方式添加清理帧缓冲区。

屠锐
2023-03-14

我还没有研究完整的代码,但一个问题马上就出现了:

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, colorTexture);
shaderProgram.setUniform("u_colortex", colorTexture);

纹理采样器的统一值不是纹理id(又名)。它是纹理绑定到的纹理单元。因此,在这种情况下,由于您将纹理第一单元用于此纹理,它应该是:

shaderProgram.setUniform("u_colortex", 1);
 类似资料:
  • 这是更大项目的一部分,基本上,我用以下方式创建带有颜色、深度和模具缓冲区的帧缓冲区: 我用以下方法将其绑定: 当我转储附加的纹理: 我收到主帧缓冲区(0)的内容。 知道我哪里错了吗?先谢谢你。

  • 这是我试图执行的主要程序: 但是,我看到的不是纹理而是白色矩形。如果我在屏幕上渲染,我会得到预期的结果(因此渲染代码应该没有问题)。这是FrameBuffer类: 其他一切都经过测试,所以问题应该在主程序的逻辑或帧缓冲区中。 顺便说一下,视口大小iz与窗口大小完全相同。

  • 我正在使用JOGL,但这个问题一般适用于OpenGL。似乎存在类似的问题,但它们要么针对GLSL代码,要么与复制帧缓冲区的内容有关,要么是一般建议-使用帧缓冲区对象,而不是。 我正在做一些阴影映射。如何使用帧缓冲对象将深度通道直接渲染到纹理? 能否请你贴一段初始化纹理和帧缓冲对象的代码,以及渲染场景前初始化一切的代码? 目前,我使用<code>glCopyTexSubImage2D<code>。我

  • 我正在尝试使用OpenGL GLSL着色器渲染到纹理。首先,我尝试用白色填充30x30纹理的每个像素。我将顶点着色器索引从0传递到899,表示纹理的每个像素。对吗?顶点着色器: 片段着色器:

  • 我是OpenGL的新手。我想使用帧缓冲区读取并保存渲染场景中的所有深度值。我设法将其设置为附加到深度组件的帧缓冲区。但是当我将深度纹理渲染到默认帧缓冲区(显示窗口)时,它只显示白色,即使我线性化了深度值(教程从https://learnopengl.com/开始)。当我在默认帧缓冲区片段着色器中从gl_FragCoord. z访问深度值时,我会得到一个深度图,并且绘图也可以,但是当将深度作为纹理从

  • 我已经找了几个小时来解决我的问题。首先我有高清7800系列amd GPU,最新的驱动程序。 我正在尝试创建一个framebuffer类,它拥有我需要的所有framebuffer函数。看起来我让renderbuffers开始工作了,但绘制到纹理会导致非常奇怪的错误。 GLCHECKFRAMEBERSTATUS在两次检查时返回GL_FRAMEBUFFER_Completed_附件。createDept