GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


bitmap loader code by mrglass
1
 
GLuint tex;

void bmp(void){
     glGenTextures(1, &tex);
     loadBitmap('wood1.bmp', tex);
     }


/* Bitmap loading code from http://www.glprogramming.com/dgs.php?dg=9 */



// This function loads a .bmp file and puts it in the GLuint specified
bool loadBitmap(const char *filename, GLuint tex)
{
    FILE *fp = fopen(filename, "r");
    if(!fp)
    {
    cout << "Could not open the file." << endl;
    return false;
    }

    unsigned short uint;
    unsigned int dword;
    unsigned short word;
    long llong;
    // read some data we don't need
    fread(&uint, sizeof(uint), 1, fp);
    fread(&dword, sizeof(dword), 1, fp);
    fread(&uint, sizeof(uint), 1, fp);
    fread(&uint, sizeof(uint), 1, fp);
    fread(&dword, sizeof(dword), 1, fp);
    fread(&dword, sizeof(dword), 1, fp);

    long width, height;
    fread(&width, sizeof(long), 1, fp);
    fread(&height, sizeof(long), 1, fp);

    fread(&word, sizeof(word), 1, fp);

    unsigned short bitcount;
    fread(&bitcount, sizeof(unsigned short), 1, fp);

    if(bitcount != 24)
    {
    cout << "invalid bitcount. Make a 24bit image." << endl;
    return false;
    }


    unsigned long compression;
    fread(&compression, sizeof(unsigned long), 1, fp);

    if(compression != 0)
    {
    cout << "invalid compression. Make sure the BMP is not using RLE." << endl;
    return false;
    }

    fread(&dword, sizeof(dword), 1, fp);
    fread(&llong, sizeof(long), 1, fp);
    fread(&llong, sizeof(long), 1, fp);
    fread(&dword, sizeof(dword), 1, fp);
    fread(&dword, sizeof(dword), 1, fp);

    unsigned char *imagedata;
    imagedata = new unsigned char[width * height * 4];

    for(int x=0; x < width; x++)
    {
    for(int y=0; y < height; y++)
    {
        fread(&imagedata[(x * height + y)*4+0], sizeof(char), 1, fp);
        fread(&imagedata[(x * height + y)*4+1], sizeof(char), 1, fp);

        fread(&imagedata[(x * height + y)*4+2], sizeof(char), 1, fp);

        int r = imagedata[(x * height + y)*4+2];
        int b = imagedata[(x * height + y)*4+0];
        imagedata[(x * height + y)*4+0] = r;
        imagedata[(x * height + y)*4+2] = b;

        if(imagedata[(x * height + y)*4+0] == 255 &&
            imagedata[(x * height + y)*4+1] == 0 &&
           imagedata[(x * height + y)*4+2] == 255)
        imagedata[(x * height + y)*4+3] = 0;
        else
        imagedata[(x * height + y)*4+3] = 255;
        
    }
    }

    // create your texture here

    delete[] imagedata;
    return true;
}

/* End bitmap loading code */