大家好。感谢点击。
这是我在编写 OpenGL 代码时遇到的一个问题,但总体来说这是一个非常普遍的问题 - 所以没有什么特定于图形的问题。
我有一个结构(不是类,只是一个简单的结构),粒子。
typedef struct
{
float x;
float y;
float z;
}float3;
typedef struct
{
float3 position;
float3 velocity;
//...other stuff
}Particle;
我正在处理一堆粒子 (Particle* particles[]),但我有一个函数需要按 x、y、z 顺序打包的 float* 位置。
因此我的问题总结:
我的数据:
//I have this in a bunch of encapsulated structs
[... {1.0f, 2.0f, 3.0f,} ... {4.0f, 5.0f, 6.0f} ...]
//I want...
[1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f]
我的问题是......我已经拥有所有数据了!我不想再次使用 malloc/memcpy。有没有办法使用已经存在的数据?任何 C 指针杂技?我也在担心对齐/填充之类的事情。
(float3 是 CUDA 中定义的结构,如果有人好奇的话)。
最佳答案
glVertexAttribPointer
有一个 stride
参数,专为这种情况而设计。
通常,您会将一组 Particle
对象加载到 VBO 中,然后绑定(bind) VBO:
glVertexAttribPointer(shader_arg_position, 3, GL_FLOAT, GL_FALSE, sizeof (Particle), offsetof(Particle, position));
关于c++ - 来自结构数组的平面数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5696685/