George
George

Reputation: 7317

Defining a pointer to an OpenGL Vertex Array Object

Consider the following OpenGL code snippet:

GLuint vao;
glCreateVertexArrays(1, glBindVertexArray(&vao));    

I'm under the impression that what this code does is

  1. Creates a pointer, of type GLuint, that will eventually point to an OpenGL Vertex Array Object. The name of this pointer is vao.
  2. Indirectly allocates memory through OpenGL for the actual Vertex Array Object (i.e., no use of malloc in C or new in C++). The way that this is done is through the function glCreateVertexArrays. This function also makes the pointer vao point to the Vertex Array Object.

Is my understanding correct? If so, in (1), why is the type of the pointer GLuint and not explicitly defined as a pointer type (for example with * in C/C++ or with the OpenGL type GLintptr)?

Upvotes: 2

Views: 272

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72479

This code is invalid, glBindVertexArray returns void so you cannot pass its return value as a parameter to glCreateVertexArrays, because there is none.

The correct way to create a VAO, as per OpenGL 4.5 profile, is:

GLuint vao;
glCreateVertexArrays(1, &vao);

As per the meaning of vao and why its type is GLuint: it is not a pointer. It is an opaque value that is used internally to locate the VAO inside the OpenGL implementation tables. The driver may allocate memory on both, the CPU and/or GPU for storing the state associated with the VAO. It is a complex state with quite a lot of book-keeping, which is much more involved than malloc or new. (In particular malloc can't be used to allocate GPU memory.)

Upvotes: 6

Related Questions