1

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 ?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Sodino
  • 587
  • 6
  • 16
  • Instead of guessing why won't you just read what this argument means in [OpenGL documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml)? – Alexey S. Larionov Jun 17 '21 at 07:37
  • 0 is the default value of this argument and 0 is synonymous to "no pointer" in C/C++ languages – Alexey S. Larionov Jun 17 '21 at 07:39
  • @AlexeyLarionov I have read it. But i can get the key. – Sodino Jun 17 '21 at 07:39
  • [`glVertexAttribPointer`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml): if a non-zero named buffer object is bound, pointer is treated as a byte offset into the buffer object's data store – Rabbid76 Jun 17 '21 at 07:39
  • @Rabbid76 ok, i'll follow. – Sodino Jun 17 '21 at 07:47

1 Answers1

2

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?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174