在GLM中使用offsetof(OpenGL数学) [英] Use offsetof with GLM (OpenGL maths)

查看:365
本文介绍了在GLM中使用offsetof(OpenGL数学)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GLM OpenGL数学库编写OpenGL程序.我想像这样将顶点位置,法线和纹理坐标组合到一个类中

I am writing an OpenGL program using the GLM OpenGL maths library. 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;
};

,然后使用这些数组作为我的顶点缓冲区对象(VBO).但是,当调用glVertexAttribPointer映射我的VBO时,我需要为normaltexcoord成员的组合Vertex结构提供一个偏移量.

and then use an array of these as my vertex buffer object (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.

如果这些只是POD,我本可以使用类似的东西

Had these just been PODs I could have used something like

offsetof(Vertex, position)

但不适用于glm数据类型(或至少适用于g ++ 4.4.3纾困).

but that does not work with glm data types (or at least g++ 4.4.3 bails out).

获取Vertex成员偏移的推荐方法是什么?

What is the recommended way to get the offset of the members of Vertex?

(我了解为什么我不能为任意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的一般原因,但在这种情况下,事情似乎定义清楚了)

(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 ++ 98/03标准,它们的定义不明确. C ++ 11通过放宽对类型的要求来改善了这一点,该类型被认为是"standard-layout",这是一组较弱的规则(C ++ 11中的offsetof需要标准布局类型,而不是POD).我不知道GLM的类是否遵循标准布局规则.

By C++98/03 standards, they are not well defined. 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). I don't know if GLM's classes follow the standard-layout rules or not.

当然,这无关紧要,因为您要处理的是C ++ 98/03编译器.该标准不需要任何机制来使成员与非POD类型的偏移量有关.您的选择是要么坚持不使用GLM类型就使顶点数据成为POD的标准,要么只做对您感兴趣的编译器有用的事情.

Of course, that's all irrelevant, since you're dealing with a C++98/03 compiler. There is no mechanism that is required by the standard to work to get the offset of a member from a non-POD type. 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.

从实际的角度来看,后一种情况实际上还不错. POD的定义在C ++ 11中被更改的原因是,大多数编译器已经遵循新规则.标准委员会只是使人们普遍认为可以在编译器之间工作的行为合法化.因此,您可以简单地做到这一点.您知道glm::vec4的大小为16个字节,因此请手动计算偏移量.

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; 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中使用offsetof(OpenGL数学)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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