ios - 在 Objective C 中动态设置 C 结构数组的大小?

标签 ios objective-c c opengl-es struct

在我的 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/

相关文章:

ios - 进入前台时出现 UIRefreshControl 错误

c - 在这种 for 循环的情况下如何猜测输出

c - scanf() 如何读取未格式化的输入?

xcode - TARGET_OS_IPHONE 和 ApplicationTests

objective-c - 在XML文档中使用“小于”字符(<)并进行解析

iphone - 设置 iOS 背景时我缺少哪一部分?

objective-c - 这是使用NSAutoreleasePool的正确方法吗?

c 没有正确打印 "┌──┐"字符

ios - Firebase 与 Swift 3.0 UITableView

android - 使用 Flutter 录音