I have seen the following macro being used in OpenGL VBO implementations:
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
//...
glNormalPointer(GL_FLOAT, 32, BUFFER_OFFSET(x));
Could you provide a little detail on how this macro works? Can it be replaced with a function?
More exactly, what is the result of incrementing a NULL pointer?
Let’s take a trip back through the sordid history of OpenGL. Once upon a time, there was OpenGL 1.0. You used
glBeginandglEndto do drawing, and that was all. If you wanted fast drawing, you stuck things in a display list.Then, somebody had the bright idea to be able to just take arrays of objects to render with. And thus was born OpenGL 1.1, which brought us such functions as
glVertexPointer. You might notice that this function ends in the word "Pointer". That’s because it takes pointers to actual memory, which will be accessed when one of theglDraw*suite of functions is called.Fast-forward a few more years. Now, graphics cards have the ability to perform vertex T&L on their own (up until this point, fixed-function T&L was done by the CPU). The most efficient way to do that would be to put vertex data in GPU memory, but display lists are not ideal for that. Those are too hidden, and there’s no way to know whether you’ll get good performance with them. Enter buffer objects.
However, because the ARB had an absolute policy of making everything as backwards compatible as possible (no matter how silly it made the API look), they decided that the best way to implement this was to just use the same functions again. Only now, there’s a global switch that changes
glVertexPointer‘s behavior from "takes a pointer" to "takes a byte offset from a buffer object." That switch being whether or not a buffer object is bound toGL_ARRAY_BUFFER.Of course, as far as C/C++ is concerned, the function still takes a pointer. And the rules of C/C++ do not allow you to pass an integer as a pointer. Not without a cast. Which is why macros like
BUFFER_OBJECTexist. It’s one way to convert your integer byte offset into a pointer.The
(char *)NULLpart simply takes the NULL pointer (which is usually avoid*in C and the literal 0 in C++) and turns it into achar*. The+ ijust does pointer arithmetic on thechar*. Because the null pointer usually has a zero address, addingito it will increment the byte offset byi, thus generating a pointer who’s value is the byte offset you passed in.Of course, the C++ specification lists the results of BUFFER_OBJECT as undefined behavior. By using it, you’re really relying on the compiler to do something reasonable. After all,
NULLdoes not have to be zero; all the specification says is that it is an implementation-defined null pointer constant. It doesn’t have to have the value of zero at all. On most real systems, it will. But it doesn’t have to.That’s why I just use a cast.
It’s not guaranteed behavior either way (int->ptr->int conversions are conditionally supported, not required). But it’s also shorter than typing "BUFFER_OFFSET". GCC and Visual Studio seem to find it reasonable. And it doesn’t rely on the value of the NULL macro.
Personally, if I were more C++ pedantic, I’d use a
reinterpret_cast<void*>on it. But I’m not.Or you can ditch the old API and use
glVertexAttribFormatet. al., which is better in every way.