Jump to content

  • Log In with Google      Sign In   
  • Create Account


OpenGL Texture Loading with FreeImage


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Katherine1   Members   -  Reputation: 132

Like
0Likes
Like

Posted 11 July 2012 - 11:08 AM

I have been working on a project in OpenGL, and up until this point, I have been using SDL for the main structure of my program. This worked fine for the most part, as I could use SDL's image loading library to take care of texture loading. Unfortunately, I've reached a point where I want to be able to use my texture class in other OpenGL programs, most notably a Qt OpenGL app for a map editor. Unfortunately, as long as my texture loader is dependent on SDL, I can't really use the library with Qt.

I decided instead to use a separate image loading library, FreeImage. This should be easy, but for whatever reason, the texture is not being loaded. I have no clue what I am doing wrong. The path to the png I am trying to load is unchanged from when I was using SDL, so I know I'm pointing to the correct location.

I am saving the file information to instance variables, however, the printf at the bottom of loadAtlas always reports a width and height of 0 for the image. I am not sure what is going wrong.

trimmed header file

class TextureAtlas {
    protected:

        /**
         * Texture information
         */
        GLuint texture;

        unsigned int width;
        unsigned int height;
        GLenum texture_format;
        GLubyte* data;

    public:

        TextureAtlas();
        virtual ~TextureAtlas();

        /**
         * loadAtlas()
         * Loads a Texture as a Texture Atlas
         * Parameters:
         * loc - The location of the file to load.
         * tileWidth - The width of the tiles in the given atlus.
         * tileHeight - The height of the tiles in the given atlus.
         */
        void loadAtlas(std::string loc, unsigned char tileWidth, unsigned char tileHeight);
};

Relevent Code
void TextureAtlas::loadAtlas(std::string loc, unsigned char width, unsigned char height) {

    const char *filename = loc.c_str();
FREE_IMAGE_FORMAT format;
FIBITMAP *dib(0);
FREE_IMAGE_COLOR_TYPE type;

format = FreeImage_GetFileType(filename);
dib = FreeImage_Load(format, filename);
data = FreeImage_GetBits(dib);
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
type = FreeImage_GetColorType(dib);

switch ( type )
{
case FIC_RGB:
texture_format = GL_RGB;
break;
case FIC_RGBALPHA:
texture_format = GL_RGBA;
break;
    default:
        break;
}

//Generate the texture
glGenTextures( 1, &texture );

    //Store the width and height of the tiles contained within the texture.
tileWidth = width;
tileHeight = height;
//Save the texture location for later use.
    textureLocation = loc;

    printf("Image information:\nFilename: %s\nWidth: %d\nHeight %d\n",loc.c_str(), width, height);

}


Sponsor:

#2 teutonicus   Members   -  Reputation: 518

Like
0Likes
Like

Posted 11 July 2012 - 05:23 PM

To debug this problem, these are the steps I'd take:

1. Make sure you're initialising the library correctly if necessary.

2. Make sure FreeImage isn't giving you any errors (give it an output message callback using FreeImage_SetOutputMessage).

3. Assuming there aren't any useful errors, separate logic for opening/reading the file and parsing the file. Use a FILE* or whatever you're comfortable with to open and read the file yourself, verify that the contents of it are what you're expecting, then tell FreeImage to parse it from memory. This way you will be absolutely sure that the path's correct and the file exists. So the first few lines of your loadAtlas functions would look something like this:
FILE * file = fopen(filename, "rb");
// ... verify file exists, contents are correct, check format if necessary, etc.
FIMEMORY stream = FreeImage_OpenMemory(file_data, file_size);
dib = FreeImage_LoadFromMemory(format, stream);
// ...
The above's untested but you should get the idea; open the file itself separately and use FreeImage's load from memory functions so that you can be sure that FreeImage isn't/SDL wasn't doing something strange or unexpected under the hood with your file path.

4. If FreeImage is still failing to parse the file correctly and doesn't give you any useful errors after you've gotten the above working, then try using different formats for your images.

5. Use stb_image instead.

Edited by teutonicus, 11 July 2012 - 05:27 PM.


#3 FireInDark   Members   -  Reputation: 148

Like
0Likes
Like

Posted 24 July 2012 - 02:38 AM

int loadImageToSkinPool(char* image_path)
{
if(!image_path) return 0;
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP* dib(0);
BYTE* bits(0);
unsigned int width(0), height(0);
GLuint glHandle;
fif = FreeImage_GetFileType(image_path,0);
if(fif == FIF_UNKNOWN)
fif = FreeImage_GetFIFFromFilename(image_path);
if(fif == FIF_UNKNOWN)
{
return false;
}
if(FreeImage_FIFSupportsReading(fif))
dib = FreeImage_Load(fif,image_path);
if(!dib)
{
return false;
}
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
if(FREE_IMAGE_COLOR_TYPE::FIC_RGBALPHA != type)
{
FIBITMAP* ndib = FreeImage_ConvertTo32Bits(dib);
dib = ndib;
}
bits = FreeImage_GetBits(dib);
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
if((bits == 0) || (width == 0) || (height == 0))
return false;
SkinInfo skin;
glGenTextures(1,&skin.mHandle);
glBindTexture(GL_TEXTURE_2D,skin.mHandle);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_BGRA,GL_UNSIGNED_BYTE,bits);
skin.mWidth = width;
skin.mHeight = height;
skin.mName = image_path;
mSkinPool.push_back(skin);
FreeImage_Unload(dib);
return 1;
}

Codes above not so safe but can load images to build texture.. check it your self

Edited by FireInDark, 24 July 2012 - 02:40 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS