PDA

View Full Version : How does 'glVertexAttribPointer' bind to a buffer object



jphilip777
10-26-2013, 10:40 AM
Hi,

I am new to OpenGL and this might be a very basic question which is little confusing.
I am using opengles-book.com by aftab munshi to learn the basics and its going well except for one thing on page 123.

How do you bind the buffer object to point to the shader indices using glVertexAttribPointer. I am guessing you need to bind the buffer before you use the glVertexAttribPointer and the last parameter will point to the buffer object instead of the client memory.

But in the text book i am referring


glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]);
glEnableVertexAttribArray(VERTEX_POS_INDX);

glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]);
glEnableVertexAttribArray(VERTEX_NORMAL_INDX);

glBindBuffer(GL_ARRAY_BUFFER, vboIds[2]);
glEnableVertexAttribArray{VERTEX_TEXCOORD0_INDX);

glVertexAttribPointer(VERTEX_POS_INDX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[0], 0);
glVertexAttribPointer(VERTEX_NORMAL_INDX, VERTEX_NORMAL_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[1], 0);
glVertexAttribPointer(VERTEX_TEXCOORD0_INDX, VERTEX_TEXCOORD0_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[2], 0);


because the last buffer binded is the vboIds[2] all the 3 glVertexAttribPointer points to this last buffer ??? Isn't that wrong ?

Or does this mean something
glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]);
glEnableVertexAttribArray(VERTEX_POS_INDX);

Is this binding the buffer object to the the shader indices ? I know what glEnableVertexAttribArray means but does it also bind the buffer to the indices ?

Dan Bartlett
10-26-2013, 11:19 AM
Yeah, you have to bind to GL_ARRAY_BUFFER before calling glVertexAttribPointer so the book has a mistake. There's a list of corrections at http://opengles-book.com/errata.html that shows the correct code.

This would also work:

glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]);
glVertexAttribPointer(VERTEX_POS_INDX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[0], 0);

glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]);
glVertexAttribPointer(VERTEX_NORMAL_INDX, VERTEX_NORMAL_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[1], 0);

glBindBuffer(GL_ARRAY_BUFFER, vboIds[2]);
glVertexAttribPointer(VERTEX_TEXCOORD0_INDX, VERTEX_TEXCOORD0_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[2], 0);

glEnableVertexAttribArray(VERTEX_POS_INDX);
glEnableVertexAttribArray(VERTEX_NORMAL_INDX);
glEnableVertexAttribArray{VERTEX_TEXCOORD0_INDX);
because you can enable/disable attributes whenever you like.

It's okay to have multiple attributes in the same buffer object, interleaved eg. {VNTVNTVNT...}, separate {VVV...NNN...TTT...} or a mixture, but as this example has them all at offset 0 it wouldn't function as the author intended.

jphilip777
10-26-2013, 01:10 PM
Thanks for the reply. Thats clears my doubt Dan.