Sign in to follow this  
jon723

Distorted texture map

Recommended Posts

Hello, Im trying to texture map an object with a jpeg image. The image loads correctly but when I display the object the image is distorted (i.e. displaying colors that are not part of the image) can someone tell me what is going wrong with this code??
bool CreateTexture(UINT* Array, const char* FileName, int ID)
{
	Fl_JPEG_Image* pJpegImage = NULL;
	
	if(!FileName)
		return false;

	string TextureName(FileName);  // create a string from the filename


	// if the user has selected that they want to create a bolt then a bolt reference geometry has to be created
	if(TextureName.indexof(".JPG"))
	{
		pJpegImage = new Fl_JPEG_Image(FileName);
		if(pJpegImage->data() == NULL)
		{
			return false;	// the data for this image was bad
		}
	}

	if(pJpegImage != NULL)
	{
		glGenTextures(1, &Array[ID]);
//		glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

		glBindTexture(GL_TEXTURE_2D, Array[ID]);

		gluBuild2DMipmaps(GL_TEXTURE_2D, 4, pJpegImage->w(), pJpegImage->h(), GL_RGBA, GL_UNSIGNED_BYTE, pJpegImage->data());
	
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
	}

		pJpegImage->uncache();
	//	delete pJpegImage;
	//	pJpegImage = NULL;


	return true;
}


Share this post


Link to post
Share on other sites
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, pJpegImage->w(), pJpegImage->h(), GL_RGBA, GL_UNSIGNED_BYTE, pJpegImage->data());

I don't think this is right. JPGs only have RGB data = 3 components. Try something like:

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pJpegImage->w(), pJpegImage->h(), GL_RGB, GL_UNSIGNED_BYTE, pJpegImage->data());

Share this post


Link to post
Share on other sites
Are you sure your jpeg has 4 color channels (rgba)? Jpeg doesnt support alpha, really, so I'd be surprised. Try changing your call to gluBuild2DMipmaps to use '3' and 'GL_RGB'... that or make sure your image library is converting it to RGBA on load.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this