To go along with my native PNG loader I thought I'd whip up a quick and dirty TGA loader as well. Extremely simple and straight forward, should be easy for anyone to make use of if they so wish.

The code is pretty much completely hand rolled following a bit of the NeHe tutorial for the compression stuff.

This code ONLY LOADS TGA's from file or from a TStream, no saving. It only requires classes (for stream support).

Download at: http://www.eonclash.com/gl/uTGASupport.pas

Here is how I'm testing it with OpenGL:

Loading a TGA file from disk:
Code:
 TGA := TRawTGA.Create(loadFileName);
 Writeln(loadFileName, ': ', TGA.LoadErrorMessage);
Converting the TGA to an OpenGL texture:
Code:
 glShadeModel(GL_SMOOTH);
 glGenTextures(1, @FGLImage);
 glBindTexture(GL_TEXTURE_2D, FGLImage);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, TGA.Width, TGA.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, TGA.Data );
Displaying the loaded TGA in OpenGL:
Code:
 glEnable(GL_TEXTURE_2D);
 glEnable(GL_BLEND);
 glDisable(GL_DEPTH_TEST);
  glLoadIdentity();
  glTranslatef(1.5,0.0,-6.0);//1.5
  if(TGA.BackgroundColor.C > 0)then
   glColor4f(TGA.BackgroundColor.R/255, TGA.BackgroundColor.G/255, TGA.BackgroundColor.B/255, TGA.BackgroundColor.A/255)
  else
   glColor4f(1.0, 1.0, 1.0, 1.0);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glBindTexture(GL_TEXTURE_2D, FGLImage);
  glRotatef(-rotation, 0.0, 1.0, 0.0);
 	glBegin(GL_QUADS);
   glTexCoord2f(0.0, 1.0);
   glVertex3f(-1.0, 1.0, 0.0);
   glTexCoord2f(1.0, 1.0);
   glVertex3f( 1.0, 1.0, 0.0);
   glTexCoord2f(1.0, 0.0);
   glVertex3f( 1.0,-1.0, 0.0);
   glTexCoord2f(0.0, 0.0);
   glVertex3f(-1.0,-1.0, 0.0);
 	glEnd();
 glEnable(GL_DEPTH_TEST);
 glDisable(GL_BLEND);
 glDisable(GL_TEXTURE_2D);
And here is a screenshot (with a triangle displayed using glColor3f) and a 32 bit compressed star from a TGA file.



- Jeremy