# Handling varying texture sizes in an array texture

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.

uv*=width/max_width.


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.