Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to enable depth testing in QOpenGLFrameBufferObject
Qt 6.11 is out! See what's new in the release blog

How to enable depth testing in QOpenGLFrameBufferObject

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 380 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leosunshine
    wrote on last edited by
    #1

    I want to render the scene to a texture and the QOpenGLFrameBufferObject was used, but the depth testing was not work in the rendered texture, even if I bind the a depth render buffer to the FBO. It is all right if I render the scene to the screen.

    #include "fbowidget.h"

    FBOWidget::FBOWidget(QWidget *parent): QOpenGLWidget(parent), QOpenGLFunctions_3_3_Core()
    {

    }

    void FBOWidget::initializeGL(){
    initializeOpenGLFunctions();

    vertex.create();
    GLfloat v[] = {
        0,0,-0.5,
        0,0.5,-0.5,
        -0.5,0,-0.5,
        0,0,-1,
        0,1,-1,
        -1,0,-1,
    };
    vertex.bind();
    vertex.allocate(v, 18 * sizeof(GLfloat));
    
    this->program = new QOpenGLShaderProgram();
    this->program->addShaderFromSourceFile(QOpenGLShader::Vertex, "v.glsl");
    this->program->addShaderFromSourceFile(QOpenGLShader::Fragment, "f.glsl");
    this->program->link();
    this->program->bind();
    this->vertexLocation = this->program->attributeLocation("aPosition");
    
    rectVertex.create();
    GLfloat rect[] = {
        -0.5, 0.5, -1,
        -0.5, -0.5, -1,
        0.5, -0.5, -1,
        -0.5, 0.5, -1,
        0.5, -0.5, -1,
        0.5, 0.5, -1
    };
    rectVertex.bind();
    rectVertex.allocate(rect, 18 * sizeof(GLfloat));
    
    this->rectProgram = new QOpenGLShaderProgram();
    this->rectProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "vertex.glsl");
    this->rectProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "fragment.glsl");
    this->rectProgram->link();
    this->rectProgram->bind();
    
    this->rectVertexLocation = this->rectProgram->attributeLocation("aPosition");
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    this->fbo = new QOpenGLFramebufferObject(901,571);
    
    glGenRenderbuffers(1, &this->depthTextureId);
    glBindRenderbuffer(GL_RENDERBUFFER, this->depthTextureId);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 901, 571);
    this->fbo->bind();
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthTextureId);
    

    }

    void FBOWidget::resizeGL(int w, int h){
    this->glViewport(0,0,w,h);
    }

    void FBOWidget::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0f,1.0f,0.0f,1.0f);
    this->program->bind();
    this->fbo->setAttachment(QOpenGLFramebufferObject::Depth);
    this->fbo->setAttachment(QOpenGLFramebufferObject::NoAttachment);
    this->fbo->bind();
    glEnable(GL_DEPTH_TEST);
    vertex.bind();
    this->program->setAttributeBuffer(this->vertexLocation, GL_FLOAT, 0, 3, 0);
    this->program->enableAttributeArray(this->vertexLocation);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDisableVertexAttribArray(this->vertexLocation);
    this->fbo->bindDefault();

    glBindTexture(GL_TEXTURE_2D, this->fbo->texture());
    this->fbo->toImage().save("hello.jpg");
    this->rectProgram->bind();
    
    rectVertex.bind();
    this->rectProgram->setAttributeBuffer(this->rectVertexLocation, GL_FLOAT, 0, 3, 0);
    this->rectProgram->enableAttributeArray(this->rectVertexLocation);
    
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDisableVertexAttribArray(this->rectVertexLocation);
    

    }

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved