main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <stdbool.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <math.h>
  9. #include <GLFW/glfw3.h>
  10. #define DEFAULT_SCREEN_WIDTH 1600
  11. #define DEFAULT_SCREEN_HEIGHT 900
  12. #define MANUAL_TIME_STEP 0.1
  13. #include "glextloader.c"
  14. char *slurp_file(const char *file_path)
  15. {
  16. #define SLURP_FILE_PANIC \
  17. do { \
  18. fprintf(stderr, "Could not read file `%s`: %s\n", file_path, strerror(errno)); \
  19. exit(1); \
  20. } while (0)
  21. FILE *f = fopen(file_path, "r");
  22. if (f == NULL) SLURP_FILE_PANIC;
  23. if (fseek(f, 0, SEEK_END) < 0) SLURP_FILE_PANIC;
  24. long size = ftell(f);
  25. if (size < 0) SLURP_FILE_PANIC;
  26. char *buffer = malloc(size + 1);
  27. if (buffer == NULL) SLURP_FILE_PANIC;
  28. if (fseek(f, 0, SEEK_SET) < 0) SLURP_FILE_PANIC;
  29. fread(buffer, 1, size, f);
  30. if (ferror(f) < 0) SLURP_FILE_PANIC;
  31. buffer[size] = '\0';
  32. if (fclose(f) < 0) SLURP_FILE_PANIC;
  33. return buffer;
  34. #undef SLURP_FILE_PANIC
  35. }
  36. const char *shader_type_as_cstr(GLuint shader)
  37. {
  38. switch (shader) {
  39. case GL_VERTEX_SHADER:
  40. return "GL_VERTEX_SHADER";
  41. case GL_FRAGMENT_SHADER:
  42. return "GL_FRAGMENT_SHADER";
  43. default:
  44. return "(Unknown)";
  45. }
  46. }
  47. bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader)
  48. {
  49. *shader = glCreateShader(shader_type);
  50. glShaderSource(*shader, 1, &source, NULL);
  51. glCompileShader(*shader);
  52. GLint compiled = 0;
  53. glGetShaderiv(*shader, GL_COMPILE_STATUS, &compiled);
  54. if (!compiled) {
  55. GLchar message[1024];
  56. GLsizei message_size = 0;
  57. glGetShaderInfoLog(*shader, sizeof(message), &message_size, message);
  58. fprintf(stderr, "ERROR: could not compile %s\n", shader_type_as_cstr(shader_type));
  59. fprintf(stderr, "%.*s\n", message_size, message);
  60. return false;
  61. }
  62. return true;
  63. }
  64. bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader)
  65. {
  66. char *source = slurp_file(file_path);
  67. bool ok = compile_shader_source(source, shader_type, shader);
  68. if (!ok) {
  69. fprintf(stderr, "ERROR: failed to compile `%s` shader file\n", file_path);
  70. }
  71. free(source);
  72. return ok;
  73. }
  74. bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
  75. {
  76. *program = glCreateProgram();
  77. glAttachShader(*program, vert_shader);
  78. glAttachShader(*program, frag_shader);
  79. glLinkProgram(*program);
  80. GLint linked = 0;
  81. glGetProgramiv(*program, GL_LINK_STATUS, &linked);
  82. if (!linked) {
  83. GLsizei message_size = 0;
  84. GLchar message[1024];
  85. glGetProgramInfoLog(*program, sizeof(message), &message_size, message);
  86. fprintf(stderr, "Program Linking: %.*s\n", message_size, message);
  87. }
  88. glDeleteShader(vert_shader);
  89. glDeleteShader(frag_shader);
  90. return program;
  91. }
  92. typedef enum {
  93. RESOLUTION_UNIFORM = 0,
  94. TIME_UNIFORM,
  95. MOUSE_UNIFORM,
  96. COUNT_UNIFORMS
  97. } Uniform;
  98. static_assert(COUNT_UNIFORMS == 3, "Update list of uniform names");
  99. static const char *uniform_names[COUNT_UNIFORMS] = {
  100. [RESOLUTION_UNIFORM] = "resolution",
  101. [TIME_UNIFORM] = "time",
  102. [MOUSE_UNIFORM] = "mouse",
  103. };
  104. // Global variables (fragile people with CS degree look away)
  105. bool program_failed = false;
  106. double time = 0.0;
  107. GLuint main_program = 0;
  108. GLint main_uniforms[COUNT_UNIFORMS];
  109. bool pause = false;
  110. bool load_shader_program(const char *vertex_file_path,
  111. const char *fragment_file_path,
  112. GLuint *program)
  113. {
  114. GLuint vert = 0;
  115. if (!compile_shader_file(vertex_file_path, GL_VERTEX_SHADER, &vert)) {
  116. return false;
  117. }
  118. GLuint frag = 0;
  119. if (!compile_shader_file(fragment_file_path, GL_FRAGMENT_SHADER, &frag)) {
  120. return false;
  121. }
  122. if (!link_program(vert, frag, program)) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. void reload_shaders(void)
  128. {
  129. glDeleteProgram(main_program);
  130. program_failed = true;
  131. glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
  132. {
  133. if (!load_shader_program("main.vert", "main.frag", &main_program)) {
  134. return;
  135. }
  136. glUseProgram(main_program);
  137. for (Uniform index = 0; index < COUNT_UNIFORMS; ++index) {
  138. main_uniforms[index] = glGetUniformLocation(main_program, uniform_names[index]);
  139. }
  140. }
  141. program_failed = false;
  142. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  143. printf("Successfully Reload the Shaders\n");
  144. }
  145. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  146. {
  147. (void) window;
  148. (void) scancode;
  149. (void) action;
  150. (void) mods;
  151. if (action == GLFW_PRESS) {
  152. if (key == GLFW_KEY_F5) {
  153. reload_shaders();
  154. } else if (key == GLFW_KEY_SPACE) {
  155. pause = !pause;
  156. } else if (key == GLFW_KEY_Q) {
  157. exit(1);
  158. }
  159. if (pause) {
  160. if (key == GLFW_KEY_LEFT) {
  161. time -= MANUAL_TIME_STEP;
  162. } else if (key == GLFW_KEY_RIGHT) {
  163. time += MANUAL_TIME_STEP;
  164. }
  165. }
  166. }
  167. }
  168. void window_size_callback(GLFWwindow* window, int width, int height)
  169. {
  170. (void) window;
  171. glViewport(0, 0, width, height);
  172. }
  173. void MessageCallback(GLenum source,
  174. GLenum type,
  175. GLuint id,
  176. GLenum severity,
  177. GLsizei length,
  178. const GLchar* message,
  179. const void* userParam)
  180. {
  181. (void) source;
  182. (void) id;
  183. (void) length;
  184. (void) userParam;
  185. fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
  186. (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
  187. type, severity, message);
  188. }
  189. int main()
  190. {
  191. if (!glfwInit()) {
  192. fprintf(stderr, "ERROR: could not initialize GLFW\n");
  193. exit(1);
  194. }
  195. GLFWwindow * const window = glfwCreateWindow(
  196. DEFAULT_SCREEN_WIDTH,
  197. DEFAULT_SCREEN_HEIGHT,
  198. "OpenGL Template",
  199. NULL,
  200. NULL);
  201. if (window == NULL) {
  202. fprintf(stderr, "ERROR: could not create a window.\n");
  203. glfwTerminate();
  204. exit(1);
  205. }
  206. glfwMakeContextCurrent(window);
  207. load_gl_extensions();
  208. if (glDrawArraysInstanced == NULL) {
  209. fprintf(stderr, "Support for EXT_draw_instanced is required!\n");
  210. exit(1);
  211. }
  212. if (glDebugMessageCallback != NULL) {
  213. glEnable(GL_DEBUG_OUTPUT);
  214. glDebugMessageCallback(MessageCallback, 0);
  215. }
  216. glEnable(GL_BLEND);
  217. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  218. reload_shaders();
  219. glfwSetKeyCallback(window, key_callback);
  220. glfwSetFramebufferSizeCallback(window, window_size_callback);
  221. time = glfwGetTime();
  222. double prev_time;
  223. while (!glfwWindowShouldClose(window)) {
  224. glClear(GL_COLOR_BUFFER_BIT);
  225. if (!program_failed) {
  226. static_assert(COUNT_UNIFORMS == 3, "Update the uniform sync");
  227. int width, height;
  228. glfwGetWindowSize(window, &width, &height);
  229. glUniform2f(main_uniforms[RESOLUTION_UNIFORM], width, height);
  230. glUniform1f(main_uniforms[TIME_UNIFORM], time);
  231. double xpos, ypos;
  232. glfwGetCursorPos(window, &xpos, &ypos);
  233. glUniform2f(main_uniforms[MOUSE_UNIFORM], xpos, height - ypos);
  234. glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, 1);
  235. }
  236. glfwSwapBuffers(window);
  237. glfwPollEvents();
  238. double cur_time = glfwGetTime();
  239. if (!pause) {
  240. time += cur_time - prev_time;
  241. }
  242. prev_time = cur_time;
  243. }
  244. return 0;
  245. }