Search code examples
opengltextureslwjgltexture-atlas

Resizing an OpenGL texture without changing UV coordinates


I have a texture atlas that gets generated during gameplay. Textures are loaded and unloaded depending on which textures are visible when the player moves around.

The problem is when there are too many textures to fit in the atlas and I need to resize it, but if I simply resize the texture atlas by copying it to a larger texture, then I would need to re-render everything to update the UV coordinates of the quads.

Is it possible to somehow create an OpenGL texture and define its width and height in UV coordinates to be something other than 1?


Solution

  • One solution would be to use texel coordinates rather than normalized coordinates (assuming you are not going to scale or move the images around within the atlas when you make it bigger). If you can live with their limitations (no wrapping, no mipmaps, only linear filtering), you could just use Rectangle Textures and call it a day. Alternatively, you could simply apply scaling factors to the texture coordinates in your vertex shader, either by passing them as uniforms or directly querying the texture dimensions in the shader using the textureSize() GLSL function.

    Alternatively, you might consider using an Array Texture to hold multiple atlases. Instead of enlarging an existing atlas to make room for more subimages, you could just start a new atlas in another array slice. That way, the texture coordinates of all existing subimages would stay the same…