0
votes


I am writing a small OpenGL tile map renderer in C# with OpenTK. To render the tiles I first create a vertex array and then a vertex buffer.
At next I iterate over all tiles and dependent from the tile type I add two triangles to the vertex buffer with the corresponding vertices(texture coords, color, position).
To avoid texture expensive switching I use one big texture with all tile textures named texture atlas.
But I have a problem while working with a texture atlas.
The problem is that I get some artifacts at the edge of some tiles if I zooming out and moving the camera around.
The solutions I found on the internet were...

  1. ...to calculate texture coords with half pixel correction.
  2. ...to use a texture array

The first one I tried but it didn't worked how expected (More explained below).
The second one I don't know how to work with texture arrays and I couldn't find much about texture arrays on the internet.

I used the following texture filters:

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

Texture coords calculation with half pixel correction:

text_coord_x = (0.5f + pixel_x) / texture_width;
text_coord_y = (0.5f + pixel_x) / texture_height;

Texture coords calculation without half pixel correction:

text_coord_x = pixel_x / texture_width;
text_coord_y = pixel_y / texture_height;

One tile with half pixel correction:
One tile with half pixel correction

One tile without half pixel correction:
One tile without half pixel correction

Multiple tiles with half pixel correction:
Multiple tiles with half pixel correction

Multiple tiles without half pixel correction:
Multiple tiles without half pixel correction

1

1 Answers

1
votes

For solve your problem with border at atlas you should left N pixel border around each sprite. This border should contain pixel information depends of tiling what you need. This border may be generated by lot 3d programs.

For example Blender can bake several textures into one with margin options:

enter image description here

Also N pixels width depends from mip levels of future atlas. So if you will add one pixel border then you can to use only single mip level (zero mip level). For two - you can use 0 mip and 1 mip level etc. Make sure that your use mip levels not more them border width was.