feat: implemented controls

This commit is contained in:
Yandrik 2022-12-30 17:57:41 +01:00
parent 6011ee68b4
commit 3166e5ce7c
1 changed files with 36 additions and 12 deletions

View File

@ -1,13 +1,15 @@
use crossterm::cursor::MoveTo;
use crossterm::style::Attribute::Dim;
use crossterm::style::Print;
use crossterm::terminal::{Clear, ClearType};
use crossterm::terminal::{Clear, ClearType, enable_raw_mode};
use crossterm::{execute, QueueableCommand};
use ndarray::{arr1, arr2, Array1, Array2};
use std::io::{stdout, Write};
use std::process::exit;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use crossterm::event::{Event, KeyCode, poll, read};
const X_DRAW_SCALING_FACTOR: f32 = 2.8;
@ -144,10 +146,10 @@ fn plot_line_vertical(
// CONTRACT: line points are in u16 space
let x = ((incl * i as f32).round() + origin[0] as f32) as u16;
out.queue(MoveTo(x, y))?
.queue(match (incl > -2. && incl < 2., incl > 0.) {
(true, false) => Print("/"),
(true, true) => Print("\\"),
_ => Print("|"),
.queue(match (incl > -0.5 && incl < 0.5, incl > 0.) {
(true, _) => Print("|"),
(false, false) => Print("/"),
(false, true) => Print("\\"),
})?;
}
@ -157,11 +159,32 @@ fn plot_line_vertical(
static ANGLE: Mutex<f32> = Mutex::new(0.);
fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
let mut angle = 0.;
let mut angle = (0., 0., 0.);
loop {
if poll(Duration::from_millis(10))? {
if let Event::Key(key) = read()? {
match key.code {
KeyCode::Char('w') => angle.0 += 6.,
KeyCode::Char('s') => angle.0 -= 6.,
KeyCode::Char('a') => angle.2 += 6.,
KeyCode::Char('d') => angle.2 -= 6.,
KeyCode::Char('q') => angle.1 += 6.,
KeyCode::Char('e') => angle.1 -= 6.,
KeyCode::Esc | KeyCode::Backspace | KeyCode::Char('c') => exit(0),
_ => {},
}
}
} else {
angle.1 += 0.1;
}
angle.1 %= 360.;
// rotation and matmul
let rot_matrix = rotation_matrix(angle, Dimension::Z);
let x_matrix = rotation_matrix(angle.0, Dimension::X);
let y_matrix = rotation_matrix(angle.1, Dimension::Y);
let z_matrix = rotation_matrix(angle.2, Dimension::Z);
let cam_matrix = ortho_matrix();
let mut stdout = stdout();
@ -174,9 +197,9 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
let projected_points = points
.iter()
.map(|pt| scale(1.6, 1.6, 1.6).dot(pt))
.map(|pt| rotation_matrix((angle * 10.) % 360., Dimension::X).dot(&pt))
.map(|pt| rotation_matrix((angle * 2.) % 360., Dimension::Y).dot(&pt))
.map(|pt| rot_matrix.dot(&pt))
.map(|pt| x_matrix.dot(&pt))
.map(|pt| y_matrix.dot(&pt))
.map(|pt| z_matrix.dot(&pt))
.map(|pt| cam_matrix.dot(&pt))
.map(|pt| pt + arr1(&[30., 30.])) // draw shift
.collect::<Vec<Array1<_>>>();
@ -203,8 +226,6 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
}
stdout.flush()?;
angle += 0.1;
angle %= 360.;
thread::sleep(Duration::from_millis(10));
}
@ -212,6 +233,9 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
}
fn main() -> anyhow::Result<()> {
enable_raw_mode()?;
let cube = vec![
arr1(&[-10., -10., 10.]),
arr1(&[-10., 10., 10.]),