I am trying to use sprite sheet in OpenGL, implementing it through Array Texture
This is how I load my texture:
QImage image;
image.load("C:\\QtProjects\\project\\images\\spritesheet.png", "png");
const unsigned char* data = image.bits();
int twidth = image.width(), theight = image.height();
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA, twidth / 3, theight / 4, 12);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, twidth / 3, theight / 4, 12, GL_BGRA, GL_UNSIGNED_BYTE, data);
glUseProgram(scene_program);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
This is my fragment shader:
#version 330 core
in vec3 Color;
in vec2 Texture_coord;
out vec4 outColor;
uniform sampler2DArray tex;
void main()
{
vec2 coord;
coord.x = Texture_coord.x;
coord.y = Texture_coord.y;
vec4 color = texture(tex, vec3(coord, 0));
outColor = color;
}
I am drawing it to the quad with texture coordinates (0.0, 0.0)
in one corner and (1.0, 1.0)
in other, so I expect to get a first sprite from first row on my quad.
But I get this corrupted piece of image: http://i.stack.imgur.com/bfXgr.jpg
This is whole sprite sheet: http://i.stack.imgur.com/LaZKP.png
P.S.: whole image is loaded correctly, I can display it or it's part through traditional GL_TEXTURE_2D