Skip to main content
GameDev.net gamedev.net
🔒 Locked

.tga files

Started by bizzybone May 6, 2004 at 10:52 AM 2 replies 550+ views
Original Post
bizzybone
bizzybone
Hi there guys. I am new to OpenGL/GLUT and am trying to extract a .tga file for use in my program. I need this .tga file for use as a background into which I want to insert a model of a boat for my sailing sim. How can I go about inserting this and controlling this model?
skow
skow
"How can I go about inserting this and controlling this model?"

Well that is a huge topic, there are good tutorials here, they will give you an idea how to control the model. As for using a .tga there is a tutorial here and at gametutorials.com on this.
bizzybone
bizzybone
thanks. tutorials been helpful
haro
haro
The GameTutorials code actually has a few problems with it. Here is a slightly modified implementation picked up from a Nehe tutorial ( a site hosted by GameDev ). This is 100% portable and somewhat cleaner.



struct Image
{
GLubyte *data;
GLuint bpp;
GLuint sizeX;
GLuint sizeY;
GLuint texID;
};

bool ImageLoad( const char* fileName, Image *texture )
{


GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header

GLubyte TGAcompare[12]; // Used To Compare TGA Header

GLubyte header[6]; // First 6 Useful Bytes From The Header

GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File

GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram

GLuint temp; // Temporary Variable

GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)


FILE *file = fopen(fileName, "rb"); // Open The TGA File


if( file==NULL || // Does File Even Exist?

fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read?

memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want?

fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes

{
if (file == NULL) // Did The File Even Exist? *Added Jim Strong*

return false; // Return False

else
{
fclose(file); // If Anything Failed, Close The File

return false; // Return False

}
}

texture->sizeX = header[1] * 256 + header[0]; // Determine The TGA sizeX (highbyte*256+lowbyte)

texture->sizeY = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)


if( texture->sizeX <=0 || // Is The sizeX Less Than Or Equal To Zero

texture->sizeY <=0 || // Is The Height Less Than Or Equal To Zero

(header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?

{
fclose(file); // If Anything Failed, Close The File

return false; // Return False

}

texture->bpp = header[4]; // Grab The TGA''s Bits Per Pixel (24 or 32)

bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel

imageSize = texture->sizeX*texture->sizeY*bytesPerPixel; // Calculate The Memory Required For The TGA Data


texture->data=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data


if( texture->data==NULL || // Does The Storage Memory Exist?

fread(texture->data, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved?

{
if(texture->data!=NULL) // Was Image Data Loaded

free(texture->data); // If So, Release The Image Data


fclose(file); // Close The File

return false; // Return False

}

for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data

{ // Swaps The 1st And 3rd Bytes (''R''ed and ''B''lue)

temp=texture->data[i]; // Temporarily Store The Value At Image Data ''i''

texture->data[i] = texture->data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte

texture->data[i + 2] = temp; // Set The 3rd Byte To The Value In ''temp'' (1st Byte Value)

}

fclose (file); // Close The File

return true;
}


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.