views:

210

answers:

1

I have 2 1024x1024 sprite sheets, one containing several 480x320 background images and another containing a variety of smaller elements as entities and interface. My framerate drops like a rock whenever I enter a scene in which I have to display sprites from both textures (forcing me to re-bind twice per frame, interface texture->bg texture->interface texture).

- (void)setCurrentTexture:(int)texID{
 if(currentTex == texID)
  return;
 else
  currentTex = texID;

 // Bind the texture name. 
 glBindTexture(GL_TEXTURE_2D, [Graphics getTexture:texID]);
 // Speidfy a 2D texture image, provideing the a pointer to the image data in memory
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [Graphics getTexWidth:texID], [Graphics getTexHeight:texID], 0, GL_RGBA, GL_UNSIGNED_BYTE, [Graphics getTexData:texID]);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}

Is there something wrong with the way I'm swapping my textures?

Edit: I've removed half of the state changes I've been making. Removing any of the above 3 results in my polygons being textured with nothing (i.e. the show up white). The biggest hitter is glTexImage2D... but trying to set it elsewhere once (at init, for example) ends up with my models/quads not being textured at all.

A: 

Unless you're updating the data in the textures you should only call glTexImage2D once for each texture when your application startup.

Andreas Brinck
I'm not touching the data in any of the textures, or at least, that shouldn't be the case... but if I do it once at startup, I end up with untextured polygons... I should be calling it as soon as I have a pointer to the actual sprite data, right?
akaii
Okay, got it fixed. You were right. glTexImage2D need only be called once. I forgot to call glBindTexture prior to glTexImage2D, which was why glTexImage2D was failing.
akaii
@akaii You'll only need to call `glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);` once for each texture as well, just so you know.
Andreas Brinck
Ah, yes, figured that out at the same time as the above. Only call on glBindTexture when switching textures now. Performance seems to be fine at the moment. Hrmm, everytime I come back to raw OpenGL or DirectX, I always forget how to to setup things that I usually do only once...
akaii