简体   繁体   English

在GLM中使用offsetof(OpenGL数学)

[英]Use offsetof with GLM (OpenGL maths)

I am writing an OpenGL program using the GLM OpenGL maths library. 我正在使用GLM OpenGL数学库编写OpenGL程序。 I would like to combine vertex positions, normals and texture coordinates into one class like so 我想像这样将顶点位置,法线和纹理坐标组合到一个类中

class Vertex {
    public:
       glm::vec4 position;
       glm::vec4 normal;
       glm::vec2 texcoord;
};

and then use an array of these as my vertex buffer object (VBO). 然后使用这些数组作为我的顶点缓冲对象(VBO)。 However, when calling glVertexAttribPointer to map my VBOs I need to give it an offset into this combined Vertex struct for the normal and texcoord members. 但是,当调用glVertexAttribPointer映射我的VBO时,我需要为其normal成员和texcoord成员的组合Vertex结构提供一个偏移量。

Had these just been PODs I could have used something like 如果这些只是POD,我本可以使用类似

offsetof(Vertex, position)

but that does not work with glm data types (or at least g++ 4.4.3 bails out). 但这不适用于glm数据类型(或至少g ++ 4.4.3失败了)。

What is the recommended way to get the offset of the members of Vertex? 推荐获取顶点成员偏移的方法是什么?

(I understand the general reason why I cannot have offsetof for arbitrary C++ objects, but in this particular case things seem to be well-defined). (我理解为什么我不能对任意C ++对象使用offsetof的一般原因,但是在这种特殊情况下,事情似乎是明确定义的)。

(I understand the general reason why I cannot have offsetof for arbitrary C++ objects, but in this particular case things seem to be well-defined) (我了解为什么我不能对任意C ++对象使用offsetof的一般原因,但是在这种特殊情况下,事情似乎是明确定义的)

By C++98/03 standards, they are not well defined. 根据C ++ 98/03标准,它们的定义不明确。 C++11 improved this by relaxing the requirements for a type to be considered "standard-layout", which is a much weaker set of rules ( offsetof in C++11 requires standard-layout types, not PODs). C ++ 11通过放宽对被认为是“标准布局”的类型的要求进行了改进,该类型的规则要弱得多 (C ++ 11中的offsetof需要标准布局类型,而不是POD)。 I don't know if GLM's classes follow the standard-layout rules or not. 我不知道GLM的类是否遵循标准布局规则。

Of course, that's all irrelevant, since you're dealing with a C++98/03 compiler. 当然,这都是无关紧要的,因为您要处理的是C ++ 98/03编译器。 There is no mechanism that is required by the standard to work to get the offset of a member from a non-POD type. 该标准不需要任何机制即可使成员从非POD类型获取偏移量。 Your choices are to either stick to the standard make your vertex data POD by not using GLM types, or to just do what works for your compiler(s) of interest. 您的选择是要么坚持不使用GLM类型就使顶点数据成为POD的标准,要么只做对您感兴趣的编译器有用的工作。

The latter case is actually not too bad, from a practical standpoint. 从实际的角度来看,后一种情况实际上还不错。 The reason the definition of PODs was changed in C++11 was because most compilers already follow the new rules; 在C ++ 11中更改POD定义的原因是因为大多数编译器已经遵循了新规则。 the standards committee was simply legitimizing behavior that was widely known to work across compilers. 标准委员会只是使在编译器中众所周知的行为合法化。 So you could simply just do that. 因此,您可以简单地做到这一点。 You know the size of a glm::vec4 will be 16 bytes, so compute the offset manually. 您知道glm::vec4的大小为16个字节,因此请手动计算偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM