Last active
October 7, 2019 22:35
-
-
Save Herschel/a7088696f8c89d831f07ce820c715e28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate glium; | |
extern crate lyon; | |
use glium::glutin::dpi::LogicalSize; | |
use glium::Surface; | |
use lyon::math::*; | |
use lyon::path::PathEvent; | |
use lyon::tessellation; | |
use lyon::tessellation::geometry_builder::{BuffersBuilder, VertexBuffers, VertexConstructor}; | |
use lyon::tessellation::{FillOptions, FillTessellator}; | |
#[derive(Copy, Clone)] | |
struct Vertex { | |
position: [f32; 2], | |
} | |
implement_vertex!(Vertex, position); | |
// A very simple vertex constructor that only outputs the vertex position | |
struct VertexCtor; | |
impl VertexConstructor<tessellation::FillVertex, Vertex> for VertexCtor { | |
fn new_vertex(&mut self, vertex: tessellation::FillVertex) -> Vertex { | |
assert!(!vertex.position.x.is_nan()); | |
assert!(!vertex.position.y.is_nan()); | |
Vertex { | |
// (ugly hack) tweak the vertext position so that the logo fits roughly | |
// within the (-1.0, 1.0) range. | |
position: (vertex.position * 0.0145 - vector(1.0, 1.0)).to_array(), | |
} | |
} | |
} | |
fn main() { | |
use lyon::geom::LineSegment; | |
use lyon::math::Point; | |
use lyon::path::PathEvent::*; | |
let path = vec![ | |
MoveTo(Point::new(54.55, 14.1)), | |
Line(LineSegment { | |
from: Point::new(54.55, 14.1), | |
to: Point::new(55.35, 9.0), | |
}), | |
Line(LineSegment { | |
from: Point::new(55.35, 9.0), | |
to: Point::new(51.55, 9.0), | |
}), | |
Line(LineSegment { | |
from: Point::new(51.55, 9.0), | |
to: Point::new(54.55, 14.1), | |
}), | |
MoveTo(Point::new(0.0, 0.0)), | |
Line(LineSegment { | |
from: Point::new(0.0, 0.0), | |
to: Point::new(71.0, 0.0), | |
}), | |
Line(LineSegment { | |
from: Point::new(71.0, 0.0), | |
to: Point::new(71.0, 143.45), | |
}), | |
Line(LineSegment { | |
from: Point::new(71.0, 143.45), | |
to: Point::new(0.0, 143.45), | |
}), | |
Line(LineSegment { | |
from: Point::new(0.0, 143.45), | |
to: Point::new(0.0, 0.0), | |
}), | |
MoveTo(Point::new(38.25, 138.2)), | |
Line(LineSegment { | |
from: Point::new(38.25, 138.2), | |
to: Point::new(33.75, 131.0), | |
}), | |
Line(LineSegment { | |
from: Point::new(33.75, 131.0), | |
to: Point::new(27.8, 138.2), | |
}), | |
Line(LineSegment { | |
from: Point::new(27.8, 138.2), | |
to: Point::new(38.25, 138.2), | |
}), | |
MoveTo(Point::new(28.2, 129.2)), | |
Line(LineSegment { | |
from: Point::new(28.2, 129.2), | |
to: Point::new(37.2, 131.0), | |
}), | |
Line(LineSegment { | |
from: Point::new(37.2, 131.0), | |
to: Point::new(41.25, 126.95), | |
}), | |
Line(LineSegment { | |
from: Point::new(41.25, 126.95), | |
to: Point::new(28.2, 129.2), | |
}), | |
MoveTo(Point::new(58.05, 115.95)), | |
Line(LineSegment { | |
from: Point::new(58.05, 115.95), | |
to: Point::new(58.05, 9.0), | |
}), | |
Line(LineSegment { | |
from: Point::new(58.05, 9.0), | |
to: Point::new(2.9, 115.95), | |
}), | |
Line(LineSegment { | |
from: Point::new(2.9, 115.95), | |
to: Point::new(58.05, 115.95), | |
}), | |
]; | |
let mut tessellator = FillTessellator::new(); | |
let mut mesh: VertexBuffers<Vertex, u16> = VertexBuffers::new(); | |
tessellator | |
.tessellate_path( | |
path.into_iter(), | |
&FillOptions::tolerance(0.01), | |
&mut BuffersBuilder::new(&mut mesh, VertexCtor), | |
) | |
.unwrap(); | |
println!( | |
" -- fill: {} vertices {} indices", | |
mesh.vertices.len(), | |
mesh.indices.len() | |
); | |
let mut events_loop = glium::glutin::EventsLoop::new(); | |
let context = glium::glutin::ContextBuilder::new().with_vsync(true); | |
let window = glium::glutin::WindowBuilder::new() | |
.with_dimensions(LogicalSize { | |
width: 400.0, | |
height: 400.0, | |
}) | |
.with_decorations(true) | |
.with_title("lyon + glium basic example"); | |
let display = glium::Display::new(window, context, &events_loop).unwrap(); | |
let vertex_buffer = glium::VertexBuffer::new(&display, &mesh.vertices).unwrap(); | |
let indices = glium::IndexBuffer::new( | |
&display, | |
glium::index::PrimitiveType::TrianglesList, | |
&mesh.indices, | |
) | |
.unwrap(); | |
let program = | |
glium::Program::from_source(&display, VERTEX_SHADER, FRAGMENT_SHADER, None).unwrap(); | |
let mut status = true; | |
loop { | |
if !status { | |
break; | |
} | |
let mut target = display.draw(); | |
target.clear_color(0.8, 0.8, 0.8, 1.0); | |
target | |
.draw( | |
&vertex_buffer, | |
&indices, | |
&program, | |
&glium::uniforms::EmptyUniforms, | |
&Default::default(), | |
) | |
.unwrap(); | |
target.finish().unwrap(); | |
events_loop.poll_events(|event| { | |
use glium::glutin::{Event, WindowEvent}; | |
match event { | |
Event::WindowEvent { event, .. } => match event { | |
WindowEvent::Destroyed => status = false, | |
WindowEvent::KeyboardInput { | |
input: | |
glium::glutin::KeyboardInput { | |
virtual_keycode: Some(glium::glutin::VirtualKeyCode::Escape), | |
.. | |
}, | |
.. | |
} => status = false, | |
_ => (), | |
}, | |
_ => (), | |
} | |
}); | |
} | |
} | |
pub static VERTEX_SHADER: &'static str = r#" | |
#version 140 | |
in vec2 position; | |
void main() { | |
gl_Position = vec4(0.75 * position, 0.0, 1.0); | |
gl_Position.y *= -1.0; | |
} | |
"#; | |
pub static FRAGMENT_SHADER: &'static str = r#" | |
#version 140 | |
out vec4 color; | |
void main() { | |
color = vec4(0.0, 0.0, 0.0, 1.0); | |
} | |
"#; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment