Compare commits

...

1 Commits

Author SHA1 Message Date
358ece1f4b check 2024-07-19 11:02:37 +08:00
6 changed files with 134 additions and 63 deletions

View File

@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut};
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use glow::{HasContext, NativeBuffer, NativeVertexArray}; 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::errors::*;
use crate::graphics::transforms::viewport::Viewport; use crate::graphics::transforms::viewport::Viewport;
use crate::graphics::transforms::Transform; use crate::graphics::transforms::Transform;
@ -15,9 +15,11 @@ use super::Colletion;
pub struct AggFastPath { pub struct AggFastPath {
program: Program, program: Program,
buffer: Vec<Point>, buffer: Vec<Point>,
indice: Vec<[u32; 3]>,
vao: Option<NativeVertexArray>, vao: Option<NativeVertexArray>,
vbo: Option<NativeBuffer>, vbo: Option<NativeBuffer>,
ebo: Option<NativeBuffer>,
} }
impl AggFastPath { impl AggFastPath {
@ -63,6 +65,8 @@ impl AggFastPath {
Ok(Self { Ok(Self {
program, program,
buffer: Vec::with_capacity(128), buffer: Vec::with_capacity(128),
indice: Vec::with_capacity(128 * 2),
ebo: None,
vao: None, vao: None,
vbo: None, vbo: None,
}) })
@ -111,6 +115,7 @@ impl Colletion for AggFastPath {
fn append(&mut self, item: Self::Item) { fn append(&mut self, item: Self::Item) {
self.buffer.extend(item.points); 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<()> { fn compile(&mut self, gl: &glow::Context) -> Result<()> {
use bytemuck::cast_slice; use bytemuck::cast_slice;
self.program.compile(gl)?; 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 { unsafe {
let vao = gl.create_vertex_array().unwrap(); let vao = gl.create_vertex_array().unwrap();
@ -143,8 +151,18 @@ impl Graphics for AggFastPath {
gl.enable_vertex_attrib_array(3); gl.enable_vertex_attrib_array(3);
gl.vertex_attrib_pointer_f32(3, 1, glow::FLOAT, false, 40, 36); 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.vao = Some(vao);
self.vbo = Some(vbo); self.vbo = Some(vbo);
self.ebo = Some(ebo);
gl.bind_vertex_array(None); gl.bind_vertex_array(None);
} }
@ -152,12 +170,20 @@ impl Graphics for AggFastPath {
Ok(()) Ok(())
} }
fn draw(&self, gl: &glow::Context) -> Result<()> { fn draw(&mut self, gl: &glow::Context) -> Result<()> {
unsafe { unsafe {
gl.clear(glow::COLOR_BUFFER_BIT); gl.clear(glow::COLOR_BUFFER_BIT);
gl.use_program(Some(self.program.native_program.unwrap())); gl.use_program(Some(self.program.native_program.unwrap()));
gl.bind_vertex_array(Some(self.vao.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); gl.bind_vertex_array(None);
} }
@ -169,6 +195,7 @@ impl Graphics for AggFastPath {
self.program.destroy(gl); self.program.destroy(gl);
unsafe { unsafe {
self.ebo.map(|ebo| gl.delete_buffer(ebo));
self.vao.map(|vao| gl.delete_vertex_array(vao)); self.vao.map(|vao| gl.delete_vertex_array(vao));
self.vbo.map(|vbo| gl.delete_buffer(vbo)); self.vbo.map(|vbo| gl.delete_buffer(vbo));
} }
@ -191,6 +218,7 @@ impl Ty for Point {}
#[derive(Debug)] #[derive(Debug)]
pub struct Path { pub struct Path {
points: Vec<Point>, points: Vec<Point>,
ebo: Vec<[u32; 3]>,
is_closed: bool, is_closed: bool,
is_empty: bool, is_empty: bool,
} }
@ -199,6 +227,7 @@ impl Default for Path {
fn default() -> Self { fn default() -> Self {
Self { Self {
points: Vec::with_capacity(500), points: Vec::with_capacity(500),
ebo: Vec::with_capacity(500),
is_closed: false, is_closed: false,
is_empty: true, is_empty: true,
} }
@ -224,6 +253,7 @@ impl PathBuilder {
pub fn build(&self) -> Path { pub fn build(&self) -> Path {
Path { Path {
points: Vec::with_capacity(500), points: Vec::with_capacity(500),
ebo: Vec::with_capacity(500),
is_closed: self.is_closed, is_closed: self.is_closed,
is_empty: true, is_empty: true,
} }
@ -234,6 +264,7 @@ impl Path {
pub fn new(is_closed: bool) -> Self { pub fn new(is_closed: bool) -> Self {
Self { Self {
points: Vec::with_capacity(500), points: Vec::with_capacity(500),
ebo: Vec::with_capacity(500),
is_closed, is_closed,
is_empty: true, is_empty: true,
} }
@ -304,6 +335,12 @@ impl Path {
self.points[len - 1].next = curr; self.points[len - 1].next = curr;
self.points[len - 2].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 { pub fn len(&self) -> usize {

View File

@ -64,7 +64,7 @@ impl Graphics for Hello {
Ok(()) Ok(())
} }
fn draw(&self, gl: &glow::Context) -> Result<()> { fn draw(&mut self, gl: &glow::Context) -> Result<()> {
unsafe { unsafe {
gl.clear_color(0.05, 0.05, 0.1, 1.0); gl.clear_color(0.05, 0.05, 0.1, 1.0);
gl.clear(glow::COLOR_BUFFER_BIT); gl.clear(glow::COLOR_BUFFER_BIT);

View File

@ -12,7 +12,7 @@ pub use colormesh::ColorMesh;
use imgui::TextureId; use imgui::TextureId;
pub trait Graphics { 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<()>; 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<()>; fn attach_with_buffer(&mut self, gl: &glow::Context, data: &Self::Data) -> Result<()>;
} }
pub trait AttaWithWindow { pub trait AttaWithWindow {
fn attach_with_window(&mut self, gl: &glow::Context) -> Result<TextureId>; fn attach_with_window(&mut self, gl: &glow::Context) -> Result<TextureId>;
}
}

View File

@ -25,6 +25,8 @@ pub struct PPI {
framebuffer: Option<NativeFramebuffer>, framebuffer: Option<NativeFramebuffer>,
frametexture: Option<NativeTexture>, frametexture: Option<NativeTexture>,
buffer: Vec<[f32; 3]>, buffer: Vec<[f32; 3]>,
need_redraw: bool,
} }
impl PPI { impl PPI {
@ -55,6 +57,7 @@ impl PPI {
framebuffer: None, framebuffer: None,
frametexture: None, frametexture: None,
cmap: None, cmap: None,
need_redraw: true,
}) })
} }
@ -85,6 +88,7 @@ impl PPI {
pub fn add_ppi_layer(&mut self, layer: isize) { pub fn add_ppi_layer(&mut self, layer: isize) {
self.layer = self.layer + layer; self.layer = self.layer + layer;
self.layer.clamp(0, 23); self.layer.clamp(0, 23);
self.need_redraw = true;
} }
pub fn program_ref(&self) -> &Program { pub fn program_ref(&self) -> &Program {
@ -215,43 +219,36 @@ impl Graphics for PPI {
Ok(()) Ok(())
} }
fn draw(&self, gl: &glow::Context) -> Result<()> { fn draw(&mut self, gl: &glow::Context) -> Result<()> {
if !self.need_redraw {
return Ok(());
}
unsafe { unsafe {
gl.clear(glow::COLOR_BUFFER_BIT);
gl.use_program(Some(self.program.native_program.unwrap())); 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 self.cmap
.as_ref() .as_ref()
.unwrap() .unwrap()
.bind_texture(gl, &self.program)?; .bind_texture(gl, &self.program)?;
if self.framebuffer.is_some() {
gl.bind_framebuffer(glow::FRAMEBUFFER, self.framebuffer);
}
gl.depth_mask(false); gl.depth_mask(false);
gl.bind_vertex_array(Some(self.vao.unwrap())); gl.bind_vertex_array(Some(self.vao.unwrap()));
gl.draw_arrays(glow::POINTS, 0, self.buffer.len() as i32); gl.draw_arrays(glow::POINTS, 0, self.buffer.len() as i32);
gl.depth_mask(true); gl.depth_mask(true);
if self.framebuffer.is_some() { 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_framebuffer(glow::FRAMEBUFFER, None);
} }
gl.bind_vertex_array(None); gl.bind_vertex_array(None);
gl.use_program(None); gl.use_program(None);
self.need_redraw = false;
} }
Ok(()) Ok(())
} }
@ -274,6 +271,7 @@ impl AttaWithBuffer for PPI {
glow::STATIC_DRAW, glow::STATIC_DRAW,
); );
} }
self.need_redraw = true;
Ok(()) Ok(())
} }
@ -311,8 +309,21 @@ impl AttaWithWindow for PPI {
glow::LINEAR as i32, 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_framebuffer(glow::FRAMEBUFFER, None);
gl.bind_texture(glow::TEXTURE_2D, None); self.framebuffer = Some(framebuffer);
self.frametexture = Some(texture); self.frametexture = Some(texture);
Ok(imgui::TextureId::from(texture.0.get() as usize)) Ok(imgui::TextureId::from(texture.0.get() as usize))
} else { } else {

View File

@ -2,11 +2,13 @@ use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use data_loader::Data; use data_loader::Data;
use graphics::collections::agg_fast_path::{AggFastPath, Path};
use graphics::collections::Colletion;
use graphics::colormap::linear::LinearColormap; use graphics::colormap::linear::LinearColormap;
use graphics::ppi::PPI; use graphics::ppi::PPI;
use graphics::transforms::position::Position; use graphics::transforms::position::Position;
use graphics::transforms::viewport::Viewport; use graphics::transforms::viewport::Viewport;
use graphics::{AttaWithBuffer, Graphics, AttaWithWindow}; use graphics::{AttaWithBuffer, AttaWithWindow, Graphics};
use imgui::Condition; use imgui::Condition;
mod camera; mod camera;
@ -20,12 +22,28 @@ use imgui::*;
mod final_pg; mod final_pg;
type Graphic = Rc<RefCell<dyn Graphics>>;
fn main() { fn main() {
env_logger::init(); 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 transform = Position::new().unwrap();
let mut viewport = Viewport::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(); let mut cmap = LinearColormap::new().unwrap();
@ -55,29 +73,42 @@ fn main() {
let ppi = Rc::new(RefCell::new(ppi)); let ppi = Rc::new(RefCell::new(ppi));
let registers = vec![ppi.clone()]; let fast_path = Rc::new(RefCell::new(fast_path));
let registers = registers.into_iter().map(|r| { let registers = vec![fast_path.clone() as Graphic];
r as Rc<RefCell<dyn Graphics>>
}).collect();
let mut data =
Data::from_path(r#"/Users/tsuki/Desktop/ZJSXAA_20230113070200_R.dat.gz"#).unwrap();
support::supporter::init(move |run, ui, window, gl| { support::supporter::init(
let mut ppi = ppi.borrow_mut(); move |run, ui, window, gl| {
let texture = ppi.attach_with_window(gl).unwrap(); // 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(||{ ui.window("test").build(|| {
if ui.button("load") { 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();
ppi.attach_with_buffer(gl, &data).unwrap(); }
}
});
ui.window("Test").size([200.0,400.0], imgui::Condition::FirstUseEver).build(|| { ui.separator();
imgui::Image::new(texture, [200.0, 400.0]).build(ui); if ui.button("Add Layer") {
}); // ppi.add_ppi_layer(1);
// ppi.attach_with_buffer(gl, &data).unwrap();
}
if ui.button("Remove Layer") {
}, // ppi.add_ppi_layer(-1);
registers, // 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,
);
}

View File

@ -37,7 +37,7 @@ use std::num::NonZeroU32;
use std::rc::Rc; use std::rc::Rc;
use std::time::Instant; use std::time::Instant;
pub fn init<FUi>(mut run_ui: FUi, mut register: Vec<Rc<RefCell<dyn Graphics>>>) pub fn init<FUi>(mut run_ui: FUi, mut register: Vec<Rc<RefCell<dyn Graphics>>>)
where where
FUi: FnMut(&mut bool, &mut Ui, &Window, &glow::Context) + 'static, FUi: FnMut(&mut bool, &mut Ui, &Window, &glow::Context) + 'static,
{ {
@ -60,7 +60,7 @@ where
for r in register.iter_mut() { for r in register.iter_mut() {
r.borrow_mut().compile(&gl_context).unwrap(); r.borrow_mut().compile(&gl_context).unwrap();
} }
let mut last_frame = Instant::now(); let mut last_frame = Instant::now();
event_loop event_loop
@ -96,13 +96,11 @@ where
if ui.is_mouse_pos_valid(ui.io().mouse_pos) { if ui.is_mouse_pos_valid(ui.io().mouse_pos) {
let mouse_pos = ui.io().mouse_pos; let mouse_pos = ui.io().mouse_pos;
if ui.is_mouse_dragging(imgui::MouseButton::Right) { if ui.is_mouse_dragging(imgui::MouseButton::Right) {}
}
}
for r in register.iter_mut() {
r.borrow_mut().draw(&gl_context).unwrap();
} }
// for r in register.iter_mut() {
// r.borrow_mut().draw(&gl_context).unwrap();
// }
let mut run = true; let mut run = true;
@ -140,16 +138,13 @@ where
NonZeroU32::new(new_size.width).unwrap(), NonZeroU32::new(new_size.width).unwrap(),
NonZeroU32::new(new_size.height).unwrap(), NonZeroU32::new(new_size.height).unwrap(),
); );
} }
winit_platform.handle_event(imgui_context.io_mut(), &window, &event); winit_platform.handle_event(imgui_context.io_mut(), &window, &event);
} }
winit::event::Event::LoopExiting => { winit::event::Event::LoopExiting => {
for r in register.iter_mut() { for r in register.iter_mut() {
r.borrow_mut().destroy(&gl_context).unwrap(); r.borrow_mut().destroy(&gl_context).unwrap();
} }
} }
event => { event => {
winit_platform.handle_event(imgui_context.io_mut(), &window, &event); winit_platform.handle_event(imgui_context.io_mut(), &window, &event);