void Texture::LoadTGA(char * filename)
{
GLubyte tgaHeader[12];
GLubyte compressedTgaHeader[12] = {0,0,10,0,0,0,0,0,0,0,0,0};
FILE * tgaFile; // Deklarer filpeker
tgaFile = fopen(filename, "rb"); // Åpner filen for binær lesing (readonly binary)
fread(&tgaHeader, sizeof(tgaHeader), 1, tgaFile);
if (memcmp(compressedTgaHeader, &tgaHeader, sizeof(tgaHeader)) == 0) // Sjekk etter RLE
{
fread(tgaHeader, sizeof(tgaHeader), 1, tgaFile);
GLuint bytesPerPixel = (tgaHeader[4] / 8); // Hvor mange bytes består en pixel av?
GLuint pixelCount = 256*256;
GLuint currentHeight = 0;
GLuint currentWidth = 0;
GLubyte * colorBuffer = (GLubyte *)malloc(bytesPerPixel); // Tmp. lagring for en pixel
do
{
GLubyte chunkHeader = 0;
fread(&chunkHeader, sizeof(GLubyte), 1, tgaFile);
if (chunkHeader < 128) // Hvis RAW
{
chunkHeader++;
for (short counter = 0; counter < chunkHeader; counter++)
{
fread(colorBuffer, 1, bytesPerPixel, tgaFile); // Les og snu BGRA til RGBA
data[currentHeight][currentWidth][0] = colorBuffer[2];
data[currentHeight][currentWidth][1] = colorBuffer[1];
data[currentHeight][currentWidth][2] = colorBuffer[0];
if (bytesPerPixel == 4) // Hvis 32 bit (med alpha kanal altså)
data[currentHeight][currentWidth][3] = colorBuffer[3];
else
data[currentHeight][currentWidth][3] = 150;
}
if (currentWidth >= 256)
{
currentWidth = 0;
currentHeight++;
}
else
{
currentWidth++;
}
}
else // Hvis RLE
{
chunkHeader -= 127; // Trekk fra 127 for å bli kvitt ID bitten
fread(colorBuffer, 1, bytesPerPixel, tgaFile);
for (short counter = 0; counter < chunkHeader; counter++)
{
fread(colorBuffer, 1, bytesPerPixel, tgaFile); // Les og snu BGRA til RGBA
data[currentHeight][currentWidth][0] = colorBuffer[2];
data[currentHeight][currentWidth][1] = colorBuffer[1];
data[currentHeight][currentWidth][2] = colorBuffer[0];
if (bytesPerPixel == 4) // Hvis 32 bit (med alpha kanal altså)
data[currentHeight][currentWidth][3] = colorBuffer[3];
else
data[currentHeight][currentWidth][3] = 150;
}
if (currentWidth == 256)
{
currentWidth = 0;
currentHeight++;
}
else
{
currentWidth++;
}
}
}
while (currentHeight < 256 && currentWidth < 256);
fclose(tgaFile);
}
else
{
MessageBox(NULL,"Need 256x256x24 or 32 RLE Compressed TGA file","ERROR",MB_OK|MB_ICONSTOP);
fclose(tgaFile);
}
TGA loading, whats wrong with my code?
I try to code a demo. Already coded a simple one (called Simple) in wich I used NeHe code for loading bmp's. Now I want to load TGA's. But I do not feel comfortable with just copy-pasting the code on Nehe. So I tried writing my own. I want to load everything into a three dimensional array. My texture generator functions work with three dimensional array's so I want the TGA loading to do work the same way.
Whats wrong? It only generates a black texture :/ :
Sorry for the Norwegian comments...
[edited by - tech_cf on January 23, 2003 3:36:35 PM]
[edited by - tech_cf on January 24, 2003 1:23:28 PM]
Well, open gl requiers a one-dimentional array not a tree dimentional array
www.flashbang.se | www.thegeekstate.com | nehe.gamedev.net | glAux fix for lesson 6 | [twitter]thegeekstate[/twitter]
No it don''t. As I said, I have a texture generator that uses a three dimensional array. And it works.
I have:
GLubyte data[256][256][4]; // height,width,color (rgba)
and uploads it with:
glTexImage2D(GL_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,data);
I have:
GLubyte data[256][256][4]; // height,width,color (rgba)
and uploads it with:
glTexImage2D(GL_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,data);
To determine if a tga is stored as compressed image you have to test only the significant field of tga header not compare the whole header using memcmp with initialized array. I think this way you always get the error message box saying "Need 256x256x24 or 32 RLE Compressed TGA file" as you written in your code. I suggest you to look here for more details about tga format.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement