Texture error

Hello everyone,

I got an error on displaying texture on a quad in OpenGL.
When I place the fist texture it will work (256x256).
But when I place the second texture (201x81) I see a wrong result on the quad.

Both times I use the code shown below.

You can see the textures and results as attachments.

Thanks in advance,

Dennis

Code used both times:

	GLuint texId;
	glGenTextures(1, &texId);
	glBindTexture(GL_TEXTURE_2D, texId);

	glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);

	glEnable (GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texId);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin (GL_QUADS);
	glTexCoord2f (0.0, 1.0);
	glVertex3f (-4.0, 3.0, 0.0);

	glTexCoord2f (1.0, 1.0);
	glVertex3f (4.0, 3.0, 0.0);

	glTexCoord2f (1.0, 0.0);
	glVertex3f (4.0, -3.0, 0.0);

	glTexCoord2f (0.0, 0.0);
	glVertex3f (-4.0, -3.0, 0.0);
	glEnd ();

When you use the TEXTURE_2D target, the size must be a power of two. Otherwise, can use the TEXTURE_RECTANGLE target for that texture or resize it to fit in a power of two texture.

That’s was in old OpenGL.

In GL 2.0 and above, you can create any texture and in any size.

Could it be the pixel unpack setting? I think the default is 4 bytes alignment try to change it to 1. So try that


 glPixelStorei(GL_UNPACK_ALIGNMENT,1)
...

Now I use the glPixelStorei function the image looks a bit better, but it still does not fit in the corners.

(See in attachment)

Can you try with GL_NEAREST for both texture min and mag filter. Also maybe your second texture is in RGBA format.

Using GL_NEAREST I’ve already tried and also doesn’t work.
The origional image file is an bmp file (I only use PNG for uploading), and bmp can’t be RGBA.

But I made sure it isn’t RGBA and checked the pixels array.

That could not be the problem.

Can you attach the source code of your small program to see what happen?

Errrr… I think I already know what the problem is…

The problem was largely in the loading of the bitmap…
(Who I got from an tutorial from the internet)

When I use the code shown below as pixels it will work.

Luckily the other error was hindsight for a part of the “glPixelStorei” function.

So… all thanks for help!

Working code for pixels (c = pixels):

	char* red = new char[3]; red[0]=255; red[1]=0; red[2]=0;
	char* green = new char[3]; green[0]=0; green[1]=255; green[2]=0;
	char* blue = new char[3]; blue[0]=0; blue[1]=0; blue[2]=255;
	char* yellow = new char[3]; yellow[0]=255; yellow[1]=255; yellow[2]=0;
	char* orange = new char[3]; orange[0]=255; orange[1]=128; orange[2]=0;
	char* purple = new char[3]; purple[0]=128; purple[1]=0; purple[2]=128;
	char* c = new char[6*3*3];

	c[0]=orange[0];		c[1]=orange[1];		c[2]=orange[2];
	c[3]=purple[0];		c[4]=purple[1];		c[5]=purple[2];
	c[6]=red[0];		c[7]=red[1];		c[8]=red[2];
	c[9]=green[0];		c[10]=green[1];		c[11]=green[2];
	c[12]=blue[0];		c[13]=blue[1];		c[14]=blue[2];
	c[15]=yellow[0];	c[16]=yellow[1];	c[17]=yellow[2];
	c[18]=purple[0];	c[19]=purple[1];	c[20]=purple[2];
	c[21]=red[0];		c[22]=red[1];		c[23]=red[2];
	c[24]=green[0];		c[25]=green[1];		c[26]=green[2];
	c[27]=blue[0];		c[28]=blue[1];		c[29]=blue[2];
	c[30]=yellow[0];	c[31]=yellow[1];	c[32]=yellow[2];
	c[33]=orange[0];	c[34]=orange[1];	c[35]=orange[2];
	c[36]=red[0];		c[37]=red[1];		c[38]=red[2];
	c[39]=green[0];		c[40]=green[1];		c[41]=green[2];
	c[42]=blue[0];		c[43]=blue[1];		c[44]=blue[2];
	c[45]=yellow[0];	c[46]=yellow[1];	c[47]=yellow[2];
	c[48]=orange[0];	c[49]=orange[1];	c[50]=orange[2];
	c[51]=purple[0];	c[52]=purple[1];	c[53]=purple[2];

	delete red, green, blue, yellow, orange, purple;