views:

276

answers:

3

I have an array of GLuint with fixed size:

GLuint textures[10];

Now I need to set a size of array dynamically. I wrote something like this:

*.h:

GLuint *textures; 

*.m:

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

where N - needed size.

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.

A: 

If you set the value of N to 10, then the behavior of the two methods will be identical. You should therefore look for the reason for the failure in different places.

Claus Broch
A: 

void glGenTextures( GLsizei n, GLuint * textures);

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

maybe just try

glGenTextures(N, textures);

JC
Thanks for suggestion, but the behavior of this code is equal to provided.
sashaeve
A: 

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:

@interface Texture : NSObject
    GLuint texture;
@end

@property (nonatomic, readwrite) GLuint texture;

Then we can create NSMutableArray of Textures:

NSMutableArray *textures;

In *.m file we should fill our array:

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, e.g.:

t.texture = i+M;

where M - size of earlier used array of GLuint's.

Then getTexture will be rewritten as following:

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

I am not excited with this approach but it's a single one I make work.

sashaeve