Results 1 to 6 of 6

Thread: tga loading problems

  1. #1
    Member Contributor
    Join Date
    Sep 2001
    Location
    Lewisville, TX
    Posts
    93

    tga loading problems

    my tga loader isn't loading tga's! grrr... if you could take a look at my loader and spot any errors that i might have missed that would help me out alot. Thanx!

    void tga: efine(){
    glTexImage2D(GL_TEXTURE_2D,0,m_type,m_Width,m_Heig ht,0,GL_RGB,GL_UNSIGNED_BYTE,m_data);
    }
    void tga::GenerateID(){
    glGenTextures(1,&m_textID);
    }

    void tga::Bind(){
    glBindTexture(GL_TEXTURE_2D,m_textID);
    }

    void tga::Filter(){
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }

    bool tga::Load(char *filename){
    fstream file;
    long x = 0;
    unsigned char rgbSwitch;

    file.open(filename,ios::in | ios::binary);
    if(!file.is_open()){
    file.close();
    return false;
    }else{
    file.seekg(2,ios::beg);
    file.read((char*)&m_type,sizeof(unsigned char));
    if((m_type != 2) | | (m_type != 3)){
    file.close();
    return false;
    }else{
    file.seekg(13,ios::cur);
    file.read((char*)&m_Width,sizeof(short int));
    file.read((char*)&m_Height,sizeof(short int));
    file.read((char*)&m_bites,sizeof(unsigned char));
    file.seekg(1,ios::cur);

    m_channels = m_bites / 8;
    m_size = m_Width * m_Height;
    m_size *= m_channels;
    m_data = new unsigned char[m_size];

    file.read((char*)&m_data,sizeof(unsigned char));

    while(x < m_size){
    rgbSwitch = m_data[x];
    m_data[x] = m_data[x + 2];
    m_data[x + 2] = rgbSwitch;
    x += m_channels;
    }
    file.close();
    return true;
    }
    }
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    California, USA
    Posts
    167

    Re: tga loading problems

    file.read((char*)&m_data,sizeof(unsigned char));

    Your only reading in one byte of data.

  3. #3
    Member Contributor
    Join Date
    Sep 2001
    Location
    Lewisville, TX
    Posts
    93

    Re: tga loading problems

    i changed the line to:
    file.read((char*)&m_data,sizeof(unsigned char)*m_size);
    and it still doesnt work!

  4. #4
    Member Contributor
    Join Date
    Sep 2001
    Location
    Lewisville, TX
    Posts
    93

    Re: tga loading problems

    i think i found were the error could be happening:

    if((m_type != 2) | | (m_type != 3)){
    file.close();
    return false;
    }

    i dont think im putting in the right value for m_type. i want to skip the first 2 bytes of data of the header and get the type. i try to do this using the file.seekg(2,ios::beg); but i think that im using it wrong and putting wrong data into m_type. how can i skip data in a file/fix this problem?

  5. #5
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    California, USA
    Posts
    167

    Re: tga loading problems

    You want to check the third byte in the file for either 2 or 3. 2 being black and white, 3 being true color, both being uncompressed. Check the spec for the other identifiers. www.wotsit.org

    EDIT:

    You should be doing a logical AND (&&) rather than a logical OR.

    Old GLman

    [This message has been edited by Old GLman (edited 12-12-2002).]

  6. #6
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    292

    Re: tga loading problems

    Yeah, try wotsi.

    /*****************************/

    You are right about your diagnose of the m_type values not being the appropriate ones...
    The 1, 2, 3, 4 are simply the number of channels (bytes) you are reading per pixel. But there is no such thing in the header of a TGA file. Instead, you read the number of bits per pixel (bpp). So, you would have 8, 16, 24, 32.
    Well, where do these 1, 2, 3, 4 come from?
    You divide the bpp, which is the third unsigned char in the header as you are reading, and divide it by 8. 8 is the number of bits in a byte.


    You will also have more problems if the tga is compressed or palletted or so and so and more sos.

    8 -> grayscale
    16 -> Why use it? Palletted maybe?
    24 -> RGB
    32 -> RGBA

    If you want you can use one I wrote.
    Download the normal map generator from: http://cheo.resnet.wayne.edu/miguel/
    You can use and abuse it.

    Good luck.



    [This message has been edited by mancha (edited 12-15-2002).]

Similar Threads

  1. Problems loading a bitmap
    By BrandonLP in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 10-27-2006, 03:40 PM
  2. problems loading tga
    By ajp17th in forum OpenGL: Basic Coding
    Replies: 13
    Last Post: 12-02-2003, 12:34 PM
  3. problems loading bmp
    By ajp17th in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-28-2003, 07:12 AM
  4. TGA Loading Problems
    By nukem in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 07-10-2003, 04:11 AM
  5. Problems loading a bitmat
    By Psyc in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-29-2001, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean