Misaligned Targa

Hello,
I followed the NeHe tutorial here: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=24

I can load Targas and use them as textures but I’ve encountered a problem and I don’t know how to solve it.

The two images are using the same drawing code. The only difference is that the targa file in the first image was saved at 32 BPP and the targa in the second image was saved at 24 BPP.
There is no transparency in the image.

32 BPP:

24 BPP:

As you can see, the 24BPP file draws misaligned. The only change is a resave of the image at a different BPP.

Here is the code I’m using to draw:
glPushAttrib(GL_ALL_ATTRIB_BITS);

glLoadIdentity();

glColor4f(1.0f, 1.0f, 1.0f, mTransparency);

glBindTexture(GL_TEXTURE_2D, mTextureID[0]);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D); glEnable(GL_CULL_FACE);

if (mRotation){
glTranslatef(x, y + height, 0); // Move to bottom left of image
glRotatef(mRotation, 0, 0, 1);
glTranslatef(-x, -(y + height), 0.0f); // Reverse move
}

glTranslatef(x, y, 0);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2d(0, height + 1); // Bottom Left Of The Texture and Quad

glTexCoord2f(1.0f, 0.0f);
glVertex2d(width + 1, height + 1); // Bottom Right Of The Texture and Quad

glTexCoord2f(1.0f, 1.0f);
glVertex2d(width + 1, 0); // Top Right Of The Texture and Quad

glTexCoord2f(0.0f, 1.0f);
glVertex2d(0, 0); // Top Left Of The Texture and
glEnd();

glPopAttrib();

Can anyone tell me why this occurs?

The problem will probably be your texture loading code.

Probably this issue
http://www.opengl.org/wiki/index.php/Common_Mistakes#Texture_Upload

Look at the glPixelStore function.
Use glPixelStore(GL_UNPACK_ALIGNMENT, 1) before allowing opengl texture data with glTexImage2D if there is no row alignement in your texture data.

It would be helpful if you posted some actually relevant code, you should also double check your code as a single typo could cause this, though i do not know where exactly.

Tell me which code sections you consider relevant and I’d be happy to post them.

The Targa loader.

the targa loader itself looks pretty simple.

// read pixels
imageSize = texture->widthtexture->heightbytesPerPixel;
texture->imageData=(GLubyte *)malloc(imageSize);
fread(texture->imageData, 1, imageSize, file)!=imageSize);
// swap channels
for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
{ temp=texture->imageData[i];
texture->imageData[i] = texture->imageData[i + 2];
texture->imageData[i + 2] = temp;
}

makes no difference in 24/32 bit. seems to be probably the OpenGL loading code. better to debug step by step and confirm that f.e. imageSize is your actuall image width * height * bpp/8 (calculator).

Sarashinai, have you tried what V-man and me pointed out about pixel unpack alignment? This is typically this kind of problem that happens with bad alignment.

I have found the Kilgard’s explanation which is very instructive. Look at paragraph 7 for pixel alignement.
http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

IMO, the tga loader is good, I don’t know anyone who has not copied and paste the tga loading source code or just translated in his favourite language. Why reinvent the wheel.