Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
3  
possible solutions: stackoverflow.com/questions/7195130/… –  Jimmy Jan 23 '13 at 6:16

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.