From 2609d8c00d9a07c82208cc640fdbc481ff7b4a59 Mon Sep 17 00:00:00 2001 From: Mike Welsh Date: Sat, 27 Apr 2019 20:58:33 -0700 Subject: [PATCH] Shapes on glium backend --- desktop/Cargo.toml | 2 +- lib/src/backend/render/glium.rs | 74 +++++++++++++++++---------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/desktop/Cargo.toml b/desktop/Cargo.toml index 4e62b245e..edccda35a 100644 --- a/desktop/Cargo.toml +++ b/desktop/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "fluster" +name = "fluster_desktop" version = "0.1.0" authors = ["Mike Welsh "] edition = "2018" diff --git a/lib/src/backend/render/glium.rs b/lib/src/backend/render/glium.rs index a9a9b2cfd..a3260b73d 100644 --- a/lib/src/backend/render/glium.rs +++ b/lib/src/backend/render/glium.rs @@ -37,42 +37,44 @@ impl RenderBackend for GliumRenderBackend { use lyon::tessellation::FillOptions; - // let mut mesh: VertexBuffers<_, u16> = VertexBuffers::new(); - // let paths = swf_shape_to_lyon_paths(shape); - // let fill_tess = FillTessellator::new(); - // for path in paths { - // let mut buffers_builder = BuffersBuilder::new(&mut mesh, ()); - // fill_tess.tessellate_path( - // path.into_iter(), - // &FillOptions::even_odd(), - // &mut buffers_builder, - // ); - // } + let mut mesh: VertexBuffers<_, u32> = VertexBuffers::new(); + let paths = swf_shape_to_lyon_paths(shape); + let mut fill_tess = FillTessellator::new(); - let vertices = vec![ - Vertex { - position: [0.0, 0.0], - color: [1.0, 0.0, 0.0, 1.0], - }, - Vertex { - position: [100.0, 0.0], - color: [1.0, 0.0, 0.0, 1.0], - }, - Vertex { - position: [100.0, 100.0], - color: [1.0, 0.0, 0.0, 1.0], - }, - Vertex { - position: [0.0, 100.0], - color: [1.0, 0.0, 0.0, 1.0], - }, - ]; - let indices = vec![0, 1, 2, 0, 2, 3]; - let vertex_buffer = glium::VertexBuffer::new(&self.display, &vertices).unwrap(); + for (cmd, path) in paths { + if let &PathCommandType::Stroke(_) = &cmd { + continue; + } + let color = match cmd { + PathCommandType::Fill(FillStyle::Color(color)) => [ + f32::from(color.r) / 255.0, + f32::from(color.g) / 255.0, + f32::from(color.b) / 255.0, + f32::from(color.a) / 255.0, + ], + PathCommandType::Fill(_) => [1.0, 0.0, 0.0, 1.0], + PathCommandType::Stroke(_) => unreachable!(), + }; + let vertex_ctor = move |vertex: tessellation::FillVertex| Vertex { + position: [vertex.position.x, vertex.position.y], + color, + }; + + let mut buffers_builder = BuffersBuilder::new(&mut mesh, vertex_ctor); + fill_tess + .tessellate_path( + path.into_iter(), + &FillOptions::even_odd(), + &mut buffers_builder, + ) + .expect("Tessellation error"); + } + + let vertex_buffer = glium::VertexBuffer::new(&self.display, &mesh.vertices[..]).unwrap(); let index_buffer = glium::IndexBuffer::new( &self.display, glium::index::PrimitiveType::TrianglesList, - &indices, + &mesh.indices[..], ) .unwrap(); @@ -178,13 +180,15 @@ fn point(x: f32, y: f32) -> lyon::math::Point { lyon::math::Point::new(x, y) } -fn swf_shape_to_lyon_paths(shape: &swf::Shape) -> Vec> { +fn swf_shape_to_lyon_paths( + shape: &swf::Shape, +) -> Vec<(PathCommandType, Vec)> { let cmds = get_paths(shape); let mut out_paths = vec![]; let mut prev; use lyon::geom::{LineSegment, QuadraticBezierSegment}; for cmd in cmds { - if let PathCommandType::Fill(fill_style) = cmd.command_type { + if let PathCommandType::Fill(fill_style) = &cmd.command_type { let mut out_path = vec![PathEvent::MoveTo(point(cmd.path.start.0, cmd.path.start.1))]; prev = point(cmd.path.start.0, cmd.path.start.1); for edge in cmd.path.edges { @@ -213,7 +217,7 @@ fn swf_shape_to_lyon_paths(shape: &swf::Shape) -> Vec from: prev, to: prev, })); - out_paths.push(out_path); + out_paths.push((cmd.command_type, out_path)); } } out_paths