feat: added zoom and colors
This commit is contained in:
parent
99dbfeaaa9
commit
b57ed1fa70
42
src/main.rs
42
src/main.rs
@ -1,6 +1,6 @@
|
|||||||
use crossterm::cursor::MoveTo;
|
use crossterm::cursor::MoveTo;
|
||||||
use crossterm::style::Attribute::Dim;
|
use crossterm::style::Attribute::Dim;
|
||||||
use crossterm::style::Print;
|
use crossterm::style::{Color, Print, SetForegroundColor};
|
||||||
use crossterm::terminal::{Clear, ClearType, enable_raw_mode};
|
use crossterm::terminal::{Clear, ClearType, enable_raw_mode};
|
||||||
use crossterm::{execute, QueueableCommand};
|
use crossterm::{execute, QueueableCommand};
|
||||||
use ndarray::{arr1, arr2, Array1, Array2};
|
use ndarray::{arr1, arr2, Array1, Array2};
|
||||||
@ -22,6 +22,8 @@ enum Dimension {
|
|||||||
struct Mesh {
|
struct Mesh {
|
||||||
points: Vec<Array1<f32>>,
|
points: Vec<Array1<f32>>,
|
||||||
edges: Vec<[usize; 2]>,
|
edges: Vec<[usize; 2]>,
|
||||||
|
line_color: Option<Color>,
|
||||||
|
node_color: Option<Color>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -160,6 +162,7 @@ static ANGLE: Mutex<f32> = Mutex::new(0.);
|
|||||||
|
|
||||||
fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
|
fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
|
||||||
let mut angle = (0., 0., 0.);
|
let mut angle = (0., 0., 0.);
|
||||||
|
let mut zoom = 1.;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
||||||
@ -172,6 +175,8 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
|
|||||||
KeyCode::Char('d') => angle.2 -= 6.,
|
KeyCode::Char('d') => angle.2 -= 6.,
|
||||||
KeyCode::Char('q') => angle.1 += 6.,
|
KeyCode::Char('q') => angle.1 += 6.,
|
||||||
KeyCode::Char('e') => angle.1 -= 6.,
|
KeyCode::Char('e') => angle.1 -= 6.,
|
||||||
|
KeyCode::Char(',') => zoom = 0.2f32.max(zoom - 0.02),
|
||||||
|
KeyCode::Char('.') => zoom = 1f32.min(zoom + 0.02),
|
||||||
KeyCode::Esc | KeyCode::Backspace | KeyCode::Char('c') => exit(0),
|
KeyCode::Esc | KeyCode::Backspace | KeyCode::Char('c') => exit(0),
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
@ -185,6 +190,7 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
|
|||||||
let x_matrix = rotation_matrix(angle.0, Dimension::X);
|
let x_matrix = rotation_matrix(angle.0, Dimension::X);
|
||||||
let y_matrix = rotation_matrix(angle.1, Dimension::Y);
|
let y_matrix = rotation_matrix(angle.1, Dimension::Y);
|
||||||
let z_matrix = rotation_matrix(angle.2, Dimension::Z);
|
let z_matrix = rotation_matrix(angle.2, Dimension::Z);
|
||||||
|
let zoom_matrix = scale(zoom, zoom, zoom);
|
||||||
let cam_matrix = ortho_matrix();
|
let cam_matrix = ortho_matrix();
|
||||||
|
|
||||||
let mut stdout = stdout();
|
let mut stdout = stdout();
|
||||||
@ -200,10 +206,14 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
|
|||||||
.map(|pt| x_matrix.dot(&pt))
|
.map(|pt| x_matrix.dot(&pt))
|
||||||
.map(|pt| y_matrix.dot(&pt))
|
.map(|pt| y_matrix.dot(&pt))
|
||||||
.map(|pt| z_matrix.dot(&pt))
|
.map(|pt| z_matrix.dot(&pt))
|
||||||
|
.map(|pt| zoom_matrix.dot(&pt))
|
||||||
.map(|pt| cam_matrix.dot(&pt))
|
.map(|pt| cam_matrix.dot(&pt))
|
||||||
.map(|pt| pt + arr1(&[30., 30.])) // draw shift
|
.map(|pt| pt + arr1(&[30., 30.])) // draw shift
|
||||||
.collect::<Vec<Array1<_>>>();
|
.collect::<Vec<Array1<_>>>();
|
||||||
|
|
||||||
|
// set edge color before edge drawing
|
||||||
|
stdout.queue(SetForegroundColor(mesh.line_color.unwrap_or(Color::White)))?;
|
||||||
|
|
||||||
for edge in edges.iter() {
|
for edge in edges.iter() {
|
||||||
let origin = &projected_points[edge[0]];
|
let origin = &projected_points[edge[0]];
|
||||||
let dest = &projected_points[edge[1]];
|
let dest = &projected_points[edge[1]];
|
||||||
@ -217,6 +227,9 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
|
|||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set mesh point color
|
||||||
|
stdout.queue(SetForegroundColor(mesh.node_color.unwrap_or(Color::White)))?;
|
||||||
|
|
||||||
for pt in &projected_points {
|
for pt in &projected_points {
|
||||||
let pt = pt;
|
let pt = pt;
|
||||||
stdout
|
stdout
|
||||||
@ -236,6 +249,26 @@ fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
|
|
||||||
|
let random_rectangle = Mesh {
|
||||||
|
points: vec![
|
||||||
|
arr1(&[-10., 0., 0.]),
|
||||||
|
arr1(&[0., 10., 0.]),
|
||||||
|
arr1(&[10., 0., 0.]),
|
||||||
|
arr1(&[0., -10., 0.]),
|
||||||
|
|
||||||
|
|
||||||
|
],
|
||||||
|
edges: vec![
|
||||||
|
[0, 1],
|
||||||
|
[1, 2],
|
||||||
|
[2, 3],
|
||||||
|
[3, 0],
|
||||||
|
],
|
||||||
|
line_color: Some(Color::Red),
|
||||||
|
node_color: Some(Color::DarkRed)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
let cube = vec![
|
let cube = vec![
|
||||||
arr1(&[-10., -10., 10.]),
|
arr1(&[-10., -10., 10.]),
|
||||||
arr1(&[-10., 10., 10.]),
|
arr1(&[-10., 10., 10.]),
|
||||||
@ -294,14 +327,19 @@ fn main() -> anyhow::Result<()> {
|
|||||||
[2, 4],
|
[2, 4],
|
||||||
[2, 5],
|
[2, 5],
|
||||||
],
|
],
|
||||||
|
line_color: Some(Color::Blue),
|
||||||
|
node_color: Some(Color::White),
|
||||||
};
|
};
|
||||||
|
|
||||||
draw_rotating_mesh(vec![
|
draw_rotating_mesh(vec![
|
||||||
|
random_rectangle,
|
||||||
|
thing,
|
||||||
Mesh {
|
Mesh {
|
||||||
points: cube,
|
points: cube,
|
||||||
edges,
|
edges,
|
||||||
|
line_color: Some(Color::Green),
|
||||||
|
node_color: Some(Color::White),
|
||||||
},
|
},
|
||||||
thing,
|
|
||||||
])?;
|
])?;
|
||||||
|
|
||||||
unreachable!()
|
unreachable!()
|
||||||
|
Loading…
Reference in New Issue
Block a user