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.

89 lines
3.9 KiB

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