Results 1 to 5 of 5

Thread: Native TGA Loader with no 3rd party requirements

  1. #1

    Native TGA Loader with no 3rd party requirements

    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

  2. #2

    Re: Native TGA Loader with no 3rd party requirements

    This is pretty sweet! I'll load this up in D2007 and see how it goes.

  3. #3

    Re: Native TGA Loader with no 3rd party requirements

    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


  4. #4

    Re: Native TGA Loader with no 3rd party requirements

    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.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  5. #5

    Re: Native TGA Loader with no 3rd party requirements

    @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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •