将NSMutableArray复制到float数组中以进行GLES渲染 [英] Copy NSMutableArray into array of floats for GLES rendering

查看:70
本文介绍了将NSMutableArray复制到float数组中以进行GLES渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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[]?

推荐答案

假定值存储为NSNumbers,则可以执行以下操作:

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);

这篇关于将NSMutableArray复制到float数组中以进行GLES渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆