像这样加载openGL纹理是否正确?
几个月前,我开始了一个 OpenGL 项目,该项目变得越来越大......我从 crashLanding 示例开始,并使用Texture2D。
我还使用单例类来加载我的纹理,这是纹理加载的样子:
//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];
等等,几乎 50 张图片
它在 3GS 上运行良好,但有时在 3G 上会出现一些问题。
我这一切都错了吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要考虑的事项:
Things that you need to consider:
您可能会耗尽纹理内存,这并不奇怪
如果您认为您正在使用的类可能会将您的图像填充到二维次方。
您可以通过将精灵组合成更好地使用纹理内存
所谓的精灵图集。
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.