Draw Canary DrawCommands
This commit is contained in:
parent
28c6a3e1e6
commit
a058d027ef
|
@ -1,3 +1,4 @@
|
|||
use canary::DrawCommand;
|
||||
use glium::{glutin, Display, Surface};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
@ -45,14 +46,57 @@ const FRAGMENT_SHADER_SRC: &str = r#"
|
|||
"#;
|
||||
|
||||
pub struct Graphics {
|
||||
pub display: glium::Display,
|
||||
pub program: glium::Program,
|
||||
}
|
||||
|
||||
impl Graphics {
|
||||
pub fn new(display: &glium::Display) -> Self {
|
||||
pub fn new(display: glium::Display) -> Self {
|
||||
let program =
|
||||
glium::Program::from_source(display, VERTEX_SHADER_SRC, FRAGMENT_SHADER_SRC, None)
|
||||
glium::Program::from_source(&display, VERTEX_SHADER_SRC, FRAGMENT_SHADER_SRC, None)
|
||||
.unwrap();
|
||||
Self { program }
|
||||
Self { display, program }
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, commands: &[DrawCommand]) {
|
||||
let mut joined_vs: Vec<Vertex> = Vec::new();
|
||||
let mut joined_is = Vec::new();
|
||||
|
||||
for command in commands.iter() {
|
||||
match command {
|
||||
canary::DrawCommand::Mesh { vertices, indices } => {
|
||||
let voff = joined_vs.len() as canary::MeshIndex;
|
||||
joined_vs.extend(vertices.iter().map(Vertex::from));
|
||||
joined_is.extend(indices.iter().map(|i| i + voff));
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
let vertex_buffer = glium::VertexBuffer::new(&self.display, &joined_vs).unwrap();
|
||||
let index_buffer = glium::IndexBuffer::new(
|
||||
&self.display,
|
||||
glium::index::PrimitiveType::TrianglesList,
|
||||
&joined_is,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let params = glium::DrawParameters {
|
||||
blend: glium::Blend::alpha_blending(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let mut target = self.display.draw();
|
||||
target.clear_color(0.0, 0.0, 0.0, 1.0);
|
||||
target
|
||||
.draw(
|
||||
&vertex_buffer,
|
||||
&index_buffer,
|
||||
&self.program,
|
||||
&glium::uniforms::EmptyUniforms,
|
||||
¶ms,
|
||||
)
|
||||
.unwrap();
|
||||
target.finish().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ pub enum WindowMessage {
|
|||
pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
|
||||
|
||||
pub struct Window {
|
||||
pub display: glium::Display,
|
||||
pub graphics: Graphics,
|
||||
}
|
||||
|
||||
|
@ -29,13 +28,17 @@ impl Window {
|
|||
let wb = glutin::window::WindowBuilder::new();
|
||||
let cb = glutin::ContextBuilder::new();
|
||||
let display = glium::Display::new(wb, cb, &event_loop)?;
|
||||
let graphics = Graphics::new(&display);
|
||||
let graphics = Graphics::new(display);
|
||||
|
||||
Ok(Self { display, graphics })
|
||||
Ok(Self { graphics })
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> WindowId {
|
||||
self.graphics.display.gl_window().window().id()
|
||||
}
|
||||
|
||||
pub fn request_redraw(&mut self) {
|
||||
self.display.gl_window().window().request_redraw();
|
||||
self.graphics.display.gl_window().window().request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,17 +68,15 @@ impl WindowStore {
|
|||
}
|
||||
}
|
||||
Event::RedrawRequested(id) => {
|
||||
if let Some(window) = self.windows.get(&id) {
|
||||
let mut target = window.display.draw();
|
||||
target.clear_color(0.0, 0.0, 0.0, 1.0);
|
||||
target.finish().unwrap();
|
||||
if let Some(window) = self.windows.get_mut(&id) {
|
||||
window.graphics.draw(&[]);
|
||||
}
|
||||
}
|
||||
Event::UserEvent(event) => match event {
|
||||
WindowMessage::OpenWindow { id } => {
|
||||
println!("Opening window {}", id);
|
||||
let window = Window::new(&event_loop).unwrap();
|
||||
let window_id = window.display.gl_window().window().id();
|
||||
let window_id = window.get_id();
|
||||
self.windows.insert(window_id, window);
|
||||
self.ipc_to_window.insert(id, window_id);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue