Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. GLuint loadBMP( char *filename ){//code (mostly) from www.opengl-tutorial.org
  2.  
  3.    char header[54];//header is always 54 bytes
  4.  
  5.    FILE *texture = fopen(filename, "rb");//open the texture
  6.  
  7.  
  8.    if(!texture){//check if opened
  9.       cerr<<"Bitmap "<<filename<<" could not be opened\n";
  10.       return GL_FALSE;
  11.    }
  12.  
  13.    if ( fread(header, 1, 54, texture)!=54 ){ //read in the header
  14.       cerr<<"Bitmap "<<filename<<" is corrupt or invalid\n";
  15.       return GL_FALSE;
  16.    }
  17.  
  18.  
  19.    if(header[0] != 'B' || header[1] != 'M'){
  20.       cerr<<"Bitmap "<<filename<<" is invalid\n";
  21.       return GL_FALSE;
  22.    }
  23.  
  24.    //read in bitmap information
  25.    int position   = *(int*)&(header[0x0A]);
  26.    int size       = *(int*)&(header[0x22]);
  27.    int width      = *(int*)&(header[0x12]);
  28.    int height     = *(int*)&(header[0x16]);
  29.  
  30.    if(size == 0){//guess size if not given, assumes 24 bpp & no padding
  31.       size = width*height*3;
  32.    }
  33.  
  34.    if(position == 0){//guess start of map if not given(assumes normal header of 54 bits
  35.       position = 54;
  36.    }
  37.  
  38.    char *bufferData = (char*)malloc(sizeof(char)* size);
  39.    if(bufferData == NULL){
  40.       cerr<<"error, unable to buffer bitmap: "<<filename<<endl;
  41.    }
  42.  
  43.    fread(bufferData,1,size,texture);//read the map from the open texture
  44.    fclose(texture);
  45.  
  46.    GLuint textureID;//handle for new texture
  47.    glGenTextures(1, &textureID);
  48.  
  49.    glBindTexture(GL_TEXTURE_2D, textureID);
  50.  
  51.    glTexImage2D(GL_TEXTURE_2D,      //mode
  52.                 0,                  //reduction level
  53.                 GL_RGB,                  //color components
  54.                 width, height,
  55.                 0,                  //border width
  56.                 GL_BGR,             //format
  57.                 GL_UNSIGNED_BYTE,   //data type
  58.                 bufferData          //texture data
  59.                );
  60.  
  61.    free(bufferData);
  62.  
  63.    return textureID;
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement