# Problem with drawing textures in OpenGL ES

I'm developing a 2D game for Android and i'm using the framework which has been told in the book which named Beginning Android Games by Mario Zechner.So my framework is well designed and using OpenGL 1.1.It's similar to libgdx.

When i put my textures adjacent each other in my 2d surface,there are some spaces size as 1 px.But this problem only occur on my tablet.There aren't a problem like this on my phone.It's like in this picture:

What can be the problem?I can't fix it from one week.

## 2 Answers

It's hard to say what the problem is for certain. However, there are some particular things that can cause trouble at edges of tiles:

• If you are using a texture atlas (that is, graphics for multiple tiles stored in a single texture), then adjacent texture tiles may “leak through” borders due to numerical error; but more to the point, they will show through if you are using GL_LINEAR magnification (which it appears you are), because that essentially “blurs” the image by half a texel width. The sign of this problem is that the edge shows colors of the tiles which are adjacent in the texture to the tiles which are being drawn.

In order to avoid this, you must either inset your texture coordinates by half a texel width (thus avoiding the region of blending) or enlarge the tile in the texture by one pixel (thus blending to appropriate colors); these will give slightly different visual results due to the texel centers being on or off the edge of the tile.

• If you have GL_POLYGON_SMOOTH enabled, turn it off; that mode is not good for abutting objects. If you need antialiasing, use multisampling instead. The sign of this type of problem is that the line will have some amount of the color of the graphics behind it. (This may not even be available in OpenGL ES; I don't know.)

• Make sure you are computing the positions of your tiles in a numerically robust fashion. OpenGL only guarantees edges will line up if the vertex positions are identical. A simple way to ensure this is to use only integer-valued coordinates (i.e. the corner coordinate and the index of a tile are the same) when specifying the geometry of the tiles, and use the matrices to scale as needed.

• I think second and third is not my problem.Maybe i have a problem with your first suggestion.Here is my framework.Have you a suggestion for that?code.google.com/p/beginning-android-games/source/browse/… – droidmachine Jun 4 '12 at 18:22
• I took a look at the framework and it defines TextureRegions which are arbitrary sections of the texture. It is up to your game to specify texture regions which have enough margin in the texture to avoid this problem. – Kevin Reid Jun 4 '12 at 19:53
• So you mean if my texture's size is 500x122 then i set my TextureRegion 501x122,my problem will be solved.But with this solution my textures have 1px space on my phone also.So my problem was on my tablet but now it's also on my phone. – droidmachine Jun 4 '12 at 20:09
• If you're using a region which is the entire size of the texture, then you are not using a texture atlas, by definition! In that case, bobenko's suggestion of using GL_REPEAT is correct; if that doesn't help then you are having some other problem which I haven't thought of the possibility of. I suggest you get some more interactive help. – Kevin Reid Jun 4 '12 at 21:08
• In this framework,using a TextureRegion is essential.I used clamptoedge and 499px instead of 500px and my problem solved now.But this framework is coded by a professional game programmer and i can't understand how i came across with this problem.Is this a general problem? – droidmachine Jun 4 '12 at 21:45

possibly you can get better results by setting smth like: GL_TEXTURE_WRAP_S/T to GL_REPEAT rather than GL_CLAMP_TO_EDGE

Edited: is your texture is power of two?

• Nothing changed. – droidmachine Jun 4 '12 at 20:12
• Yes my texture is power of two.Before it's not power of two and there isn't any problem like that.Then i have to change texture size to power of two.So it changed to 512x512 px with its spaces from 500x122.Without power of two it's not working on a lot of device. – droidmachine Jun 5 '12 at 15:51