I have following source code for loading tga files(from nehe) . But when i add some more parameter to the function so the first lines about texture(->width = …) raises exeption EAccesViolation, JUST when i add the parameter so i have no idea whats wrong. Can somebody please help me ?
typedef struct // Create a structure
{
GLubyte *imageData; // Image data (Up to 32 bits)
GLuint bpp; // Image color depth in bits per pixel.
GLuint width; // Image width
GLuint height; // Image height
GLuint texID; // Texture ID used to select a texture
} TextureImage;
bool TEngine::MakeTGATexture(const char *Dir)
{
TextureImage *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(Dir, "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 // Otherwise
{
fclose(file); // If anything failed, close the file
return FALSE; // Return false
}
}
texture->width = header[1] * 256 + header[0]; // Determine the TGA width (highbyte*256+lowbyte)
texture->height = header[3] * 256 + header[2]; // Determine the TGA height (highbyte*256+lowbyte)
if( texture->width <=0 | | // Is the width less than or equal to zero
texture->height <=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->width*texture->height*bytesPerPixel; // Calculate the memory required for the TGA data
texture->imageData=(GLubyte *)malloc(imageSize); // Reserve memory to hold the TGA data
if( texture->imageData==NULL | | // Does the storage memory exist?
fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does the image size match the memory reserved?
{
if(texture->imageData!=NULL) // Was image data loaded
free(texture->imageData); // 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->imageData[i]; // Temporarily store the value at image data 'i'
texture->imageData[i] = texture->imageData[i + 2]; // Set the 1st byte to the value of the 3rd byte
texture->imageData[i + 2] = temp; // Set the 3rd byte to the value in 'temp' (1st byte value)
}
fclose (file); // Close the file
// Build a texture from the data
glGenTextures(1, &TextureBin[5]); // Generate OpenGL texture IDs
glBindTexture(GL_TEXTURE_2D, TextureBin[5]); // Bind our texture
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear filtered
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear filtered
if (texture[0].bpp==24) // Was the TGA 24 bits
{
type=GL_RGB; // If so set the 'type' to GL_RGB
}
glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
return true; // Texture building went ok, return true
}