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;
}
🔒 Locked
Help loading TGAs
Advertisement
Original Post
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?
What kind of error does it give u ?
A memory allocation error ???
A memory allocation error ???
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
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
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]
[edited by - Thunder_Hawk on January 3, 2003 5:07:11 PM]
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..
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;
}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement