Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Texture doesn't stretch properly. Why is this happening?

I'm using LWJGL and Slick framework to load Textures to my OpenGL-application.

I'm using this image: Japanese flag, using for texture

And this code to import and utilize the texture:

    flagTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("japan.png"));

......

    flagTexture.bind();

    GL11.glColor3f(1.0f, 1.0f, 1.0f);
    GL11.glPushMatrix();

    GL11.glTranslatef(0.0f, 0.0f, -10.0f);

    GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0.0f, 0.0f);
    GL11.glVertex2f(0.0f, 0.0f);
    GL11.glTexCoord2f(1.0f, 0.0f);
    GL11.glVertex2f(2.5f, 0.0f);
    GL11.glTexCoord2f(1.0f, 1.0f);
    GL11.glVertex2f(2.5f, 2.5f);
    GL11.glTexCoord2f(0.0f, 1.0f);
    GL11.glVertex2f(0.0f, 2.5f);
    GL11.glEnd();

    GL11.glPopMatrix();

But the end-result becomes this: Texture not stretching to the vertex-parameters

I'm not using any special settings like GL_REPEAT or anything like that. Whats going on? How can I make the texture fill the given vertices?

like image 804
JulenissensHjelper Avatar asked Dec 27 '22 01:12

JulenissensHjelper


1 Answers

It looks like the texture is getting padded out to the nearest power of two. There are two solutions here:

  1. Stretch the texture out to the nearest power of two.
  2. Calculate the difference between your texture's size and the nearest power of two and change the texture coordinates from 1.0f to textureWidth/nearestPowerOfTwoWidth and textureHeight/nearestPowerOfTwoHeight.

There might also be some specific LWJGL method to allow for non-power-of-two textures, look into that.

like image 109
Robert Rouhani Avatar answered Jan 07 '23 14:01

Robert Rouhani