texturescoordinate-transformationtexturepacker

Texture atlas UV coordinate conversion


I have got some UV coordinates that go from (0, 0) to (16, 16) for my textures. Now I have packed this texture in a texture atlas with the size dim (w, h) at the position rec (x, y, w, h). I need to convert the UV coordinates so that I can address them in the atlas, but my brain has exploded recently and I can't figure it out myself.

How to convert the coordinates?

(The idea is to transform the How into a texture matrix, so a matrix also counts as answer)


Solution

  • I finally found it myself:

    translate(rec.x, rec.y)
    scale(1 / dim.w, 1 / dim.h)
    scale(rec.w / 16, rec.h / 16)
    
    if (invertY)
      scale(1, -1)
      translate(0, dim.h)
    

    Note: