C++ 如何在 OSX 下的 SDL/OpenGL 应用程序中加载 JPG/PNG 纹理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4153055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:38:19  来源:igfitidea点击:

How to load JPG/PNG Textures in an SDL/OpenGL App under OSX

c++macosopenglpngjpeg

提问by Patrick Oscity

i am writing an SDL / OpenGL application that runs under OSX. I have to use existing code which uses the DevIL library for loading JPG and PNG textures. Unfortunately, this works very bad under OS X, so i decided not to use DevIL at all, and rewrite the respective parts of the application using another library. I want to keep it flexible (DevIL can handle a lot of image formats) and easy to use. Is there a good replacement for DevIL that you can recommend? The application is entirely written in C++.

我正在编写一个在 OSX 下运行的 SDL/OpenGL 应用程序。我必须使用使用 DevIL 库的现有代码来加载 JPG 和 PNG 纹理。不幸的是,这在 OS X 下工作得非常糟糕,所以我决定根本不使用 DevIL,并使用另一个库重写应用程序的各个部分。我想保持它的灵活性(DevIL 可以处理很多图像格式)并且易于使用。是否有可以推荐的 DevIL 的良好替代品?该应用程序完全用 C++ 编写。

回答by lx.

Have a look at the SDL_imagelibrary. It offers functions like IMG_LoadPNGthat load your picture "as an" SDL_Surface. Since you already work with SDL this should fit quite well in your program.

看看SDL_image库。它提供的功能类似于IMG_LoadPNG将您的图片“作为”SDL_Surface 加载。由于您已经使用 SDL,这应该非常适合您的程序。

Sample taken from the SDL_image documentation:

SDL_image 文档中获取的示例

// Load sample.png into image
SDL_Surface* image = IMG_Load("sample.png");
if (image == nullptr) {
    std::cout << "IMG_Load: " << IMG_GetError() << "\n";
}

回答by uggwar

Take a look at freeimage. It supports all major formats and is easily built with macports. Nice to work with as well. Auto-detects image format etc.

看看freeimage。它支持所有主要格式,并且可以使用 macports 轻松构建。也很高兴与他们合作。自动检测图像格式等。

FREE_IMAGE_FORMAT format = FreeImage_GetFileType(filename.c_str(), 0);
FIBITMAP *bitmap = FreeImage_Load(format, filename.c_str());
if (!bitmap)
{
    LOG_ERROR("Unable to load texture: " + filename);
    return false;
}
mlWidth = FreeImage_GetWidth(bitmap);
mlHeight = FreeImage_GetHeight(bitmap);
glGenTextures(1, &mpTextures[0]);
glBindTexture(GL_TEXTURE_2D, mpTextures[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mlWidth, mlHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE,
       (GLvoid*)FreeImage_GetBits(bitmap));
FreeImage_Unload(bitmap);

回答by Chris Becke

If you're on Mac OS anyway, why not just use CGImageSourceto do the loading? OS X natively supports loading many file formats including PNG and JPEG.

如果您使用的是 Mac OS,为什么不直接使用CGImageSource进行加载呢?OS X 本身支持加载许多文件格式,包括 PNG 和 JPEG。