Sodino
Sodino

Reputation: 587

question about the last parameter 'pointer' of 'glVertexAttribPointer' in learnOpenGL demo

I read two demos about 'Getting started with OpenGL'

For the last parameter of glVertexAttribPointer :
google android demo : use array name gTriangleVertices
learnOpenGL.com demo : use 0.


google android demo :

glUseProgram(gProgram);
checkGlError("glUseProgram");
glVertexAttribPointer(gvPositionHandle, 2, 
            GL_FLOAT, GL_FALSE, 0, 
            gTriangleVertices);       // array name
checkGlError("glVertexAttribPointer");
glEnableVertexAttribArray(gvPositionHandle);

learnOpenGL.com code segment :

// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
    -0.5f, -0.5f, 0.0f, // left  
    0.5f, -0.5f, 0.0f, // right 
    0.0f,  0.5f, 0.0f  // top   
}; 

unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, 
        GL_FLOAT, GL_FALSE, 
        3 * sizeof(float), 
        (void*)0);              // my question : `0` 
glEnableVertexAttribArray(0);

// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0); 

// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0); 

click : code comparision screenshot


I tend to think that learnOpenGL's demo is wrong,
but learnOpenGL is a well-known website, it's impossible to make mistakes.
Finally , both demos can display triangle correctly.

I'm confused.

I'm confused about when to pass 0 and when to pass the pointer value ?

Upvotes: 1

Views: 269

Answers (1)

Rabbid76
Rabbid76

Reputation: 210908

The last parameter of glVertexAttribPointer has the type const void * for reasons of compatibility.

If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target, the last argument (pointer) is treated as a byte offset into the buffer object's data store:

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);

In legacy OpenGL (compatibility profile OpenGL Context) it is possible to specify a data pointer directly (in this case it must not be bound a buffer object, because the pointer should not be treated as a byte offset into the buffer):

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), gTriangleVertices);

However, there is more. See glVertexAttribPointer and glVertexAttribFormat: What's the difference?

Upvotes: 2

Related Questions