QOpenGLVertexArrayObject Class

The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object. More...

Header: #include <QOpenGLVertexArrayObject>
qmake: QT += gui
Since: Qt 5.1
Inherits: QObject.

Public Types

class Binder

Public Functions

QOpenGLVertexArrayObject(QObject * parent = 0)
~QOpenGLVertexArrayObject()
void bind()
bool create()
void destroy()
bool isCreated() const
GLuint objectId() const
void release()
  • 31 public functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 2 signals inherited from QObject
  • 1 public variable inherited from QObject
  • 10 static public members inherited from QObject
  • 9 protected functions inherited from QObject
  • 2 protected variables inherited from QObject

Detailed Description

The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object.

A Vertex Array Object (VAO) is an OpenGL container object that encapsulates the state needed to specify per-vertex attribute data to the OpenGL pipeline. To put it another way, a VAO remembers the states of buffer objects (see QOpenGLBuffer) and their associated state (e.g. vertex attribute divisors). This allows a very easy and efficient method of switching between OpenGL buffer states for rendering different "objects" in a scene. The QOpenGLVertexArrayObject class is a thin wrapper around an OpenGL VAO.

For the desktop, VAOs are supported as a core feature in OpenGL 3.0 or newer and by the GL_ARB_vertex_array_object for older versions. On OpenGL ES 2, VAOs are provided by the optional GL_OES_vertex_array_object extension. You can check the version of OpenGL with QOpenGLContext::surfaceFormat() and check for the presence of extensions with QOpenGLContext::hasExtension().

As with the other Qt OpenGL classes, QOpenGLVertexArrayObject has a create() function to create the underlying OpenGL object. This is to allow the developer to ensure that there is a valid current OpenGL context at the time.

Once you have successfully created a VAO the typical usage pattern is:

  • In scene initialization function, for each visual object:
    • Bind the VAO
    • Set vertex data state for this visual object (vertices, normals, texture coordinates etc.)
    • Unbind (release()) the VAO
  • In render function, for each visual object:
    • Bind the VAO (and shader program if needed)
    • Call a glDraw*() function
    • Unbind (release()) the VAO

The act of binding the VAO in the render function has the effect of restoring all of the vertex data state setup in the initialization phase. In this way we can set a great deal of state when setting up a VAO and efficiently switch between state sets of objects to be rendered. Using VAOs also allows the OpenGL driver to amortise the validation checks of the vertex data.

Note: Vertex Array Objects, like all other OpenGL container objects, are specific to the context for which they were created and cannot be shared amongst a context group.

See also QOpenGLVertexArrayObject::Binder and QOpenGLBuffer.

Member Function Documentation

QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QObject * parent = 0)

Creates a QOpenGLVertexArrayObject with the given parent. You must call create() with a valid OpenGL context before using.

QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject()

Destroys the QOpenGLVertexArrayObject and the underlying OpenGL resource.

void QOpenGLVertexArrayObject::bind()

Binds this vertex array object to the OpenGL binding point. From this point on and until release() is called or another vertex array object is bound, any modifications made to vertex data state are stored inside this vertex array object.

If another vertex array object is then bound you can later restore the set of state associated with this object by calling bind() on this object once again. This allows efficient changes between vertex data states in rendering functions.

bool QOpenGLVertexArrayObject::create()

Creates the underlying OpenGL vertex array object. There must be a valid OpenGL context that supports vertex array objects current for this function to succeed.

Returns true if the OpenGL vertex array object was successfully created.

void QOpenGLVertexArrayObject::destroy()

Destroys the underlying OpenGL vertex array object. There must be a valid OpenGL context that supports vertex array objects current for this function to succeed.

bool QOpenGLVertexArrayObject::isCreated() const

Returns true is the underlying OpenGL vertex array object has been created. If this returns true and the associated OpenGL context is current, then you are able to bind() this object.

GLuint QOpenGLVertexArrayObject::objectId() const

Returns the id of the underlying OpenGL vertex array object.

void QOpenGLVertexArrayObject::release()

Unbinds this vertex array object by binding the default vertex array object (id = 0).