如何从值复制的NSMutableArray在float数组在OpenGL中使用 [英] How to copy values from NSMutableArray into array of floats to be used in OpenGL

查看:194
本文介绍了如何从值复制的NSMutableArray在float数组在OpenGL中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着<一个href=\"http://stackoverflow.com/questions/9285559/copy-nsmutablearray-into-array-of-floats-for-gles-rendering\">this螺纹,我看到了另一种方法来初始化该协会致力于我的情况数组的值。我不能使用标准的浮动* simpleArray = {1.0F,2.0F} ,因为这个数组必须具有全局范围,它也需要经过多次进行初始化不能在全球级别(对象指针)被初始化的变量。我试图实现从一个简单的例子是这样的线程不涉及第二阵列的方法。

Following this thread, I saw an alternative way to initialize the values of an array which works for my situation. I can't use the standard float *simpleArray = {1.0f, 2.0f} because this array needs to be have global scope, and it also needs to be initialized after several variables that can't be initialized at the global-level (object pointers). I tried implementing one method from the thread like this on a simpler example which doesn't involve a second array.

float *simpleArray = malloc(_numVerts * sizeof(float));
for (int i = 0; i < 3; i++)
{
    simpleArray[i] = i * 5.0f;
}

然而,当我去从这个数组访问值,我没有得到我想要的东西,如本屏幕截图。它不支持OpenGL,当我把里面要么阵列工作。我没有得到一个错误,但没有我的顶点,正在制定:

However, when I go to access the values from this array, I don't get what I want, as shown in this screenshot. And it doesn't work for OpenGL when I place the array inside either. I'm not getting an error, but none of my vertices are being drawn:

然而,然而,当我这样做,我可以访问我从阵列所需的价值观,我看到他们被安置在阵列中:

However-however, when I do this, I can access the values that I want from the array, and I see that they were placed in the array:

float *x = &simpleArray[0];
float *y = &simpleArray[1];
float *z = &simpleArray[2];

这是伟大的,所有,但它并不能帮助我使用OpenGL,我不知道从挂钩线程OP如何做的。也许我做错了什么在这个层面上,或者它的东西,我用OpenGL做错了,所以这里正是我想要做什么,都会响起的X code提供的OpenGL的模板:

That's great and all, but it doesn't help me with OpenGL, and I don't know how the OP from the linked thread did it. Maybe I am doing something wrong on this level, or it's something I'm doing wrong with OpenGL, so here is exactly what I'm trying to do, going off of the OpenGL template provided by XCode:

在接口:

@property(nonatomic) GLfloat *glCubeVertexData;

在执行

#define STRIDE (NSUInteger) 6

后来......

Later...

_glCubeVertexData = malloc(_numVerts * sizeof(GLfloat));
for (NSUInteger i = 0; i < _numVerts; i+= STRIDE)
{
    _glCubeVertexData[i + 0] = (GLfloat)[[[_ndVerts objectAtIndex:i] objectAtIndex:0] floatValue];
    _glCubeVertexData[i + 1] = (GLfloat)[[[_ndVerts objectAtIndex:i] objectAtIndex:1] floatValue];
    _glCubeVertexData[i + 2] = (GLfloat)[[[_ndVerts objectAtIndex:i] objectAtIndex:2] floatValue];
    _glCubeVertexData[i + 3] = (GLfloat) 0.0f;
    _glCubeVertexData[i + 4] = (GLfloat) 0.0f;
    _glCubeVertexData[i + 5] = (GLfloat) 0.0f;
}

setUpGL

glBufferData(GL_ARRAY_BUFFER, sizeof(_glCubeVertexData), _glCubeVertexData, GL_STATIC_DRAW);

我可以在 _ndVerts 看到我的顶点正是他们需要的,但在 _glCubeVertexData 未被绘制可言,好像他们甚至没有包含在阵列中的

And I can see in _ndVerts that my vertices are exactly what they need to be, but the vertices in _glCubeVertexData are not being drawn at all, as if they weren't even contained in the array.

推荐答案

您的问题是不是100%我清楚,但你似乎是关于指针和调试器有点困惑。在C数组访问符号的作品,这样下面的两行是等价的:

Your question isn't 100% clear to me, but you seem to be a little confused about pointers and the debugger. The array access notation in C works such that the following two lines are equivalent:

float x = simpleArray[i];
float x = *(simpleArray + i);

在code,你说的作品似乎不太可能是你真正想要的东西。代替数组中获得的值的,你得到的指针数组中的值。你有:

The code that you say works seems unlikely to be what you actually want. Instead of getting the values in the array, you're getting pointers to values in the array. You've got:

float *x = &simpleArray[0];

这简化为:

float *x = (simpleArray + 0);

presumably你不想一个指针值虽然,你想要本身的价值:

Presumably you don't want a pointer to the value though, you want the value itself:

float x = simpleArray[0];

这似乎是你的主要困惑是,你没有看到你在调试器中会发生什么。你已经声明 simpleArray 作为一个指向一个浮动,并指出(年初)与分配的内存块的malloc()。调试器只是显示你指针的值(即在的malloc的起始地址() ED块),与复引用指针沿(即中的的数组中的第一条浮法),这是0正是如你所期望的。换句话说,你的屏幕截图显示什么都不缺的或意外的。

It seems like your main confusion is that you don't see what you expect in the debugger. You've declared simpleArray as a pointer to a float, and pointed it to (the beginning of) a block of memory allocated with malloc(). The debugger is simply showing you the value of the pointer (ie. the address of the beginning of the malloc()ed block), along with the dereferenced pointer (ie. the value of the first float in the array), which is 0 exactly as you'd expect. In other words, your screenshot shows nothing amiss or unexpected at all.

至于你的OpenGL顶点不被吸引,我无法提供太多建议在那里,因为我有任何使用OpenGL没有经验,但我怀疑问题无关与数组访问低层语义你'再询问。

As for your OpenGL vertexes not being drawn, I can't provide much advice there, as I have no experience whatsoever with OpenGL, but I suspect the problem has nothing to do with the low-level semantics of array access that you're asking about.

这篇关于如何从值复制的NSMutableArray在float数组在OpenGL中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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