You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.7 KiB

4 years ago
  1. #include "utils.h"
  2. unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height) {
  3. if (0x4D42 == *((unsigned short*)bmpFileData)) {
  4. int pixelDataOffset = *((int*)(bmpFileData + 10));
  5. width = *((int*)(bmpFileData + 18));
  6. height = *((int*)(bmpFileData + 22));
  7. unsigned char*pixelData = bmpFileData + pixelDataOffset;
  8. for (int i = 0; i < width*height * 3; i += 3) {
  9. unsigned char temp = pixelData[i];
  10. pixelData[i] = pixelData[i + 2];
  11. pixelData[i + 2] = temp;
  12. }
  13. return pixelData;
  14. }
  15. return nullptr;
  16. }
  17. GLuint CreateTexture2D(unsigned char*pixelData, int width, int height, GLenum type) {
  18. GLuint texture;
  19. glGenTextures(1, &texture);
  20. glBindTexture(GL_TEXTURE_2D, texture);
  21. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  22. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  23. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  25. glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData);
  26. glBindTexture(GL_TEXTURE_2D, 0);
  27. return texture;
  28. }
  29. GLuint CreateTexture2DFromBMP(const char*bmpPath) {
  30. int nFileSize = 0;
  31. unsigned char *bmpFileContent = LoadFileContent(bmpPath, nFileSize);
  32. if (bmpFileContent == nullptr) {
  33. return 0;
  34. }
  35. int bmpWidth = 0, bmpHeight = 0;
  36. unsigned char*pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);
  37. if (bmpWidth == 0) {
  38. delete bmpFileContent;
  39. return 0;
  40. }
  41. GLuint texture = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB);
  42. delete bmpFileContent;
  43. return texture;
  44. }
  45. GLuint CreateTextureCubeFromBMP(const char *front, const char *back, const char *left,
  46. const char *right, const char *top, const char *bottom) {
  47. GLuint texture;
  48. glGenTextures(1, &texture);
  49. glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
  50. int nFileSize = 0, width = 0, height = 0;
  51. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  52. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  53. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  54. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  55. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  56. unsigned char *bmpFileContent = LoadFileContent(front, nFileSize);
  57. unsigned char*pixelData = DecodeBMP(bmpFileContent, width, height);
  58. glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  59. delete bmpFileContent;
  60. bmpFileContent = LoadFileContent(back, nFileSize);
  61. pixelData = DecodeBMP(bmpFileContent, width, height);
  62. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  63. delete bmpFileContent;
  64. bmpFileContent = LoadFileContent(left, nFileSize);
  65. pixelData = DecodeBMP(bmpFileContent, width, height);
  66. glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  67. delete bmpFileContent;
  68. bmpFileContent = LoadFileContent(right, nFileSize);
  69. pixelData = DecodeBMP(bmpFileContent, width, height);
  70. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  71. delete bmpFileContent;
  72. bmpFileContent = LoadFileContent(top, nFileSize);
  73. pixelData = DecodeBMP(bmpFileContent, width, height);
  74. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  75. delete bmpFileContent;
  76. bmpFileContent = LoadFileContent(bottom, nFileSize);
  77. pixelData = DecodeBMP(bmpFileContent, width, height);
  78. glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  79. glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
  80. delete bmpFileContent;
  81. return texture;
  82. }