2
0
rexim 4 жил өмнө
parent
commit
a53f56b505
2 өөрчлөгдсөн 21 нэмэгдсэн , 5 устгасан
  1. 5 2
      README.md
  2. 16 3
      main.c

+ 5 - 2
README.md

@@ -2,6 +2,9 @@
 
 
 Just a simple OpenGL template that I use on my streams.
 Just a simple OpenGL template that I use on my streams.
 
 
-## Shaders Hot Reloading
+## Controls
 
 
-Press <kbd>F5</kbd> to reload [main.frag](./main.frag) and [main.vert](./main.vert) shaders. Red screen indicates a compilation or linking error, check the output of the program if you see it.
+|Shortcut|Description|
+|---|
+|<kbd>F5</kbd>|Reload [main.frag](./main.frag) and [main.vert](./main.vert) shaders. Red screen indicates a compilation or linking error, check the output of the program if you see it.|
+|<kbd>SPACE</kbd>|Pause/unpause the time uniform variable in shaders|

+ 16 - 3
main.c

@@ -106,10 +106,12 @@ bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
     return program;
     return program;
 }
 }
 
 
+// Global variables (fragile people with CS degree look away)
 bool program_failed = false;
 bool program_failed = false;
 GLuint program = 0;
 GLuint program = 0;
 GLint resolution_location = 0;
 GLint resolution_location = 0;
 GLint time_location = 0;
 GLint time_location = 0;
+bool pause = false;
 
 
 void reload_shaders(void)
 void reload_shaders(void)
 {
 {
@@ -153,8 +155,12 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
     (void) action;
     (void) action;
     (void) mods;
     (void) mods;
 
 
-    if (key == GLFW_KEY_F5 && action == GLFW_PRESS) {
-        reload_shaders();
+    if (action == GLFW_PRESS) {
+        if (key == GLFW_KEY_F5) {
+            reload_shaders();
+        } else if (key == GLFW_KEY_SPACE) {
+            pause = !pause;
+        }
     }
     }
 }
 }
 
 
@@ -219,6 +225,8 @@ int main()
     glfwSetKeyCallback(window, key_callback);
     glfwSetKeyCallback(window, key_callback);
     glfwSetFramebufferSizeCallback(window, window_size_callback);
     glfwSetFramebufferSizeCallback(window, window_size_callback);
 
 
+    double time = glfwGetTime();
+    double prev_time;
     while (!glfwWindowShouldClose(window)) {
     while (!glfwWindowShouldClose(window)) {
         glClear(GL_COLOR_BUFFER_BIT);
         glClear(GL_COLOR_BUFFER_BIT);
 
 
@@ -226,13 +234,18 @@ int main()
             glUniform2f(resolution_location,
             glUniform2f(resolution_location,
                         SCREEN_WIDTH,
                         SCREEN_WIDTH,
                         SCREEN_HEIGHT);
                         SCREEN_HEIGHT);
-            glUniform1f(time_location, glfwGetTime());
+            glUniform1f(time_location, time);
 
 
             glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
             glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
         }
         }
 
 
         glfwSwapBuffers(window);
         glfwSwapBuffers(window);
         glfwPollEvents();
         glfwPollEvents();
+        double cur_time = glfwGetTime();
+        if (!pause) {
+            time += cur_time - prev_time;
+        }
+        prev_time = cur_time;
     }
     }
 
 
     return 0;
     return 0;