1
\$\begingroup\$

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

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?

\$\endgroup\$
5
  • \$\begingroup\$ It seems the texture is not stretching properly, the rectangle shape is actually stretching to the artifacts. \$\endgroup\$
    – DLabinac
    Aug 28, 2012 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, 2012 at 3:47
  • \$\begingroup\$ Is your texture power of 2? \$\endgroup\$
    – Yevhen
    Aug 28, 2012 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, 2012 at 17:44
  • \$\begingroup\$ You can download the source with the bug fixed at: betaic.com/javalwjgltemplate.zip \$\endgroup\$
    – DLabinac
    Aug 29, 2012 at 4:27

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\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, 2012 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, 2012 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, 2012 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, 2012 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, 2012 at 4:29
1
\$\begingroup\$

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .