Question

I am about to go mad because of texture mapping of openGL. It nevers maps the right thing, please have a look.

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, speed.texID);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

glBegin(GL_QUADS);
    glTexCoord2f(1,0); 
    glVertex2f(210, 638);   //Top-Left
    glTexCoord2f(1,1);
    glVertex2f(210, 438);   //Top-Right
    glTexCoord2f(0,1); 
    glVertex2f(10, 438);    //Bottom-Right
    glTexCoord2f(0,0);
    glVertex2f(10, 638);    //Bottom-Left
glEnd();
glDisable(GL_TEXTURE_2D);

After this code output image becomes this: enter image description here

But I want it to be like this: enter image description here

When I change the texture to something else, it maps it correctly but apparently not this one. Why do you think this could be?

Was it helpful?

Solution

If other textures work, chances are this texture is an odd size or different format, and has been loaded without observing some necessary padding.

It is common for texture formats to need to be an even number of pixels, or to be rounded to some other multiple.

The 45-degree slant to the texture is also indicative of this kind of issue, as it often results from an extra pixel being read (or skipped) every line at some point in the pipeline.

Possible fixes for this are:

  • Change the dimensions of your texture, such that there will be no padding.
  • Change the loading of your texture, such that the padding is consistent with what GL expects
  • Tell GL how your texture is packed, using glPixelStorei()

OTHER TIPS

If you are working with textures which cannot be aligned to 4 bytes you have to care about a few things, one is to set the correct pixel pack and unpack alignment:

glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

For more information see here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top