Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Godji

Texture Loading Problem

This topic is 5237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I''m trying to make a function that loads a 24/32 bit uncompress TGA texture. Mostly has been taken from the NeHe TGA tutorial. While it works perfectly for anything of size at least 256x256 (tested it up to 4096x4096), it does NOT work for 128x128! The function, as it is, still returns 1 for success, but the polygons are white. Can anyone help? Godji\ int CTexture::Load (char *filename) { const char TargaHeader[] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}; char FileHeader[12]; char InfoHeader[6]; char Swap; unsigned int ImageDataSize; unsigned int i; FILE *File; Free (); File = fopen (filename, "rb"); if (!File) return (0); if (fread (FileHeader, 1, sizeof (FileHeader), File) != sizeof (FileHeader)) { fclose (File); return (0); } if (memcmp (TargaHeader, FileHeader, sizeof (FileHeader))) { fclose (File); return (0); } if (fread (InfoHeader, 1, sizeof (InfoHeader), File) != sizeof (InfoHeader)) { fclose (File); return (0); } Width = (InfoHeader[1] << 8) + InfoHeader[0]; Height = (InfoHeader[3] << 8) + InfoHeader[2]; BitsPerPixel = InfoHeader[4]; if (Width <= 0 || Height <= 0 || (BitsPerPixel != 24 && BitsPerPixel != 32)) { fclose (File); return (0); } ImageDataSize = Width * Height * BitsPerPixel / 8; ImageData = new char[ImageDataSize]; if (!ImageData) { fclose (File); return (0); } if (fread (ImageData, 1, ImageDataSize, File) != ImageDataSize) { fclose (File); delete (ImageData); return (0); } for (i = 0; i < ImageDataSize; i += BitsPerPixel / 8) { Swap = ImageData; ImageData[i] = ImageData[i + 2]; ImageData[i + 2] = Swap; } fclose (File); glGenTextures (1, &ID); glBindTexture (GL_TEXTURE_2D, ID); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D (GL_TEXTURE_2D, 0, BitsPerPixel / 8, Width, Height, 0, BitsPerPixel == 32 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, ImageData); delete (ImageData); return (1); } Godji

Share this post


Link to post
Share on other sites
Advertisement
You would think that there''s some super universal TGA loading code, but sadly there isnt. Also, i need to be able to put the texture into a 3d array to modify it by-pixel. Also, Nehe mentioned there was a way to load irregular textures, but i have yet to find it. You know, this would be a lot easier if someone wrote an article about every single detail of how textures work and how to work them towards your own will. but that''s just a thought.
anyway, here''s the code I have... I''ve tried it with a 128x2 32 bit TGA...

typedef struct
{
GLubyte Header[12]; // TGA File Header
} TGAHeader;

typedef struct
{
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; // RGB or RGBA
GLuint Height; //Height of Image
GLuint Width; //Width ofImage
GLuint Bpp; // Bits Per Pixel
} TGA;

GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header

if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0) // Read TGA header
{
MessageBox(NULL, "Could not read info header", "ERROR", MB_OK); // Display error
if(fTGA != NULL) // if file is still open
{
fclose(fTGA); // Close it
}
return false; // Return failure
}

texture->width = tga.header[1] * 256 + tga.header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture->height = tga.header[3] * 256 + tga.header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
texture->bpp = tga.header[4]; // Determine the bits per pixel
tga.Width = texture->width; // Copy width into local structure
tga.Height = texture->height; // Copy height into local structure
tga.Bpp = texture->bpp; // Copy BPP into local structure

if((texture->width <= 0) || (texture->height <= 0) || ((texture->bpp != 24) && (texture->bpp !=32))) // Make sure all information is valid
{
MessageBox(NULL, "Invalid texture information", "ERROR", MB_OK); // Display Error
if(fTGA != NULL) // Check if file is still open
{
fclose(fTGA); // If so, close it
}
return false; // Return failed
}

if(texture->bpp == 24) //If the BPP of the image is 24...
{
texture->type = GL_RGB; // Set Image type to GL_RGB
}
else // Else if its 32 BPP
{
texture->type = GL_RGBA; // Set image type to GL_RGBA
}

tga.bytesPerPixel = (tga.Bpp / 8); // Compute the number of BYTES per pixel
tga.imageSize = (tga.bytesPerPixel * tga.Width * tga.Height); // Compute the total amout ofmemory needed to store data
texture->imageData = (GLubyte *)malloc(tga.imageSize); // Allocate that much memory

if(texture->imageData == NULL) // If no space was allocated
{
MessageBox(NULL, "Could not allocate memory for image", "ERROR", MB_OK); // Display Error
fclose(fTGA); // Close the file
return false; // Return failed
}

if(fread(texture->imageData, 1, tga.imageSize, fTGA) != tga.imageSize) // Attempt to read image data
{
MessageBox(NULL, "Could not read image data", "ERROR", MB_OK); // Display Error
if(texture->imageData != NULL) // If imagedata has data in it
{
free(texture->imageData); // Delete data from memory
}
fclose(fTGA); // Close file
return false; // Return failed
}

// Byte Swapping Optimized By Steve Thomas
for(GLuint cswap = 0; cswap < (int)tga.imageSize; cswap += tga.bytesPerPixel)
{
texture->imageData[cswap] ^= texture->imageData[cswap+2] ^=
texture->imageData[cswap] ^= texture->imageData[cswap+2];
}

fclose(fTGA); // Close file
return true; // Return success

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!