将 NSMutableArray 复制到浮点数组中以进行 GLES 渲染

发布于 2025-01-05 08:36:53 字数 523 浏览 2 评论 0原文

我有一个名为 mVerticies 的 NSMutableArray 存储为类的成员,我想将它们放入浮点数组中以使用 glVertexAttribPointer 绘制它们。

通常在绘图时,我会得到如下内容:

float verticies[] = {-1, -1, 1, -1};

// ... prepare to draw

glVertexAttribPointer(GLKVertexAttribPosition,
    2, GL_FLOAT, GL_FALSE, 0, verticies);

// ... draw

但是为了使用 glVertexAttribPointer 函数,我需要一个 float[]。顶点存储为 NSMutableArray 因为它们经常更改。有没有一种简单的方法可以将动态 float[] 存储为成员或快速将 NSMutableArray 转换为 float[]?

I have an NSMutableArray called mVerticies stored as a member of my class and I'd like to place them into a float array to draw them with glVertexAttribPointer.

Normally when drawing, I would have something like:

float verticies[] = {-1, -1, 1, -1};

// ... prepare to draw

glVertexAttribPointer(GLKVertexAttribPosition,
    2, GL_FLOAT, GL_FALSE, 0, verticies);

// ... draw

But in order to use the glVertexAttribPointer function, i need a float[]. The verticies are stored as an NSMutableArray because they change quite often. Is there an easy way to either store a dynamic float[] as a member or to quickly convert an NSMutableArray to a float[]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2025-01-12 08:36:53

假设这些值存储为 NSNumbers,您可以执行以下操作:

float *floatsArray = malloc([numbersArray count] * sizeof(float));
if (floatsArray == NULL) {
    // handle error
}
[numbersArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSNumber *number, NSUInteger idx, BOOL *stop) {
    floatsArray[idx] = [number floatValue];
}];

// use floatsArray

free(floatsArray);

Assuming the values are stored as NSNumbers, you can do something like this:

float *floatsArray = malloc([numbersArray count] * sizeof(float));
if (floatsArray == NULL) {
    // handle error
}
[numbersArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSNumber *number, NSUInteger idx, BOOL *stop) {
    floatsArray[idx] = [number floatValue];
}];

// use floatsArray

free(floatsArray);
小镇女孩 2025-01-12 08:36:53

如果您只想使用原始块内存直接读取和写入值,则需要使用malloc

如果你有一个包含 NSNumber 实例的 NSArray array

float *vertices = malloc(sizeof(float) * [array count]);
for (int i = 0; i < [array count]; i++) {
    vertices[i] = [[array objectAtIndex:i] floatValue];
}

// draw cool 3D objects and stuff
glVertexAttribPointer(....)

// then, when you're totally done with the memory
free(vertices);

与 Objective-C 对象不同,vertices 指针没有保留计数,因此你需要自己释放它,并且跟踪何时可以释放它,因为当您调用 free 时,它​​会立即消失。

You need to use malloc if you just want a raw chunk memory to read and write values directly.

If you have an NSArray array containing NSNumber instances:

float *vertices = malloc(sizeof(float) * [array count]);
for (int i = 0; i < [array count]; i++) {
    vertices[i] = [[array objectAtIndex:i] floatValue];
}

// draw cool 3D objects and stuff
glVertexAttribPointer(....)

// then, when you're totally done with the memory
free(vertices);

Unlike Objective-C objects, the vertices pointer doesn't have a retain count, so you need to free it yourself, and keep track of when you can free it, because when you call free it will be gone immediately.

无人接听 2025-01-12 08:36:53

只需循环遍历这些值即可。假设您的浮点数存储为 NSNumbers:

NSUInteger count = [mutableVerticesArray count];
float vertices[count];
for (NSUInteger i = 0; i < count; i++) {
    vertices[i] = [[mutableVerticesArray objectAtIndex:i] floatValue];
}
// Drawing etc.

Just loop through the values. Assuming your floats are stored as NSNumbers:

NSUInteger count = [mutableVerticesArray count];
float vertices[count];
for (NSUInteger i = 0; i < count; i++) {
    vertices[i] = [[mutableVerticesArray objectAtIndex:i] floatValue];
}
// Drawing etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文