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

后端 未结 4 1015
长情又很酷
长情又很酷 2020-12-10 06:27

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

4条回答
  •  醉梦人生
    2020-12-10 06:51

    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.

    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);
    

提交回复
热议问题