Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QOpenGLFrameBufferObject for 3D Objects



  • Hi,
    I have been trying to bind my openGL objects to a QOPenGLFrameBuferObject. I've followed what I could on the net, and have ended up with something like:

            QOpenGLFramebufferObjectFormat fboFormat;
            fboFormat.setAttachment(QOpenGLFramebufferObject::Depth);
            fbo = new QOpenGLFramebufferObject(bufferSize, fboFormat);
            auto bindRet = fbo->bind();
            // Draw here (some glbegins and glends on QUADS)
            buildingBuffer->release();
            // Find random method to blit later
    

    I also found some people setting QSurfaceFormat. I personally don't see the point.

    Now, all of this is happening inside a QOpenGLWidget which has been initialized and is creates a canvas and everything (I got no idea how though). I have currently not done any blitting or anything and I expected my current draws to no longer be visible. I on the other hand am seeing the objects on the canvas (only the last one drawn).

    Any suggestions? I'd love to go though some sample program for achieving something similar. If someone could point me to some code repository for the same, I'd be much obliged.

    Thanks



  • @Ankit.Jain said in QOpenGLFrameBufferObject for 3D Objects:

    buildingBuffer

    What is buildingBuffer ? You create an FBO called fbo, then you don't do anything with it in the few lines of code you shared, so it can't have any particular effect. You bind buildingBuffer, but you don't show what it is. As a result, I have no idea what that code does, nor what exactly you want it to do.

    Any suggestions?

    Well, explain what you are actually trying to do. Then explain what's going wrong. Then share how you are actually doing it.



  • @wrosecrans Ah sorry about that. I have a lot of code juggling there.

    I've moved the code to a separate function over time.
    New Code:

    QOpenGLFramebufferObject* SBuildingEditorUtils::getFboForModel(Model mObj)
    {
        glPushMatrix();
        QSurfaceFormat format;
        format.setMajorVersion(4);
        format.setMinorVersion(3);
    
        QWindow window;
        window.setSurfaceType(QWindow::OpenGLSurface);
        window.setFormat(format);
        window.create();
    
        QOpenGLContext context;
        context.setFormat(format);
        if (!context.create())
            qFatal("Cannot create the requested OpenGL context!");
        context.makeCurrent(&window);
    
        // TODO: fbo size = model size
        QOpenGLFramebufferObjectFormat fboFormat;
        fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
    
        auto fbo = new QOpenGLFramebufferObject(1500, 1500, fboFormat);
        auto res = glGetError();
        auto bindRet = fbo->bind();
        glViewport(0, 0, 1500, 1500);
        glEnable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION); 
        glPushMatrix(); 
        glLoadIdentity(); 
        glOrtho(0, 1500, 0, 1500, 0, 1500); 
        glMatrixMode(GL_MODELVIEW);
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    
        glBegin(GL_POLYGON);
        glVertex3d(500, 500, 500);
        glVertex3d(500, 1000, 1000);
        glVertex3d(1000, 1000, 1000);
        glVertex3d(1000, 500, 500);
        glEnd();
        glDisable(GL_DEPTH_TEST);
        fbo->toImage().save("uniqueName.png");
        fbo->release();
        fbo->bindDefault();
        glPopMatrix();
    
        return fbo;
    }
    

    Requirement: Create a model in an FBO for each model details object (contains multiple planes and for each, coordinates are defined) to blit later.
    So, I created the above code to convert the model details to an FBO. It's resulting in blank "uniqueName.png" images.

    P.S. For testing purposes, I've reduced the drawing to draw a single plane.



  • Off the top of my head, I am not sure if fbo::save() is valid while the FBO is still bound as the target for drawing. Try releasing it first and then calling save. An FBO can only be used as a texture when it is not bound as a target -- I think that applies to all readback operations.

    Also, you have a glClearColor, but you never glClear(). If there is garbage in the depth buffer of the FBO, maybe your stuff is never getting drawn. glClear() the depth buffer and the image buffer before you draw. At very least you should be able to see the clear color in the FBO when you save it, so you know the binding is working at that point.


Log in to reply