Hello, I picked up freeimage last night, and, It seems like a really awesome library. I have been having issues getting it to work however. I am using GLWF, and, installed FreeImage correctly into my project, as well as linked it to my project. I copied the TextureManager class from the downloads examples, but, I am having issues getting my image to display properly.
If somebody could take a look at my code, as well as fill me in on common mistakes new users of this library face, I would be very appreciative.
TextureManager.cpp
#include "TextureManager.h"
TextureManager::TextureManager()
{
}
TextureManager::~TextureManager()
{
}
void TextureManager::LoadTexture(string name, char* imageURL)
{
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP *dib(0);
BYTE* bits(0);
unsigned int width(0), height(0);
cout << "Texture Number: " << textures.size() << endl;
GLuint gl_texID = textures.size();
fif = FreeImage_GetFileType(imageURL, 0);
if(fif == FIF_UNKNOWN)
{
fif = FreeImage_GetFIFFromFilename(imageURL);
cout << "Getting Image Type" << endl;
}
if(fif == FIF_UNKNOWN)
{
cout << "Failed to get image type" << endl;
}
if(FreeImage_FIFSupportsReading(fif))
{
dib = FreeImage_Load(fif, imageURL);
cout << "Trying to load image: " << endl;
if(fif == 13)
{
cout << "Image Format is PNG" << endl;
}
}
if(!dib)
{
cout << "Failed to load image" << endl;
}
bits = FreeImage_GetBits(dib);
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
cout << "Image Height: " << height << " Width: " << width << endl;
if((bits == 0) || (width == 0) || (height == 0))
if(textures.find(name) != textures.end())
{
cout << "Deleting Texture: " << name << endl;
glDeleteTextures(1, &(textures[name]));
}
cout << "Texture Loaded, generating texture..." << endl;
textures.insert ( pair<string,int>(name, gl_texID) );
glGenTextures(1, &gl_texID);
glBindTexture(GL_TEXTURE_2D, gl_texID);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
FreeImage_Unload(dib);
}
void TextureManager::UnloadTexture(string name)
{
if(textures.find(name) != textures.end())
{
glDeleteTextures(1, &(textures[name]));
textures.erase(name);
}
}
void TextureManager::UnloadAllTextures()
{
std::map<string, GLuint>::iterator i = textures.begin();
while(i != textures.end())
{
UnloadTexture(i->first);
}
textures.clear();
}
void TextureManager::BindTexture(string name)
{
if(textures.find(name) != textures.end())
{
glBindTexture(GL_TEXTURE_2D, textures[name]);
}
}
Graphics.cpp
#include "Graphics.h"
Graphics::Graphics()
{
FreeImage_Initialise();
}
Graphics::~Graphics()
{
}
void Graphics::release()
{
}
void Graphics::initGraphics()
{
textureManager.LoadTexture("courior", "Resources\\font\\courior2.png");
}
void Graphics::draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
textureManager.BindTexture("courior");
glBegin(GL_QUADS);
glTexCoord2f(0, 1);
glVertex2f(-128, 128);
glTexCoord2f(1, 1);
glVertex2f(128, 128);
glTexCoord2f(1, 0);
glVertex2f(128, -128);
glTexCoord2f(0, 0);
glVertex2f(-128, -128);
glEnd();
glfwSwapBuffers();
}
My texture is loaded in the initGraphics() function. Again, I am super appreciative of any help I can get on this. I must be doing something small wrong.
Also, again, my issue is that the bound image displays as completely white on the quad. Through testing, I made sure courior2.png was a 512 x512 multicolored image with no transparent portions. If the image was displaying in any way, shape, or form. It would show.
I know that I should post this on the FreeImage support forums, but, my attempts to get answers there have been unsuccessful thus far. As such, I am hoping that somebody in this community has some experience with loading PNGs in openGL :). You guys have always helped me before. :)
Thanks,
Shawn