Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: glGetVertexAttribPointerv usage

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2015
    Posts
    9

    glGetVertexAttribPointerv usage

    I want to use this function to get data back from vbo to a pointer, where p[0]=x p[2]=y p[3]=z and so on.

    Code :
    ...
    glEnableVertexAttribArray(0);
    GLfloat *ptr = (float*)calloc(vcount* 3, sizeof(float)); //pointer with preallocated memory. 
    GIvoid *vptr;
    glGetVertexAttribPointerv(0, GL_VERTEX_ATTRIB_ARRAY_POINTER,&vptr);
    ptr = (GLfloat*)&vptr;

    Shouldn't ptr[0] be the x value of the first vertex??

    Thanks
    Last edited by ionas_; 01-31-2016 at 06:21 AM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,480
    Quote Originally Posted by ionas_ View Post
    I want to use this function to get data back from vbo to a pointer
    It's probably the wrong function. To read data from a buffer, either use glGetBufferSubData() or map the buffer then read from the mapped region.

    Quote Originally Posted by ionas_ View Post
    Code :
    glGetVertexAttribPointerv(0, GL_VERTEX_ATTRIB_ARRAY_POINTER,&vptr);
    ptr = (GLfloat*)&vptr;

    Shouldn't ptr[0] be the x value of the first vertex??
    vptr will only be a pointer if you're using a client-side array rather than a VBO. For a VBO, vptr will be the offset into the buffer.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2015
    Posts
    9
    in the notes section it says:
    The state returned is retrieved from the currently bound vertex array object.

    The initial value for each pointer is 0.

    isn't it supposed to return the address of the specified generic vertex attribute pointer?

  4. #4
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,928
    Quote Originally Posted by ionas_ View Post
    in the notes section it says:
    The state returned is retrieved from the currently bound vertex array object.

    The initial value for each pointer is 0.

    isn't it supposed to return the address of the specified generic vertex attribute pointer?
    It does. It returns exactly what you set.

    You call glVertexAttribPointer, which is a function that takes a pointer. glGetVertexAttribPointer returns the pointer you set with the first call.

    Now you will probably say, "Wait! glVertexAttribPointer doesn't take a pointer; it takes an offset into a buffer object." Yes, kinda. But OpenGL pretends that this offset is the value of a pointer. Therefore, glGetVertexAttribPointer returns the pointer value set by glVertexAttribPointer, even though it's not actually a pointer.

    Just look at the docs for glGetVertexAttribPointer. It says right there what it returns:

    Quote Originally Posted by The Docs
    The pointer returned is a byte offset into the data store of the buffer object that was bound to the GL_ARRAY_BUFFER target (see glBindBuffer) when the desired pointer was previously specified.
    If you want to access data in a buffer object, you use the standard tools for accessing data in a buffer object. Namely, must use map the buffer or use glGetBufferSubData. Just because the buffer object is used for vertex data does not make it special.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2015
    Posts
    9
    so in order to retrieve data on client side we do the following?

    Code :
    GLfloat *pointer = (GLfloat*)calloc(numOfvertices* 3, sizeof(GLfloat));
    glGetVertexAttribPointerv(0, GL_VERTEX_ATTRIB_ARRAY_POINTER,&vptr);
    glGetBufferSubData(GL_ARRAY_BUFFER,vptr,3*sizeof(float)*numOfvertices, pointer );

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,480
    Quote Originally Posted by ionas_ View Post
    so in order to retrieve data on client side we do the following?

    Code :
    GLfloat *pointer = (GLfloat*)calloc(numOfvertices* 3, sizeof(GLfloat));
    glGetVertexAttribPointerv(0, GL_VERTEX_ATTRIB_ARRAY_POINTER,&vptr);
    glGetBufferSubData(GL_ARRAY_BUFFER,vptr,3*sizeof(float)*numOfvertices, pointer );
    Yes, roughly. The above assumes that the buffer holding the attribute data is already bound to GL_ARRAY_BUFFER. A slightly more thorough version might use glGetVertexAttribIuiv(GL_VERTEX_ATTRIB_ARRAY_BUFFE R_BINDING) to obtain the buffer name then bind it (or use glGetNamedBufferSubData()).

    Beyond that, it's generally better to keep track of this information yourself than to use glGet* to retrieve it from OpenGL.

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2015
    Posts
    9
    Thanks a lot for your answers. I will try it out.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •