2

I will like to generate a texture atlas from some textures that are already loaded in memory.

The textures I will like to include in the atlas are generated procedurally on runtime so I can't just create a png tilemap that already contains them.

Is this possible to do with OpenGL?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ellipticaldoor
  • 1,122
  • 11
  • 24
  • Yes, it is possible. – Hihikomori Dec 20 '20 at 22:46
  • So I've been thinking that one thing I can do is to merge in some way the data that I pass to glTexImage2D instead of actually creating the textures to then merge them into an atlas. Do you know how I can do that? – ellipticaldoor Dec 20 '20 at 22:52
  • You can pass several textures to fragment shader, and make merging there. – Hihikomori Dec 20 '20 at 22:56
  • I have more than 100 tiny textures, the idea is to have a big texture that can easily access on one draw call – ellipticaldoor Dec 20 '20 at 22:57
  • @ellipticaldoor Create a texture which is large enough with [`glTexImage2D`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml) and use [`glTexSubImage2D`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexSubImage2D.xhtml) to put the "100 tiny textures" in it. – Rabbid76 Dec 20 '20 at 22:59
  • glTexSubImage2D is valid only if "100 tiny textures" are rectangles. – Hihikomori Dec 20 '20 at 23:03
  • 1
    @МатвейВислоух Ah, yes, of course. The contributor is taking about a texture atlas. Very likely we are taking about square textures. – Rabbid76 Dec 20 '20 at 23:07
  • I mean if texture peaces has complicated geometry and alpha channel, and if this peaces must be rotated ... – Hihikomori Dec 20 '20 at 23:09
  • 1
    yes they are square, thank you @Rabbid76 glTexSubImage2D its what I was looking for – ellipticaldoor Dec 20 '20 at 23:09

1 Answers1

1

Create a texture big enough with glTexImage2D and use glTexSubImage2D to put the 100 tiny square textures into it. glTexSubImage2D allows the specification of the x and y offsets in the target texture and the size (width and height) of a tile.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Seems like it works but all the tiles in the tile map look like the last one. It seems like the last glTexSubImage2D call overrides all the previous subimages with the pixels from the last call. `glTexSubImage2D(GL_TEXTURE_2D,0,tileWidth * i,0,tileWidth,tileHeight,GL_RGBA,GL_UNSIGNED_BYTE,textures[i].pixels);` – ellipticaldoor Dec 21 '20 at 00:42
  • @ellipticaldoor No it does not. Very likely this is caused by a bug in your program. – Rabbid76 Dec 21 '20 at 07:26