In OpenGL, why does glVertexAttribPointer require the “pointer” argument to be passed in as void*?

后端 未结 2 1182
误落风尘
误落风尘 2021-02-09 12:53

The specification for the glVertexAttribPointer is as follows:

void glVertexAttribPointer( GLuint index,
    GLint size,
    GLenum type,
    GLbool         


        
2条回答
  •  野的像风
    2021-02-09 13:33

    One way of looking at it is that the last argument is always a pointer:

    • If no VBO is bound, it's a pointer relative to base address 0. Which is a regular memory address, just the way pointers are normally used in C/C++.
    • If a VBO bound, it's a pointer relative to the base address of the buffer.

    At least that's the only logical explanation I could ever find.

    Personally, I think that overloading the entry point this way was a very unfortunate decision, for a number of reasons:

    • It confuses people to no end.
    • It requires ugly type casts in C/C++.
    • It does not work at all in more type safe languages, like Java.

    In languages like Java, you typically end up with overloaded versions of the function that accept different types. As a somewhat curious historical note, the overloaded version with an int argument was missing in the initial version of the GLES20 bindings in Android, which meant that you could not use VBOs from Java. So this has tripped up more that just the occasional casual OpenGL programmer.

提交回复
热议问题