如何将int转换为const GLvoid *? [英] How to cast int to const GLvoid*?

查看:190
本文介绍了如何将int转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆