渲染到纹理

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

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

在这里输入图像描述

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

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着色器中从它们采样数据时,由粒子着色器填充的纹理是空的。 每个深度,位置和颜色纹理采样器返回零。 我使用与天空盒相同的类和方法,但天空盒贴图工作正常。

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

我想连接粒子着色器与深度纹理有问题。 但是我仍然无法找到确切的错误。 我希望我的消化会有帮助。


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

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

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

shaderProgram.setUniform("u_colortex", 1);

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

需要启用它才能使glClear(GL_DEPTH_BUFFER_BIT)发挥作用。

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

链接地址: http://www.djcxy.com/p/34059.html

上一篇: rendering to texture

下一篇: How to properly render to a texture in OpenGL 3.1