Skip to main content
gamedev.net
🔒 Locked

Help loading TGAs

Started by Daaark 10 replies 550+ views
Original Post
Daaark
Daaark
I''m trying to load uncompressed TGAs like in lesson 33(I think it was 33), and I keep crashing at the line where I try to assign something to Texture->Width, I tried changing it to Texture->Width = 256; and it still crashes. What am I doing wrong?
  
extern GLuint GameTextures[21];

typedef struct
{
    GLubyte*    ImageData;    
    GLuint      BitsPerPixel;
    GLuint      Width;
    GLuint      Height;
    GLuint      TexId;
    GLuint      Type;  
}TEXTURE;

typedef struct
{
    GLubyte     Header[12];
}TGA_HEADER;

typedef struct
{
    GLubyte     Header[6];
    GLuint      BytesPerPixel;
    GLuint      ImageSize;
    GLuint      Height;
    GLuint      Width;
    GLuint      BitsPerPixel;       
}TGA;

GLubyte uTGAcompare[12]={0,0,2,0,0,0,0,0,0,0,0,0};

bool TextureLoadTga(char *FileName,int TextureNumber)
{
    
    TGA_HEADER  TgaHeader;
    TGA         Tga;
    TEXTURE     *Texture;
    FILE        *File;
    char        Error[80];    

    File=fopen(FileName,"rb");
    
    if(File==NULL)
    {
        sprintf(Error,"Missing File: %s.",FileName);
        MessageBox(NULL,Error,APPNAME,MB_OK);
        return false;        
    }

    //try to read in the header

    if(fread(&TgaHeader,sizeof(TGA_HEADER), 1,File)== 0)
    {
        fclose(File);
        return false;
    }

    //make sure it''s an uncompressed tga

    if(memcmp(uTGAcompare,&TgaHeader,sizeof(TgaHeader))!=0)
    {
        MessageBox(NULL,"TGA must be uncompressed.",APPNAME,MB_OK);
        fclose(File);
        return false;
    }
    
    //try to read next 6 bytes

    if(fread(Tga.Header,sizeof(Tga.Header),1,File)==0)
    {
        MessageBox(NULL,"Couldn''t read TGA.",APPNAME,MB_OK);
        fclose(File);
        return false;
    }
    
    MessageBox(NULL,"Checkpoint 1",APPNAME,MB_OK);
    Texture->Width=Tga.Header[1]*256+Tga.Header[0];
	MessageBox(NULL,"Checkpoint 2",APPNAME,MB_OK);
    Texture->Height=Tga.Header[3]*256+Tga.Header[2];
	Texture->BitsPerPixel=Tga.Header[4];
	Tga.Width=Texture->Width;
	Tga.Height=Texture->Height;
	Tga.BitsPerPixel=Texture->BitsPerPixel;

    fclose(File);
    return true;
}  
titan2782
titan2782
i''d copy the code from lesson 32. This is the TGA cod ei always use flawlessly.

IT Administrator / Software Engineere
http://drdsoftware.cjb.net
Daaark
Daaark
This is a "copy" of that lesson, I''m just writing it so it only loads uncompressed tgas. it''s pretty much the same code, I just gave the variables different spelling to match my own code.
snisarenko
snisarenko
What kind of error does it give u ?

A memory allocation error ???
Daaark
Daaark
I get this as soon as it hits the Texture->Width line

EXENAME caused an invalid page fault in
module EXENAME.EXE at 0167:00401936.
Registers:
EAX=00000100 CS=0167 EIP=00401936 EFLGS=00010206
EBX=7803b6d0 SS=016f ESP=0073fba0 EBP=0073fc48
ECX=00000000 DS=016f ESI=00402020 FS=62ef
EDX=00000000 ES=016f EDI=0073fc44 GS=0000
Bytes at CS:EIP:
89 41 08 6a 00 68 dd 17 40 00 68 1f 18 40 00 6a
Stack dump:
0073fc88 0073fd00 0073fd04 80000000 00000000 80000000 3f800000 80000000 00000000 00000000 00000000 3f800000 00000000 00000000 00000000 00000000
Thunder_Hawk
Thunder_Hawk
Of course you get a runtime error Texture is a wild pointer. You have to initialize it to something (i.e. Texture = (TEXTURE *)malloc(sizeof(TEXTURE); or Texture = new TEXTURE; or Texture = &Texture2 where Texture2 comes from the outside world).

[edited by - Thunder_Hawk on January 3, 2003 5:07:11 PM]
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
Nukem
Nukem
You there is two types of TGA files, compressed and uncompressed. You should really support both. Not sure were I found the tutorial for loading compressed and uncompressed but I think it was here.
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
Daaark
Daaark
Why should I support both if I''m only going to be loading a few textures?

So what should my TEXTURE Texture line look like then? Texture = new(TEXTURE)?
Daaark
Daaark
Ok it works now. just going to post the code to make sure I did it right, and I''m freeing up the memory right..
Still not sure what Texture->TexId does since I never assigned anything to it..


bool TextureLoadTga(char *FileName,int TextureNumber)
{

TGA_HEADER TgaHeader;
TGA Tga;
TEXTURE *Texture = new TEXTURE;
FILE *File;
char Error[80]; //To hold error messages
int cswap; //For swapping RGB values

File=fopen(FileName,"rb");

if(File==NULL)
{
sprintf(Error,"Missing File: %s.",FileName);
MessageBox(NULL,Error,APPNAME,MB_OK);
return false;
}

//try to read in the header
if(fread(&TgaHeader,sizeof(TGA_HEADER), 1,File)== 0)
{
fclose(File);
return false;
}

//make sure it''s an uncompressed tga
if(memcmp(uTGAcompare,&TgaHeader,sizeof(TgaHeader))!=0)
{
MessageBox(NULL,"TGA must be uncompressed.",APPNAME,MB_OK);
fclose(File);
return false;
}

//try to read next 6 bytes
if(fread(Tga.Header,sizeof(Tga.Header),1,File)==0)
{
MessageBox(NULL,"Couldn''t read TGA.",APPNAME,MB_OK);
fclose(File);
return false;
}

Texture->Width=Tga.Header[1]*256+Tga.Header[0];
Texture->Height=Tga.Header[3]*256+Tga.Header[2];
Texture->BitsPerPixel=Tga.Header[4];
Tga.Width=Texture->Width;
Tga.Height=Texture->Height;
Tga.BitsPerPixel=Texture->BitsPerPixel;
sprintf(Error,"%d",(int)Tga.Width);
MessageBox(NULL,ERROR,APPNAME,MB_OK);

if((Texture->Width<=0)||(Texture->Height<=0)||((Texture->BitsPerPixel!=24)&&(Texture->BitsPerPixel!=32)))
{
sprintf(Error,"Error: %s is not a valid TGA image.",FileName);
MessageBox(NULL,Error,APPNAME,MB_OK);
fclose(File);
return false;
}

//Set the pixel type
if(Texture->BitsPerPixel==24)Texture->Type=GL_RGB;
else Texture->Type=GL_RGBA;

Tga.BytesPerPixel=(Tga.BitsPerPixel/8);
Tga.ImageSize=(Tga.BytesPerPixel*Tga.Width*Tga.Height);
Texture->ImageData=(GLubyte *)malloc(Tga.ImageSize);

if(fread(Texture->ImageData,1,Tga.ImageSize,File)!=Tga.ImageSize)
{
sprintf(Error,"%s: Could not read image data!",FileName);
MessageBox(NULL,Error,APPNAME,MB_OK);
free(Texture->ImageData);
fclose(File);
return false;
}

//close the file
fclose(File);

//swap BGR into RGB
for(cswap=0;cswap<(int)Tga.ImageSize;cswap+=Tga.BytesPerPixel)
{
Texture->ImageData[cswap] ^= Texture->ImageData[cswap+2] ^=
Texture->ImageData[cswap] ^= Texture->ImageData[cswap+2];
}

MessageBox(NULL,"Checkpoint",APPNAME,MB_OK);

//now build a texture using the ImageData
//Or more likely.. crash my sys
glGenTextures(1,&Texture->TexId);
glBindTexture(GL_TEXTURE_2D,GameTextures[TextureNumber]);
glTexImage2D(GL_TEXTURE_2D,0,3,Texture->Width,Texture->Height,0,GL_RGB,GL_UNSIGNED_BYTE,Texture->ImageData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

free(Texture->ImageData);
delete(Texture);

return true;
}
titan2782
titan2782
TexID is used when binding the texture. at least it is in all of the tutorials.

IT Administrator / Software Engineere
http://drdsoftware.cjb.net
Lukerd
Lukerd
glGenTextures(1,&Texture->TexId); << TexID being used

then when you bind the texture you use that.

Lukerd.

"To err is human, to really mess up requires a computer"
"To err is human, to really mess up requires a computer"
Daaark
Daaark
Ok, i fixed it to point to my GameTextures array. It''s working good now. Am I freeing up the memory for Texture right?

Topic Locked

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

Sign in to reply to this topic.