像这样加载openGL纹理是否正确?

发布于 2024-08-19 23:27:48 字数 2022 浏览 4 评论 0原文

几个月前,我开始了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2024-08-26 23:27:49

您需要考虑的事项:

  • iPhone 仅适用于尺寸为 2 的幂的纹理 - 如果您的纹理没有这样的尺寸,但它们仍然有效,这意味着它们会被软件调整大小 - 这需要时间,更重要的是宝贵的纹理内存。
  • iPhone 拥有大约 3 个 1024x1024 大小的纹理的视频内存 - 您可能会耗尽纹理内存。
  • 在渲染过程中切换纹理很慢...非常慢。切换纹理越少越好 - 理想情况下,最多创建 3 个纹理,并最多切换 3 次,
  • 以实现您需要学习一种名为 纹理图集

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
梦中楼上月下 2024-08-26 23:27:49

您可能会耗尽纹理内存,这并不奇怪
如果您认为您正在使用的类可能会将您的图像填充到二维次方。

您可以通过将精灵组合成更好地使用纹理内存
所谓的精灵图集。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文