2x2 texture gets repeated

I’m using orthographic projection that maps units to pixels 1:1. So if my window is WxH, it will display primitives in 0…W, 0…H with origin in left top corner. This is basically a 2D application. App draws a textured quad of almost full window size (300x300). And it is textured with 2x2 texture. I left default texture wrap modes for both S and T coordinates (GL_REPEAT) and used GL_NEAREST for MIN/MAG filtering parameters. The problem is that I get bottom/right edges of the drawn quad showing colors from top/left edges. Texture coords are 0.0…1.0x0.0…1.0.

As far as I know quad is rasterized into fragments center coordinates of which are inside of the quad, and texture coordinates are assigned to them shouldn’t be <0.0 or >1.0, moreover there shouldn’t be even exact 0.0 and 1.0.

So how does it come to draw repeated? How do I make it draw it correct?

NOTE: I found that this happen only on large quad + small texture.

GPU is GMA950.

If you are stretching a quad over the entire screen, you probably want GL_CLAMP_TO_EDGE in the wrap modes.

Can you post a screen shot of the problem?

Test quad rendered:

On this image top left black part is 63x63, red is 64 rows x 63 columns, green is 63x64, yellow 64x64, and as you can see there are repeated lines of size 1.

I expect all color regions to be 64x64.

Whole quad has correct size 128x128.

setup 2D with origin in top-left corner:

    glViewport(0, 0, WWIDTH, WHEIGHT);
    glOrtho(0.0, WWIDTH, WHEIGHT, 0.0, -1.0, 1.0);

2x2 image data:

    unsigned char ttt_bytes[] =
        '\x00', '\x00', '\x00', '\xFF',
        '\x00', '\xFF', '\x00', '\xFF',
        '\xFF', '\x00', '\x00', '\xFF',
        '\xFF', '\xFF', '\x00', '\xFF'

setup texture:

    GLuint ttt;
    glGenTextures(1, &ttt);
    glBindTexture(GL_TEXTURE_2D, ttt);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, ttt_bytes);

draw the quad 128x128 at 20,20

  glTexCoord2f(0.0f, 0.0f); glVertex2f(20.0,   20.0f);
  glTexCoord2f(1.0f, 0.0f); glVertex2f(148.0f, 20.0f);
  glTexCoord2f(1.0f, 1.0f); glVertex2f(148.0f, 148.0f);
  glTexCoord2f(0.0f, 1.0f); glVertex2f(20.0f,  148.0f);

I tried both Fixed Function texturing (glEnable(GL_TEXTURE_2D)) and via simple GLSL fragment shader. Result is the same.

Use of CLAMP or CLAMP_TO_EDGE reduces repeated lines but yellow part becomes 65x65.

happen only on large quad + small texture

this looks like a common issue which you can see on other hardware as well , which is somewhat related to the limited precision of the texture addressing. it is not really a bug, although it gives unexpected behavior, and the real fix is to change the clamp mode.

Pierre B.

Even if I use GL_CLAMP or GL_CLAMP_TO_EDGE it is not rendered as I expect: I get non-equal regions of black, red, green, yellow. Naturally they should be all 64x64.

I also ran my test on different GPU – nVidia, it works fine there.

Seems like that is Intel GMA950/MacOSX specific bug.

This makes a problem since I’m trying to draw some accurate pixel data (text).

Do you have any suggestions/workarounds?

this behavior is ASIC dependent, and would not appear on any dx11 capable HW (precision for addressing unit is higher).

I found a workaround… To use Rectangular Textures (ARB_texture_rectangle).
Now I specify 0…Wx0…H texture coords and it renders equally sized areas for each cell in the texture.

This is not related to your problem, but you should consider using
GL_RGBA8 as the texture format in glTexImage2D.

Leaving it up to the driver to select the internal texture format has lead to people wondering why textures look different on different cards.(Notably, ATi selecting 16 bpp modes)

Hm, thanks, will keep that in mind.