如何将 int 转换为 const GLvoid*? [英] How to cast int to const GLvoid*?
问题描述
在我的跨平台 OpenGL 应用程序中,我想使用顶点缓冲区对象进行绘制.但是我在调用 glDrawRangeElements 时遇到了问题.
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)));
编译器(Mac OS X 上的 CLang)不喜欢最后一个参数错误:无法从类型 'unsigned long' 转换为指针类型 'GLvoid *'(又名 'void *')".OpenGL API 将最后一个参数的类型定义为 const GLvoid * 并在此 api 与顶点数组一起使用时需要一个指针.但是我知道当使用顶点缓冲区对象而不是指针时,期望将表示偏移量的整数值传递到缓冲区数据中.这就是我想要做的,因此我必须投.如何通过编译器进行严格检查来协调 api 要求?
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?
推荐答案
当我使用古老的 c 样式转换时,我使用 CLang 和 c++11 编译它.
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));
这篇关于如何将 int 转换为 const GLvoid*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!