Texture mapping problem

Hi,

I am trying to map a simple texture onto a quad, its all loaded in fine but it displays at a dogi angle, which im pretty sure is because i have done the glTexCoord2f() coordinates incorrectly.

Heres my code:

glBegin(GL_QUADS);
glTexCoord2f(0.0,1.0); glVertex2f(-100, 100); // top left
glTexCoord2f(1.0,1.0); glVertex2f(100, 100); // top right
glTexCoord2f(1.0,0.0); glVertex2f(100, -100); // bottom right
glTexCoord2f(0.0,0.0); glVertex2f(-100, -100); // bottom left
glEnd();

This is how it looks (the rendered quad on the left, and the actual texture on the right):

Any ideas on what im doing wrong?

Thanks,
Jason

Looks like your texture data is not aligned correctly, or you’re not passing the right width to glTexImage2D.

How do you create/load the texture data and pass it to GL?

Thanks for the reply, im using NeHes bitmap loader, specifically this code:


int LoadGLTextures()									// Load Bitmaps And Convert To Textures
{
	int Status=FALSE;									// Status Indicator

	AUX_RGBImageRec *TextureImage[1];					// Create Storage Space For The Texture

	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL

	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
	if (TextureImage[0]=LoadBMP("C:\\Users\\Jack\\Desktop\\SpaceInvader\\Data\\invader_texture.bmp"))
	{
		Status=TRUE;									// Set The Status To TRUE

		glGenTextures(1, &texture[0]);					// Create The Texture

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                     GL_REPEAT);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                     GL_REPEAT);
	}

	if (TextureImage[0])									// If Texture Exists
	{
		if (TextureImage[0]->data)							// If Texture Image Exists
		{
			free(TextureImage[0]->data);					// Free The Texture Image Memory
		}

		free(TextureImage[0]);								// Free The Image Structure
	}

	return Status;										// Return The Status
}

The code detects the width and height of the texture correctly when glTexImage2D() is called so i dont think thats the problem?

I simply use the LoadGLTextures() function, then enable and bind the texture.

Any ideas?

Have you set the pixel unpack alignment correctly?
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

That one caused me a lot of trouble.

Yep got that, makes no difference :frowning:

The code detects the width and height of the texture correctly when glTexImage2D() is called so i dont think thats the problem?

You are really sure of that ? Maybe the width and height are just reversed ? Or off by 1, or something ?

NeHes loader detects the width and height from the bitmap dynaically:

i.e:
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY,

My debugger indicates these the sizes are defiantly correct.

Really strange, i cant think of anything else that could do it?

You upload RGB data (3 component), but maybe the BMP-loader generates RGBA (4 component) data.

I would say, either the BMP-loader does not work as it should, or it does not work as you expect it to do.

If you don’t find the problem, post the BMP-loader code, it is most likely the reason for your problem.

Jan.

:wink:

glTexImage2D seems to read one pixel too much per line, as each line is shifted one more pixel to the left (from bottom to top) and the right half of the topmost line is filled with random pixels.

Could you try to TextureImage[0]->sizeX - 1 as width?

Hi,

Thanks again for the help, much appreciated.

This is the bitmap loading code i am using:


AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image
{
	FILE *File=NULL;									// File Handle

	if (!Filename)										// Make Sure A Filename Was Given
	{
		return NULL;									// If Not Return NULL
	}

	File=fopen(Filename,"r");							// Check To See If The File Exists

	if (File)											// Does The File Exist?
	{
		fclose(File);									// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
	}

	return NULL;										// If Load Failed Return NULL
}

The returned data is then used by the LoadGLTextures() function i posted above.

:wink:

glTexImage2D seems to read one pixel too much per line, as each line is shifted one more pixel to the left (from bottom to top) and the right half of the topmost line is filled with random pixels.

Could you try to TextureImage[0]->sizeX - 1 as width?[/QUOTE]

Thanks, tried this and it resulted in this:
:frowning:

Yup, you’re off by one pixel.

I’ve seen some nasty padding/alignment nonsense in BMP loaders.

Make sure the BMP is 2^n wide, or at least that is not an odd number of pixels wide. You may have found a bug in that aux lib code. It’s a deprecated library now although tutorials like NeHe still use it.

Hmm, given your last post it might be a driver issue although you’re definitely relying on non power of two textures now and I doubt you’ve tested for the extension.

What is sizeX and sizeY?

Used the quad that quadWidth/texImageWidth=quadHeight/texImageHeight again.if no effect,how do you set the viewport?

Thanks very much for the help guys, turns out i made a stupid mistake and the bitmaps width/height were not powers of two :sorrow: , which caused the pixel mess up, Oh well i wont be making that mistake again!

Jason

bmp files are typically (always?) aligned on 4 bytes per row, which means 24bit images often have extra bytes at the end of each row. With POT textures this will not be a problem unless it’s smaller than 4 pixels wide.