簡體   English   中英

SOIL_load_image() 返回 null

[英]SOIL_load_image() returns null

回答

使用SOIL_last_result() ,正如dcook所建議的,我發現了兩件事:

1)它找不到圖像,正如PaulMcKenzie所說,所以我的工作目錄確實像genpfault提到的那樣不正確。

2)設置完整路徑后,它提到不支持我的jpeg格式(漸進式)。 我將圖像設置為標准 jpeg 格式並且它工作正常。

謝謝您的幫助!


原問題

我目前正在嘗試使用 SOIL 加載圖像以與 OpenGL 一起使用。 但是,它似乎無法正確加載圖像,因為它分配給的變量最終為空。 我試過看這個,但似乎這個人在設置屬性時只是放置了錯誤的布局位置。 我讓它在每一行之后檢查錯誤( glGetError() ),但為了可讀性,我在這里省略了它。

OpenGL 錯誤發生在帶有GL_INVALID_VALUE glTexImage2D()之后。 這更可能是因為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);

讓我知道是否需要更多數據。 謝謝!

編輯 1:是的,圖像位於正確的位置:bin目錄

另外,我嘗試使用完整路徑,但沒有幫助。

由於這似乎是 SOIL 而不是 GL 的問題,請在調用SOIL_load_image后使用SOIL_last_result檢查最后一個 SOIL 錯誤。 這應該為您提供有關實際問題的更好線索。

我遇到過同樣的問題。 解決方案是設置文件的完整路徑,例如

C:/TestProjectsCPP/OpenGLTutorials/OpenGLTutorials/res/images/image1.jpg 

加載並顯示此圖像后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM