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.

124 lines
4.1 KiB

4 years ago
4 years ago
4 years ago
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. glDetachShader(program, vsShader);
  33. glDetachShader(program, fsShader);
  34. GLint nResult;
  35. glGetProgramiv(program, GL_LINK_STATUS, &nResult);
  36. if (nResult == GL_FALSE) {
  37. char log[1024] = { 0 };
  38. GLsizei writed = 0;
  39. glGetProgramInfoLog(program, 1024, &writed, log);
  40. printf("create gpu program fail,link error : %s\n", log);
  41. glDeleteProgram(program);
  42. program = 0;
  43. }
  44. return program;
  45. }
  46. unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height) {
  47. if (0x4D42 == *((unsigned short*)bmpFileData)) {
  48. int pixelDataOffset = *((int*)(bmpFileData + 10));
  49. width = *((int*)(bmpFileData + 18));
  50. height = *((int*)(bmpFileData + 22));
  51. unsigned char*pixelData = bmpFileData + pixelDataOffset;
  52. for (int i = 0; i < width*height * 3; i += 3) {
  53. unsigned char temp = pixelData[i];
  54. pixelData[i] = pixelData[i + 2];
  55. pixelData[i + 2] = temp;
  56. }
  57. return pixelData;
  58. }
  59. return nullptr;
  60. }
  61. GLuint CreateTexture2D(unsigned char*pixelData, int width, int height, GLenum type) {
  62. GLuint texture;
  63. glGenTextures(1, &texture);
  64. glBindTexture(GL_TEXTURE_2D, texture);
  65. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  66. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  67. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  68. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  69. glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData);
  70. glBindTexture(GL_TEXTURE_2D, 0);
  71. return texture;
  72. }
  73. GLuint CreateTexture2DFromBMP(const char*bmpPath) {
  74. int nFileSize = 0;
  75. unsigned char *bmpFileContent = LoadFileContent(bmpPath, nFileSize);
  76. if (bmpFileContent == nullptr) {
  77. return 0;
  78. }
  79. int bmpWidth = 0, bmpHeight = 0;
  80. unsigned char*pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);
  81. if (bmpWidth == 0) {
  82. delete bmpFileContent;
  83. return 0;
  84. }
  85. GLuint texture = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB);
  86. delete bmpFileContent;
  87. return texture;
  88. }
  89. GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void*data /* = nullptr */) {
  90. GLuint object;
  91. glGenBuffers(1, &object);
  92. glBindBuffer(bufferType, object);
  93. glBufferData(bufferType, size, data, usage);
  94. glBindBuffer(bufferType, 0);
  95. return object;
  96. }
  97. GLuint CreateProcedureTexture(int size)
  98. {
  99. unsigned char *imageData = new unsigned char[size*size * 4];
  100. float halfSize = (float)size / 2.0f;
  101. float maxDistance = sqrtf(halfSize*halfSize + halfSize*halfSize);
  102. float centerX = halfSize;
  103. float centerY = halfSize;
  104. for (int y = 0; y < size; ++y) {
  105. for (int x = 0; x < size; ++x) {
  106. int currentPixelOffset = (x + y*size) * 4;
  107. imageData[currentPixelOffset] = 255;
  108. imageData[currentPixelOffset + 1] = 255;
  109. imageData[currentPixelOffset + 2] = 255;
  110. float deltaX = (float)x - centerX;
  111. float deltaY = (float)y - centerY;
  112. float distance = sqrtf(deltaX*deltaX + deltaY*deltaY);
  113. float alpha = powf(1.0f - (distance / maxDistance), 8.0f);
  114. alpha = alpha > 1.0f ? 1.0f : alpha;
  115. imageData[currentPixelOffset + 3] = (unsigned char)(alpha*255.0f);
  116. }
  117. }
  118. GLuint texture = CreateTexture2D(imageData, size, size, GL_RGBA);
  119. delete imageData;
  120. return texture;
  121. }