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.

198 lines
4.9 KiB

4 years ago
4 years ago
4 years ago
  1. #include "misc.h"
  2. #include "FreeImage.h"
  3. #include <stdio.h>
  4. #pragma comment(lib,"FreeImage.lib")
  5. GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void * data)
  6. {
  7. GLuint object;
  8. glGenBuffers(1, &object);
  9. glBindBuffer(bufferType, object);
  10. glBufferData(bufferType, size, data, usage);
  11. glBindBuffer(bufferType, 0);
  12. return object;
  13. }
  14. char* LoadFileContent(const char *path) {
  15. FILE* pFile = fopen(path, "rb");
  16. if (pFile) {
  17. fseek(pFile, 0, SEEK_END);
  18. int nLen = ftell(pFile);
  19. char* buffer = nullptr;
  20. if (nLen != 0) {
  21. buffer = new char[nLen + 1];
  22. rewind(pFile);
  23. fread(buffer, nLen, 1, pFile);
  24. buffer[nLen] = '\0';
  25. }
  26. else {
  27. printf("load file %s fail, content is 0\n", path);
  28. }
  29. fclose(pFile);
  30. return buffer;
  31. }
  32. else {
  33. printf("open file %s fail\n", path);
  34. }
  35. fclose(pFile);
  36. return nullptr;
  37. }
  38. GLuint CompileShader(GLenum shaderType, const char * shaderPath)
  39. {
  40. //����shader
  41. GLuint shader = glCreateShader(shaderType);
  42. if (shader == 0) {
  43. printf("glCreateShader false\n");
  44. return 0;
  45. }
  46. //��ȡshader����
  47. const char* shaderCode = LoadFileContent(shaderPath);
  48. if (shaderCode == nullptr) {
  49. printf("load shader code from file: %s false\n", shaderPath);
  50. return 0;
  51. }
  52. //��shader���� ���ڴ洫���Դ�
  53. glShaderSource(shader, 1, &shaderCode, nullptr);
  54. glCompileShader(shader);
  55. GLint compileResult = GL_TRUE;
  56. glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
  57. if (compileResult == GL_FALSE) {
  58. char szLog[1024] = { 0 };
  59. GLsizei logLen = 0;//ʵ�ʴ�����־����
  60. glGetShaderInfoLog(shader, 1024, &logLen, szLog);
  61. printf("Compile shader fail error log is : %s \n shader code :\n %s \n ", szLog, shaderCode);
  62. glDeleteShader(shader);
  63. return 0;
  64. }
  65. return shader;
  66. }
  67. /**
  68. * һGPU
  69. */
  70. GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) {
  71. GLuint vsShader = CompileShader(GL_VERTEX_SHADER,vsShaderPath);
  72. GLuint fsShader = CompileShader(GL_FRAGMENT_SHADER,fsShaderPath);
  73. //����program
  74. GLuint program = glCreateProgram();
  75. //����shader
  76. glAttachShader(program, vsShader);
  77. glAttachShader(program, fsShader);
  78. //����
  79. glLinkProgram(program);
  80. //����shader
  81. glDetachShader(program, vsShader);
  82. glDetachShader(program, fsShader);
  83. //ɾ��shader
  84. glDeleteShader(vsShader);
  85. glDeleteShader(fsShader);
  86. //��������
  87. GLint nResult;
  88. glGetProgramiv(program, GL_LINK_STATUS, &nResult);
  89. if (nResult == GL_FALSE) {
  90. char log[1024] = { 0 };
  91. GLsizei writed = 0;
  92. glGetProgramInfoLog(program, 1024, &writed, log);
  93. printf("Create CPU program fail error %s\n", log);
  94. glDeleteProgram(program);
  95. program = 0;
  96. }
  97. return program;
  98. }
  99. GLuint CreateTextureFromBMP(const char * imagePath)
  100. {
  101. unsigned char* imgData = (unsigned char*)LoadFileContent(imagePath);
  102. if (*((unsigned short*)imgData) != 0x4D42) {
  103. printf("cannot decode %s\n", imagePath);
  104. return 0;
  105. }
  106. //decode bmp
  107. int pixelDataOffset = *((int*)(imgData + 10));
  108. int width = *((int*)(imgData + 18));
  109. int height = *((int*)(imgData + 22));
  110. unsigned char* pixelData = imgData + pixelDataOffset;
  111. //bgr ת rgb
  112. for (int i = 0; i < width * height * 3; i+=3) {
  113. unsigned char tmp = pixelData[i + 2];
  114. pixelData[i + 2] = pixelData[i + 0];
  115. pixelData[i + 0] = tmp;
  116. }
  117. GLuint texture;
  118. glGenTextures(1, &texture);
  119. glBindTexture(GL_TEXTURE_2D, texture);
  120. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  121. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  122. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  123. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  124. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  125. glBindTexture(GL_TEXTURE_2D, 0);
  126. delete imgData;
  127. return texture;
  128. }
  129. GLuint CreateTexture(int w, int h, const void* data, GLenum type)
  130. {
  131. GLuint texId;
  132. glGenTextures(1, &texId);
  133. glBindTexture(GL_TEXTURE_2D, texId);
  134. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  135. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  136. glTexImage2D(GL_TEXTURE_2D, 0, type, w, h, 0, type, GL_UNSIGNED_BYTE, data);
  137. return texId;
  138. }
  139. GLuint CreateTextureFromFile(const char * imagePath)
  140. {
  141. //1 ��ȡͼƬ��ʽ
  142. FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(imagePath, 0);
  143. if (fifmt == FIF_UNKNOWN)
  144. {
  145. printf("File %s not found! ", imagePath);
  146. return 0;
  147. }
  148. //2 ����ͼƬ
  149. FIBITMAP *dib = FreeImage_Load(fifmt, imagePath, 0);
  150. FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
  151. //! ��ȡ����ָ��
  152. FIBITMAP* temp = dib;
  153. dib = FreeImage_ConvertTo32Bits(dib);
  154. FreeImage_Unload(temp);
  155. BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
  156. int width = FreeImage_GetWidth(dib);
  157. int height = FreeImage_GetHeight(dib);
  158. for (int i = 0; i < width * height * 4; i += 4)
  159. {
  160. BYTE temp = pixels[i];
  161. pixels[i] = pixels[i + 2];
  162. pixels[i + 2] = temp;
  163. }
  164. GLuint res = CreateTexture(width, height, pixels, GL_RGBA);
  165. FreeImage_Unload(dib);
  166. return res;
  167. }