我正在尝试使用 glVertexAttribPointer
具有 Eigen 对象的结构,类似于此:
struct Vertex {
Eigen::Vector3f position;
Eigen::Vector3f normal;
};
问题是设置 glVertexAttribPointer
的偏移量.由于没有公共(public)访问 m_data
成员用于在 Eigen 内部存储数据,offset
无法使用。
似乎没有什么好的方法可以做到这一点。我目前的做法是这样的:
(void*)((char*)vertices[0].Position.data() - (char*)(&vertices[0]))
,其中 vertices
是 std::vector<Vertex>
.
这绝不是什么好事(尤其是在现代 C++ 中)。我怀疑是否有一个好的解决方案,但这样做更安全的方法是什么,或者至少如何尽可能地隔离此操作,所以我不必为每次调用 glVertexAttribPointer
都编写它.
最佳答案
Eigen documentation保证 Eigen::Vector3f = Eigen::Matrix<float,3,1>
的布局如下:
struct {
float data[Rows*Cols]; // with (size_t(data)%A(Rows*Cols*sizeof(T)))==0
};
换句话说,float[3]
位于 Eigen::Vector3f
的偏移量 0结构。您可以传递 position
的偏移量和 normal
按原样( offsetof(Vertex, position)
和 offsetof(Vertex, normal)
)到您的 glVertexAttrib 调用偏移量,并且 sizeof(Eigen::Vector3f)
尺寸。
关于c++ - 将 OpenGL 与 Eigen 一起用于存储顶点数据和 glVertexAttribPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58217443/