Say we've idetified that we have a varying number of textures that are upper capped by 2 parametrs. thus we allocate the maximum amount of memory needed to store all textures as follows:

glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, max_width, max_height, texture_num);

One by one we allocate each texture to each layer as follows:

glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, file, width, height, 1, color_format,
            GL_UNSIGNED_BYTE, data);

Now since for some textures width < max_width and height < max_height, when sampling, our sample is off as trying to sample at a value of 1 would result in sampling outside of the bounds of the texture.

One could naively try to solve the issue by multiplying the sampling coordinate by the stretching coefficient.


However this only solves the problem for sampling that happens within the (-1,1) rnage. However, any texture that is meant to be tiled will be wrong. Thus we must do floating mod as well. the final calculation is thus:

u = (u*width/max_width)%(width/max_width)
v = (v*height/max_height)%(height/max_height)

(Each coordinate is differnece since the vertical stretch could be different from the horizontal stretch)

Thus to do our final rendering we would need to create and store an array of vec2's where the first value is the horizontal stretching coefficient and the second value is the vertical stretching coefficient on teh CPU. Then load that into a uniform buffer or SSBO.

And read these values on the shader side.

This seems a little bit cumbersome. Is there a way to instruct OpenGL to handle these varying sizes or is the only way to solve this issue the one I described?

  • 1
    $\begingroup$ Yes, this is cumbersome. But it has already been explained in the answers to this question that there is no way for OpenGL to directly handle those varying sizes in a texture array. This of course assumes that you need a texture array to begin with and aren't just using it for storage convenience. Otherwise texture views might offer some flexibility in reinterpreting an existing array layer. But in that case you of course can't have array texturing and would just be stuck with individual textures again. $\endgroup$ Commented Jul 10, 2018 at 9:54
  • 1
    $\begingroup$ Possible duplicate of How do you load multiple textures into an array texture with OpenGL? $\endgroup$ Commented Jul 10, 2018 at 22:46


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.