Use of QOpenGLVertexArrayObject



  • Hello,
    first of all, excuse my poor English...

    I'm programming with Qt and try to display different vehicles : car, plane, ship...
    I have a parent class 'Vehicle' and child classes : 'car', 'plane', 'ship'...
    Each member of class 'vehicle' has attributes for vertex, indexes, color, which enable me to define QOpenGLBuffer.
    'Vehicle' class also has an attribute QOpenGLVertexArrayObject.

    class Vehicle
    {
        public:
            Vehicle();
            QOpenGLVertexArrayObject* getVAO();
            void load(QOpenGLShaderProgram *m_program);
     
        protected:
            QOpenGLVertexArrayObject m_vao;
            QVector <QVector3D> m_vertices;
            QVector <QVector3D> m_color;
            QVector <int> m_indexes;
    };
    

    One widget enables me to display all these instances with OpenGL and has an attribute QOpenGLShaderProgram :

    class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
     
    public:
        GLWidget();
        void initializeGL();
        void resizeGL(int width, int height);
        void paintGL();
     
    private:
        QOpenGLShaderProgram m_program;
        QVector <Vehicle> m_vehicles;
    };
    

    Unfortunately, despite my researches in OpenGL, C++ or Qt lessons, I don't know how to display 'Vehicle' instances from the Widget...

    I thought I could load in a vector 'm_vehicle' all the instances of the child classes of 'Vehicle', then thanks to a method 'displayVehicle(Vehicle m)' called for each element of the list, load each of the VAO in the shader :

    void GLWidget::displayVehicle(Vehicle m)
    {
        m.getVAO().bind();
        glDrawElements(GL_TRIANGLES, 10*3, GL_UNSIGNED_INT, 0);
        m.getVAO().release();
    }
    

    Unfortunately, it doesn't work...
    Is it the right way to do ?

    Thanks a lot to those who could help me !!!



  • Hi, and welcome to the Qt forum! Have a look at this example, it has everything you need.



  • Thanks for your welcome, and for your answer, but in this sample there is only one object to display, and the VAO and VBO are defined in the widget class. What if I have multiple objects from various classes ?


Log in to reply
 

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