Просмотр исходного кода

[GPU] make the threshold customizable with mouse wheel

rexim 4 лет назад
Родитель
Сommit
dc83abdaec
3 измененных файлов с 17 добавлено и 4 удалено
  1. 0 1
      cpu.c
  2. 13 0
      gpu.c
  3. 4 3
      gpu.frag

+ 0 - 1
cpu.c

@@ -55,7 +55,6 @@ Vec2 beziern_sample(Vec2 *ps, Vec2 *xs, size_t n, float p)
     return xs[0];
 }
 
-// TODO: explore how to render bezier curves on GPU using fragment shaders
 void render_bezier_markers(SDL_Renderer *renderer,
                            Vec2 *ps, Vec2 *xs, size_t n,
                            float s, uint32_t color)

+ 13 - 0
gpu.c

@@ -71,11 +71,13 @@ int main(void)
     GLint u_p2 = glGetUniformLocation(program, "p2");
     GLint u_p3 = glGetUniformLocation(program, "p3");
     GLint u_marker_radius = glGetUniformLocation(program, "marker_radius");
+    GLint u_bezier_curve_threshold = glGetUniformLocation(program, "bezier_curve_threshold");
 
     Vec2 p1 = vec2(100.0f, 100.0f);
     Vec2 p2 = vec2(200.0f, 200.0f);
     Vec2 p3 = vec2(300.0f, 300.0f);
     Vec2 *p_selected = NULL;
+    float bezier_curve_threshold = 10.0f;
 
     int quit = 0;
     while (!quit) {
@@ -91,6 +93,7 @@ int main(void)
                     event.button.x,
                     SCREEN_HEIGHT - event.button.y);
                 if (event.button.button == SDL_BUTTON_LEFT) {
+                    // TODO: dragging does not work if the window size is not correct
                     if (vec2_length(vec2_sub(p1, mouse_pos)) < MARKER_SIZE) {
                         p_selected = &p1;
                     }
@@ -119,6 +122,15 @@ int main(void)
                     *p_selected = mouse_pos;
                 }
             } break;
+
+            case SDL_MOUSEWHEEL: {
+#define BEZIER_CURVE_THRESHOLD_STEP 5.0f
+                if (event.wheel.y > 0) {
+                    bezier_curve_threshold = bezier_curve_threshold + BEZIER_CURVE_THRESHOLD_STEP;
+                } else if (event.wheel.y < 0) {
+                    bezier_curve_threshold = fmaxf(bezier_curve_threshold - BEZIER_CURVE_THRESHOLD_STEP, 0.0f);
+                }
+            } break;
             }
         }
 
@@ -130,6 +142,7 @@ int main(void)
         glUniform2f(u_p2, p2.x, p2.y);
         glUniform2f(u_p3, p3.x, p3.y);
         glUniform1f(u_marker_radius, MARKER_SIZE);
+        glUniform1f(u_bezier_curve_threshold, bezier_curve_threshold);
         glDrawArrays(GL_QUADS, 0, 4);
 
         SDL_GL_SwapWindow(window);

+ 4 - 3
gpu.frag

@@ -1,13 +1,14 @@
 #version 130
 
 #define MARKER_COLOR vec4(0.75, 0.0, 0.0, 1.0)
-#define BEZIER_CURVE_THRESHOLD 10.0
 #define BEZIER_CURVE_COLOR vec4(0.0, 0.75, 0.0, 1.0)
 
+// TODO: at some combinations of p1, p2, p3 nothing is drawn
 uniform vec2 p1;
 uniform vec2 p2;
 uniform vec2 p3;
 uniform float marker_radius;
+uniform float bezier_curve_threshold;
 
 void main()
 {
@@ -30,8 +31,8 @@ void main()
             float t2 = (-b - sqrt(dx)) / (2 * a);
             float y1 = p1.y + 2 * t1 * (p2.y - p1.y) + t1 * t1 * (p3.y - 2 * p2.y + p1.y);
             float y2 = p1.y + 2 * t2 * (p2.y - p1.y) + t2 * t2 * (p3.y - 2 * p2.y + p1.y);
-            if ((0.0f <= t1 && t1 <= 1.0f && abs(p0.y - y1) < BEZIER_CURVE_THRESHOLD) ||
-                (0.0f <= t2 && t2 <= 1.0f && abs(p0.y - y2) < BEZIER_CURVE_THRESHOLD))
+            if ((0.0f <= t1 && t1 <= 1.0f && abs(p0.y - y1) < bezier_curve_threshold) ||
+                (0.0f <= t2 && t2 <= 1.0f && abs(p0.y - y2) < bezier_curve_threshold))
             {
                 gl_FragColor = BEZIER_CURVE_COLOR;
             }