# How to properly repeat a texture on a quad in OpenGL?

I'm drawing a quad and binding a texture to this quad like this:

#define TERRAIN_WIDTH         2000
#define TERRAIN_LENGTH        2000
#define TERRAIN_WIDTH_2       (TERRAIN_WIDTH / 2)
#define TERRAIN_LENGTH_2      (TERRAIN_LENGTH / 2)

glBindTexture(GL_TEXTURE_2D, gameTextures.terrain);
glTexCoord2f(0, 0); glVertex3f(-TERRAIN_WIDTH_2, 0.0f,  TERRAIN_LENGTH_2);
glTexCoord2f(X, 0); glVertex3f( TERRAIN_WIDTH_2, 0.0f,  TERRAIN_LENGTH_2);
glTexCoord2f(X, X); glVertex3f( TERRAIN_WIDTH_2, 0.0f, -TERRAIN_LENGTH_2);
glTexCoord2f(0, X); glVertex3f(-TERRAIN_WIDTH_2, 0.0f, -TERRAIN_LENGTH_2);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);


Normally, X would be equal to 1 but this will stretch the texture because the quad is very large. The idea is to increase that value so the texture is repeated (the texture was generated with GL_REPEAT of course).

But how should I find the best value for X? I have a few 512x512 textures which look nice if I replace X=512. Then I have another 1024x1024 texture which looks good to with X=512 but doesn't start to look so good with a higher/lower value. Another texture is 2048x2048 and now I need to go down to X=256, otherwise it won't look so good.

What's confusing me even more is the fact that both 512x512 and 1024x1024 textures look good with X=512. The way I see it, the lower the texture resolution, the more times I need to repeat it.

How should I calculate the value of X then?

• If you need to calculate X so that it “looks good” I doubt there is a formula. Could you maybe provide some screenshots of the visual effects that disturb you? Also, remember that quads are deprecated in OpenGL and you should use triangles instead (together with vertex buffers). Finally, displaying several quads instead of a large one will help you organise your scene better. Commented Apr 7, 2011 at 18:06
• I can't post screenshots right now, but the problem is that the textures look distorted. Just pick any texture of any given size and stretch it. How does it look? Bad. That's the problem I'm having. What I want is to put the texture on the floor without stretching, no matter the size of the texture. Commented Apr 7, 2011 at 18:18
• Did you generate mipmaps for your texture? From what you appear to experience, X = 524288 / S where S is your texture size should work. Commented Apr 7, 2011 at 18:26
• That solves it, even for the 512x521 textures. I thought they looked good with X=512 (the same X for 1024 textures) but it actually looks much better with X=1024, which is the number that little formula gives for S=512. Everything makes sense now. You want to answer that below so I can mark the answer as accepted? About mipmaps, I haven't generated them yet, why? Commented Apr 7, 2011 at 19:38
• You may also retrieve the texture size using glGetTexLevelParameter(). Commented Apr 7, 2011 at 19:48

If 2048×2048 textures look good with X = 256, and 1024×1024 ones look good with X = 512, then it is probably that each time the texture size shrinks by a factor of 2, X should grow by a factor of 2, leading to the following formula:
X = 256 * 2048 / TextureSize