简体   繁体   English

Objective-C:如何分配GLuint数组

[英]Objective-C: how to allocate array of GLuint

I have an array of GLuint with fixed size: 我有固定大小的GLuint数组:

GLuint textures[10];

Now I need to set a size of array dynamically. 现在,我需要动态设置数组的大小。 I wrote something like this: 我写了这样的东西:

*.h: *。H:

GLuint *textures; 

*.m: * .m:

textures = malloc(N * sizeof(GLuint)); 

where N - needed size. N-所需大小。

Then it used like this: 然后它像这样使用:

glGenTextures(N, &textures[0]);
// load texture from image

-(GLuint)getTexture:(int)index{
    return textures[index];
}

I used the answer from here , but program fell in runtime. 我从这里使用了答案,但是程序在运行时失败了。 How to fix this? 如何解决这个问题?

Program is written on Objective-C and uses OpenGL ES. 程序是用Objective-C编写的,并使用OpenGL ES。

I figured out this issue. 我发现了这个问题。

This code is valid but seems like not working. 此代码有效,但似乎无法正常工作。 This problem is described here , but it's not so clear. 这里描述此问题,但目前尚不清楚。

The solution that works for me is to create separate class with GLuint property: 对我有用的解决方案是使用GLuint属性创建单独的类:

@interface Texture : NSObject
    GLuint texture;
@end

@property (nonatomic, readwrite) GLuint texture;

Then we can create NSMutableArray of Textures: 然后我们可以创建NSMutableArray of Textures:

NSMutableArray *textures;

In *.m file we should fill our array: 在* .m文件中,我们应该填充数组:

for(int i=0;i<N;i++){
    Texture *t = [[Texture alloc] init];
    t.texture = i;
    GLuint l = t.texture;
    [textures addObject:t];
    glGenTextures(1, &l);
}

If you use other array(s) with textures you have to shift GLuint indexes, eg: 如果您将其他数组与纹理一起使用,则必须移动GLuint索引,例如:

t.texture = i+M;

where M - size of earlier used array of GLuint's. 其中M-较早使用的GLuint数组的大小。

Then getTexture will be rewritten as following: 然后,将getTexture重写如下:

-(GLuint)getTexture:(int)index{
    return textures[index].texture;
}

I am not excited with this approach but it's a single one I make work. 我对这种方法并不感到兴奋,但这是我工作的唯一方法。

If you set the value of N to 10, then the behavior of the two methods will be identical. 如果将N的值设置为10,则两种方法的行为将相同。 You should therefore look for the reason for the failure in different places. 因此,您应该在不同位置查找失败的原因。

void glGenTextures( GLsizei n, GLuint * textures); void glGenTextures(GLsizei n,GLuint *纹理);

accepts an array of unsigned ints, and it looks like you are passing a pointer to the first element in the array, which I think is not what you want to do or what the function accepts 接受一个无符号的int数组,看起来您正在传递一个指向数组中第一个元素的指针,我认为这不是您想要执行的操作或函数接受的内容

maybe just try 也许只是尝试

glGenTextures(N, textures); glGenTextures(N,textures);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM