在我的 OpenGL ES 2.0 项目中,我在一个对象中包含以下代码,用于初始化 Vertice 和 Indice c-Struct 数组:
Vertex Vertices [] = {
{{0.0, 0.0, 0}, {0.9, 0.9, 0.9, 1}},
{{0.0 , 0.0 , 0}, {0.9, 0.9, 0.9, 1}},
{{0.0, 0.0, 0}, {0.9, 0.9, 0.9, 1}},
{{0.0, 0.0, 0}, {0.9, 0.9, 0.9, 1}},
};
static GLubyte Indices []= {
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
};
由于我想要渲染不同的多边形,因此我希望能够从调用类动态设置顶点和索引。我试过只是放:
Vertex Vertices [] = {
};
static GLubyte Indices []= {
};
这是我将顶点设置为 verticeArray 的位置(这是从调用类设置的数组)。
- (void)setupMesh {
int count = 0;
for (VerticeObject * object in verticesArray) {
Vertices[count].Position[0] = object.x;
Vertices[count].Position[1] = object.y;
Vertices[count].Position[2] = object.z;
count ++;
}
}
但是,我认为这会导致崩溃,因为数组从未分配/分配内存。谁能建议我如何实现这一目标?
最佳答案
你必须做这样的事情。
int *arr = (int *)malloc(5*sizeof(int));
您可以用 int 替换您的结构类型,即.
typedef struct vertex {
int x[3];
float y[4];
} vertex;
int count = 10;
vertex *Vertices = (vertex *)malloc(count * sizeof(vertex));
完成后,您确实需要释放内存。
free(Vertices);
Vertices = NULL;
关于ios - 在 Objective C 中动态设置 C 结构数组的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19424223/