BMPLoader.cpp
上传用户:arsena_zhu
上传日期:2022-07-12
资源大小:399k
文件大小:3k
- #include "main.h"
- ////////////////////////////////////////////////////////////////////////////////
- /*
- bool LoadBMP(BMPTexture *bmptex, char *filename);
- int LoadBMPIntoGL(char *filename); // LoadBMPData() and returns GenerateTexture()
- bool LoadBMPData(BMPTexture *t); // loads data into BMPTexture
- int GenerateTexture(BMPTexture *t); // returns ID of opengl texture
- */
- ////////////////////////////////////////////////////////////////////////////////
- bool LoadBMP(BMPTexture *bmptex, char *filename)
- {
- strcpy(bmptex->filename, filename);
- if (!LoadBMPData(bmptex))
- {
- MessageBox(NULL, "error loading bmp data", "?", MB_OK);
- return false;
- }
- bmptex->width = bmptex->infoHeader.biWidth;
- bmptex->height = bmptex->infoHeader.biHeight;
- bmptex->bpp = bmptex->infoHeader.biBitCount;
- bmptex->size = bmptex->width * bmptex->height;
- return true;
- }
- GLuint LoadBMPIntoGL(char *filename)
- {
- BMPTexture texture;
- if (!LoadBMP(&texture, filename)) return 0;
- int id = GenerateTexture(&texture);
- free(texture.image);
- return id;
- }
- int GenerateTexture(BMPTexture *t)
- {
- // give pointer to id holder
- glGenTextures(1, &t->texID);
- // bind texture
- glBindTexture(GL_TEXTURE_2D, t->texID);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, t->width, t->height, GL_RGB, GL_UNSIGNED_BYTE, t->image);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, t->width, t->height, 0, GL_RGB, GL_UNSIGNED_BYTE, t->image);
- return t->texID;
- }
- bool LoadBMPData(BMPTexture *t)
- {
- // null pointers
- t->file = NULL; t->image = NULL;
- // open file
- t->file = fopen(t->filename, "rb");
- // if not opened, then return false
- if(t->file == NULL) return false;
- // load header
- fread(&t->fileHeader, sizeof(BITMAPFILEHEADER), 1, t->file);
-
- // check if it is valid bmp file
- if(t->fileHeader.bfType != BITMAP_ID)
- {
- MessageBox(NULL, "BMP is probably corupted or stuff", "invalid BMP", MB_OK);
- fclose(t->file);
- return false;
- }
- // load info header
- fread(&t->infoHeader, sizeof(BITMAPINFOHEADER), 1, t->file);
- t->size = t->infoHeader.biWidth * t->infoHeader.biHeight;
- // set pointer to data start;
- fseek(t->file, t->fileHeader.bfOffBits, SEEK_SET);
- // allocate memory
- t->image = new unsigned char[t->size];
- // finally load image
- fread(t->image, 1, t->size, t->file);
- if(t->image == NULL) { MessageBox(NULL, "", "img loading failed", MB_OK); fclose(t->file); return false; }
- // close file
- fclose(t->file);
- // return true, because everything is ok
- return true;
- }