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.

97 lines
3.2 KiB

4 years ago
  1. #include "utils.h"
  2. /**
  3. * һshader
  4. */
  5. GLuint CompileShader(GLenum shaderType, const char * shaderCode)
  6. {
  7. GLuint shader = glCreateShader(shaderType);//���ݸ�����shader���ʹ���shader
  8. glShaderSource(shader, 1, &shaderCode, nullptr);//1��ʾshaderֻ��һ�д���
  9. glCompileShader(shader);//����shader
  10. //�ж�shader�ǵı����ɹ�
  11. GLint compileResult = GL_TRUE;
  12. glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
  13. if (compileResult == GL_FALSE) {
  14. char szLog[1024] = { 0 };
  15. GLsizei logLen = 0;
  16. glGetShaderInfoLog(shader, 1024, &logLen, szLog);
  17. printf("Compile Shader fail error log : %s \nshader code :\n%s\n", szLog, shaderCode);
  18. glDeleteShader(shader);
  19. shader = 0;
  20. }
  21. return shader;
  22. }
  23. /**
  24. * ѱõvsShaderfsShaderӴһshader
  25. */
  26. GLuint CreateProgram(GLuint vsShader, GLuint fsShader)
  27. {
  28. GLuint program = glCreateProgram();//�ȴ���һ��shader����
  29. glAttachShader(program, vsShader);//�󶨱����õ�shader
  30. glAttachShader(program, fsShader);
  31. glLinkProgram(program);//���ӳ�һ������
  32. //�ж������Ƿ��ɹ�
  33. GLint nResult = GL_TRUE;
  34. glGetProgramiv(program, GL_LINK_STATUS, &nResult);
  35. if (nResult == GL_FALSE) {
  36. char log[1024] = { 0 };
  37. GLsizei writed = 0;
  38. glGetProgramInfoLog(program, 1024, &writed, log);
  39. printf("create gpu program fail,link error : %s\n", log);
  40. glDeleteProgram(program);
  41. program = 0;
  42. }
  43. return program;
  44. }
  45. unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height) {
  46. if (0x4D42 == *((unsigned short*)bmpFileData)) {
  47. int pixelDataOffset = *((int*)(bmpFileData + 10));
  48. width = *((int*)(bmpFileData + 18));
  49. height = *((int*)(bmpFileData + 22));
  50. unsigned char*pixelData = bmpFileData + pixelDataOffset;
  51. for (int i = 0; i < width*height * 3; i += 3) {
  52. unsigned char temp = pixelData[i];
  53. pixelData[i] = pixelData[i + 2];
  54. pixelData[i + 2] = temp;
  55. }
  56. return pixelData;
  57. }
  58. return nullptr;
  59. }
  60. GLuint CreateTexture2D(unsigned char*pixelData, int width, int height, GLenum type) {
  61. GLuint texture;
  62. glGenTextures(1, &texture);
  63. glBindTexture(GL_TEXTURE_2D, texture);
  64. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  65. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  66. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  67. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  68. glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData);
  69. glBindTexture(GL_TEXTURE_2D, 0);
  70. return texture;
  71. }
  72. GLuint CreateTexture2DFromBMP(const char*bmpPath) {
  73. int nFileSize = 0;
  74. unsigned char *bmpFileContent = LoadFileContent(bmpPath, nFileSize);
  75. if (bmpFileContent == nullptr) {
  76. return 0;
  77. }
  78. int bmpWidth = 0, bmpHeight = 0;
  79. unsigned char*pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);
  80. if (bmpWidth == 0) {
  81. delete bmpFileContent;
  82. return 0;
  83. }
  84. GLuint texture = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB);
  85. delete bmpFileContent;
  86. return texture;
  87. }
  88. GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void*data /* = nullptr */) {
  89. GLuint object;
  90. glGenBuffers(1, &object);
  91. glBindBuffer(bufferType, object);
  92. glBufferData(bufferType, size, data, usage);
  93. glBindBuffer(bufferType, 0);
  94. return object;
  95. }