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.

79 lines
2.0 KiB

5 years ago
  1. #include "utils.h"
  2. unsigned char * DecodeBMP(unsigned char * bmpFileData, int & width, int & height)
  3. {
  4. if (0x4D42 == *((unsigned short*)bmpFileData)) {//ͨ��ǰ�����ֽ�ȥ�ж������Ƿ���BMP����
  5. int pixelDataOffset = *((int*)(bmpFileData + 10));//ȡ��λͼ�������ݵ�ƫ�Ƶ�ַ��
  6. width = *((int*)(bmpFileData + 18));//��ȡͼƬ����
  7. height = *((int*)(bmpFileData + 22));//��ȡͼƬ����
  8. /*��ȡͼƬ����*/
  9. unsigned char* pixelData = bmpFileData + pixelDataOffset;
  10. for (int i = 0; i < width * height * 3; i += 3) {
  11. unsigned char temp = pixelData[i];
  12. //BGR ת���� RGB
  13. pixelData[i] = pixelData[i + 2];
  14. pixelData[i + 2] = temp;
  15. }
  16. return pixelData;
  17. }
  18. return nullptr;
  19. }
  20. GLuint CreateTexture2D(unsigned char * pixelData, int width, int height, GLenum type)
  21. {
  22. GLuint texture;
  23. glGenTextures(1, &texture);
  24. //��������
  25. glBindTexture(GL_TEXTURE_2D, texture);
  26. //���������Ŵ�/��Сʱ�����ݲɼ����㷨
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//ʹ�����Թ���
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//ʹ�����Թ���
  29. //���õ�������������1.0ʱ�������ݲɼ���ʽ
  30. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  31. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  32. //�����ϴ����Կ�
  33. glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData);
  34. //�ͷ�����
  35. glBindTexture(GL_TEXTURE_2D, 0);
  36. return texture;
  37. }
  38. GLuint CreateTexture2DFromBMP(const char * bmpPath)
  39. {
  40. int nFileSize = 0;
  41. unsigned char* bmpFileContent = LoadFileContent(bmpPath, nFileSize);
  42. if (bmpFileContent == nullptr) {
  43. return 0;
  44. }
  45. int bmpWidth = 0;
  46. int bmpHeight = 0;
  47. unsigned char* pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);
  48. if (bmpWidth == 0) {
  49. delete bmpFileContent;
  50. return 0;
  51. }
  52. GLuint textrue = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB);
  53. return textrue;
  54. }
  55. GLuint CreateDisplayList(std::function<void()> foo)
  56. {
  57. GLuint displayList = glGenLists(1);//����һ����ʾ�б�
  58. glNewList(displayList, GL_COMPILE);
  59. foo();
  60. glEndList();
  61. return displayList;
  62. }