是否在glVertexAttribPointer中强制转换为void *称为合法C ++? [英] Is cast to void* in a glVertexAttribPointer call a legal C++?

查看:89
本文介绍了是否在glVertexAttribPointer中强制转换为void *称为合法C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上是关于C ++的问题,而不是OpenGL.我正在OpenGL上教程中,作者使用C ++(不是C).我的问题是glVertexAttribPointer 取得偏移参数为const void*.由于此参数只是告诉OpenGL给定顶点属性第一次出现在用glBufferData()复制的顶点数组中的位置,因此我希望它应该是std::ptrdiff_t类型. 有关的帖子解释了为什么将void*用作参数类型的原因,但是我想知道为什么这种用法在C ++中是合法的.例如,提到的教程只是将偏移量的值以字节为单位,就像在下面的通话中一样:

This is really a question about C++, not OpenGL. I'm following this tutorial on OpenGL (I've just started), and the author uses C++ (not C). My problem is that glVertexAttribPointer takes an offset parameter as const void*. Since this parameter simply tells OpenGL where a given vertex attribute appears for the first time in the array of vertices that was copied with glBufferData(), I would expect that it should be of std::ptrdiff_t type. This post on SO explains the reason why void* is used as an argument type, but I wonder why such a usage is legal in C++. For instance, the mentioned tutorial simply casts to void* the value of the offset in bytes, like in the call below:

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));

在我看来,这种方法不合法,因为 cpprefence reinterpret_cast的文档说

It seems to me that this approach cannot be legal because the cpprefence documentation of reinterpret_cast says

任何整数或枚举类型的值都可以转换为指针类型.保证将指针转换为足够大的整数并返回相同的指针类型,才能保证其具有原始值,否则,不能安全地取消对结果指针的引用(不能保证在相反方向的往返转换可以保证同一指针可能具有多个整数表示形式

A value of any integral or enumeration type can be converted to a pointer type. A pointer converted to an integer of sufficient size and back to the same pointer type is guaranteed to have its original value, otherwise the resulting pointer cannot be dereferenced safely (the round-trip conversion in the opposite direction is not guaranteed the same pointer may have multiple integer representations)

因此,如果我正确理解这一点,则将偏移量的std::ptrdiff_t值(似乎是这里所需的值)在转换为void*之后可能会丢失.我想念什么?

So, if I understand this correctly, the std::ptrdiff_t value of the offset (which seems to be the one that is needed here) can be lost after the cast to void*. What do I miss?

推荐答案

不保证"并不意味着保证失败";它的意思是不保证".这意味着该实现可能会保留转换,也可能不会保留转换.

"Not guaranteed " does not mean "guaranteed to fail"; it means "not guaranteed". Which means the implementation may or may not preserve the conversion.

glVertexAttribPointer API需要C或C ++的实现,该实现保留整数->指针->整数转换.就像它需要使用提供IEEE-754浮点类型以及8/16/32位整数的C或C ++实现一样.

The glVertexAttribPointer API requires an implementation of C or C++ that preserves integer->pointer->integer conversions. Much like it requires a C or C++ implementation that offers IEEE-754 floating-point types, as well as 8/16/32-bit integers.

这篇关于是否在glVertexAttribPointer中强制转换为void *称为合法C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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