PDA

View Full Version : Misaligned Targa



Sarashinai
12-30-2008, 04:46 AM
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:
http://i248.photobucket.com/albums/gg179/sarashinai/32.jpg

24 BPP:
http://i248.photobucket.com/albums/gg179/sarashinai/24.jpg

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?

Zengar
12-30-2008, 05:15 AM
The problem will probably be your texture loading code.

V-man
12-30-2008, 07:59 AM
Probably this issue
http://www.opengl.org/wiki/index.php/Common_Mistakes#Texture_Upload

dletozeun
12-30-2008, 12:52 PM
Look at the glPixelStore (http://www.opengl.org/sdk/docs/man/xhtml/glPixelStore.xml) function.
Use glPixelStore(GL_UNPACK_ALIGNMENT, 1) before allowing opengl texture data with glTexImage2D if there is no row alignement in your texture data.

zeoverlord
12-30-2008, 04:07 PM
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.

Sarashinai
12-31-2008, 12:21 PM
Tell me which code sections you consider relevant and I'd be happy to post them.

IneQuation.pl
12-31-2008, 11:30 PM
The Targa loader.

_NK47
01-02-2009, 07:40 AM
the targa loader itself looks pretty simple.

// read pixels
imageSize = texture->width*texture->height*bytesPerPixel;
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).

dletozeun
01-02-2009, 12:07 PM
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.