如何使用 ResIL 加载文件?

问题描述 投票:0回答:1

因此,我正在遵循一些教程来学习如何使用 OpenGL(可在here找到),并尝试加载第 6 课中的图像。

我遇到的问题是无法加载文件。我收到 IL_INVALID_EXTENTION 错误代码 (1291)。首先是代码示例:

// Here is the code calling the load function
if (!gTexture.loadTextureFromFile("LazyFooTutorials/texture.png"))
{
    printf("Unable to load texture from file...\n");
    return false;
}

// Here is the code that calls ResIL
bool LTexture::loadTextureFromFile(std::string path) {
    bool textureLoaded = false;

    ILuint imgID = 0;
    ilGenImages(1, &imgID);
    ilBindImage(imgID);

    ILboolean success = ilLoadImage(path.c_str());

    if (success == IL_TRUE)
    {
        success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

        if (success == IL_TRUE)
        {
            textureLoaded = loadTextureFromPixels32((GLuint*)ilGetData(), (GLuint)ilGetInteger(IL_IMAGE_WIDTH), (GLuint)ilGetInteger(IL_IMAGE_HEIGHT));
        }

        ilDeleteImages(1, &imgID);
    }

    if (!textureLoaded)
    {
        printf("Failed to load image: %s\n", path.c_str());
    }

    return textureLoaded;
}

我以为ResIL可以加载png,我会去找ResIL文档来验证这一点。

c++ opengl freeglut
1个回答
0
投票

问题是 ilLoadImage 在 Windows 上需要 UTF-16 字符串。 C++ std::string 的问题在于它给用户留下了编码问题,这对于非英语用户来说是一个常见问题。考虑这个例子:

std::string brokenFN = "E:\\BGTestFileCollection\\png\\Manga.png";
auto loadResult = il2Load(image, IL_TYPE_UNKNOWN, (const wchar_t*)brokenFN.c_str());
std::wstring fixedFN = L"E:\\BGTestFileCollection\\png\\Manga.png";
auto loadResult2 = il2Load(image, IL_TYPE_UNKNOWN, fixedFN.c_str());

上面的代码对于 BrokenFN 失败,因为 ASCII 字符串作为 UTF-16 没有意义,而在 Windows 上此函数需要 UTF-16。然而,fixedFN 是一个 UTF-16 字符串,并且它可以工作。另请注意,在非 Windows(如 MacOS 或 Linux)上,ilLoadImage 确实需要 8 位编码的字符串(ASCII 或 UTF-8)。 ResIL 只是期望本机操作系统函数使用的编码。

一个简单的解决方法是定义一个新类型 SystemString,它将是 std::string 或 std::wstring,具体取决于您当前正在编译的目标操作系统。

请使用 ResIL 的 Sourceforge 论坛询问有关 ResIL 的问题,因为我收到了有关该论坛上提出的问题的电子邮件:

https://sourceforge.net/projects/resil/support

© www.soinside.com 2019 - 2024. All rights reserved.