我有一门课,我试图从中画出一颗小行星(八边形)。小行星类有两个成员。 vertex
指定其位置和 vertices
指定八角形中的点。我的问题是如何计算出此类对象的步幅和偏移量(对于 glVertexAttribPointer 和 glBufferData)以便绘制它?
class asteroid
{
std::pair <float, float> vertex;
std::vector<std::pair <float, float>> vertices;
}
然后我有一个vector<asteroid> asteroids;
存储我所有的小行星。
最佳答案
顶点的步长是sizeof(vertices[0])
上传到 glBufferData 的偏移量是 reinterpret_cast<void*>(&vertices[0])
glVertexAttribPointer 的偏移量是 reinterpret_cast<void*>(&vertices[0].first) - reinterpret_cast<void*>(&vertices[0])
但是保持这样的顶点不是一个好主意,只需为所有小行星创建一个(共享的)VBO 并取消 vector 。
然后你可以使用instancing所以小行星的位置将在一个单独的 VBO 中,并带有 glVertexAttribDivisor
属性放。在顶点着色器中你有一个属性 vertex
和属性 position
. vertex
属性是顶点 VBO 和 position
是小行星的位置场。
关于c++ - 找出 OpenGL 中 C++ 类的步幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216837/