TGAFILE *tgaFile = NULL;
FILE *filePtr;
unsigned char ucharBad; // garbage unsigned char data
short int sintBad; // garbage short int data
long imageSize; // size of the TGA image
int colorMode; // 4 for RGBA or 3 for RGB
long imageIdx; // counter variable
unsigned char colorSwap; // swap variable
// open the TGA file
filePtr = fopen(filename, "rb");
if (!filePtr)
return 0;
// read first two bytes of garbage
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
// read in the image type
fread(&tgaFile->imageTypeCode, sizeof(unsigned char), 1, filePtr);
// for our purposes, the image type should be either a 2 (color) or a 3 (greyscale)
if ((tgaFile->imageTypeCode != 2) && (tgaFile->imageTypeCode != 3))
{
fclose(filePtr);
return 0;
}
// read 13 bytes of garbage data
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
// read image dimensions
fread(&tgaFile->imageWidth, sizeof(short int), 1, filePtr);
fread(&tgaFile->imageHeight, sizeof(short int), 1, filePtr);
// read image bit depth
fread(&tgaFile->bitCount, sizeof(unsigned char), 1, filePtr);
// read 1 byte of garbage data
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
// colorMode -> 3 = BGR, 4 = BGRA
colorMode = tgaFile->bitCount / 8;
imageSize = tgaFile->imageWidth * tgaFile->imageHeight * colorMode;
// allocate memory for image data
tgaFile->imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);
// read in image data
fread(tgaFile->imageData, sizeof(unsigned char), imageSize, filePtr);
// change BGR to RGB so OpenGL can read the image data
for (imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode)
{
colorSwap = tgaFile->imageData[imageIdx];
tgaFile->imageData[imageIdx] = tgaFile->imageData[imageIdx + 2];
tgaFile->imageData[imageIdx + 2] = colorSwap;
}
// close the file
fclose(filePtr);
//Build A Texture From The Data
glGenTextures(1, &ID); //Generate OpenGL texture IDs
glBindTexture(GL_TEXTURE_2D, ID); //Bind the texture to a texture object
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); //Filtering for if texture is bigger than should be
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter); //Filtering for if texture is smaller than it should be
glTexImage2D(GL_TEXTURE_2D, 0, tgaFile->bitCount, tgaFile->imageWidth, tgaFile->imageHeight, 0, tgaFile->bitCount, GL_UNSIGNED_BYTE, tgaFile->imageData);
Please help me. If you see anything wrong or have any ideas on how i can fix this please tell me.
Thnx a million
Can't find this bug!
Hey,
I''m getting an access violation error when using the OpenGL Game Programming (book) TGA loading function.
It gives me an unhandled exeption and then when it goes in to debug mode it goes to this line..
00408692 mov byte ptr [edi],al
This is the section of code where i think the problem is
Unless I'm missing something you're reading into unallocated memory (tgaFile points to NULL) starting at the line
Edited by - spock on December 9, 2001 10:24:43 PM
// read in the image typefread(&tgaFile->imageTypeCode, sizeof(unsigned char), 1, filePtr);
Edited by - spock on December 9, 2001 10:24:43 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement