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.

enter image description here


glTexCoord2f(0, 0);
glVertex2i(10, 10);
glTexCoord2f(1, 0);
glVertex2i(500, 10);
glTexCoord2f(1, 1);
glVertex2i(500, 500);
glTexCoord2f(0, 1);
glVertex2i(10, 500);

What could be the cause?

  • \$\begingroup\$ It seems the texture is not stretching properly, the rectangle shape is actually stretching to the artifacts. \$\endgroup\$ – DLabinac Aug 28 '12 at 2:11
  • \$\begingroup\$ It seems to work fine with lo res images(like 64x64), but anything bigger and it breaks... \$\endgroup\$ – DLabinac Aug 28 '12 at 3:47
  • \$\begingroup\$ Is your texture power of 2? \$\endgroup\$ – Yevhen Aug 28 '12 at 15:29
  • \$\begingroup\$ 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. \$\endgroup\$ – DLabinac Aug 28 '12 at 17:44
  • \$\begingroup\$ You can download the source with the bug fixed at: betaic.com/javalwjgltemplate.zip \$\endgroup\$ – 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.

  • \$\begingroup\$ 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! \$\endgroup\$ – DLabinac Aug 29 '12 at 1:49
  • \$\begingroup\$ @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. \$\endgroup\$ – Kevin Reid Aug 29 '12 at 1:54
  • \$\begingroup\$ 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(); } \$\endgroup\$ – DLabinac Aug 29 '12 at 1:59
  • \$\begingroup\$ @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. \$\endgroup\$ – Kevin Reid Aug 29 '12 at 2:44
  • \$\begingroup\$ Stretching the texture fixes the issue too, so for now it all works with the code that i posted. \$\endgroup\$ – 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:

  • \$\begingroup\$ I tried to paste it every step of the lwjgl initialization code, but no effect. \$\endgroup\$ – DLabinac Aug 28 '12 at 5:36
  • \$\begingroup\$ Try putting it directly before the draw code. \$\endgroup\$ – MichaelHouse Aug 28 '12 at 14:24
  • \$\begingroup\$ 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... \$\endgroup\$ – DLabinac Aug 28 '12 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.