views:

126

answers:

2

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?

A: 

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);
Rock
@Rock, nope, it is distorted never the less. Added clarification to the question. (note you have to scale the image down a bit. Otherwise you'll only see a single big texel).
Elazar Leibovich
Could you post/upload an image of the distortion? Maybe it would be easier, if I could see how it is distorted. Or describe the distortion verbally, is it stretched, or squeezed?
Rock
@Rock, did you try out my code and it came OK to you? What I'm seeing is a horizontal stripe of the leftmost part of the image followed by another horizontal stripe of the rightmost part of the image.
Elazar Leibovich
I haven't had the time to test your code deeply, but I will give it a shot tomorrow.Curious: you load your texture as RGBA from a bmp. Did you save your 64x128.bmp with an alpha channel too? If not, this would cause a color distortion as well as diagonal (but not horizontal :-/) stripe-effect. And: what GPU do you use for rendering? I once had a problem with an old ATI-Radeon which would only load square 2^n-textures correctly. Maybe try your code out on a different machine?
Rock
A: 

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.

mbaitoff
@mbaitoff, nope. it IS a POT rectangle, but it is a rectangle and not a square.
Elazar Leibovich
what do you mean by 'distorted'? is it 'unproperly scaled in one of the directions' or 'garbage pixels', or 'skewed texture'? this will help much understanding the issue.
mbaitoff