BMPLoader.cpp
上传用户:arsena_zhu
上传日期:2022-07-12
资源大小:399k
文件大小:3k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #include "main.h"
  2. ////////////////////////////////////////////////////////////////////////////////
  3. /*
  4. bool LoadBMP(BMPTexture *bmptex, char *filename);
  5. int LoadBMPIntoGL(char *filename); // LoadBMPData() and returns GenerateTexture()
  6. bool LoadBMPData(BMPTexture *t); // loads data into BMPTexture
  7. int GenerateTexture(BMPTexture *t); // returns ID of opengl texture
  8. */
  9. ////////////////////////////////////////////////////////////////////////////////
  10. bool LoadBMP(BMPTexture *bmptex, char *filename)
  11. {
  12. strcpy(bmptex->filename, filename);
  13. if (!LoadBMPData(bmptex))
  14. {
  15. MessageBox(NULL, "error loading bmp data", "?", MB_OK);
  16. return false;
  17. }
  18. bmptex->width = bmptex->infoHeader.biWidth;
  19. bmptex->height = bmptex->infoHeader.biHeight;
  20. bmptex->bpp = bmptex->infoHeader.biBitCount;
  21. bmptex->size = bmptex->width * bmptex->height;
  22. return true;
  23. }
  24. GLuint LoadBMPIntoGL(char *filename)
  25. {
  26. BMPTexture texture;
  27. if (!LoadBMP(&texture, filename)) return 0;
  28. int id = GenerateTexture(&texture);
  29. free(texture.image);
  30. return id;
  31. }
  32. int GenerateTexture(BMPTexture *t)
  33. {
  34. // give pointer to id holder
  35. glGenTextures(1, &t->texID);
  36. // bind texture
  37. glBindTexture(GL_TEXTURE_2D, t->texID);
  38. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  39. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  40. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  41. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  43. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, t->width, t->height, GL_RGB, GL_UNSIGNED_BYTE, t->image);
  44. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, t->width, t->height, 0, GL_RGB, GL_UNSIGNED_BYTE, t->image);
  45. return t->texID;
  46. }
  47. bool LoadBMPData(BMPTexture *t)
  48. {
  49. // null pointers
  50. t->file = NULL; t->image = NULL;
  51. // open file
  52. t->file = fopen(t->filename, "rb");
  53. // if not opened, then return false
  54. if(t->file == NULL) return false;
  55. // load header
  56. fread(&t->fileHeader, sizeof(BITMAPFILEHEADER), 1, t->file);
  57. // check if it is valid bmp file
  58. if(t->fileHeader.bfType != BITMAP_ID)
  59. {
  60. MessageBox(NULL, "BMP is probably corupted or stuff", "invalid BMP", MB_OK);
  61. fclose(t->file);
  62. return false;
  63. }
  64. // load info header
  65. fread(&t->infoHeader, sizeof(BITMAPINFOHEADER), 1, t->file);
  66. t->size = t->infoHeader.biWidth * t->infoHeader.biHeight;
  67. // set pointer to data start;
  68. fseek(t->file, t->fileHeader.bfOffBits, SEEK_SET);
  69. // allocate memory
  70. t->image = new unsigned char[t->size];
  71. // finally load image
  72. fread(t->image, 1, t->size, t->file);
  73. if(t->image == NULL) { MessageBox(NULL, "", "img loading failed", MB_OK); fclose(t->file); return false; }
  74. // close file
  75. fclose(t->file);
  76. // return true, because everything is ok
  77. return true;
  78. }