Targa (TGA) loading December 08, 2010 06:54AM | Registered: 13 years ago Posts: 118 |
bool LoadTextureFromTGA(char * fileName, GXTexObj &textureObj){ const u8 TGAheader[12] = {0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA header u8 TGAcompare[12]; // Used to compare TGA header u8 header[6]; // First 6 useful bytes from the header u32 bytesPerPixel; // Holds number of bytes per pixel used in the TGA file u32 bpp; // Image color depth in bits per pixel u32 imageSize; // Used to store the image size when setting aside ram u8 *imageData = 0; // Image data (Up To 32 Bits) u32 temp; // Temporary variable //u32 type = GL_RGBA; // Set the default GL mode to RBGA (32 BPP) u32 width; // Image width u32 height; // Image height FILE *file = fopen(fileName, "rb"); // Open the TGA file if( !file || fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || fread(header,1,sizeof(header),file)!=sizeof(header)){ if (!file){ fprintf(stderr,"Error: Cannot load %s\n", fileName); return false; }else{ fprintf(stderr,"Error: %s is an invalid TGA texture\n", fileName); fclose(file); // If anything failed, close the file return false; } } width = header[1] * 256 + header[0]; // Determine the TGA width (highbyte*256+lowbyte) height = header[3] * 256 + header[2]; // Determine the TGA height (highbyte*256+lowbyte) if(width <= 0 || height <= 0 || (header[4]!=24 && header[4]!=32)){ // Is the TGA 24 or 32 bit? fprintf(stderr,"Error: %s is an invalid TGA texture\n", fileName); fclose(file); // If anything failed, close the file return false; } bpp = header[4]; // Grab the TGA's bits per pixel (24 or 32) bytesPerPixel = bpp/8; // Divide by 8 to get the bytes per pixel imageSize = width*height*bytesPerPixel; // Calculate the memory required for the TGA data #ifdef NEW_TEXTURE_ALLOCATION // We add in an alpha channel if there isn't one, since there is no texture format: GX_RGB u8* tempData = new u8 [imageSize]; if(!tempData || fread(tempData, 1, imageSize, file) != imageSize){ delete[] tempData; fprintf(stderr,"Error: cannot load %s\n", fileName); fclose(file); // Close the file return false; } fclose (file); // Close the file imageData = new u8 [width*height*4]; //This will always be 32 bits if(bytesPerPixel == 4){ // We have an alpha channel for(u32 i = 0; i < imageSize; i += bytesPerPixel){ imageData = tempData[i+2]; imageData[i+1] = tempData[i+1]; imageData[i+2] = tempData[i+0]; imageData[i+3] = tempData[i+3]; } }else{ // We do NOT have an alpha channel; add our own. u32 j = 0; for(u32 i = 0; i < imageSize; i += bytesPerPixel){ imageData[j+0] = tempData[i+2]; imageData[j+1] = tempData[i+1]; imageData[j+2] = tempData[i+0]; imageData[j+3] = 0xff; j += 4; // 4 bytes per pixel } } #else // This is the original: imageData = new u8 [imageSize]; // Reserve memory to hold the TGA data if(!imageData || // Does the storage memory exist? fread(imageData, 1, imageSize, file)!=imageSize){ // Does the image size match the memory reserved? delete[] imageData; // If so, release the image data fprintf(stderr,"Error: cannot load %s\n", fileName); fclose(file); // Close the file return false; } fclose (file); // Close the file for(u32 i = 0; i < imageSize; i += bytesPerPixel){ // Loop through the image data // Swaps the 1st and 3rd bytes ('R'ed and 'B'lue) temp=imageData; // Temporarily store the value at image data 'i' imageData = imageData[i + 2]; // Set the 1st byte to the value of the 3rd byte imageData[i + 2] = temp; // Set the 3rd byte to the value in 'temp' (1st byte value) } #endif GX_InitTexObj(&textureObj, imageData, width, height, GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE); // Mipmaping... maybe later. //GX_InitTexObjLOD(&textureObj, GX_LIN_MIP_LIN, GX_LINEAR, 0, 5, 0, GX_DISABLE, GX_DISABLE, GX_ANISO_1); // I also made up values for min LOD, max LOD, and the LOD bias. delete[] imageData; return true; // Texture building went Ok, return true }What am I doing wrong?
Re: Targa (TGA) loading December 08, 2010 11:14AM | Registered: 13 years ago Posts: 379 |
Re: Targa (TGA) loading December 08, 2010 09:28PM | Registered: 13 years ago Posts: 118 |
Re: Targa (TGA) loading December 08, 2010 09:33PM | Registered: 13 years ago Posts: 379 |
Re: Targa (TGA) loading December 09, 2010 02:28AM | Moderator Registered: 14 years ago Posts: 703 |
Re: Targa (TGA) loading December 09, 2010 03:32AM | Registered: 13 years ago Posts: 686 |
Re: Targa (TGA) loading December 09, 2010 06:48PM | Moderator Registered: 14 years ago Posts: 703 |