Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C++
#include "TextureManager.h"

TextureManager::TextureManager()
{
}

TextureManager::~TextureManager()
{

}

void TextureManager::LoadTexture(string name, char* imageURL)
{
	//image format
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	//pointer to the image, once loaded
	FIBITMAP *dib(0);
	//pointer to the image data
	BYTE* bits(0);
	//image width and height
	unsigned int width(0), height(0);

	//OpenGL's image ID to map to...This is set to the next index in the map
	cout << "Texture Number: " << textures.size() << endl;
	GLuint gl_texID = textures.size();
	
	//check the file signature and deduce its format
	fif = FreeImage_GetFileType(imageURL, 0);
	//if still unknown, try to guess the file format from the file extension
	if(fif == FIF_UNKNOWN)
	{
		fif = FreeImage_GetFIFFromFilename(imageURL);
		cout << "Getting Image Type" << endl;
	}
	//if still unkown, return failure
	if(fif == FIF_UNKNOWN)
	{
		cout << "Failed to get image type" << endl;
	}

	//check that the plugin has reading capabilities and load the file
	if(FreeImage_FIFSupportsReading(fif))
	{
		//dib = FreeImage_ConvertTo32Bits(dib);
		dib = FreeImage_Load(fif, imageURL);
		cout << "Trying to load image: " << endl;
		if(fif == 13)
		{
			cout << "Image Format is PNG" << endl;
		}
	}
	//if the image failed to load, return failure
	if(!dib)
	{
		cout << "Failed to load image" << endl;
	}

	//retrieve the image data
	bits = FreeImage_GetBits(dib);

	//get the image width and height
	width = FreeImage_GetWidth(dib);
	height = FreeImage_GetHeight(dib);
	cout << "Image Height: " << height << "  Width: " << width << endl;

	//if this somehow one of these failed (they shouldn't), return failure
	if((bits == 0) || (width == 0) || (height == 0))
	
	//if this texture ID is in use, unload the current texture
	if(textures.find(name) != textures.end())
	{
		cout << "Deleting Texture: " << name << endl;
		glDeleteTextures(1, &(textures[name]));
	}

	cout << "Texture Loaded, generating texture..." << endl;

	//Insert GL Texture Index Into Image Map
	textures.insert ( pair<string,int>(name, gl_texID) );

	//generate an OpenGL texture ID for this texture
	glGenTextures(1, &gl_texID);
	//bind to the new texture ID
	glBindTexture(GL_TEXTURE_2D, gl_texID);
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
	//store the texture data for OpenGL use
	//glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height, border, image_format, GL_UNSIGNED_BYTE, bits);

	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); // If the u,v coordinates overflow the range 0,1 the image is repeated
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	//Free FreeImage's copy of the data
	FreeImage_Unload(dib);

	//return success
	//glBindTexture(GL_TEXTURE_2D, gl_texID);
}

void TextureManager::UnloadTexture(string name)
{
	if(textures.find(name) != textures.end())
	{
		glDeleteTextures(1, &(textures[name]));
		textures.erase(name);
	}
}

void TextureManager::UnloadAllTextures()
{
	//start at the begginning of the texture map
	std::map<string, GLuint>::iterator i = textures.begin();

	//Unload the textures untill the end of the texture map is found
	while(i != textures.end())
	{
		UnloadTexture(i->first);
	}

	//clear the texture map
	textures.clear();
}

void TextureManager::BindTexture(string name)
{
	//bool result(true);
	//if this texture ID mapped, bind it's texture as current
	if(textures.find(name) != textures.end())
	{
		glBindTexture(GL_TEXTURE_2D, textures[name]);
	}
	//otherwise, binding failed
}


Graphics.cpp
C++
#include "Graphics.h"
//Graphics* Graphics::graphics = NULL;

Graphics::Graphics()
{
	FreeImage_Initialise();
}

Graphics::~Graphics()
{

}

/*Graphics* Graphics::get()
{
	if(graphics == NULL){ graphics = new Graphics(); }
    return graphics;
}
*/
void Graphics::release()
{
	//delete graphics;
}

void Graphics::initGraphics()
{
	textureManager.LoadTexture("courior", "Resources\\font\\courior2.png");
}

void Graphics::draw()
{
	// OpenGL rendering goes here...
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//Enable Blending
	//glEnable(GL_BLEND);
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	//glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	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();

	//Disable Blending
	//glDisable(GL_BLEND);
	// Swap front and back rendering buffers
	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
Posted

//Initialize OpenGL
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        //Define Viewport and lower left corner
		glViewport(0, 0, wWidth, wHeight);
    glMatrixMode(GL_MODELVIEW);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, wWidth, wHeight, 0, -1, 1);
    
	//Set the center of the screen as (0, 0)
    glTranslatef(wWidth / 2, wHeight / 2, 0);
 
Share this answer
 
I want everybody to know that this issue is solved. I was trying to create and bind the image before the openGL context was created. My oversight. I feel like a dufas :P
 
Share this answer
 
1. Did you initialize opengl context ?
2. Did you enabled TEXTURE_2D before calling texture functions of opengl
 
Share this answer
 
Comments
shawndeprey 1-Mar-12 9:56am    
Holy Crap! I don't think I enabled TEXTURE_2D! Give me 1 minute lol
shawndeprey 1-Mar-12 10:06am    
Dang. I was really hopeful for a second, but, that didn't work. I left my enabling of GL_TEXTURE_2D in the code though. Can't believe I forgot it XD. Any other ideas as to how or why my image would still appear white after the texture 2d being enabled? I did initialize my openGL context BTW. I will post below what I did for openGL Initialization. Perhaps you will see something in there.
jk chan 1-Mar-12 10:43am    
one more suggestion.. create a buffer which contains full of red.(ie only alpha,and red bytes are 256) then pass it to TextImage2D function.. see what is happening.. check if it is still white..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900