2

How to draw via GLSL Sampler only part of Texture2D Atlas stored inside Texture Array? For example i have Texture atlas, and i will put them together (with other atlases of same size) inside Texture2D Array. (glTexSubImage3D)

enter image description here

Well, how my sampler should looks like in that case?

https://www.opengl.org/wiki/Array_Texture

https://www.opengl.org/wiki/Sampler_(GLSL)

I've found only examples, how select & apply whole texture from Array, but nothing related if inside our Array we store Texture Atlas.

Happy Day
  • 297
  • 1
  • 5
  • 14

1 Answers1

2

Luckily this is the area in which I have been working recently, and to use a 2d texture array in the fragment shader you can do something like the following:

#version 330

uniform sampler2DArray tex;

flat in uint fragLayer;
in vec2 fragTexCoord;

out vec4 colour;

void main()
{
    colour = texture( tex, vec3( fragTexCoord, fragLayer ) );
}

So the texture layer is the 3rd element of a 3d vector used to access individual pixels in the texture array.

Also note that using the naive (and simplest) glTexSubImage3d straight on your raw image will only work if all your tiles are in a vertical line, not a grid.

Nonanon
  • 560
  • 3
  • 10
  • 1
    I got texture atlases to work with array textures, but they have to be in a vertical line instead of a grid like you said. How can it be done with a grid? Can it still be done with one array texture and one pixel array pointer? I'm guessing you can just mess with the glTexSubImage3D offset parameters? – Willy Goat Jul 19 '17 at 10:20
  • The reason you cannot use a grid is because OpenGL is not meant to be a comprehensive utility library, and so wants the pixels for each image to be fully contiguous. What can be done however, is a memory operation to turn the grid into a vertical line, then passing that into the atlas (at least that's what I do). – Nonanon Jul 23 '17 at 04:07