在 OpenGL ES iPhone 中加载非二次方图像

发布于 2024-12-01 11:55:30 字数 120 浏览 1 评论 0原文

有谁知道将大小不是 2 的幂的纹理加载到 OpenGL ES 中的有效方法吗?我是 OpenGL 新手,正在为 iPhone 开发 2D 游戏,并且已经制作了很多纹理。返回并将所有纹理的大小调整为 2 的幂将是非常乏味的工作。

Does anyone know an efficient way to load textures into OpenGL ES that are not in sizes of a power of two? I am new to OpenGL, and I'm working on a 2D game for iPhone and I have a lot of textures already made. It would be very tedious job to go back and resize all of my textures to a power of two.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

从此见与不见 2024-12-08 11:55:30

出于性能原因,最好将所有精灵放入图集中。图集是一个大型纹理,其中包含所有精灵。有一些工具可以自动化这个过程。例如,TexturePacker: http://www.texturepacker.com/

根据您使用的技术,您可以可能必须解析纹理打包器的输出才能获取 UV 偏移。

For performance reasons, it's best to putt all your sprites into an atlas. An atlas is a large texture, that contains all your sprites. There are tools to automate this process. For example TexturePacker: http://www.texturepacker.com/

Depending on which technology you're using, you might have to parse the output from texture packer to get the UV-Offsets.

故事与诗 2024-12-08 11:55:30
unsigned int NextPOT(unsigned int x)
{
    x = x - 1;
    x = x | (x >> 1);
    x = x | (x >> 2);
    x = x | (x >> 4);
    x = x | (x >> 8);
    x = x | (x >>16);
    return x + 1;
}

unsigned int width  = CGImageGetWidth(image.CGImage);
unsigned int height = CGImageGetHeight(image.CGImage);
unsigned int Width_POT  = NextPOT(width);
unsigned int Height_POT = NextPOT(height);
CGRect Rect = CGRectMake(0,0,width, height);
CGSize size         = CGSizeMake(Width_POT, Height_POT);
UIGraphicsBeginImageContext(size);
[image drawInRect:Rect];
UIImage* result     = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

image 是源图像,大小不等于 2,result 是可以传递给 OpenGL 的图像

unsigned int NextPOT(unsigned int x)
{
    x = x - 1;
    x = x | (x >> 1);
    x = x | (x >> 2);
    x = x | (x >> 4);
    x = x | (x >> 8);
    x = x | (x >>16);
    return x + 1;
}

unsigned int width  = CGImageGetWidth(image.CGImage);
unsigned int height = CGImageGetHeight(image.CGImage);
unsigned int Width_POT  = NextPOT(width);
unsigned int Height_POT = NextPOT(height);
CGRect Rect = CGRectMake(0,0,width, height);
CGSize size         = CGSizeMake(Width_POT, Height_POT);
UIGraphicsBeginImageContext(size);
[image drawInRect:Rect];
UIImage* result     = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

image is the source image which size is non pow of 2, result is the image you can pass to OpenGL

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