Refactor: rename Frame to Mesh

This commit is contained in:
Emil Ernerfeldt 2019-03-10 21:00:28 +01:00
parent f2040c6fc5
commit c15768dbf4
6 changed files with 68 additions and 72 deletions

View file

@ -8,7 +8,7 @@ use crate::{
style, style,
types::{Color, GuiCmd, GuiInput, PaintCmd}, types::{Color, GuiCmd, GuiInput, PaintCmd},
widgets::*, widgets::*,
FontSizes, Fonts, Frame, RawInput, Texture, FontSizes, Fonts, Mesh, RawInput, Texture,
}; };
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default)]
@ -68,9 +68,9 @@ fn show_font_texture(texture: &Texture, gui: &mut Region) {
uv: (texture.width as u16 - 1, texture.height as u16 - 1), uv: (texture.width as u16 - 1, texture.height as u16 - 1),
color: Color::WHITE, color: Color::WHITE,
}; };
let mut frame = Frame::default(); let mut mesh = Mesh::default();
frame.add_rect(top_left, bottom_right); mesh.add_rect(top_left, bottom_right);
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)])); gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Mesh(mesh)]));
if let Some(mouse_pos) = gui.input().mouse_pos { if let Some(mouse_pos) = gui.input().mouse_pos {
if interact.hovered { if interact.hovered {
@ -107,9 +107,9 @@ fn show_font_texture(texture: &Texture, gui: &mut Region) {
uv: ((u + texel_radius) as u16, (v + texel_radius) as u16), uv: ((u + texel_radius) as u16, (v + texel_radius) as u16),
color: Color::WHITE, color: Color::WHITE,
}; };
let mut frame = Frame::default(); let mut mesh = Mesh::default();
frame.add_rect(top_left, bottom_right); mesh.add_rect(top_left, bottom_right);
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)])); gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Mesh(mesh)]));
}); });
} }
} }
@ -161,16 +161,16 @@ impl Emigui {
} }
} }
pub fn paint(&mut self) -> Frame { pub fn paint(&mut self) -> Mesh {
let gui_commands = self.data.graphics.lock().unwrap().drain(); let gui_commands = self.data.graphics.lock().unwrap().drain();
let paint_commands = style::into_paint_commands(gui_commands, &self.style); let paint_commands = style::into_paint_commands(gui_commands, &self.style);
let mut mesher = Mesher::new(self.last_input.pixels_per_point); let mut mesher = Mesher::new(self.last_input.pixels_per_point);
mesher.paint(&self.data.fonts, &paint_commands); mesher.paint(&self.data.fonts, &paint_commands);
let frame = mesher.frame; let mesh = mesher.mesh;
self.stats.num_vertices = frame.vertices.len(); self.stats.num_vertices = mesh.vertices.len();
self.stats.num_triangles = frame.indices.len() / 3; self.stats.num_triangles = mesh.indices.len() / 3;
frame mesh
} }
pub fn example(&mut self, region: &mut Region) { pub fn example(&mut self, region: &mut Region) {

View file

@ -21,7 +21,7 @@ pub use crate::{
emigui::Emigui, emigui::Emigui,
fonts::{FontSizes, Fonts, TextStyle}, fonts::{FontSizes, Fonts, TextStyle},
layout::{Align, LayoutOptions, Region}, layout::{Align, LayoutOptions, Region},
mesher::{Frame, Vertex}, mesher::{Mesh, Vertex},
style::Style, style::Style,
texture_atlas::Texture, texture_atlas::Texture,
types::RawInput, types::RawInput,

View file

@ -18,19 +18,19 @@ pub struct Vertex {
} }
#[derive(Clone, Debug, Default, Serialize)] #[derive(Clone, Debug, Default, Serialize)]
pub struct Frame { pub struct Mesh {
/// Draw as triangles (i.e. the length is a multiple of three) /// Draw as triangles (i.e. the length is a multiple of three)
pub indices: Vec<u32>, pub indices: Vec<u32>,
pub vertices: Vec<Vertex>, pub vertices: Vec<Vertex>,
} }
impl Frame { impl Mesh {
pub fn append(&mut self, frame: &Frame) { pub fn append(&mut self, mesh: &Mesh) {
let index_offset = self.vertices.len() as u32; let index_offset = self.vertices.len() as u32;
for index in &frame.indices { for index in &mesh.indices {
self.indices.push(index_offset + index); self.indices.push(index_offset + index);
} }
self.vertices.extend(frame.vertices.iter()); self.vertices.extend(mesh.vertices.iter());
} }
fn triangle(&mut self, a: u32, b: u32, c: u32) { fn triangle(&mut self, a: u32, b: u32, c: u32) {
@ -74,7 +74,7 @@ pub struct Mesher {
pub aa_size: f32, pub aa_size: f32,
/// Where the output goes /// Where the output goes
pub frame: Frame, pub mesh: Mesh,
} }
impl Mesher { impl Mesher {
@ -82,7 +82,7 @@ impl Mesher {
Mesher { Mesher {
anti_alias: true, anti_alias: true,
aa_size: 1.0 / pixels_per_point, aa_size: 1.0 / pixels_per_point,
frame: Default::default(), mesh: Default::default(),
} }
} }
@ -94,32 +94,30 @@ impl Mesher {
uv: (0, 0), uv: (0, 0),
color, color,
}; };
let frame = &mut self.frame; let mesh = &mut self.mesh;
if self.anti_alias { if self.anti_alias {
let color_outer = color.transparent(); let color_outer = color.transparent();
let idx_inner = frame.vertices.len() as u32; let idx_inner = mesh.vertices.len() as u32;
let idx_outer = idx_inner + 1; let idx_outer = idx_inner + 1;
for i in 2..n { for i in 2..n {
frame.triangle(idx_inner + 2 * (i - 1), idx_inner, idx_inner + 2 * i); mesh.triangle(idx_inner + 2 * (i - 1), idx_inner, idx_inner + 2 * i);
} }
let mut i0 = n - 1; let mut i0 = n - 1;
for i1 in 0..n { for i1 in 0..n {
let dm = normals[i1 as usize] * self.aa_size * 0.5; let dm = normals[i1 as usize] * self.aa_size * 0.5;
frame.vertices.push(vert(points[i1 as usize] - dm, color)); mesh.vertices.push(vert(points[i1 as usize] - dm, color));
frame mesh.vertices
.vertices
.push(vert(points[i1 as usize] + dm, color_outer)); .push(vert(points[i1 as usize] + dm, color_outer));
frame.triangle(idx_inner + i1 * 2, idx_inner + i0 * 2, idx_outer + 2 * i0); mesh.triangle(idx_inner + i1 * 2, idx_inner + i0 * 2, idx_outer + 2 * i0);
frame.triangle(idx_outer + i0 * 2, idx_outer + i1 * 2, idx_inner + 2 * i1); mesh.triangle(idx_outer + i0 * 2, idx_outer + i1 * 2, idx_inner + 2 * i1);
i0 = i1; i0 = i1;
} }
} else { } else {
let idx = frame.vertices.len() as u32; let idx = mesh.vertices.len() as u32;
frame mesh.vertices
.vertices
.extend(points.iter().map(|&pos| vert(pos, color))); .extend(points.iter().map(|&pos| vert(pos, color)));
for i in 2..n { for i in 2..n {
frame.triangle(idx, idx + i - 1, idx + i); mesh.triangle(idx, idx + i - 1, idx + i);
} }
} }
} }
@ -135,14 +133,14 @@ impl Mesher {
assert_eq!(points.len(), normals.len()); assert_eq!(points.len(), normals.len());
let n = points.len() as u32; let n = points.len() as u32;
let hw = width / 2.0; let hw = width / 2.0;
let idx = self.frame.vertices.len() as u32; let idx = self.mesh.vertices.len() as u32;
let vert = |pos, color| Vertex { let vert = |pos, color| Vertex {
pos, pos,
uv: (0, 0), uv: (0, 0),
color, color,
}; };
let frame = &mut self.frame; let mesh = &mut self.mesh;
if self.anti_alias { if self.anti_alias {
let color_outer = color.transparent(); let color_outer = color.transparent();
@ -159,39 +157,37 @@ impl Mesher {
if thin_line { if thin_line {
let p = points[i1 as usize]; let p = points[i1 as usize];
let n = normals[i1 as usize]; let n = normals[i1 as usize];
frame.vertices.push(vert(p + n * self.aa_size, color_outer)); mesh.vertices.push(vert(p + n * self.aa_size, color_outer));
frame.vertices.push(vert(p, color_inner)); mesh.vertices.push(vert(p, color_inner));
frame.vertices.push(vert(p - n * self.aa_size, color_outer)); mesh.vertices.push(vert(p - n * self.aa_size, color_outer));
if connect_with_previous { if connect_with_previous {
frame.triangle(idx + 3 * i0 + 0, idx + 3 * i0 + 1, idx + 3 * i1 + 0); mesh.triangle(idx + 3 * i0 + 0, idx + 3 * i0 + 1, idx + 3 * i1 + 0);
frame.triangle(idx + 3 * i0 + 1, idx + 3 * i1 + 0, idx + 3 * i1 + 1); mesh.triangle(idx + 3 * i0 + 1, idx + 3 * i1 + 0, idx + 3 * i1 + 1);
frame.triangle(idx + 3 * i0 + 1, idx + 3 * i0 + 2, idx + 3 * i1 + 1); mesh.triangle(idx + 3 * i0 + 1, idx + 3 * i0 + 2, idx + 3 * i1 + 1);
frame.triangle(idx + 3 * i0 + 2, idx + 3 * i1 + 1, idx + 3 * i1 + 2); mesh.triangle(idx + 3 * i0 + 2, idx + 3 * i1 + 1, idx + 3 * i1 + 2);
} }
} else { } else {
let hw = (width - self.aa_size) * 0.5; let hw = (width - self.aa_size) * 0.5;
let p = points[i1 as usize]; let p = points[i1 as usize];
let n = normals[i1 as usize]; let n = normals[i1 as usize];
frame mesh.vertices
.vertices
.push(vert(p + n * (hw + self.aa_size), color_outer)); .push(vert(p + n * (hw + self.aa_size), color_outer));
frame.vertices.push(vert(p + n * (hw + 0.0), color_inner)); mesh.vertices.push(vert(p + n * (hw + 0.0), color_inner));
frame.vertices.push(vert(p - n * (hw + 0.0), color_inner)); mesh.vertices.push(vert(p - n * (hw + 0.0), color_inner));
frame mesh.vertices
.vertices
.push(vert(p - n * (hw + self.aa_size), color_outer)); .push(vert(p - n * (hw + self.aa_size), color_outer));
if connect_with_previous { if connect_with_previous {
frame.triangle(idx + 4 * i0 + 0, idx + 4 * i0 + 1, idx + 4 * i1 + 0); mesh.triangle(idx + 4 * i0 + 0, idx + 4 * i0 + 1, idx + 4 * i1 + 0);
frame.triangle(idx + 4 * i0 + 1, idx + 4 * i1 + 0, idx + 4 * i1 + 1); mesh.triangle(idx + 4 * i0 + 1, idx + 4 * i1 + 0, idx + 4 * i1 + 1);
frame.triangle(idx + 4 * i0 + 1, idx + 4 * i0 + 2, idx + 4 * i1 + 1); mesh.triangle(idx + 4 * i0 + 1, idx + 4 * i0 + 2, idx + 4 * i1 + 1);
frame.triangle(idx + 4 * i0 + 2, idx + 4 * i1 + 1, idx + 4 * i1 + 2); mesh.triangle(idx + 4 * i0 + 2, idx + 4 * i1 + 1, idx + 4 * i1 + 2);
frame.triangle(idx + 4 * i0 + 2, idx + 4 * i0 + 3, idx + 4 * i1 + 2); mesh.triangle(idx + 4 * i0 + 2, idx + 4 * i0 + 3, idx + 4 * i1 + 2);
frame.triangle(idx + 4 * i0 + 3, idx + 4 * i1 + 2, idx + 4 * i1 + 3); mesh.triangle(idx + 4 * i0 + 3, idx + 4 * i1 + 2, idx + 4 * i1 + 3);
} }
} }
i0 = i1; i0 = i1;
@ -199,12 +195,12 @@ impl Mesher {
} else { } else {
let last_index = if path_type == Closed { n } else { n - 1 }; let last_index = if path_type == Closed { n } else { n - 1 };
for i in 0..last_index { for i in 0..last_index {
frame.triangle( mesh.triangle(
idx + (2 * i + 0) % (2 * n), idx + (2 * i + 0) % (2 * n),
idx + (2 * i + 1) % (2 * n), idx + (2 * i + 1) % (2 * n),
idx + (2 * i + 2) % (2 * n), idx + (2 * i + 2) % (2 * n),
); );
frame.triangle( mesh.triangle(
idx + (2 * i + 2) % (2 * n), idx + (2 * i + 2) % (2 * n),
idx + (2 * i + 1) % (2 * n), idx + (2 * i + 1) % (2 * n),
idx + (2 * i + 3) % (2 * n), idx + (2 * i + 3) % (2 * n),
@ -212,8 +208,8 @@ impl Mesher {
} }
for (&p, &n) in points.iter().zip(normals) { for (&p, &n) in points.iter().zip(normals) {
frame.vertices.push(vert(p + hw * n, color)); mesh.vertices.push(vert(p + hw * n, color));
frame.vertices.push(vert(p - hw * n, color)); mesh.vertices.push(vert(p - hw * n, color));
} }
} }
} }
@ -254,8 +250,8 @@ impl Mesher {
); );
} }
} }
PaintCmd::Frame(cmd_frame) => { PaintCmd::Mesh(cmd_frame) => {
self.frame.append(cmd_frame); self.mesh.append(cmd_frame);
} }
PaintCmd::Line { PaintCmd::Line {
points, points,
@ -369,7 +365,7 @@ impl Mesher {
uv: glyph.max, uv: glyph.max,
color: *color, color: *color,
}; };
self.frame.add_rect(top_left, bottom_right); self.mesh.add_rect(top_left, bottom_right);
} }
} }
} }

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
fonts::TextStyle, fonts::TextStyle,
math::{Rect, Vec2}, math::{Rect, Vec2},
mesher::Frame, mesher::Mesh,
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -160,7 +160,7 @@ pub enum PaintCmd {
outline: Option<Outline>, outline: Option<Outline>,
radius: f32, radius: f32,
}, },
Frame(Frame), Mesh(Mesh),
Line { Line {
points: Vec<Vec2>, points: Vec<Vec2>,
color: Color, color: Color,

View file

@ -4,7 +4,7 @@ use {
web_sys::{WebGlBuffer, WebGlProgram, WebGlRenderingContext, WebGlShader, WebGlTexture}, web_sys::{WebGlBuffer, WebGlProgram, WebGlRenderingContext, WebGlShader, WebGlTexture},
}; };
use emigui::{Frame, Texture}; use emigui::{Mesh, Texture};
type Gl = WebGlRenderingContext; type Gl = WebGlRenderingContext;
@ -145,7 +145,7 @@ impl Painter {
pub fn paint( pub fn paint(
&mut self, &mut self,
frame: &Frame, mesh: &Mesh,
texture: &Texture, texture: &Texture,
pixels_per_point: f32, pixels_per_point: f32,
) -> Result<(), JsValue> { ) -> Result<(), JsValue> {
@ -163,19 +163,19 @@ impl Painter {
// -------------------------------------------------------------------- // --------------------------------------------------------------------
let indices: Vec<u16> = frame.indices.iter().map(|idx| *idx as u16).collect(); let indices: Vec<u16> = mesh.indices.iter().map(|idx| *idx as u16).collect();
let mut positions: Vec<f32> = Vec::with_capacity(2 * frame.vertices.len()); let mut positions: Vec<f32> = Vec::with_capacity(2 * mesh.vertices.len());
let mut tex_coords: Vec<u16> = Vec::with_capacity(2 * frame.vertices.len()); let mut tex_coords: Vec<u16> = Vec::with_capacity(2 * mesh.vertices.len());
for v in &frame.vertices { for v in &mesh.vertices {
positions.push(v.pos.x); positions.push(v.pos.x);
positions.push(v.pos.y); positions.push(v.pos.y);
tex_coords.push(v.uv.0); tex_coords.push(v.uv.0);
tex_coords.push(v.uv.1); tex_coords.push(v.uv.1);
} }
let mut colors: Vec<u8> = Vec::with_capacity(4 * frame.vertices.len()); let mut colors: Vec<u8> = Vec::with_capacity(4 * mesh.vertices.len());
for v in &frame.vertices { for v in &mesh.vertices {
colors.push(v.color.r); colors.push(v.color.r);
colors.push(v.color.g); colors.push(v.color.g);
colors.push(v.color.b); colors.push(v.color.b);

View file

@ -50,10 +50,10 @@ impl State {
region.add(label!("Everything: {:.1} ms", self.everything_ms)); region.add(label!("Everything: {:.1} ms", self.everything_ms));
let frame = self.emigui.paint(); let mesh = self.emigui.paint();
let result = let result =
self.webgl_painter self.webgl_painter
.paint(&frame, self.emigui.texture(), raw_input.pixels_per_point); .paint(&mesh, self.emigui.texture(), raw_input.pixels_per_point);
self.everything_ms = 1000.0 * (now_sec() - everything_start); self.everything_ms = 1000.0 * (now_sec() - everything_start);