Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Texture Loading with FreeImage

This topic is 2335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 {

* Texture information
GLuint texture;

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


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();
FIBITMAP *dib(0);

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;
texture_format = GL_RGBA;

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


Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
int loadImageToSkinPool(char* image_path)
if(!image_path) return 0;
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;
dib = FreeImage_Load(fif,image_path);
return false;
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
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;
skin.mWidth = width;
skin.mHeight = height;
skin.mName = image_path;
return 1;

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!