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.

75 lines
1.8 KiB

4 years ago
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include "utils.h"
  4. char* LoadFileContent(const char*path)
  5. {
  6. char *pFileContent = NULL;
  7. FILE*pFile = fopen(path, "rb");
  8. if (pFile)
  9. {
  10. fseek(pFile, 0, SEEK_END);
  11. int nLen = ftell(pFile);
  12. if (nLen > 0)
  13. {
  14. rewind(pFile);
  15. pFileContent = new char[nLen + 1];
  16. fread(pFileContent, 1, nLen, pFile);
  17. pFileContent[nLen] = '\0';
  18. }
  19. fclose(pFile);
  20. }
  21. return pFileContent;
  22. }
  23. unsigned char* LoadBMP(const char*path, int &width, int &height)
  24. {
  25. unsigned char*imageData = nullptr;
  26. FILE *pFile = fopen(path, "rb");
  27. if (pFile)
  28. {
  29. BITMAPFILEHEADER bfh;
  30. fread(&bfh, sizeof(BITMAPFILEHEADER), 1, pFile);
  31. if (bfh.bfType == 0x4D42)
  32. {
  33. BITMAPINFOHEADER bih;
  34. fread(&bih, sizeof(BITMAPINFOHEADER), 1, pFile);
  35. width = bih.biWidth;
  36. height = bih.biHeight;
  37. int pixelCount = width*height * 3;
  38. imageData = new unsigned char[pixelCount];
  39. fseek(pFile, bfh.bfOffBits, SEEK_SET);
  40. fread(imageData, 1, pixelCount, pFile);
  41. unsigned char temp;
  42. for (int i = 0; i < pixelCount; i += 3)
  43. {
  44. temp = imageData[i + 2];
  45. imageData[i + 2] = imageData[i];
  46. imageData[i] = temp;
  47. }
  48. }
  49. fclose(pFile);
  50. }
  51. return imageData;
  52. }
  53. GLuint CreateTextureFromFile(const char*filePath)
  54. {
  55. unsigned char*imageData;
  56. int width, height;
  57. imageData = LoadBMP(filePath, width, height);
  58. GLuint texture;
  59. glGenTextures(1, &texture);
  60. glBindTexture(GL_TEXTURE_2D, texture);
  61. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  62. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  63. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  64. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  65. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
  66. glBindTexture(GL_TEXTURE_2D, 0);
  67. delete imageData;
  68. return texture;
  69. }