mod lerp; mod number; mod poly; use lerp::Lerp; use number::Number; use std::thread; use std::time::Duration; use std::os::raw::c_void; use std::ffi::{CString, CStr}; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; use sdl2::rect::Rect; 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 main() { let a = Lerp::new(vec![200.0, 200.0, 200.0]); let b = Lerp::new(vec![100.0, 550.0, 1000.0]); let pa = lerp::lp(a); let pb = lerp::lp(b); let p = poly::gcd(&pa, &pb); 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] = [ // first triangle 1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, // // second triangle 1.0, -1.0, 0.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0 ]; 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 = b" #version 410 core out vec4 FragColor; float p1[3] = float[3](200, 400, 1300); float p2[3] = float[3](100, 900, -970); float threshold = 0.1; float eval(float[3] v, float t) { return v[2] * t * t + v[1] * t + v[0]; } float inside(vec2 pos) { float res = 100; float thr = 32; float closest = 10000; float t = 0.0; while (t < 1.0) { float step = t + 1.0 / res; vec2 a = vec2(eval(p1, t), eval(p2, t)); vec2 b = vec2(eval(p1, step), eval(p2, step)); vec2 n = vec2(-(b - a).y, (b - a).x); float d = dot(a - pos, n) / length(n); if (d < closest) { closest = d; } t = step; } return 1 - pow(closest / thr, 2); } void main() { float r = inside(gl_FragCoord.xy); FragColor = vec4(r * 0.8, r * 0.2, r, 1.0); } ".to_vec(); 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::()) as GLsizeiptr, vertices.as_ptr() as *const GLvoid, gl::STATIC_DRAW); gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, (3 * std::mem::size_of::()) as GLint, std::ptr::null()); gl::EnableVertexAttribArray(0); } let mut shader_program: GLuint = 0; 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); } 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(); } }