From e79499cd864dc64017316ef4512991df68837576 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Tue, 3 Mar 2020 09:14:31 -0600 Subject: idea on how to split source files --- src/main.rs | 83 ++++++++++++---------------------------------------- src/tom.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 src/tom.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 07d3857..917bd64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ pub mod constants; +pub mod tom; + use luminance::context::GraphicsContext; use luminance::pipeline::{PipelineState, Viewport}; use luminance::render_state::RenderState; @@ -6,50 +8,7 @@ use luminance::shader::program::Program; use luminance::tess::{Mode, TessBuilder, TessSliceIndex}; use luminance_derive::{Semantics, Vertex}; use luminance_glfw::{GlfwSurface, Surface, WindowDim, WindowEvent, WindowOpt}; - -#[derive(Vertex)] -#[vertex(sem = "VertexSemantics")] -pub struct Vertex { - position: VertexPosition, - #[vertex(normalized = "true")] - color: VertexRGB, -} - -#[derive(Copy, Clone, Debug, Semantics)] -pub enum VertexSemantics { - #[sem(name = "position", repr = "[f32; 2]", wrapper = "VertexPosition")] - Position, - #[sem(name = "color", repr = "[u8; 3]", wrapper = "VertexRGB")] - Color, -} - -fn gen_vertices() -> Vec { - let mut vertices: Vec = Vec::new(); - - vertices.push(Vertex { - position: VertexPosition::new([-0.5, -0.5]), - color: VertexRGB::new([255, 0, 0]), - }); - vertices.push(Vertex { - position: VertexPosition::new([0.5, -0.5]), - color: VertexRGB::new([0, 255, 0]), - }); - vertices.push(Vertex { - position: VertexPosition::new([0.0, 0.5]), - color: VertexRGB::new([0, 0, 255]), - }); - - vertices -} - -fn alter_vertices(vertices: &mut Vec) { - for vertex in vertices { - vertex.position[0] += 0.01; - if vertex.color[1] < 255 { - vertex.color[1] += 1; - } - } -} +use tom::{Tom, VertexSemantics}; fn gen_viewports() -> Vec { let mut viewports = Vec::new(); @@ -63,7 +22,7 @@ fn gen_viewports() -> Vec { viewports.push(Viewport::Specific { x: 0, - y: constants::WIDTH / 2, + y: constants::HEIGHT / 2, width: constants::WIDTH / 2, height: constants::HEIGHT / 2, }); @@ -88,7 +47,7 @@ fn gen_viewports() -> Vec { fn main() { let mut surface = GlfwSurface::new( WindowDim::Windowed(constants::WIDTH, constants::HEIGHT), - "art", + "revision2020", WindowOpt::default(), ) .unwrap(); @@ -98,7 +57,7 @@ fn main() { .unwrap() .ignore_warnings(); - let mut vertices = gen_vertices(); + let mut tom = Tom::new(); let viewports = gen_viewports(); let mut run = true; @@ -109,29 +68,23 @@ fn main() { } } let back_buffer = surface.back_buffer().unwrap(); - alter_vertices(&mut vertices); - let triangle = TessBuilder::new(&mut surface) - .add_vertices(&vertices) - .set_mode(Mode::Triangle) - .build() - .unwrap(); + tom.update(); let mut pipeline_state = PipelineState::default(); - for viewport in viewports.iter() { + surface + .pipeline_builder() + .pipeline(&back_buffer, &pipeline_state, |_, _| {}); + + for (i, viewport) in viewports.iter().enumerate() { pipeline_state = pipeline_state.set_viewport(*viewport); + pipeline_state = pipeline_state.enable_clear_color(false); - surface.pipeline_builder().pipeline( - &back_buffer, - &pipeline_state, - |_pipeline, mut shd_gate| { - shd_gate.shade(&program, |_, mut rdr_gate| { - rdr_gate.render(&RenderState::default(), |mut tess_gate| { - tess_gate.render(triangle.slice(..)); - }); - }); - }, - ); + if i == 0 { + tom.draw(); + } else if i == 1 { + // lowman.draw() + } } surface.swap_buffers(); diff --git a/src/tom.rs b/src/tom.rs new file mode 100644 index 0000000..d1daa0a --- /dev/null +++ b/src/tom.rs @@ -0,0 +1,96 @@ +use luminance::context::GraphicsContext; +use luminance::pipeline::{PipelineState, Viewport}; +use luminance::render_state::RenderState; +use luminance::shader::program::Program; +use luminance::tess::{Mode, TessBuilder, TessSliceIndex}; +use luminance_derive::{Semantics, Vertex}; +use luminance_glfw::{GlfwSurface, Surface, WindowDim, WindowEvent, WindowOpt}; + +#[derive(Vertex)] +#[vertex(sem = "VertexSemantics")] +pub struct Vertex { + position: VertexPosition, + #[vertex(normalized = "true")] + color: VertexRGB, +} + +#[derive(Copy, Clone, Debug, Semantics)] +pub enum VertexSemantics { + #[sem(name = "position", repr = "[f32; 2]", wrapper = "VertexPosition")] + Position, + #[sem(name = "color", repr = "[u8; 3]", wrapper = "VertexRGB")] + Color, +} + +fn gen_vertices() -> Vec { + let mut vertices: Vec = Vec::new(); + + vertices.push(Vertex { + position: VertexPosition::new([-0.5, -0.5]), + color: VertexRGB::new([255, 0, 0]), + }); + vertices.push(Vertex { + position: VertexPosition::new([0.5, -0.5]), + color: VertexRGB::new([0, 255, 0]), + }); + vertices.push(Vertex { + position: VertexPosition::new([0.0, 0.5]), + color: VertexRGB::new([0, 0, 255]), + }); + + vertices +} + +fn alter_vertices(vertices: &mut Vec) { + for vertex in vertices { + vertex.position[0] += 0.01; + if vertex.color[1] < 255 { + vertex.color[1] += 1; + } + } +} + +pub struct Tom { + vertices: Vec, + //tessalation: Tess, +} + +impl Tom { + pub fn new() -> Tom { + let mut vertices = gen_vertices(); + //let tessalation = TessBuilder::new(&mut surface) + // .add_vertices(&vertices) + // .set_mode(Mode::Triangle) + // .build() + // .unwrap(); + Tom { + vertices, + // tessalation, + } + } + + pub fn update(&mut self) { + alter_vertices(&mut self.vertices); + //let tessalation = TessBuilder::new(&mut surface) + // .add_vertices(&self.vertices) + // .set_mode(Mode::Triangle) + // .build() + // .unwrap(); + } + + pub fn draw(&self) { + /* + surface.pipeline_builder().pipeline( + &back_buffer, + &pipeline_state, + |_pipeline, mut shd_gate| { + shd_gate.shade(&program, |_, mut rdr_gate| { + rdr_gate.render(&RenderState::default(), |mut tess_gate| { + //tess_gate.render(triangle.slice(..)); + }); + }); + }, + ); + */ + } +} -- cgit v1.2.3