0

Following another question from me, here is a specific example where I want to avoid offsetof.

For using with glVertexAttribPointer, I have to use offsetof for the last parameter.

 glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
            (const GLvoid *) offsetof(Vertex, _color));

Vertex is a class. Is there a way I can avoid using this one? I tried with pointer to members, but no luck.

Cannot compile in the following

glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
        (const GLvoid *)&Vertex::_color);
genpfault
  • 51,148
  • 11
  • 85
  • 139
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • 2
    Why do you even want to avoid `offsetof`? – derhass Mar 29 '14 at 02:59
  • It is a pure C feature and i think C++ should have something better – Adam Lee Mar 29 '14 at 03:06
  • that does not make sense. `offsetof` is specified in the C++ spec and thus as much a 'C++' feature as everything else. – derhass Mar 29 '14 at 03:11
  • It is just a macro,and I would like to see ono-macro solution. Someone said offsetof can be totally bypassed, I want to see if this can be done in this specific example. – Adam Lee Mar 29 '14 at 03:14
  • It is a standard macro. So sure it can be bypassed. You can do the same thing the macro does. It typically involves ugly typcasting of NULL pointers, although modern compilers typically provide builtins for that anyway. There is nothing to gain here. – derhass Mar 29 '14 at 03:21
  • OP was discouraged by the posts here: http://stackoverflow.com/questions/22716560/about-offsetof-usage-in-c-and-c and wants a different solution to their problem. – Brandon Mar 29 '14 at 04:13
  • OK, that might explain the reasoning behind the question. However, this is exaclty a case where those "low-level" details _must not_ be abstracted away, as the API in question is just defined on that low level. – derhass Mar 29 '14 at 14:00

1 Answers1

1

You cannot get the address of a non-static member qualified as Vertex::_color. You would need an instance of Vertex, but even then the address returned would be relative to the program's address space and this is not what you want when you use VBOs.

offsetof (...) is used is to find the address of an element in a data structure. This address does not point to actual memory, it is literally just an offset from the beginning of the structure (and this is why it uses size_t rather than intptr_t or void *).

Historically, when vertex arrays were introduced in OpenGL 1.1, it used the data pointer in a call to glVertexPointer (...) to reference client (program) memory. Back then, the address you passed actually pointed to memory in your program. Beginning with Vertex Buffer Objects (GL 1.5), OpenGL re-purposed the pointer parameter in glVertexPointer (...) to serve as a pointer to server (GPU) memory. If you have a non-zero VBO bound when you call glVertexPointer (...) then the pointer is actually an offset.

More precisely, the pointer when using VBOs is relative to the bound VBO's data store and the first datum in your VBO begins at address 0. This is why offsetof (...) makes sense in this context, and there is no reason to avoid using it.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106