Archived

This topic is now archived and is closed to further replies.

ChaosWars

Help with C++/TGA loading

Recommended Posts

I've been at this for a day and if I had any hair to loose I would have lost it by now :/ I want to use the C++ fstream functions to read in the header data of an uncompressed (and later compressed) targa file. I can't get the information to read into the struct that I've made however - I'm new to OpenGL programming and C++ BTW. This is what I've written so far : Header file
#ifndef TARGA_H
#define TARGA_H

#include <GL/gl.h>
#include <fstream>
#include <iostream>

/*typedef struct
{
	char idLength;
	char colourMapType;
	char imageType;
	//colormapspecification:
	short int firstEntry;
	short int colorMapLength;
	char colorMapEntrySize;
	//image specification
	short int xOrigin;
	short int yOrigin;
	short int imageWidth;
	short int imageHeight;
	char pixelDepth;
	char imageDescriptor;
} TGAHeader;*/

class LoadTexture
{
	public:
	LoadTexture(){};
	~LoadTexture(){};
	typedef struct
	{
		GLbyte idLength;
		GLbyte colourMapType;
		GLbyte imageType;
		GLbyte colorMapSpecification[5];
		GLbyte imageSpecification[10];
	} TGAHeader;
	
	typedef struct
	{
		GLubyte *imageData;
		GLint bitDepth;
		GLint width;
		GLint height;
		GLint texID;
		GLuint type;
	} Texture;
	
	TGAHeader tgaHeader;
	Texture texture;
	bool LoadTGATexture( char *, TGAHeader * );
	//void LoadUncompressedTexture( TGAFile *, char *, std::ifstream * );

	//void LoadCompressedTexture( TGAFile *, char *, std::ifstream * );

};

#endif	//TARGA_H
cpp file
bool LoadTexture::LoadTGATexture( char *fileName, TGAHeader *tgaHeader )
{
	std::ifstream file;
	file.open( fileName, std::ios::in | std::ios::binary );
	if ( !file ){
		printf( "Error : Could not open file %s\n", fileName );
		return false;
	}
	else{
		printf( "Opened file %s succesfully\n", fileName );
	}
	
	file.read( ( char * )tgaHeader, sizeof( GLbyte ) );
	file.close();
	return true;
}
How do I now read the information from the file stream I've opened into the struct tgaHeader? I am at a loss here I've searched these forums and found quite a number of posts, a few of them involving the c++ manner of loading the textures but non of them went into the specifics I'm looking for. [edited by - ChaosWars on April 3, 2004 12:56:32 PM]

Share this post


Link to post
Share on other sites
I just made a variable :

GLubyte textureArray[18];


and edited the line

file.read( ( char * )tgaHeader, sizeof( GLbyte ) );


to read :

file.read( ( char * )textureArray, sizeof( GLbyte ) );
for( int i = 0; i <=17; i++ ){
printf( "Array value = %s\n",
textureArray[i] );
}


And the output it gave was :

Array value = (null)

in every case - hardly inspiring So it seems as though that line of source is not going to cut it :/ But this begs the question : What syntax must I use to read the first 18 bytes of the TGA file into some sort of array or struct so that I can then get around to processing the information?

[edited by - ChaosWars on April 3, 2004 1:11:35 PM]

Share this post


Link to post
Share on other sites
Sorted. Here''s the code for anyone who''s searching :

Header

#ifndef TARGA_H
#define TARGA_H

#include <GL/gl.h>
#include <fstream>
#include <iostream>

class LoadTexture
{
public:
LoadTexture();
~LoadTexture();

typedef struct
{
GLbyte idLength;
GLbyte colourMapType;
GLbyte imageType;
//Color Map Specification:

GLbyte firstEntryIndex[2]; //2

GLbyte colorMapLength[2]; //2

GLbyte colorMapEntrySize;
//image specification

GLbyte xOrigin[2]; //2

GLbyte yOrigin[2]; //2

GLbyte imageWidth[2]; //2

GLbyte imageHeight[2]; //2

GLbyte pixelDepth;
GLbyte imageDescriptor;
} TGAHeader;

typedef struct
{
GLubyte *imageData;
GLint bitDepth;
GLint width;
GLint height;
GLint texID;
GLuint type;
} Texture;

TGAHeader tgaHeader;
Texture texture;
bool LoadTGATexture( char * );
void LoadUncompressedTexture( TGAHeader *, std::fstream * );
void LoadCompressedTexture( TGAHeader *, std::fstream * );
};

#endif //TARGA_H



Source

#include "targa.h"

bool LoadTexture::LoadTGATexture( char *fileName )
{
std::fstream file;
file.open( fileName, std::ios::in | std::ios::binary );
if ( !file ){
printf( "Error : Could not open file %s\n", fileName );
return false;
}
else{
printf( "Opened file %s succesfully\n", fileName );
}

file.seekg( 0, std::ios::beg );
file.read( ( char * )&tgaHeader, sizeof( tgaHeader ) );
if( ( tgaHeader.imageType == 2) ){
LoadUncompressedTexture( &tgaHeader, &file );
}
else if( ( tgaHeader.imageType == 10 ) ){
LoadCompressedTexture( &tgaHeader, &file );
}

file.close();
return true;
}

void LoadTexture::LoadUncompressedTexture( TGAHeader *tgaHeader, std::fstream *file )
{
printf( "Loading uncompressed tga image\n" );
}

void LoadTexture::LoadCompressedTexture( TGAHeader *tgaHeader, std::fstream *file )
{
printf( "Loading compressed tga image\n" );
}

Share this post


Link to post
Share on other sites