From 358ece1f4b81ebf9adc610fd9228809a560c21d8 Mon Sep 17 00:00:00 2001 From: Tsuki Date: Fri, 19 Jul 2024 11:02:37 +0800 Subject: [PATCH] check --- src/graphics/collections/agg_fast_path.rs | 43 ++++++++++++- src/graphics/hello.rs | 2 +- src/graphics/mod.rs | 7 +-- src/graphics/ppi.rs | 51 +++++++++------ src/main.rs | 77 ++++++++++++++++------- src/support/supporter.rs | 17 ++--- 6 files changed, 134 insertions(+), 63 deletions(-) diff --git a/src/graphics/collections/agg_fast_path.rs b/src/graphics/collections/agg_fast_path.rs index ca97129..656649b 100644 --- a/src/graphics/collections/agg_fast_path.rs +++ b/src/graphics/collections/agg_fast_path.rs @@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut}; use bytemuck::{Pod, Zeroable}; use glow::{HasContext, NativeBuffer, NativeVertexArray}; -use crate::components::{fetchcode, CodeComponent, CodeType, Program, Shader, Snippet}; +use crate::components::{CodeComponent, CodeType, Program, Shader, Snippet}; use crate::errors::*; use crate::graphics::transforms::viewport::Viewport; use crate::graphics::transforms::Transform; @@ -15,9 +15,11 @@ use super::Colletion; pub struct AggFastPath { program: Program, buffer: Vec, + indice: Vec<[u32; 3]>, vao: Option, vbo: Option, + ebo: Option, } impl AggFastPath { @@ -63,6 +65,8 @@ impl AggFastPath { Ok(Self { program, buffer: Vec::with_capacity(128), + indice: Vec::with_capacity(128 * 2), + ebo: None, vao: None, vbo: None, }) @@ -111,6 +115,7 @@ impl Colletion for AggFastPath { fn append(&mut self, item: Self::Item) { self.buffer.extend(item.points); + self.indice.extend(item.ebo); } } @@ -118,6 +123,9 @@ impl Graphics for AggFastPath { fn compile(&mut self, gl: &glow::Context) -> Result<()> { use bytemuck::cast_slice; self.program.compile(gl)?; + self.set_anatialias(gl, 1.0); + self.set_linecolor(gl, [1.0, 1.0, 1.0, 1.0]); + self.set_linewidth(gl, 20.0); unsafe { let vao = gl.create_vertex_array().unwrap(); @@ -143,8 +151,18 @@ impl Graphics for AggFastPath { gl.enable_vertex_attrib_array(3); gl.vertex_attrib_pointer_f32(3, 1, glow::FLOAT, false, 40, 36); + let ebo = gl.create_buffer().unwrap(); + gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(ebo)); + + gl.buffer_data_u8_slice( + glow::ELEMENT_ARRAY_BUFFER, + cast_slice(&self.indice), + glow::STATIC_DRAW, + ); + self.vao = Some(vao); self.vbo = Some(vbo); + self.ebo = Some(ebo); gl.bind_vertex_array(None); } @@ -152,12 +170,20 @@ impl Graphics for AggFastPath { Ok(()) } - fn draw(&self, gl: &glow::Context) -> Result<()> { + fn draw(&mut self, gl: &glow::Context) -> Result<()> { unsafe { gl.clear(glow::COLOR_BUFFER_BIT); gl.use_program(Some(self.program.native_program.unwrap())); gl.bind_vertex_array(Some(self.vao.unwrap())); - gl.draw_arrays(glow::TRIANGLE_STRIP, 0, self.buffer.len() as i32); + + gl.polygon_mode(glow::FRONT_AND_BACK, glow::LINE); + // gl.draw_arrays(glow::TRIANGLE_STRIP, 0, self.buffer.len() as i32); + gl.draw_elements( + glow::TRIANGLES, + (self.indice.len() * 3) as i32, + glow::UNSIGNED_INT, + 0, + ); gl.bind_vertex_array(None); } @@ -169,6 +195,7 @@ impl Graphics for AggFastPath { self.program.destroy(gl); unsafe { + self.ebo.map(|ebo| gl.delete_buffer(ebo)); self.vao.map(|vao| gl.delete_vertex_array(vao)); self.vbo.map(|vbo| gl.delete_buffer(vbo)); } @@ -191,6 +218,7 @@ impl Ty for Point {} #[derive(Debug)] pub struct Path { points: Vec, + ebo: Vec<[u32; 3]>, is_closed: bool, is_empty: bool, } @@ -199,6 +227,7 @@ impl Default for Path { fn default() -> Self { Self { points: Vec::with_capacity(500), + ebo: Vec::with_capacity(500), is_closed: false, is_empty: true, } @@ -224,6 +253,7 @@ impl PathBuilder { pub fn build(&self) -> Path { Path { points: Vec::with_capacity(500), + ebo: Vec::with_capacity(500), is_closed: self.is_closed, is_empty: true, } @@ -234,6 +264,7 @@ impl Path { pub fn new(is_closed: bool) -> Self { Self { points: Vec::with_capacity(500), + ebo: Vec::with_capacity(500), is_closed, is_empty: true, } @@ -304,6 +335,12 @@ impl Path { self.points[len - 1].next = curr; self.points[len - 2].next = curr; } + + for s in 0..self.points.len() - 1 { + let s = s as u32; + self.ebo.push([s * 2, s * 2 + 1, (s + 1) * 2 + 1]); + self.ebo.push([s * 2, (s + 1) * 2, (s + 1) * 2 + 1]); + } } pub fn len(&self) -> usize { diff --git a/src/graphics/hello.rs b/src/graphics/hello.rs index f6b63ef..76f1470 100644 --- a/src/graphics/hello.rs +++ b/src/graphics/hello.rs @@ -64,7 +64,7 @@ impl Graphics for Hello { Ok(()) } - fn draw(&self, gl: &glow::Context) -> Result<()> { + fn draw(&mut self, gl: &glow::Context) -> Result<()> { unsafe { gl.clear_color(0.05, 0.05, 0.1, 1.0); gl.clear(glow::COLOR_BUFFER_BIT); diff --git a/src/graphics/mod.rs b/src/graphics/mod.rs index 73d45ea..63c62d5 100644 --- a/src/graphics/mod.rs +++ b/src/graphics/mod.rs @@ -12,7 +12,7 @@ pub use colormesh::ColorMesh; use imgui::TextureId; pub trait Graphics { - fn draw(&self, gl: &glow::Context) -> Result<()>; + fn draw(&mut self, gl: &glow::Context) -> Result<()>; fn compile(&mut self, gl: &glow::Context) -> Result<()>; @@ -28,9 +28,6 @@ pub trait AttaWithBuffer { fn attach_with_buffer(&mut self, gl: &glow::Context, data: &Self::Data) -> Result<()>; } - pub trait AttaWithWindow { - fn attach_with_window(&mut self, gl: &glow::Context) -> Result; - -} \ No newline at end of file +} diff --git a/src/graphics/ppi.rs b/src/graphics/ppi.rs index 4911d55..0430c2c 100644 --- a/src/graphics/ppi.rs +++ b/src/graphics/ppi.rs @@ -25,6 +25,8 @@ pub struct PPI { framebuffer: Option, frametexture: Option, buffer: Vec<[f32; 3]>, + + need_redraw: bool, } impl PPI { @@ -55,6 +57,7 @@ impl PPI { framebuffer: None, frametexture: None, cmap: None, + need_redraw: true, }) } @@ -85,6 +88,7 @@ impl PPI { pub fn add_ppi_layer(&mut self, layer: isize) { self.layer = self.layer + layer; self.layer.clamp(0, 23); + self.need_redraw = true; } pub fn program_ref(&self) -> &Program { @@ -215,43 +219,36 @@ impl Graphics for PPI { Ok(()) } - fn draw(&self, gl: &glow::Context) -> Result<()> { + fn draw(&mut self, gl: &glow::Context) -> Result<()> { + if !self.need_redraw { + return Ok(()); + } unsafe { - gl.clear(glow::COLOR_BUFFER_BIT); gl.use_program(Some(self.program.native_program.unwrap())); + if self.framebuffer.is_some() { + gl.bind_framebuffer(glow::FRAMEBUFFER, self.framebuffer); + } + + gl.viewport(0, 0, 800, 600); + gl.clear(glow::COLOR_BUFFER_BIT); self.cmap .as_ref() .unwrap() .bind_texture(gl, &self.program)?; - if self.framebuffer.is_some() { - gl.bind_framebuffer(glow::FRAMEBUFFER, self.framebuffer); - } - gl.depth_mask(false); gl.bind_vertex_array(Some(self.vao.unwrap())); gl.draw_arrays(glow::POINTS, 0, self.buffer.len() as i32); gl.depth_mask(true); if self.framebuffer.is_some() { - gl.framebuffer_texture_2d( - glow::FRAMEBUFFER, - glow::COLOR_ATTACHMENT0, - glow::TEXTURE_2D, - self.frametexture, - 0, - ); - - assert_eq!( - gl.check_framebuffer_status(glow::FRAMEBUFFER), - glow::FRAMEBUFFER_COMPLETE - ); - gl.bind_texture(glow::TEXTURE_2D, None); gl.bind_framebuffer(glow::FRAMEBUFFER, None); } gl.bind_vertex_array(None); gl.use_program(None); + + self.need_redraw = false; } Ok(()) } @@ -274,6 +271,7 @@ impl AttaWithBuffer for PPI { glow::STATIC_DRAW, ); } + self.need_redraw = true; Ok(()) } @@ -311,8 +309,21 @@ impl AttaWithWindow for PPI { glow::LINEAR as i32, ); + gl.framebuffer_texture_2d( + glow::FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + glow::TEXTURE_2D, + Some(texture), + 0, + ); + + assert_eq!( + gl.check_framebuffer_status(glow::FRAMEBUFFER), + glow::FRAMEBUFFER_COMPLETE + ); + gl.bind_framebuffer(glow::FRAMEBUFFER, None); - gl.bind_texture(glow::TEXTURE_2D, None); + self.framebuffer = Some(framebuffer); self.frametexture = Some(texture); Ok(imgui::TextureId::from(texture.0.get() as usize)) } else { diff --git a/src/main.rs b/src/main.rs index cef3258..805480e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,13 @@ use std::cell::RefCell; use std::rc::Rc; use data_loader::Data; +use graphics::collections::agg_fast_path::{AggFastPath, Path}; +use graphics::collections::Colletion; use graphics::colormap::linear::LinearColormap; use graphics::ppi::PPI; use graphics::transforms::position::Position; use graphics::transforms::viewport::Viewport; -use graphics::{AttaWithBuffer, Graphics, AttaWithWindow}; +use graphics::{AttaWithBuffer, AttaWithWindow, Graphics}; use imgui::Condition; mod camera; @@ -20,12 +22,28 @@ use imgui::*; mod final_pg; +type Graphic = Rc>; + fn main() { env_logger::init(); + let mut path = Path::builder().build(); + + path.push([-1.0, 0.0, 0.0]); + path.push([0.0, 0.5, 0.0]); + path.push([1.0, 1.0, 0.0]); + + path.finish(); + + let mut fast_path = AggFastPath::new().unwrap(); + let transform = Position::new().unwrap(); let mut viewport = Viewport::new().unwrap(); - viewport.set_global(0.0, 0.0, 600.0, 800.0); + viewport.set_global(0.0, 0.0, 800.0, 600.0); + + fast_path.set_transform(&transform); + fast_path.set_viewport(&viewport); + fast_path.append(path); let mut cmap = LinearColormap::new().unwrap(); @@ -55,29 +73,42 @@ fn main() { let ppi = Rc::new(RefCell::new(ppi)); - let registers = vec![ppi.clone()]; - let registers = registers.into_iter().map(|r| { - r as Rc> - }).collect(); + let fast_path = Rc::new(RefCell::new(fast_path)); + let registers = vec![fast_path.clone() as Graphic]; + let mut data = + Data::from_path(r#"/Users/tsuki/Desktop/ZJSXAA_20230113070200_R.dat.gz"#).unwrap(); - support::supporter::init(move |run, ui, window, gl| { - let mut ppi = ppi.borrow_mut(); - let texture = ppi.attach_with_window(gl).unwrap(); + support::supporter::init( + move |run, ui, window, gl| { + // let mut ppi = ppi.borrow_mut(); + let mut fast_path = fast_path.borrow_mut(); + // let texture = ppi.attach_with_window(gl).unwrap(); - ui.window("test").build(||{ - if ui.button("load") { - let data = Data::from_path(r#"C:\Users\qwin7\Downloads\ZJSXAA_20230113070200_R.dat.gz"#).unwrap(); - ppi.attach_with_buffer(gl, &data).unwrap(); - } - }); + ui.window("test").build(|| { + if ui.button("load") { + // ppi.attach_with_buffer(gl, &data).unwrap(); + } - ui.window("Test").size([200.0,400.0], imgui::Condition::FirstUseEver).build(|| { - imgui::Image::new(texture, [200.0, 400.0]).build(ui); - }); + ui.separator(); + if ui.button("Add Layer") { + // ppi.add_ppi_layer(1); + // ppi.attach_with_buffer(gl, &data).unwrap(); + } - - }, - registers, -); -} \ No newline at end of file + if ui.button("Remove Layer") { + // ppi.add_ppi_layer(-1); + // ppi.attach_with_buffer(gl, &data).unwrap(); + } + }); + + // ui.window("Test") + // .size([800.0, 600.0], imgui::Condition::FirstUseEver) + // .build(|| { + // let window_size = ui.window_size(); + // // imgui::Image::new(texture, window_size).build(ui); + // }); + }, + registers, + ); +} diff --git a/src/support/supporter.rs b/src/support/supporter.rs index 1f3b67f..5d9ebfd 100644 --- a/src/support/supporter.rs +++ b/src/support/supporter.rs @@ -37,7 +37,7 @@ use std::num::NonZeroU32; use std::rc::Rc; use std::time::Instant; -pub fn init(mut run_ui: FUi, mut register: Vec>>) +pub fn init(mut run_ui: FUi, mut register: Vec>>) where FUi: FnMut(&mut bool, &mut Ui, &Window, &glow::Context) + 'static, { @@ -60,7 +60,7 @@ where for r in register.iter_mut() { r.borrow_mut().compile(&gl_context).unwrap(); } - + let mut last_frame = Instant::now(); event_loop @@ -96,13 +96,11 @@ where if ui.is_mouse_pos_valid(ui.io().mouse_pos) { let mouse_pos = ui.io().mouse_pos; - if ui.is_mouse_dragging(imgui::MouseButton::Right) { - } - } - - for r in register.iter_mut() { - r.borrow_mut().draw(&gl_context).unwrap(); + if ui.is_mouse_dragging(imgui::MouseButton::Right) {} } + // for r in register.iter_mut() { + // r.borrow_mut().draw(&gl_context).unwrap(); + // } let mut run = true; @@ -140,16 +138,13 @@ where NonZeroU32::new(new_size.width).unwrap(), NonZeroU32::new(new_size.height).unwrap(), ); - } winit_platform.handle_event(imgui_context.io_mut(), &window, &event); } winit::event::Event::LoopExiting => { - for r in register.iter_mut() { r.borrow_mut().destroy(&gl_context).unwrap(); } - } event => { winit_platform.handle_event(imgui_context.io_mut(), &window, &event);