Happy Day Happy Day - 10 months ago 63
C++ Question

Draw Texture2D atlas inside Texture Array

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?



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

Answer Source

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.