I managed to get working LWJGL in Java, and I loaded a test image (tv test card), but I keep getting weird artifacts outside the image.

Code:

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(10, 10);
glTexCoord2f(1, 0);
glVertex2i(500, 10);
glTexCoord2f(1, 1);
glVertex2i(500, 500);
glTexCoord2f(0, 1);
glVertex2i(10, 500);
glEnd();


What could be the cause?

• It seems the texture is not stretching properly, the rectangle shape is actually stretching to the artifacts. – DLabinac Aug 28 '12 at 2:11
• It seems to work fine with lo res images(like 64x64), but anything bigger and it breaks... – DLabinac Aug 28 '12 at 3:47
• Is your texture power of 2? – Yevhen Aug 28 '12 at 15:29
• No, so i tried an 1024x1024 image, and it works... But my screen is 4:3 and i want this to be the background image. Anyway i watched the tutorials, and it mentioned that the image size must be even, not power of 2. – DLabinac Aug 28 '12 at 17:44
• You can download the source with the bug fixed at: betaic.com/javalwjgltemplate.zip – DLabinac Aug 29 '12 at 4:27

Unless you explicitly set up non-power-of-two (often imprecisely called "rectangle") textures using an extension, each dimension of your texture must be a power of two.

To draw an image with a different size, add margins so that it has power-of-two sizes, then adjust your texture coordinates (‛glTexCoord‛) to crop off the margins.

• It seems that when the size is not a power of two, it automatically adds blank space to the texture to fit into a power of two size. I calculated the bind coordinates, and not it binds perfectly, and the entire background is the texture image. Thanks! – DLabinac Aug 29 '12 at 1:49
• @DajgoroLabinac OpenGL has no such feature built in. If you're using a library to load image files/resources, it may be doing that for you. If you are in fact using a direct glTexImage call and it's working anyway, then you probably should not rely on that working on other hardware/drivers. – Kevin Reid Aug 29 '12 at 1:54
• Here is the code that works: private void MapQuad43(int px,int py,int sx,int sy) { glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2i(px, py); glTexCoord2f(0.75f, 0); glVertex2i(px+sx, py); glTexCoord2f(0.75f, 0.5625f); glVertex2i(px+sx, py+sy); glTexCoord2f(0, 0.5625f); glVertex2i(px, py+sy); glEnd(); } – DLabinac Aug 29 '12 at 1:59
• @DajgoroLabinac That code is not relevant, I am talking about the part that loads the texture. You need to make sure that it either is loading a power-of-two texture or that you are using a library which takes care of the padding for you. – Kevin Reid Aug 29 '12 at 2:44
• Stretching the texture fixes the issue too, so for now it all works with the code that i posted. – DLabinac Aug 29 '12 at 4:29

Try setting your textures to clamp to the size of your quad and ensure that you're not setting the textures to linear interpolation. When initializing your OpenGL code, do something like this:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

• I tried to paste it every step of the lwjgl initialization code, but no effect. – DLabinac Aug 28 '12 at 5:36
• Try putting it directly before the draw code. – MichaelHouse Aug 28 '12 at 14:24
• No effect. I tried pasting it in the render loop to... It accepts only images with sizes that is the power of two, but that is not helping, since i want other sizes too... – DLabinac Aug 28 '12 at 17:46