PDA

View Full Version : Problem in texturing using FreeImage



jenny_wui
10-27-2011, 02:03 AM
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 );



}
/************************************************** ****/

jenny_wui
10-27-2011, 05:17 AM
Actually in my posted question, I wanted to mean that I am not getting good quality in color as I find in original image. My textured image produces low quality, what's the reason? Could any one suggest how can I do that?

BionicBytes
10-27-2011, 09:11 AM
Your textures are 'stretched' across the verticies that you submit during rendering and the colour will be sampled from the texture and then filtered.
What's the colour depth of the franebuffer (16-bits,24 or 32?)

Perhaps post a screen shot?

You also have enabled (GL_COLOR_Material) for each vertex? why?

BionicBytes
10-27-2011, 09:15 AM
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

You also are not using mipmapping, so depending upon the size of the image on screen, the texture filtering could be the cause..... like I said, a screen shot is the best option.

jenny_wui
10-27-2011, 07:41 PM
Hello, I have uploaded one doc file to the following link.

http://www.sendspace.com/file/63lz7o

It contains the original image. I have also screenshot the textured output below it.

Please have a look and let me know what is wrong with my code or whether I need to do any thing else. Thanks.