ipodishima
ipodishima

Reputation: 284

Is it correct to load openGL textures like this?

I started some months ago an OpenGL project which became larger and larger... I began with the crashLanding sample and i use Texture2D.

I also use a singleton class to load my textures, and here is what the texture load looks like :

//Load the background texture and configure it
_textures[kTexture_Background] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"fond.png"]];
glBindTexture(GL_TEXTURE_2D, [_textures[kTexture_Background] name]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// Load the textures
_textures[kTexture_Batiment] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"batiment_Ext.png"]];
_textures[kTexture_Balcon] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"balcon.png"]];
_textures[kTexture_Devanture] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"devanture.png"]];
_textures[kTexture_Cactus_Troncs] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"cactus-troncs.png"]];
_textures[kTexture_Cactus_Gauche] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"cactus1.png"]];
_textures[kTexture_Cactus_Droit] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"cactus2.png"]];
_textures[kTexture_Pierre] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"pierre.png"]];
_textures[kTexture_Enseigne] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"enseigne.png"]];
_textures[kTexture_Menu] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"saloonIntro.jpg"]];
_textures[kTexture_GameOver] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"gameOver.jpg"]];

for (int i = 0 ; i < kNumTexturesScene ; i ++)
{
    [arrayOfText addObject:[[[NSData alloc] init] autorelease]];    
}
// sort my array
for (int i = 0 ; i < kNumTexturesScene ; i ++)
{
    [arrayOfText replaceObjectAtIndex:i withObject:_textures[i]];
}

[dictionaryOfTexture setObject:[arrayOfText copy] forKey:kTextureDecor];
[arrayOfText removeAllObjects];

and so on for almost 50 pictures

It works well on the 3GS but there are some issues sometimes with 3G.

Am i wrong with all of this ?

Thanks

Upvotes: 3

Views: 1086

Answers (2)

Kornel Kisielewicz
Kornel Kisielewicz

Reputation: 57625

Things that you need to consider:

  • iPhone works only with textures that have dimensions of a power of 2 -- if your textures don't have such dimensions, and they still works, that means that they are resized by the software -- that takes time, and more importantly valuable texture memory.
  • iPhone has video memory for about 3 textures of 1024x1024 size -- you may run out of texture memory.
  • switching textures during rendering is slow... extremely slow. The less you switch textures the better – ideally, create at most 3 textures, and switch them at most 3 times
  • to achieve that you need to learn a technique called texture atlasing

Upvotes: 3

Gordon Childs
Gordon Childs

Reputation: 36169

You might be running out of texture memory, which isn't surprising if you consider that the class you're using probably pads your image out to power of 2 dimensions.

You could use the texture memory better by combining your sprites into a so called sprite atlas.

Upvotes: 1

Related Questions