From be18a9a0503fa09318b5d99452a3407a92db4366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 17 Jan 2021 21:21:55 -0300 Subject: Add new points to the bezier curve interactively --- src/main.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 2c5dbcc..f01526b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { } } -fn compile_bezier_shader(c: Bezier) -> GLuint { +fn compile_bezier_shader(c: &Bezier) -> GLuint { let vertex_shader_source = b" #version 410 core layout (location = 0) in vec3 aPos; @@ -121,17 +121,13 @@ fn compile_bezier_shader(c: Bezier) -> GLuint { gl::LinkProgram(shader_program); gl::DeleteShader(vertex_shader); gl::DeleteShader(fragment_shader); + gl::UseProgram(shader_program); } shader_program } fn main() { let mut curve = Bezier::new(); - curve.push(200, 200); - curve.push(300, 800); - curve.push(800, 100); - curve.push(1300, 800); - curve.push(1400, 200); let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); @@ -185,10 +181,10 @@ fn main() { gl::EnableVertexAttribArray(0); } - let shader_program = compile_bezier_shader(curve); - let mut event_pump = sdl_context.event_pump().unwrap(); 'running: loop { + let x = event_pump.mouse_state().x(); + let y = event_pump.mouse_state().y(); for event in event_pump.poll_iter() { match event { Event::Quit { .. } @@ -196,13 +192,19 @@ fn main() { keycode: Some(Keycode::Escape), .. } => break 'running, + Event::KeyDown { + keycode: Some(Keycode::A), + .. + } => { + curve.push(x, window_h as i32 - y); + compile_bezier_shader(&curve); + }, _ => {} } } unsafe { gl::Clear(gl::COLOR_BUFFER_BIT); - gl::UseProgram(shader_program); gl::DrawArrays(gl::TRIANGLES, 0, 6); } -- cgit v1.2.3