Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GLuint loadBMP( char *filename ){//code (mostly) from www.opengl-tutorial.org
- char header[54];//header is always 54 bytes
- FILE *texture = fopen(filename, "rb");//open the texture
- if(!texture){//check if opened
- cerr<<"Bitmap "<<filename<<" could not be opened\n";
- return GL_FALSE;
- }
- if ( fread(header, 1, 54, texture)!=54 ){ //read in the header
- cerr<<"Bitmap "<<filename<<" is corrupt or invalid\n";
- return GL_FALSE;
- }
- if(header[0] != 'B' || header[1] != 'M'){
- cerr<<"Bitmap "<<filename<<" is invalid\n";
- return GL_FALSE;
- }
- //read in bitmap information
- int position = *(int*)&(header[0x0A]);
- int size = *(int*)&(header[0x22]);
- int width = *(int*)&(header[0x12]);
- int height = *(int*)&(header[0x16]);
- if(size == 0){//guess size if not given, assumes 24 bpp & no padding
- size = width*height*3;
- }
- if(position == 0){//guess start of map if not given(assumes normal header of 54 bits
- position = 54;
- }
- char *bufferData = (char*)malloc(sizeof(char)* size);
- if(bufferData == NULL){
- cerr<<"error, unable to buffer bitmap: "<<filename<<endl;
- }
- fread(bufferData,1,size,texture);//read the map from the open texture
- fclose(texture);
- GLuint textureID;//handle for new texture
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexImage2D(GL_TEXTURE_2D, //mode
- 0, //reduction level
- GL_RGB, //color components
- width, height,
- 0, //border width
- GL_BGR, //format
- GL_UNSIGNED_BYTE, //data type
- bufferData //texture data
- );
- free(bufferData);
- return textureID;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement