SFML community forums

General => General discussions => Topic started by: petej on August 19, 2024, 10:03:52 am

Title: Texture atlas tiling
Post by: petej on August 19, 2024, 10:03:52 am
I am trying to draw a 2D terrain.  I have defined a heightmap, created a VertexArray of type Triangles, and populated it with the vertices of my heightmap.  Specifying vertex colors works great, but I am now trying to use textures instead.

I have a texture atlas (e.g. see attached) that contains sub-textures for the different areas (e.g. grass, rock, etc.).  I need to have those sub-textures tile across the triangles because some triangles are bigger than the sub-texture and most do not align with a texture boundary.  Note that I am using an irregular grid (think something derived from a voronoi diagram) for the heightmap, so using grid-aligned quads is not possible.

When I set the texture to repeat, and specify texture coordinates, it tiles the whole texture atlas, not just a single texture.  I can make this work (e.g. see attached) by breaking the atlas into individual textures, and creating one VertexArray per area (e.g. grass, rock, etc.).  However, this means I must use a single texture across an area, with no ability to vary the texture, unless I create more individual textures and VertexArrays.

Is there another way to accomplish tiling of a sub-texture in an atlas?  Does the question make sense?
Title: Re: Texture atlas tiling
Post by: eXpl0it3r on August 19, 2024, 04:09:10 pm
I don't think it's possible to auto-tile (i.e. repeat) just part of a texture.
Title: Re: Texture atlas tiling
Post by: petej on August 19, 2024, 09:42:03 pm
Thanks for the reply!  A little broader research suggests this is possible with OpenGL shaders, so maybe I can do this by using the Shader class and binding a Texture to it.

https://community.khronos.org/t/repeat-tile-from-texture-atlas/104500
Title: Re: Texture atlas tiling
Post by: eXpl0it3r on August 19, 2024, 10:22:00 pm
Yes, this may be possible indeed.
Title: Re: Texture atlas tiling
Post by: kojack on August 20, 2024, 10:29:04 am
Yep, shaders can let you repeat a section of a texture (doing a bit of modulus and stuff with the UV coordinates).
One thing to be careful of though is a sudden jump in UV (like going off one side of the tile and back on the other side) will trip up mip mapping and it will do an edge of 1x1 mipmap, so a solid colour. You need to manually control the mipmapping to avoid it.