Search code examples
opengltexturesshadertexture-mappingtexture-atlas

Modern OpenGL tiling region in texture atlas (properly in shader)


I am using modern OpenGL to render a 3D scene with a texture atlas which holds all of my textures.

The texture coordinate calculation is done very simply and gets loaded into a VBO for the shader. Here is some basic calculation in pseudo code:

int posX = slot % texturesInAtlasX, posY = slot / texturesInAtlasX;
float startX = 1f / texturesInAtlasX * posX;
float startY = 1f / texturesInAtlasY * posY;

Normally I use this to calculate the 4 texture coordinates for a rectangle to apply a part of the atlas to it. But to the complexity of my scene forces me to simplify my meshes by calculating some faces with the same texture together into one. Normally this is not a big deal, because in OpenGL you can easily tile your texture by raising your coords above 1. But as I am using a texture atlas this can not easily be done by changing the way how the texture coords gets calculated.

My idea was to load the slot of the atlas which should be used and the size of the face to the shader and somehow tile the part of the atlas across the face. But I have absolutely no idea how to do that. As my objects are individually moving across the scene, loading the objects into one VAO per texture or batching the objects per textures together is not an option, because this absolutely kills the performance.

So my question is: Is there any feature in OpenGL which can help me tiling only parts of a texture?


Solution

  • Ok, after hours of researching I finally found a solution for my problem. The feature which I found is called "texture arrays" and is in core since OpenGL 3.3. So it's safe to use and has no big impact on performance. In my case it also gave me a big performance improvment. It was also really simple to implement. If anyone got some similar problem here is a link which explaine how texture arrays work: https://www.khronos.org/opengl/wiki/Array_Texture