PDA

View Full Version : Native TGA Loader with no 3rd party requirements



jdarling
16-07-2010, 05:55 PM
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:


TGA := TRawTGA.Create(loadFileName);
Writeln(loadFileName, ': ', TGA.LoadErrorMessage);


Converting the TGA to an OpenGL texture:


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:


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.

http://www.eonclash.com/gl/TGAloadertest.jpg

- Jeremy

djcityscapes
17-07-2010, 01:53 AM
This is pretty sweet! I'll load this up in D2007 and see how it goes.

virtual
17-07-2010, 07:22 AM
thanks for sharing

i've seen than uPNGSupport require free pascal paszlib unit to work , i have fastzlib114 for delphi , i don't know if this gonna work with your PNG loader

chronozphere
17-07-2010, 07:37 AM
I'm using Vampyire Imaging for my project. Still this may be very usefull when you only want to support a single format, or when you want to have seperate pieces of code to load separate formats. :)

jdarling
19-07-2010, 01:04 PM
@virtual, the changes needed to make it work with Delphi may be more then you are willing to put into place, but feel free. My focus was more on FPC support than anything else.

@chrono, I use Vampyre myself and would definitely agree that its the superior answer to the general question of loading graphics. These are simply for exactly as you stated, you only need support for a single image format. There are A LOT of optimizations in Vampyre that I never plan on putting in place :)