rendering to texture

I made an application which renders skybox and particles over it. I want to add some effects and i need to use framebuffers to render skybox, particles color, depth and position to separate textures. Then i want to use simple shader to use values from these textures and mix them in a proper way. I wrote helper classes for textures, framebuffers and screen quad (simple rectangle to render) but unfortunately - nothing renders when i try to use it.

When binding framebuffers is commented out, my scene looks like this:

在这里输入图像描述

Modifying shader shows that depth and position values are calculated properly. Therefore problem lays in texture and framebuffers way of using. Time for some code:

Framebuffer helper class (only important methods):

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

Texture helper class:

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 class:

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

and methods for initialization and rendering scene:

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

plus screenQuad shaders: vertex:

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

and fragment:

#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 helper classes, vao and vbo helper classes are fine for sure. No errors occurs in logs.

UPDATE: particles vertex shader:

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

}

fragment shader:

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

and Particles.draw() method:

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:

The problem is that textures filled by a particle shader are empty when I try to sample data from them in a screenQuad shader. Each depth, position and color texture samplers return zeros. I use same classes and same methods as with a skybox, but skybox texture works fine.

UPDATE3: Random code changes showed me that if I comment line with attaching depth texture to framebuffer, particle color is finally passed to a texture and i can see it on a screen quad (but without any depth test. Red particles (drawed last) are always on the front).

I guess there is a problem with connecting particle shader with depth texture. But still I can't find an exact bug. I hope my sugestion will be helpful.


I haven't studied the entire code, but one problem jumps out immediately:

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

The value of a uniform for a texture sampler is not the texture id (aka name). It's the texture unit the texture is bound to. So in this case, since you're using texture unit 1 for this texture, it should be:

shaderProgram.setUniform("u_colortex", 1);

The problem was that glDepthMask() was disabled when i invoked glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); .

It needs to be enabled in order to glClear(GL_DEPTH_BUFFER_BIT) took any effect.

Plus I needed to add cleaning framebuffers in a proper way as well.

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

上一篇: JMS消息re

下一篇: 渲染到纹理