Skip to content

Commit

Permalink
Create RenderContext2, RenderPrimitive2, PrimitiveRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
dzil123 committed Jul 28, 2021
1 parent b78dd13 commit 8b81e71
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 383 deletions.
37 changes: 15 additions & 22 deletions nannou/src/draw/primitive/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,15 @@ impl Into<Option<Arrow>> for Primitive {
}
}

impl draw::renderer::RenderPrimitive for Arrow {
fn render_primitive(
impl draw::renderer::RenderPrimitive2 for Arrow {
fn render_primitive<R>(
self,
mut ctxt: draw::renderer::RenderContext,
mesh: &mut draw::Mesh,
) -> draw::renderer::PrimitiveRender {
_ctxt: draw::renderer::RenderContext2,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
R: draw::renderer::PrimitiveRenderer,
{
let Arrow {
line,
head_length,
Expand Down Expand Up @@ -204,26 +207,20 @@ impl draw::renderer::RenderPrimitive for Arrow {
// The line should only be drawn if there is space after drawing the triangle.
let draw_line = line_dir_len > tri_len;

// Determine the transform to apply to all points.
let global_transform = *ctxt.transform;
let theme_primitive = draw::theme::Primitive::Arrow;
let local_transform = line.path.position.transform() * line.path.orientation.transform();
let transform = global_transform * local_transform;

// Draw the tri.
let tri_points = [tri_a, tri_b, tri_c];
let tri_points = tri_points.iter().cloned().map(|p| p.to_array().into());
let close_tri = true;
let tri_events = lyon::path::iterator::FromPolyline::new(close_tri, tri_points);
path::render_path_events(
renderer.path_flat_color(
local_transform,
tri_events,
line.path.color,
transform,
theme_primitive,
path::Options::Fill(Default::default()),
&ctxt.theme,
draw::theme::Primitive::Arrow,
&mut ctxt.fill_tessellator,
&mut ctxt.stroke_tessellator,
mesh,
);

// Draw the line.
Expand All @@ -232,16 +229,12 @@ impl draw::renderer::RenderPrimitive for Arrow {
let line_points = line_points.iter().cloned().map(|p| p.to_array().into());
let close_line = false;
let line_events = lyon::path::iterator::FromPolyline::new(close_line, line_points);
path::render_path_events(
renderer.path_flat_color(
local_transform,
line_events,
line.path.color,
transform,
theme_primitive,
path::Options::Stroke(line.path.opts),
&ctxt.theme,
draw::theme::Primitive::Arrow,
&mut ctxt.fill_tessellator,
&mut ctxt.stroke_tessellator,
mesh,
);
}

Expand Down
28 changes: 15 additions & 13 deletions nannou/src/draw/primitive/ellipse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ impl Ellipse {

// Trait implementations.

impl draw::renderer::RenderPrimitive for Ellipse {
fn render_primitive(
impl draw::renderer::RenderPrimitive2 for Ellipse {
fn render_primitive<R>(
self,
ctxt: draw::renderer::RenderContext,
mesh: &mut draw::Mesh,
) -> draw::renderer::PrimitiveRender {
_ctxt: draw::renderer::RenderContext2,
renderer: R,
) -> draw::renderer::PrimitiveRender
where
R: draw::renderer::PrimitiveRenderer,
{
let Ellipse {
dimensions,
polygon,
Expand All @@ -70,6 +73,7 @@ impl draw::renderer::RenderPrimitive for Ellipse {
"z dimension support for ellipse is unimplemented"
);

let theme_primitive = draw::theme::Primitive::Ellipse;
let w = maybe_x.map(f32::abs).unwrap_or(100.0);
let h = maybe_y.map(f32::abs).unwrap_or(100.0);
match resolution {
Expand All @@ -85,25 +89,23 @@ impl draw::renderer::RenderPrimitive for Ellipse {
builder.move_to(start);
builder.arc(centre, radii, sweep_angle, x_rotation);
let path = builder.build();
polygon::render_events_themed(
polygon::render::render_events_themed(
polygon.opts,
|| (&path).into_iter(),
ctxt,
draw::theme::Primitive::Ellipse,
mesh,
theme_primitive,
renderer,
);
}
}
Some(resolution) => {
let rect = geom::Rect::from_w_h(w, h);
let ellipse = geom::Ellipse::new(rect, resolution);
let points = ellipse.circumference().map(Vec2::from);
polygon::render_points_themed(
polygon::render::render_points_themed(
polygon.opts,
points,
ctxt,
draw::theme::Primitive::Ellipse,
mesh,
theme_primitive,
renderer,
);
}
}
Expand Down
26 changes: 11 additions & 15 deletions nannou/src/draw/primitive/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ impl Into<Option<Line>> for Primitive {
}
}

impl draw::renderer::RenderPrimitive for Line {
fn render_primitive(
impl draw::renderer::RenderPrimitive2 for Line {
fn render_primitive<R>(
self,
mut ctxt: draw::renderer::RenderContext,
mesh: &mut draw::Mesh,
) -> draw::renderer::PrimitiveRender {
_ctxt: draw::renderer::RenderContext2,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
R: draw::renderer::PrimitiveRenderer,
{
let Line { path, start, end } = self;
let start = start.unwrap_or(pt2(0.0, 0.0));
let end = end.unwrap_or(pt2(0.0, 0.0));
Expand All @@ -143,21 +146,14 @@ impl draw::renderer::RenderPrimitive for Line {
let points = points.iter().cloned().map(|p| p.to_array().into());
let events = lyon::path::iterator::FromPolyline::new(close, points);

// Determine the transform to apply to all points.
let global_transform = *ctxt.transform;
let local_transform = path.position.transform() * path.orientation.transform();
let transform = global_transform * local_transform;

path::render_path_events(
renderer.path_flat_color(
local_transform,
events,
path.color,
transform,
path::Options::Stroke(path.opts),
&ctxt.theme,
draw::theme::Primitive::Line,
&mut ctxt.fill_tessellator,
&mut ctxt.stroke_tessellator,
mesh,
path::Options::Stroke(path.opts),
);

draw::renderer::PrimitiveRender::default()
Expand Down
10 changes: 5 additions & 5 deletions nannou/src/draw/primitive/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Mesh {
}

#[derive(Clone, Debug, Default)]
struct FillColor(Option<LinSrgba>);
pub struct FillColor(Option<LinSrgba>);

// A simple iterator for flattening a fixed-size array of indices.
struct FlattenIndices<I> {
Expand Down Expand Up @@ -515,10 +515,10 @@ impl draw::renderer::RenderPrimitive for Mesh {
// Color the vertices based on whether or not we should fill, then extend the mesh!
match fill_color {
Some(fill) => {
let theme_prim = draw::theme::Primitive::Mesh;
let color = fill
.0
.unwrap_or_else(|| ctxt.theme.fill_lin_srgba(theme_prim));
let theme_primitive = draw::theme::Primitive::Mesh;
let color =
ctxt.theme
.resolve_color(fill.0, theme_primitive, draw::theme::ColorType::Fill);
let vertices = vertex_range.map(|i| {
let point = transform_point(ctxt.intermediary_mesh.points()[i]);
let tex_coords = ctxt.intermediary_mesh.tex_coords()[i];
Expand Down
7 changes: 2 additions & 5 deletions nannou/src/draw/primitive/path/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pub mod render;

pub(crate) use self::render::{
render_path_events, render_path_points_colored, render_path_points_textured,
render_path_source, PathEventSource, PathEventSourceIter,
};
pub(crate) use self::render::{render_path_points_textured, PathEventSource};
use crate::color::conv::IntoLinSrgba;
use crate::color::LinSrgba;
use crate::draw::mesh::vertex::TexCoords;
Expand Down Expand Up @@ -91,7 +88,7 @@ impl PathInit {
///
/// The returned building context allows for specifying the fill tessellation options.
pub fn fill(self) -> PathFill {
let opts = FillOptions::default();
let opts = Default::default();
PathFill::new(opts)
}

Expand Down
Loading

0 comments on commit 8b81e71

Please sign in to comment.