I'm generating a texture atlas in OpenGL, where I want to create a blank texture and copy my sprites to it.
The texture is generated like this:
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
While the sprites are copied like this:
GLenum format;
if (sprite.hasAlpha)
{
format = GL_RGBA;
}
else
{
format = GL_RGB;
}
glTexSubImage2D(
GL_TEXTURE_2D, 0,
sprite.x, sprite.y,
sprite.width, sprite.height,
format, GL_UNSIGNED_BYTE,
sprite.data);
The issue is I keep getting artifacts in the texture not covered by a sprite:
Here I'm supposed to only have two slugs, one sword in the stone, one light grey tile, and one dark grey tile.
I tried creating a width * height * sizeof(GLuint)
GLuint array filled with zeros and passing it to the glTexImage2D
call, but that didn't seem to do anything.