簡體   English   中英

如何正確加載圖像以用作OpenGL紋理?

[英]How to properly load an image to use as an OpenGL texture?

我正在嘗試使用SOIL2將圖像加載到OpenGL紋理中; 但是,除非我使用SOIL2的紋理加載功能,否則這似乎是不正確的。 我嘗試使用機頂盒圖像和Devil,但兩者都得到相似的結果。

碼:

GLuint load_image(const std::string& path) {
    int iwidth, iheight, channels;
    unsigned char* image = SOIL_load_image(path.c_str(), &iwidth, &iheight, &channels, SOIL_LOAD_RGBA);
    //  std::cout << SOIL_last_result() << std::endl;
    //  float* image = stbi_loadf(path.c_str(), &iwidth, &iheight, &channels, STBI_rgb_alpha);
    //  if(!ilLoadImage(path.c_str()))
    //      std::cout << "Devil Failed to load image: " << iluErrorString(ilGetError()) << std::endl;
    //
    //  unsigned char* image = ilGetData();
    //
    //  int iwidth = ilGetInteger(IL_IMAGE_WIDTH);
    //  int iheight = ilGetInteger(IL_IMAGE_HEIGHT);
    //  int channels = ilGetInteger(IL_IMAGE_CHANNELS);

    GLuint texture;
    glGenTextures(1, &texture);

    glActiveTexture(GL_TEXTURE0 + texture);
    GLint old_unpack_alignment;
    glGetIntegerv(GL_UNPACK_ALIGNMENT, &old_unpack_alignment);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glBindTexture(GL_TEXTURE_2D, texture);
    glCheckError();

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glCheckError();

    GLenum original_format = (channels == 4 ? GL_RGBA : GL_RGB);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);

    glPixelStorei(GL_UNPACK_ALIGNMENT, old_unpack_alignment);
    return texture;
}

屏幕截圖:

示例輸出

我應該得到什么:

正確的輸出

我想知道如何正確地將圖像加載到紋理中。

這是我的紋理加載功能的示例:

unsigned int loadTexture(char const * path)
{
    unsigned int textureID;
    glGenTextures(1, &textureID);

    int width, height, nrComponents;
    unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
    if (data)
    {
        GLenum format;
        if (nrComponents == 1)
            format = GL_RED;
        else if (nrComponents == 3)
            format = GL_RGB;
        else if (nrComponents == 4)
            format = GL_RGBA;

        glBindTexture(GL_TEXTURE_2D, textureID);
        glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        stbi_image_free(data);
    }
    else
    {
        std::cout << "Texture failed to load at path: " << path << std::endl;
        stbi_image_free(data);
    }

    return textureID
}

通常,我會先手動設置VAO和VBO,然后再使用它來加載紋理。 此后,我將配置我的着色器以供使用,然后在渲染循環中使用着色器,設置要傳遞的矩陣,傳遞所需的任何制服,然后在完成所有“模型”信息后,最后,將綁定VertexArrays,將適當的紋理設置為Active,然后將這些紋理綁定,然后繪制數組或基元進行繪制。

暫無
暫無

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

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