views:

424

answers:

2

Hi guys,

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

+1  A: 

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.

Rhythmic Fistman
+2  A: 

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 those you need to learn a technique called texture aliasing
Kornel Kisielewicz
Thank you to you two, Rhythmic Fistman and Kornel Kisielewicz. Yes, my textures are resized to fit 256x256 or less.So basically, with texture atlas, i put all my images into a single one, and then load this huge atlas (or somes if it's too big).Then, i will create an array which contains positions of each pieces of this atlas and rewrite some of my drawing method. Am i wrong ?
ipodishima
@ipodishima - yes and no -- you make it maximum 1024x1024 so you always load it completely into texture memory. BTW, if you appreciate Rhythmics and mine answer, don't forget to upvote them -- this is how SO works :).
Kornel Kisielewicz
Yeah i now have enough reputation to vote !What really piss me off is that it works well on iPod Touch or 3GS. I also tried to use shareGroup, but this is not working, the display is like piece of data of a graphic buffer. Really strange. I can't figure out why.Actually, if i have to rewrite how i display my images, maybe sould i use Cocos2D. FurtherMore, my render code is about 2000 lines and it's my first OpenGL project (was supposed to be very simple but i had to add more and more levels...)
ipodishima