UltraBoodog
UltraBoodog

Reputation: 35

LWJGL random texture distortion

Lately I've been having a very odd issue with the rendering of LWJGL textures. Sometimes when the player is moving around I get an error like this (on grass tiles):

enter image description here

Other times, when I am moving, or when I am just standing still it is normal like this:

enter image description here

Here is my texture rendering code:

public static void drawQuad(Texture texture, float x, float y, float width, float height)
{
    texture.bind();
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    GL11.glTranslatef(x, y, 0);
    GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);
    GL11.glVertex2f(0, 0);
    GL11.glTexCoord2f(1, 0);
    GL11.glVertex2f(width, 0);
    GL11.glTexCoord2f(1, 1);
    GL11.glVertex2f(width, height);
    GL11.glTexCoord2f(0, 1);
    GL11.glVertex2f(0, height);
    GL11.glEnd();
    GL11.glLoadIdentity();
}

All of the tiles are 64x64 (so it is ^2). I am completely baffled.

Upvotes: 2

Views: 213

Answers (1)

Arttu Peltonen
Arttu Peltonen

Reputation: 1260

Looks like the texture coordinates are wrapping around (using GL_REPEAT wrap mode) so it samples the top (green color) if the texture coordinates go slightly over from the bottom edge.

Try adding this:

GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

Upvotes: 1

Related Questions