Render into a frame buffer with 2 color buffer attached to (OpenGL)



  • This program actually works on Xcode. And now I want to use the same program for QT for developing on multiple platforms.

    I am working on Mac OS version 10.12.2.

    The idea is to use a frame buffer with 2 attached color buffers. In later steps I want to do some calculation during the render pass. But for the moment I just want to render for each color buffer a different color, to see if it is working.

    After this render call to the frame buffer I want to take one of these textures and render the result on the screen.

    Here is my paintGL() function:

    //paintGL
    void GLWindow::paintGL()
    {
    int a = 0;
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &a);

    //reder to 2 color attached frame buffers
    glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
    glClearColor(0.0,0.0,1.0,1.0);
    
    glBindVertexArray(_vao);
    
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);
    
    
    //render to Screen (FBO -> a)
    

    ...

    update();
    

    }

    My FBO:
    glGenFramebuffers(1, &_fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, _fbo);

    //Texture 1
    glGenTextures(1, &tex.color);
    glBindTexture(GL_TEXTURE_2D, tex.color);
    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _frameSize, _frameSize, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.color, 0);
    
    //Texture 2
    glGenTextures(1, &tex.depth);
    glBindTexture(GL_TEXTURE_2D, tex.depth);
    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, _frameSize, _frameSize, 0, GL_RGB, GL_FLOAT, NULL);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex.depth, 0);
    
    glBindTexture(GL_TEXTURE_2D, 0);
    
    //Render to 2 Images
    GLuint attachement[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
    glDrawBuffers(2, attachement);
    
    //Error check
    bool check;
    GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (error == GL_FRAMEBUFFER_COMPLETE) {
        std::cout << "FBO(TSDF) successfully loadet" << std::endl;
        check = true;
    }else{
        std::cout << "FBO(TSDF) had en ERROR !!!!!!!!!!!!!!!" << std::endl;
        check = false;
    }
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    

    The render pass to the screen has no problems, it renders the texture id he gets to the screen. So somehow must be a problem of writing into the 2 buffers. But I can't tell wat, because the code is just copy and paste from the application I had in Xcode, and there it worked.

    The Shader is also straight forward:
    /* double.vsh */
    #version 330

    layout (location = 0) in vec3 a_position;
    void main(void)
    {
    gl_Position = vec4(a_position, 1.0);
    }

    /* double.fsh */
    #version 330

    layout (location = 0) out vec3 _color;
    layout (location = 1) out vec3 _depth;

    void main(void)
    {
    _color = vec3(1.0,0.0,0.0);
    _depth = vec3(1.0,0.0,1.0);
    }

    Shader also the framebuffer do not give any errors by building.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.