summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: ee508e1a4966b4c4678c8f7a925c048dc6f2f234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
mod lerp;
mod poly;

use lerp::Lerp;
use poly::Poly;

use std::ffi::{CStr, CString};

use sdl2::event::Event;
use sdl2::keyboard::Keycode;

use gl::types::*;

fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint {
    unsafe {
        let shader: GLuint = gl::CreateShader(shader_type);
        gl::ShaderSource(shader, 1, &source.as_ptr(), std::ptr::null());
        gl::CompileShader(shader);
        let mut success: gl::types::GLint = 1;
        gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success);
        if success == 0 {
            println!("Compilation error: {}", source.to_str().unwrap());
        }
        shader
    }
}

fn compile_bezier_shader(a: Poly, b: Poly) -> GLuint {
    let vertex_shader_source = b"
    #version 410 core
    layout (location = 0) in vec3 aPos;

    void main() {
        gl_Position = vec4(aPos.xyz, 1.0);
    }
    "
    .to_vec();

    let fragment_shader_source = format!(
        "
    #version 410 core
    out vec4 FragColor;

    float pa[{sa}] = float[{sa}]({pa});
    float pb[{sb}] = float[{sb}]({pb});

    float eval_a(float[{sa}] v, float t) {{
        float result = 0.0;
        for (int i = 0; i < {sa}; i++) {{
          result += v[i] * pow(t, i);
        }}
        return result;
    }}

    float eval_b(float[{sb}] v, float t) {{
        float result = 0.0;
        for (int i = 0; i < {sb}; i++) {{
          result += v[i] * pow(t, i);
        }}
        return result;
    }}

    float inside(vec2 pos) {{
      float res = 100;
      float thr = 32;
      float closest = 1000000;
      float t = 0.0;
      while (t < 1.0) {{
        float step = t + 1.0 / res;
        vec2 a = vec2(eval_a(pa, t), eval_b(pb, t));
        vec2 b = vec2(eval_a(pa, step), eval_b(pb, 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) {{
          closest = d;
        }}
        t = step;
      }}
      return 1 - closest / thr;
    }}

    void main() {{
      float r = inside(gl_FragCoord.xy);
        FragColor = vec4(r * 0.8, r * 0.2, r, 1.0);
    }}
    ",
        sa = a.degree() + 1,
        sb = b.degree() + 1,
        pa = &a,
        pb = &b
    )
    .into_bytes();

    let shader_program: GLuint;
    unsafe {
        let vertex_shader = shader_from_source(
            &CString::from_vec_unchecked(vertex_shader_source),
            gl::VERTEX_SHADER,
        );
        let fragment_shader = shader_from_source(
            &CString::from_vec_unchecked(fragment_shader_source),
            gl::FRAGMENT_SHADER,
        );
        shader_program = gl::CreateProgram();
        gl::AttachShader(shader_program, vertex_shader);
        gl::AttachShader(shader_program, fragment_shader);
        gl::LinkProgram(shader_program);
        gl::DeleteShader(vertex_shader);
        gl::DeleteShader(fragment_shader);
    }
    shader_program
}

fn main() {
    let a = Lerp::new(vec![200.0, 500.0, 500.0]).to_poly();
    let b = Lerp::new(vec![200.0, 500.0, 200.0]).to_poly();

    let sdl_context = sdl2::init().unwrap();
    let video_subsystem = sdl_context.video().unwrap();

    let gl_attr = video_subsystem.gl_attr();

    gl_attr.set_context_profile(sdl2::video::GLProfile::Core);
    gl_attr.set_context_version(4, 1);

    let window_w: u32 = 1920;
    let window_h: u32 = 1080;

    let window = video_subsystem
        .window("bezier", window_w, window_h)
        .position_centered()
        .opengl()
        .build()
        .unwrap();

    let _gl_context = window.gl_create_context().unwrap();

    gl::load_with(|s| video_subsystem.gl_get_proc_address(s) as *const std::os::raw::c_void);

    unsafe {
        gl::Viewport(0, 0, window_w as i32, window_h as i32);
    }

    let vertices: [f32; 18] = [1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0];

    let mut vbo: GLuint = 0;
    let mut vao: GLuint = 0;
    unsafe {
        gl::GenVertexArrays(1, &mut vao);
        gl::BindVertexArray(vao);
        gl::GenBuffers(1, &mut vbo);
        gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
        gl::BufferData(
            gl::ARRAY_BUFFER,
            (vertices.len() * std::mem::size_of::<f32>()) as GLsizeiptr,
            vertices.as_ptr() as *const GLvoid,
            gl::STATIC_DRAW,
        );
        gl::VertexAttribPointer(
            0,
            3,
            gl::FLOAT,
            gl::FALSE,
            (3 * std::mem::size_of::<f32>()) as GLint,
            std::ptr::null(),
        );
        gl::EnableVertexAttribArray(0);
    }

    let shader_program = compile_bezier_shader(a, b);

    let mut event_pump = sdl_context.event_pump().unwrap();
    'running: loop {
        for event in event_pump.poll_iter() {
            match event {
                Event::Quit { .. }
                | Event::KeyDown {
                    keycode: Some(Keycode::Escape),
                    ..
                } => break 'running,
                _ => {}
            }
        }

        unsafe {
            gl::Clear(gl::COLOR_BUFFER_BIT);
            gl::UseProgram(shader_program);
            gl::DrawArrays(gl::TRIANGLES, 0, 6);
        }

        window.gl_swap_window();
    }
}