An Hoa
An Hoa

Reputation: 1267

Why does glVertexAttribPointer without glBindBuffer also work?

In several OpenGL ES examples, I have seen the following code

GL_FLOAT vVertices[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), vVertices);
glEnableVertexAttribArray(0);
glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);

to draw a triangle. (You can easily fill in with simple shaders, the only vertex attributes assume is a vec2 of 2D coordinates.)

From this piece of tutorial code, I tend to believe the last argument to glVertexAttribPointer should be the pointer to where the vertex data is located. However, the documentation https://www.khronos.org/opengl/wiki/Vertex_Specification seems to suggest that it is supposed to be an integer but purposely cast to a const void*.

But why does the code above work? How does OpenGL know when to interpret the argument as pointer and when to interpret as integer?

Upvotes: 1

Views: 457

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473174

But why does the code above work?

Because OpenGL ES still supports the functionality from before desktop OpenGL removed it in 3.2. The wiki describes core desktop OpenGL's functionality.

It should be noted that ES does discourage the practice of using client-side memory for vertex data. But they have to continue "supporting" it due to backwards compatibility.

How does OpenGL know when to interpret the argument as pointer and when to interpret as integer?

If a buffer object is bound to GL_ARRAY_BUFFER, then the Pointer argument is interpreted as an integer. If no buffer is bound to that binding point, then the Pointer argument must be an actual memory pointer.

Upvotes: 3

Related Questions