I am creating a game using OpenGL that has many textured cubes. I am trying to use an Array Texture to load multiple textures from one file. I tried to setup my texture atlas; however, all that renders is a black texture. I'm not sure what exactly I am doing wrong.
Here is the texture loading code:
// Load image
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
// Generate texture object
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D_ARRAY, textureId);
// Actually load texture
if (data) {
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, width, height, 0, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
//glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
} else {
std::cout << "Failed to load " << path << " texture" << std::endl;
}
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Free memory
stbi_image_free(data);
Here is my fragment shader
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2DArray my_sampler;
void main()
{
FragColor = texture(my_sampler, vec3(0, 0, 1)) * vec4(1.0f);
}
Here is my texture file:
Does anyone know how I can solve my issue?