2
\$\begingroup\$

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:

Texture Atlas Artifacts

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.

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

Going on Jimmy's comment, I realized my first attempt at passing a blank data array to glTexImage2D didn't work because I used width * height * sizeof(GLuint) as the size when I should have been using width * height * 4, where 4 represents the four values in an RGBA colour.

\$\endgroup\$
1
  • \$\begingroup\$ But sizeof(GLuint) = 4. So you changed 4 to 4 and magically the code worked? \$\endgroup\$
    – Marc
    Feb 16, 2022 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .