OpenGL Framebuffer - 渲染到纹理

时间:2014-06-02 15:29:35

标签: c++ opengl textures framebuffer

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

当注释掉框架缓冲区时,我的场景如下所示:

enter image description here

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

Framebuffer辅助类(仅限重要方法):

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

加上screenQuad着色器: 顶点:

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

Shader助手类,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;
}

和Particles.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();
}

UPDATE2:

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

UPDATE3: 随机代码更改告诉我,如果我通过将深度纹理附加到帧缓冲区来评论线条,则粒子颜色最终会传递到纹理,我可以在屏幕四边形上看到它(但没有任何深度测试。红色粒子(最后绘制的)始终打开前面)。

我猜连接粒子着色器与深度纹理存在问题。但我仍然找不到确切的错误。我希望我的消化能有所帮助。

2 个答案:

答案 0 :(得分:3)

我没有研究过整个代码,但是有一个问题立即跳出来了:

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

纹理采样器的均匀值不是纹理ID(也称为名称)。它是纹理绑定的纹理单元。所以在这种情况下,因为你正在使用纹理单元1来表示这个纹理,所以它应该是:

shaderProgram.setUniform("u_colortex", 1);

答案 1 :(得分:2)

问题是我调用glDepthMask()glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);被禁用了。

需要启用它才能使glClear(GL_DEPTH_BUFFER_BIT)产生任何效果。

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