LWJGL Forum

Programming => OpenGL => Topic started by: Dragbone on January 21, 2010, 15:46:17

Title: Texture drawing problem (strange borders) [solved, kind of]
Post by: Dragbone on January 21, 2010, 15:46:17
Heya,

I'm relativly new to OpenGL and finally i have a problem which i can't solve by simply using Google.

Whenever i draw a texture (i only use 2D) on a position which is not exact (meaning float-coordinates)  (or when rotating the whole thing) my texture gets some strange borders. I just can't figure out what's causing my problem. The Imagefiles are PNGs with alpha channel.

Here some code snippets i use:

Loading the textures:
tex = TextureLoader.getTexture(getImageExtension(fileName), new FileInputStream(fileName));
Drawing: ws/hs is the scaling, rot the rotation, oX/oY the origin, wt/ht the width and height of the texture (not the picture)
tex.bind();
GL11.glPushMatrix();
GL11.glScalef(ws, hs, 1);
GL11.glTranslatef((float)(x),(float)(y), 0f);
GL11.glRotatef(rot, 0, 0, 1.0f);
GL11.glBegin(GL11.GL_QUADS);
 GL11.glTexCoord2f(0.0f, 0.0f);
 GL11.glVertex2f(-oX, -oY);
 GL11.glTexCoord2f(1.0f, 0.0f);
 GL11.glVertex2f(wt-oX, -oY);
 GL11.glTexCoord2f(1.0f, 1.0f);
 GL11.glVertex2f(wt-oX, ht-oY);
 GL11.glTexCoord2f(0.0f, 1.0f);
 GL11.glVertex2f(-oX, ht-oY);
GL11.glEnd();

GL11.glPopMatrix();

Setting the whole thing up:
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0.0, Display.getDisplayMode().getWidth(),
Display.getDisplayMode().getHeight(), 0.0, -1.0, 1.0);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glViewport(0, 0, Display.getDisplayMode().getWidth(), Display
.getDisplayMode().getHeight());
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_ALPHA_TEST);

Here's a picture of the whole thing. Left one's correct, right one shows the problem.
(http://img20.imageshack.us/img20/596/textureproblem.th.png) (http://img20.imageshack.us/i/textureproblem.png/)

thanks for your time and help, best regards
Dragbone
Title: Re: Texture drawing problem (strange borders)
Post by: Fool Running on January 21, 2010, 18:22:17
It looks like you need to clamp your texture coordinates. It looks like what is happening is that OpenGL is wrapping your texture coordinates as they move past the edge of the texture image (i.e. after the end of a texture it will start at the beginning).
Try adding code like:
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

That should make OpenGL just reuse the end of the texture if it tries go past it.

Hope that helps ;D
Title: Re: Texture drawing problem (strange borders)
Post by: Dragbone on January 22, 2010, 17:50:09
Thanks for your fast answer,
it really looks like that's the problem. I coloured the left side of the image blue-ish and then run the game again, the strange lines were blue, so it really is a repeated part.
But your code didn't work or i probably put it at the wrong place ;-)
How do i have to use it? Is it texture specific so i need to set it for every texture and how do i do that (i don't see an argument specifying the texture)?

best regards
Dragbone

EDIT:
It's kinda strange, i just made the image a bit wider (on the right side) and now the line is gone. I think i can live with that :)
Title: Re: Texture drawing problem (strange borders)
Post by: Fool Running on January 25, 2010, 18:25:13
Quote from: Dragbone on January 22, 2010, 17:50:09
How do i have to use it? Is it texture specific so i need to set it for every texture and how do i do that (i don't see an argument specifying the texture)?
Yes, it has to be done for each texture you load (after the TextureLoader.getTexture() call). You might have to bind the texture before you call the code I posted (I'm not sure what TextureLoader does).
QuoteIt's kinda strange, i just made the image a bit wider (on the right side) and now the line is gone. I think i can live with that
That is strange. Is it possible that the texture is flipped (i.e. the right-hand side on the screen is the left hand side of the texture)?
Title: Re: Texture drawing problem (strange borders)
Post by: Dragbone on January 26, 2010, 11:04:38
No, the texture is oriented like it should be (except i turned the y-axis so 0/0 is in the top-left corner).
I think TextureLoader does something wrong when the image's size isn't a power of 2 (although i read it _should_ support it). So i'll just ajust all images and everything should be right.