将 PNG 纹理加载到 OpenGL

发布于 2024-08-29 00:17:13 字数 127 浏览 4 评论 0原文

我正在开发一款游戏,我的所有图形都使用洋红色作为透明/魔法颜色。 它们都是 32 位的,洋红色只是为了方便。 不管怎样,如果有人能建议我应该使用什么库来加载图像(我需要以 GL_RGBA 格式加载它们,包括内部和纹理特定的),我将不胜感激。

I'm working on a game, and all of my graphics use magenta as transparent/magic color.
They are all 32-bit and the magenta is just for conveniency.
Anyway, I would appreciate if someone could advice me what library should I use to load my images (I need to load them in GL_RGBA format, both internal and texture specific).

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

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

发布评论

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

评论(5

身边 2024-09-05 00:17:13

如果只需要 PNG 支持,请使用 libpng。 DevIL 应该很简单,但它有点臃肿(不仅仅是加载图像),并且在内部实际上调用了 OpenGL 函数,这可能会扰乱您自己的 OpenGL 逻辑。

我个人更喜欢 SDL_image,因为无论如何我都在我的项目中使用 SDL。虽然不是很明显,但 SDL_BlitSurface() 函数可以将 IMG_Load() 返回的任何内容转换为必要的像素格式。

If only PNG support is necessary, use libpng. DevIL is supposed to be easy, but it's somewhat bloated (does a lot more than just load images) and internally actually calls OpenGL functions which can mess with your own OpenGL logic.

I personally prefer SDL_image, since I'm using SDL in my projects anyway. While not immediately obvious, SDL_BlitSurface() function can do the conversion from whatever IMG_Load() returns to the necessary pixel format.

¢好甜 2024-09-05 00:17:13

DevIL 可以加载几乎所有文件格式,并可以直接创建 OpenGL 纹理。这是最简单的方法。

您还应该使用支持 Alpha 通道的文件格式(PNG、TGA...)。在 32 位图像中使用“魔法颜色”确实已经过时了!

DevIL can load virtually every file format and can directly create OpenGL textures. It is the easiest way to go.

You should also use a file format which supports an alpha channel (PNG, TGA, ...). Using a "magic color" in 32-bit images is really out-dated!

偷得浮生 2024-09-05 00:17:13

除了提到 SDL 和 DevIL 的其他答案之外,还有两个选项需要考虑:

  • 直接使用 libpng。如果重要的话,这对代码大小的影响可能最小,因为您不使用的其他格式不会出现膨胀,没有 DLL 等。
  • 使用操作系统纹理加载。如果您更喜欢使用操作系统功能而不是外部库,这可能是减少依赖性的好方法。 Windows XP 及更高版本中的 GDI+ 具有针对 PNG 和 JPEG 等几种格式的内置纹理加载,我不确定,但其他操作系统可能具有类似的功能。将 GDI+ 连接到 OpenGL 非常简单,然后操作系统就会负责纹理加载!

Apart from the other answers mentioning SDL and DevIL, there are two more options to consider:

  • Use libpng directly. This will probably have the smallest impact on the code size if that matters, since you get no bloat for other formats you're not using, no DLLs, etc.
  • Use operating-system texture loading. This can be a nice way to reduce dependencies if you prefer using OS features over external libraries. GDI+ in Windows XP and up has built-in texture loading for a few formats like PNG and JPEG, and I don't know for certain, but other OSs might have similar features. It's pretty simple to hook GDI+ up in to OpenGL and then the OS is taking care of your texture loading!
许一世地老天荒 2024-09-05 00:17:13

这里有一个非常简单的单文件示例,将 png 加载到 openGL 中:

http: //tfc.duke.free.fr/coding/src/png.c

There is a very minimalist one file example of loading a png into openGL here:

http://tfc.duke.free.fr/coding/src/png.c

最好是你 2024-09-05 00:17:13

另一种选择是 OpenCV。

它的功能不仅仅是纹理加载,而且您很有可能会发现它的其他功能的用途。

Another option is OpenCV.

It does a lot more than just texture loading, but odds are good you'll find use of its other features as well.

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