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.

257 lines
6.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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. static unsigned char* DecodeBMPData(unsigned char* imgData, int &width, int &height) {
  100. //decode bmp
  101. int pixelDataOffset = *((int*)(imgData + 10));
  102. width = *((int*)(imgData + 18));
  103. height = *((int*)(imgData + 22));
  104. unsigned char* pixelData = imgData + pixelDataOffset;
  105. //bgr ת rgb
  106. for (int i = 0; i < width * height * 3; i += 3) {
  107. unsigned char tmp = pixelData[i + 2];
  108. pixelData[i + 2] = pixelData[i + 0];
  109. pixelData[i + 0] = tmp;
  110. }
  111. return pixelData;
  112. }
  113. GLuint CreateTextureFromBMP(const char * imagePath)
  114. {
  115. unsigned char* imgData = (unsigned char*)LoadFileContent(imagePath);
  116. if (*((unsigned short*)imgData) != 0x4D42) {
  117. printf("cannot decode %s\n", imagePath);
  118. return 0;
  119. }
  120. int width, height;
  121. unsigned char* pixelData = DecodeBMPData(imgData, width, height);
  122. GLuint texture;
  123. glGenTextures(1, &texture);
  124. glBindTexture(GL_TEXTURE_2D, texture);
  125. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  126. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  127. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  129. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
  130. glBindTexture(GL_TEXTURE_2D, 0);
  131. delete imgData;
  132. return texture;
  133. }
  134. static GLuint CreateTexture(int w, int h, const void* data, GLenum type)
  135. {
  136. GLuint texId;
  137. glGenTextures(1, &texId);
  138. glBindTexture(GL_TEXTURE_2D, texId);
  139. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  140. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  141. glTexImage2D(GL_TEXTURE_2D, 0, type, w, h, 0, type, GL_UNSIGNED_BYTE, data);
  142. return texId;
  143. }
  144. GLuint CreateTextureFromFile(const char * imagePath)
  145. {
  146. //1 ��ȡͼƬ��ʽ
  147. FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(imagePath, 0);
  148. if (fifmt == FIF_UNKNOWN)
  149. {
  150. printf("File %s not found! ", imagePath);
  151. return 0;
  152. }
  153. //2 ����ͼƬ
  154. FIBITMAP *dib = FreeImage_Load(fifmt, imagePath, 0);
  155. FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
  156. //! ��ȡ����ָ��
  157. FIBITMAP* temp = dib;
  158. dib = FreeImage_ConvertTo32Bits(dib);
  159. FreeImage_Unload(temp);
  160. BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
  161. int width = FreeImage_GetWidth(dib);
  162. int height = FreeImage_GetHeight(dib);
  163. for (int i = 0; i < width * height * 4; i += 4)
  164. {
  165. BYTE temp = pixels[i];
  166. pixels[i] = pixels[i + 2];
  167. pixels[i + 2] = temp;
  168. }
  169. GLuint res = CreateTexture(width, height, pixels, GL_RGBA);
  170. FreeImage_Unload(dib);
  171. return res;
  172. }
  173. const unsigned long FORMAT_DXT1 = 0x31545844l; // ������ʵDXT1������ascii��
  174. static unsigned char* DecodeDXT1Data(unsigned char* imgData, int &width, int &height, int &pixelSize) {
  175. height = *((int*)(imgData + sizeof(unsigned long) * 3));
  176. width = *((int*)(imgData + sizeof(unsigned long) * 4));
  177. pixelSize = *((int*)(imgData + sizeof(unsigned long) * 5));
  178. unsigned long compressFormat;
  179. compressFormat = *((int*)(imgData + sizeof(unsigned long) * 21));
  180. switch (compressFormat) {
  181. case FORMAT_DXT1:
  182. printf("decode dxt1\n");
  183. break;
  184. }
  185. unsigned char* pixelData = new unsigned char[pixelSize];
  186. memcpy(pixelData, imgData + sizeof(unsigned long) * 32, pixelSize);
  187. return pixelData;
  188. }
  189. GLuint CreateTextureFromDds(const char * imagePath)
  190. {
  191. unsigned char* imgData = (unsigned char*)LoadFileContent(imagePath);
  192. if (memcmp(imgData, "DDS ", 4) != 0) {
  193. printf("cannot decode %s\n", imagePath);
  194. return 0;
  195. }
  196. int width, height;
  197. int pixelSize = 0;
  198. unsigned char* pixelData = DecodeDXT1Data(imgData, width, height, pixelSize);
  199. GLuint texture;
  200. glGenTextures(1, &texture);
  201. glBindTexture(GL_TEXTURE_2D, texture);
  202. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  203. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  204. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  205. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  206. //GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
  207. glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, pixelSize, pixelData);
  208. glBindTexture(GL_TEXTURE_2D, 0);
  209. delete imgData;
  210. return texture;
  211. }