summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-19 09:06:32 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-19 09:15:02 -0300
commit4998f5936dc7da36aba0cd018de2a1a6127dda08 (patch)
tree1363045387c3730d33fbed10f682662e3a635860 /src/main.rs
parentbe18a9a0503fa09318b5d99452a3407a92db4366 (diff)
downloadbezier-4998f5936dc7da36aba0cd018de2a1a6127dda08.tar.gz
bezier-4998f5936dc7da36aba0cd018de2a1a6127dda08.zip
Make shader sample intervals uniform
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs61
1 files changed, 44 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs
index f01526b..a61478f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,25 +37,47 @@ fn compile_bezier_shader(c: &Bezier) -> GLuint {
#version 410 core
out vec4 FragColor;
- float inf = 1000000;
float vx[{s}] = float[{s}]({vx});
float vy[{s}] = float[{s}]({vy});
+
float px[{sx}] = float[{sx}]({px});
float py[{sy}] = float[{sy}]({py});
+
+ float dpx[{dsx}] = float[{dsx}]({dpx});
+ float dpy[{dsy}] = float[{dsy}]({dpy});
+
float thr = 32;
+ float step_size = 10;
+ float inf = 1000000;
- float eval_a(float[{sx}] v, float t) {{
+ float eval_px(float t) {{
float result = 0.0;
for (int i = 0; i < {sx}; i++) {{
- result += v[i] * pow(t, i);
+ result += px[i] * pow(t, i);
}}
return result;
}}
- float eval_b(float[{sy}] v, float t) {{
+ float eval_py(float t) {{
float result = 0.0;
for (int i = 0; i < {sy}; i++) {{
- result += v[i] * pow(t, i);
+ result += py[i] * pow(t, i);
+ }}
+ return result;
+ }}
+
+ float eval_dpx(float t) {{
+ float result = 0.0;
+ for (int i = 0; i < {dsx}; i++) {{
+ result += dpx[i] * pow(t, i);
+ }}
+ return result;
+ }}
+
+ float eval_dpy(float t) {{
+ float result = 0.0;
+ for (int i = 0; i < {dsy}; i++) {{
+ result += dpy[i] * pow(t, i);
}}
return result;
}}
@@ -72,13 +94,14 @@ fn compile_bezier_shader(c: &Bezier) -> GLuint {
}}
float inside(vec2 pos) {{
- float res = 100;
float closest = inf;
float t = 0.0;
- while (t < 1.0 - 1.0 / res) {{
- float step = t + 1.0 / res;
- vec2 a = vec2(eval_a(px, t), eval_b(py, t));
- vec2 b = vec2(eval_a(px, step), eval_b(py, step));
+ float dstep = 0.0;
+ while (t < 1.0 - dstep) {{
+ dstep = step_size / length(vec2(eval_dpx(t), eval_dpy(t)));
+ float step = t + dstep;
+ vec2 a = vec2(eval_px(t), eval_py(t));
+ vec2 b = vec2(eval_px(step), eval_py(step));
float c = pow(length(a - b), 2);
float d = distance(pos, a + clamp(dot(pos - a, b - a) / c, 0, 1) * (b - a));
if (d < closest) {{
@@ -95,13 +118,17 @@ fn compile_bezier_shader(c: &Bezier) -> GLuint {
FragColor = vec4(r, c, 0.0, 1.0);
}}
",
- sx = c.px.degree() + 1,
- sy = c.py.degree() + 1,
- px = &c.px,
- py = &c.py,
- s = c.degree,
- vx = c.show_x(),
- vy = c.show_y(),
+ s = c.degree,
+ vx = c.show_x(),
+ vy = c.show_y(),
+ px = &c.px,
+ py = &c.py,
+ sx = c.px.degree() + 1,
+ sy = c.py.degree() + 1,
+ dpx = &c.dpx,
+ dpy = &c.dpy,
+ dsx = c.dpx.degree() + 1,
+ dsy = c.dpy.degree() + 1,
)
.into_bytes();