SOIL_load_image()返回null

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

ANSWER

使用SOIL_last_result()后,按照dcook的建议,我发现了两件事:

1)如PaulMcKenzie所述找不到图像,因此我的工作目录确实不正确,如genpfault所述。

2)设置完整路径后,它提到我的jpeg格式(渐进式)不受支持。我将图像设置为标准jpeg格式,并且可以正常工作。

感谢您的帮助!


原始问题

我目前正在尝试使用SOIL加载图像以用于OpenGL。但是,由于分配给该变量的变量最终为null,因此似乎无法正确加载该图像。我尝试查看this,但似乎当他设置属性时,他只是放错了布局位置。我让它在每一行之后检查错误(glGetError()),但是为了便于阅读,在此省略了它。

glTexImage2D()GL_INVALID_VALUE之后发生OpenGL错误。这很有可能是因为imgWidth / imgHeight由于空图像而大于GL_MAX_TEXTURE_SIZE

输出:

null: 1
Max size: 3379
Width: 4298563
Height: 2686488
Obj: 1
GL_INVALID_VALUE - ../src/polygon.cpp:222

代码:

    // Generate the texture object and binds it.
    glGenTextures(1, &m_texture);
    glBindTexture(GL_TEXTURE_2D, m_texture);

    // Texture image data
    int imgWidth, imgHeight;

    // Load the texture image.
    unsigned char* image = SOIL_load_image("potato.jpg",
                  &imgWidth,
                  &imgHeight,
                  0,
                  SOIL_LOAD_RGB);


    std::cout << "null: " << !image << std::endl;
    std::cout << "Max size: " << GL_MAX_TEXTURE_SIZE << std::endl;
    std::cout << "Width: " <<  imgWidth << std::endl;
    std::cout << "Height: " << imgHeight << std::endl;
    std::cout << "Obj: " << m_texture << std::endl;

    // Generate the texture to the currently bound texture object.
    glTexImage2D(GL_TEXTURE_2D,
         0,
         GL_RGB,
         imgWidth,
         imgHeight,
         0,
         GL_RGB,
         GL_UNSIGNED_BYTE,
         image);

    // Generate the mipmap to the currently bound texture object.
    glGenerateMipmap(GL_TEXTURE_2D);

    // Unbind and free image data.
    SOIL_free_image_data(image);
    glBindTexture(GL_TEXTURE_2D, 0);

让我知道是否需要更多数据。谢谢!

EDIT 1:是,图像在正确的位置:“

而且,我尝试使用完整路径,但没有帮助。

c++ opengl textures soil
2个回答
3
投票

由于这似乎是SOIL而不是GL的问题,请在调用SOIL_last_result之后再用SOIL_load_image检查最后一个SOIL错误。这应该为您提供实际发生问题的更好线索。


0
投票

我有相同的。解决方案是设置文件的完整路径,例如C:/TestProjectsCPP/OpenGLTutorials/OpenGLTutorials/res/images/image1.jpg。加载并显示该图像之后。也花了一天时间:smiley:

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