将 NSMutableArray 复制到浮点数组中以进行 GLES 渲染
我有一个名为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设这些值存储为 NSNumbers,您可以执行以下操作:
Assuming the values are stored as NSNumbers, you can do something like this:
如果您只想使用原始块内存直接读取和写入值,则需要使用
malloc
。如果你有一个包含 NSNumber 实例的 NSArray
array
:与 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: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.只需循环遍历这些值即可。假设您的浮点数存储为 NSNumbers:
Just loop through the values. Assuming your floats are stored as NSNumbers: