2
votes

I'm trying to use rectangular texture with OpenGL. When the height is equal to the width of the texture, everything looks fine, however, when the height is different than the width the texture looks distorted.

My display function is (h and w are globals storing the height and the width of the image):

Please note that the size of the drawn image doesn't matter. It is distorted regardless of the actual polygon size.

void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glEnable(GL_TEXTURE_2D);
   glEnable(GL_BLEND);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   glBindTexture(GL_TEXTURE_2D, texName);
   glTranslatef(-2.0f,-2.0f,0.0f);
   glScalef(1.0f/128.0f,1.0f/128.0f,1.0f);
   glBegin(GL_QUADS);
   glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
   glTexCoord2f(0.0, 1.0); glVertex3f(0.0, w, 0.0);
   glTexCoord2f(1.0, 1.0); glVertex3f(h, w, 0.0);
   glTexCoord2f(1.0, 0.0); glVertex3f(h, 0.0, 0.0);

   // Will be distorted also with the following:
   /*glScalef(1.0f/128.0f,1.0f/128.0f,1.0f);
   glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
   glTexCoord2f(0.0, 1.0); glVertex3f(0.0, h, 0.0);
   glTexCoord2f(1.0, 1.0); glVertex3f(w, h, 0.0);
   glTexCoord2f(1.0, 0.0); glVertex3f(w, 0.0, 0.0);*/

   glEnd();
   glFlush();
   glDisable(GL_TEXTURE_2D);
}

I'm loading the texture with:

void *data = LoadBMP("c:\\dev\\64x128_face.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
               GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
               GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w,
            h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
            data);

When I'm loading a 64x64 square texture image, it looks fine. However when I'm loading a rectangular texture image, the image looks distorted.

How does OpenGL support rectangular POT texture? What's wrong with my code?

2

2 Answers

0
votes

Your rect image is 64x128 but you render it with these commands:

   glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
   glTexCoord2f(0.0, 1.0); glVertex3f(0.0, w, 0.0);
   glTexCoord2f(1.0, 1.0); glVertex3f(h, w, 0.0);
   glTexCoord2f(1.0, 0.0); glVertex3f(h, 0.0, 0.0);

where h is height (=128) and w (=64) is width. But you placed height on x (which is width) and width on y (which is height). Maybe try this instead:

   glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
   glTexCoord2f(0.0, 1.0); glVertex3f(0.0, h, 0.0);
   glTexCoord2f(1.0, 1.0); glVertex3f(w, h, 0.0);
   glTexCoord2f(1.0, 0.0); glVertex3f(w, 0.0, 0.0);
0
votes

You should probably check the support of the GL_ARB_texture_non_power_of_two extension before you use non-power-of-two values for h and w in glTexImage2D(), because specifying arbitrary heights and widths is only valid with this extension.