3

I've been trying to display an image using FreeImage in C++ and have been having a lot of trouble in doing this.The load function seems to load the image, but it doesn't seem to bind the image. Here is what i've got so far:

    GLuint Load(){
string imgdir = "Path to my file";
FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(imgdir.c_str());
FIBITMAP *bitmap = FreeImage_Load(fif,imgdir.c_str(),0);
int height, width;
height = FreeImage_GetHeight(bitmap);
width = FreeImage_GetWidth(bitmap);
GLuint textureid;
glGenTextures(1, &textureid);
glBindTexture(GL_TEXTURE_2D, textureid);
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, width, height, 0, GL_RGBA,        GL_UNSIGNED_BYTE, bitmap);
FreeImage_Unload(bitmap);
return textureid;}

void Square(){
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(1.0, 0.0);glVertex3f(3.0, 0.0, 0.0);
glTexCoord2f(1.0, 1.0);glVertex3f(3.0, 5.0, 0.0);
glTexCoord2f(0.0, 1.0);glVertex3f(0.0, 5.0, 0.0);
glEnd();}

Using this all I am getting is just a white rectangle. Any point in the right direction would be appreciated.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
user11046
  • 39
  • 2

1 Answers1

2

You're not passing the bits, but the freeimage object. You may use something like this to get the actual bits:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(bitmap));

Make sure the format of the input image matches RGBA.

Kamyar Infinity
  • 2,711
  • 1
  • 21
  • 32
  • For some reason it still doesn't seem to be working. Is there something else that could be fixed? Thanks for your help. – user11046 May 15 '16 at 04:04
  • 1
    So what is the image format you're using? You might need different flags. You may also try to print the width, height or other information to make sure the image is correctly loaded by FreeImage. – Kamyar Infinity May 15 '16 at 04:39
  • I am using a jpeg. It is showing the correct height and width and I tested it before by saving the image as a new one after loading, so that seems to be working fine. – user11046 May 15 '16 at 05:18
  • For jpeg you don't have transparency, so you should use something like: `glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(bitmap))`. If this doesn't work, I believe the problem is the output of `FreeImage_GetBits` has a different alignment, which you'll need to create the buffer explicitly beforehand, as explained [here](http://malgouyres.org/freeimagehowto). – Kamyar Infinity May 15 '16 at 05:49