Jump to content
  • Advertisement

Archived

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

tech_cf

TGA loading, whats wrong with my code?

This topic is 5716 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

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 :/ :
      
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);
    }
      
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]

Share this post


Link to post
Share on other sites
Advertisement
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);

Share this post


Link to post
Share on other sites
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.

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!