gl_utils.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "gl_utils.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. void __check_gl(const char *file, int line)
  7. {
  8. GLenum error = glGetError();
  9. const char *name;
  10. switch (error) {
  11. case GL_NO_ERROR:
  12. return; // no error
  13. case GL_INVALID_ENUM:
  14. name = "GL_INVALID_ENUM";
  15. break;
  16. case GL_INVALID_VALUE:
  17. name = "GL_INVALID_VALUE";
  18. break;
  19. case GL_INVALID_OPERATION:
  20. name = "GL_INVALID_OPERATION";
  21. break;
  22. case GL_INVALID_FRAMEBUFFER_OPERATION:
  23. name = "GL_INVALID_FRAMEBUFFER_OPERATION";
  24. break;
  25. case GL_OUT_OF_MEMORY:
  26. name = "GL_OUT_OF_MEMORY";
  27. break;
  28. default:
  29. name = "UNKNOWN ERROR!";
  30. break;
  31. }
  32. printf("GL error at %s:%d - %s\n", file, line, name);
  33. }
  34. const GLfloat gl_quad_vertices[] = {
  35. -1, -1,
  36. 1, -1,
  37. -1, 1,
  38. 1, 1,
  39. };
  40. const GLfloat gl_quad_texcoords[] = {
  41. 0, 0,
  42. 1, 0,
  43. 0, 1,
  44. 1, 1,
  45. };
  46. GLuint
  47. gl_load_shader(const char *path, GLenum type)
  48. {
  49. check_gl();
  50. FILE *f = fopen(path, "r");
  51. fseek(f, 0, SEEK_END);
  52. GLint size = ftell(f);
  53. char *source = malloc(sizeof(char) * size);
  54. fseek(f, 0, SEEK_SET);
  55. if (fread(source, size, 1, f) == 0) {
  56. printf("Failed to read shader file\n");
  57. return 0;
  58. }
  59. GLuint shader = glCreateShader(type);
  60. assert(shader != 0);
  61. glShaderSource(shader, 1, (const GLchar * const*)&source, &size);
  62. glCompileShader(shader);
  63. check_gl();
  64. // Check compile status
  65. GLint success;
  66. glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
  67. if (success == GL_FALSE) {
  68. printf("Shader compilation failed for %s\n", path);
  69. }
  70. GLint log_length;
  71. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
  72. if (log_length > 0) {
  73. char *log = malloc(sizeof(char) * log_length);
  74. glGetShaderInfoLog(shader, log_length - 1, &log_length, log);
  75. printf("Shader %s log: %s\n", path, log);
  76. free(log);
  77. }
  78. free(source);
  79. return shader;
  80. }
  81. GLuint
  82. gl_link_program(GLuint *shaders, size_t num_shaders)
  83. {
  84. GLuint program = glCreateProgram();
  85. for (size_t i = 0; i < num_shaders; ++i) {
  86. glAttachShader(program, shaders[i]);
  87. }
  88. glLinkProgram(program);
  89. check_gl();
  90. GLint success;
  91. glGetProgramiv(program, GL_LINK_STATUS, &success);
  92. if (success == GL_FALSE) {
  93. printf("Program linking failed\n");
  94. }
  95. GLint log_length;
  96. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
  97. if (log_length > 0) {
  98. char *log = malloc(sizeof(char) * log_length);
  99. glGetProgramInfoLog(program, log_length - 1, &log_length, log);
  100. printf("Program log: %s\n", log);
  101. free(log);
  102. }
  103. check_gl();
  104. return program;
  105. }