PDA

View Full Version : OpenGL texture flickering when rendering only parts of texture



wrymnn
06-30-2015, 07:26 AM
Hi, I have a texture of 64x64 where there are 4 separate textures of 32x32 (For tilesets so I don`t have separate texture for every tile set)

Now, I know how to use UV etc. the problem is, when I cut it so every peace has it`s own uv rect from texture, I see flickering and lines etc. on screen.

Here is the example picture

When I enable mip mapping, I get texture blending between borders like here:

1904

When I disable mip mapping, I get flickering, strange lines across those textures etc.

1905

This are my texture parameters after texture binding:


glTextureParameteri(texture.id, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(texture.id, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTextureParameteri(texture.id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(texture.id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);

Please, could you give me some advice? My UV coordinates are mapped ok (0.0f, 0.0f, 0.5f, 0.5f) for example.

Thanks.

mhagain
06-30-2015, 09:44 AM
This is all normal, expected behaviour with this kind of setup.

You should use a texture array (https://www.opengl.org/wiki/Array_Texture) instead.

GClements
06-30-2015, 12:24 PM
Linear filtering, tiles and arbitrary scaling don't play well together. Mipmaps can sometimes be an issue, but shouldn't be relevant here unless the scale factor drops below 1/32.

Array textures will fix the problem, but they require OpenGL 3 (which may be considered excessive for a 2D tile map). You'll also need to use a wrap mode of GL_CLAMP_TO_EDGE instead of GL_REPEAT.

The problem with linear filtering is that it can result in blending together texels which belong to different tiles. There are ways to avoid the problem without resorting to array textures, but they're more work. They boil down to two basic options:

1. Prevent the blending from occurring by insetting the texture coordinates by half a texel, so that the texture is never sampled in the region where the texels which would be blended belong to different tiles. This requires either disabling mipmaps or adjusting the inset according to the mipmap level (and disabling blending between mipmaps, as different levels will no longer be aligned).

2. Ensure that the blending doesn't matter by laying out the tile maps so that there is no discontinuity at the boundary between tiles (which includes the wrap-around boundary if you're using GL_REPEAT). In practice, this may require some degree of redundancy in the tile map.

wrymnn
06-30-2015, 01:01 PM
Linear filtering, tiles and arbitrary scaling don't play well together. Mipmaps can sometimes be an issue, but shouldn't be relevant here unless the scale factor drops below 1/32.

Array textures will fix the problem, but they require OpenGL 3 (which may be considered excessive for a 2D tile map). You'll also need to use a wrap mode of GL_CLAMP_TO_EDGE instead of GL_REPEAT.

The problem with linear filtering is that it can result in blending together texels which belong to different tiles. There are ways to avoid the problem without resorting to array textures, but they're more work. They boil down to two basic options:

1. Prevent the blending from occurring by insetting the texture coordinates by half a texel, so that the texture is never sampled in the region where the texels which would be blended belong to different tiles. This requires either disabling mipmaps or adjusting the inset according to the mipmap level (and disabling blending between mipmaps, as different levels will no longer be aligned).

2. Ensure that the blending doesn't matter by laying out the tile maps so that there is no discontinuity at the boundary between tiles (which includes the wrap-around boundary if you're using GL_REPEAT). In practice, this may require some degree of redundancy in the tile map.

Thanks, I will try changing a code a bit or making my UV coordinates not go for example (0, 0, 0.5, 0.5), but (0, 0, 0.5 - 0.05, 0.5 - 0.05).
Also those array textures seem like a bit of work, also for restructuring the code a bit....