问题
In my cross platform OpenGL application I want to draw using vertex buffer objects. However I run into problems invoking glDrawRangeElements.
glDrawRangeElements(GL_TRIANGLES, start, start + count, count,
GL_UNSIGNED_INT, static_cast<GLvoid *> (start * sizeof(unsigned int)));
The compiler (CLang on Mac OS X) does not like the last argument "error: cannot cast from type 'unsigned long' to pointer type 'GLvoid *' (aka 'void *')". OpenGL API defines the type of last arguments as const GLvoid * and expects a pointer when this api is used with vertex arrays. However I understand that when vertex buffer objects are employed instead of pointer, one is expected to pass an integer value representing offset into buffer data. This is what I am trying to do and thus I have to cast. How do I reconcile api requirements with compiler imposing rigorous checks?
回答1:
Since it's so commonly used, people frequently use a macro for his type conversion. It can be defined like this:
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
This is a clean and safe way of doing the cast, because it does not make any assumptions about integer and pointer types having the same size, which they often don't on 64-bit systems.
Since I personally prefer C++ style casts, and don't use NULL, I would define it like this:
#define BUFFER_OFFSET(idx) (static_cast<char*>(0) + (idx))
回答2:
Like this:
reinterpret_cast <GLvoid *> (start * sizeof(unsigned int));
回答3:
This is an almost verbatim copy of my answer https://stackoverflow.com/a/8284829/524368 with slight modifications to make it match this question.
C defines (and C++ follows it), that pointers can be casted to integers, namely of type uintptr_t
, and that if the integer obtained that way, casted back into the original pointer type it came from, would yield the original pointer.
Then there's pointer arithmetic, which means if I have two pointers pointing so the same object I can take the difference of them, resulting in a integer (of type ptrdiff_t
), and that integer added or subtracted to either of the original pointers, will yield the other. It is also defines, that by adding 1 to a pointer, the pointer to the next element of an indexed object is yielded. Also the difference of two uintptr_t
, divided by sizeof(type pointed to)
of pointers of the same object must be equal to the pointers themself being subtracted. And last but not least, the uintptr_t
values may be anything. They could be opaque handles as well. They're not required to be the addresses (though most implementations do it that way, because it makes sense).
Now we can look at the infamous null pointer. C defines the pointer which is casted to for from type uintptr_u
value 0 as the invalid pointer. Note that this is always 0 in your source code. On the backend side, in the compiled program, the binary value used for actually representing it to the machine may be something entirely different! Usually it is not, but it may be. C++ is the same, but C++ doesn't allow for as much implicit casting than C, so one must cast 0 explicitly to void*
. Also because the null pointer does not refer to an object and therefore has no dereferenced size pointer arithmetic is undefined for the null pointer. The null pointer referring to no object also means, there is no definition for sensibly casting it to a typed pointer.
So if this is all undefined, why does this macro work after all? Because most implementations (means compilers) are extremely gullible and compiler coders lazy to the highest degree. The integer value of a pointer in the majority of implementations is just the value of the pointer itself on the backend side. So the null pointer is actually 0. And although pointer arithmetic on the null pointer is not checked for, most compilers will silently accept it, if the pointer got some type assigned, even if it makes no sense. char
is the "unit sized" type of C if you want to say so. So then pointer arithmetic on cast is like artihmetic on the addresses on the backend side.
To make a long story short, it simply makes no sense to try doing pointer magic with the intended result to be a offset on the C language side, it just doesn't work that way.
Let's step back for a moment and remember, what we're actually trying to do: The original problem was, that the original OpenGL vertex array functions take a pointer as their data parameter, but for Vertex Buffer Objects we actually want to specify a byte based offset into our data, which is a number. To the C compiler the function takes a pointer (a opaque thing as we learned). Instead what was defined by OpenGL is a exploit of how compilers work. Pointers and their integer equivalent are implemented as the same binary representation by most compilers. So what we have to do, it making the compiler call those functions with our number instead of a pointer.
So technically the only thing we need to do is telling to compiler "yes, I know you think this variable a
is a integer, and you are right, and that function glDrawElements
only takes a void*
for it's data parameter. But guess what: That integer was yielded from a void*
", by casting it to (void*)
and then holding thumbs, that the compiler is actually so stupid to pass the integer value as it is to the function.
So this all comes down to somehow circumventing the old function signature. Casting the pointer is the IMHO dirty method. I'd do it a bit different: I'd mess with the function signature:
typedef void (*TFPTR_DrawElementsOffset)(GLenum,GLsizei,GLenum,uintptr_t);
TFPTR_DrawElementsOffset myglDrawElementsOffset =
(TFPTR_DrawElementsOffset)glDrawElements;
Now you can use myglDrawElementsOffset
without doing any silly casts, and the offset parameter will be passed to the function, without any danger, that the compiler may mess with it. This is also the very method I use in my programs.
回答4:
I got it to compile using CLang and c++11 when I used ancient c style casting.
glDrawRangeElements(GL_TRIANGLES, start, start + count, count, GL_UNSIGNED_INT,
(GLvoid *) (start * sizeof(unsigned int)));
Alternatives that I liked less but were also accepted by compiler were
glDrawRangeElements(GL_TRIANGLES, start, start + count, count, GL_UNSIGNED_INT,
reinterpret_cast<GLvoid *>(static_cast<uintptr_t>(start * sizeof(unsigned int))));
glDrawRangeElements(GL_TRIANGLES, start, start + count, count, GL_UNSIGNED_INT,
(char *)(0) + start * sizeof(unsigned int));
回答5:
You could try to call it like this:
// The first to last vertex is 0 to 3
// 6 indices will be used to render the 2 triangles. This make our quad.
// The last parameter is the start address in the IBO => zero
glDrawRangeElements(GL_TRIANGLES, 0, 3, 6, GL_UNSIGNED_SHORT, NULL);
Please have a look to OpenGL tutorial.
来源:https://stackoverflow.com/questions/23177229/how-to-cast-int-to-const-glvoid