Hello, I have used FreeImage for texturing a rectangle. I am using jpg file. But I get color a bit distorted. Could anyone tell me what’s the reason? For clarification purpose, I have pasted the coding. Here LoadTexture function loads texture and drawRectangle function draws rectangle.
/**********************************************************/
GLuint LoadTexture( const char * filename, int wrap )
{
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
fif = FreeImage_GetFileType(filename, 0); //if still unknown, try to guess the file format from the file extension
//check that the plugin has reading capabilities and load the file
if(FreeImage_FIFSupportsReading(fif))
dib = FreeImage_Load(fif, filename);
//if the image failed to load, return failure
//retrieve the image data
bits = FreeImage_GetBits(dib); //get the image width and height
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
// allocate a texture name
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bits);
// free buffer
FreeImage_Unload(dib);
return texture;
}
/***********************************************************/
void drawRectangle( ){
// setup texture mapping
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );
glPushMatrix();
glBegin( GL_QUADS );
glEnable(GL_COLOR_MATERIAL);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glEnable(GL_COLOR_MATERIAL);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glEnable(GL_COLOR_MATERIAL);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glEnable(GL_COLOR_MATERIAL);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
glPopMatrix();
glDisable( GL_TEXTURE_2D );
}
/******************************************************/