Ver código fonte

Use screen coordinates for the default demo

rexim 3 anos atrás
pai
commit
6a2505420b
3 arquivos alterados com 37 adições e 26 exclusões
  1. 25 11
      main.c
  2. 3 14
      shaders/main.frag
  3. 9 1
      shaders/main.vert

+ 25 - 11
main.c

@@ -286,7 +286,7 @@ void reload_render_conf(const char *render_conf_path)
             // ^^^SAFETY NOTES: this is needed so we can use `value` as a NULL-terminated C-string.
             // This should not cause any problems because the original string `render_conf`
             // that we are processing the `value` from is mutable, NULL-terminated and we are splitting
-            // it by newlines which garantees that there is always a character after 
+            // it by newlines which garantees that there is always a character after
             // the end of `value`.
             //
             // Let's consider an example where `render_conf` is equal to this:
@@ -297,7 +297,7 @@ void reload_render_conf(const char *render_conf_path)
             // key = value\0
             // ```
             //
-            // There is always something after `value`. It's either `\n` or `\0`. With all of these 
+            // There is always something after `value`. It's either `\n` or `\0`. With all of these
             // invariats in place writing to `value.data[value.count]` should be safe.
 
             if (sv_eq(key, SV("vert"))) {
@@ -311,7 +311,7 @@ void reload_render_conf(const char *render_conf_path)
                 printf("Texture Path: %s\n", texture_path);
             } else {
                 printf("%s:%d:%ld: ERROR: unsupported key `"SV_Fmt"`\n",
-                       render_conf_path, row, key.data - line_start, 
+                       render_conf_path, row, key.data - line_start,
                        SV_Arg(key));
             }
         }
@@ -476,6 +476,13 @@ void renderer_init(Renderer *r)
                           (void*) offsetof(Vertex, color));
 }
 
+void renderer_clear(Renderer *r)
+{
+    r->vertex_buf_sz = 0;
+}
+
+#define COLOR_BLACK_V4F ((V4f){0.0f, 0.0f, 0.0f, 1.0f})
+
 int main(void)
 {
     reload_render_conf("render.conf");
@@ -525,10 +532,6 @@ int main(void)
 
     renderer_init(&global_renderer);
 
-    renderer_push_quad(&global_renderer, v2f(-1.0f, -1.0f), v2f(1.0f, 1.0f), (V4f) {
-        0
-    });
-    renderer_sync(&global_renderer);
     renderer_reload_textures(&global_renderer);
     renderer_reload_shaders(&global_renderer);
 
@@ -540,18 +543,29 @@ int main(void)
     while (!glfwWindowShouldClose(window)) {
         glClear(GL_COLOR_BUFFER_BIT);
 
+        int width, height;
+        glfwGetWindowSize(window, &width, &height);
+        double xpos, ypos;
+        glfwGetCursorPos(window, &xpos, &ypos);
+
         if (!global_renderer.program_failed) {
             static_assert(COUNT_UNIFORMS == 3, "Update the uniform sync");
-            int width, height;
-            glfwGetWindowSize(window, &width, &height);
             glUniform2f(global_renderer.uniforms[RESOLUTION_UNIFORM], (GLfloat) width, (GLfloat) height);
             glUniform1f(global_renderer.uniforms[TIME_UNIFORM], (GLfloat) time);
-            double xpos, ypos;
-            glfwGetCursorPos(window, &xpos, &ypos);
             glUniform2f(global_renderer.uniforms[MOUSE_UNIFORM], (GLfloat) xpos, (GLfloat) (height - ypos));
+
+            renderer_clear(&global_renderer);
+            renderer_push_quad(
+                &global_renderer,
+                v2f(width * -0.5f, height * -0.5f),
+                v2f(width * 0.5f, height * 0.5f),
+                COLOR_BLACK_V4F);
+            renderer_sync(&global_renderer);
+
             glDrawArraysInstanced(GL_TRIANGLES, 0, (GLsizei) global_renderer.vertex_buf_sz, 1);
         }
 
+
         glfwSwapBuffers(window);
         glfwPollEvents();
         double cur_time = glfwGetTime();

+ 3 - 14
shaders/main.frag

@@ -10,21 +10,10 @@ in vec2 uv;
 in vec4 color;
 out vec4 out_color;
 
-#define R 500.0
-
 void main(void) {
-#if 0
-    out_color = color;
-#else
-    vec2 coord_uv = gl_FragCoord.xy / resolution;
-    vec2 mouse_uv = mouse / resolution;
-
-    float t = 1.0 - min(length(uv - mouse_uv), R) / R;
-
     out_color = vec4(
-        (sin(t*(uv.x + time)) + 1.0) / 2.0,
-        (cos(t*(uv.y + time)) + 1.0) / 2.0,
-        (cos(t*(uv.x + coord_uv.y + time)) + 1.0) / 2.0,
+        (sin(uv.x + time) + 1.0) / 2.0,
+        (cos(uv.y + time) + 1.0) / 2.0,
+        (cos(uv.x + uv.y + time) + 1.0) / 2.0,
         1.0);
-#endif
 }

+ 9 - 1
shaders/main.vert

@@ -4,14 +4,22 @@ layout(location = 0) in vec2 ver_pos;
 layout(location = 1) in vec2 ver_uv;
 layout(location = 2) in vec4 ver_color;
 
+uniform vec2 resolution;
+
 precision mediump float;
 
 out vec2 uv;
 out vec4 color;
 
+vec2 screen_project(vec2 pos)
+{
+    return (pos / resolution) * 2.0 - 1.0;
+}
+
 void main(void)
 {
-    gl_Position = vec4(ver_pos, 0.0, 1.0);
+    float scale = 300.0;
+    gl_Position = vec4(screen_project(ver_pos + resolution * 0.5), 0.0, 1.0);
     uv = ver_uv;
     color = ver_color;
 }