mirror of
https://github.com/lisk77/comet.git
synced 2025-12-15 18:18:50 +00:00
feat: added a camera with orthographic projection and did some work restructuring the comet_app to make the setup system optional. Input handling is moved to the app
This commit is contained in:
parent
780365aeb8
commit
5a9f632e3a
22 changed files with 1173 additions and 349 deletions
|
|
@ -11,7 +11,7 @@ trait LinearTransformation {
|
|||
// ##################################################
|
||||
|
||||
|
||||
/// Representation of a 2x2 Matrix in row major
|
||||
/// Representation of a 2x2 Matrix
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
|
@ -39,13 +39,20 @@ impl Mat2 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_vec(row1: Vec2, row2: Vec2) -> Self {
|
||||
pub fn from_rows(row1: Vec2, row2: Vec2) -> Self {
|
||||
Self {
|
||||
x00: row1.x(), x01: row1.y(),
|
||||
x10: row2.x(), x11: row2.y()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_cols(col1: Vec2, col2: Vec2) -> Self {
|
||||
Self {
|
||||
x00: col1.x(), x01: col2.x(),
|
||||
x10: col1.y(), x11: col2.y()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, row: usize, col: usize) -> Option<f32> {
|
||||
assert!(row <= 1, "This row ({}) is out of bounds! Bounds: 0..1", row);
|
||||
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
|
||||
|
|
@ -68,6 +75,15 @@ impl Mat2 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_col(&self, col: usize) -> Option<Vec2> {
|
||||
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
|
||||
match col {
|
||||
0 => Some(Vec2::new(self.x00, self.x10)),
|
||||
1 => Some(Vec2::new(self.x01, self.x11)),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&mut self, row: usize, col: usize, element: f32) {
|
||||
assert!(row <= 1, "This row ({}) is out of bounds! Bounds: 0..1", row);
|
||||
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
|
||||
|
|
@ -91,6 +107,16 @@ impl Mat2 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_col(&mut self, col: usize, col_content: Vec2) {
|
||||
assert!(col <= 1, "This row ({}) is out of bounds! Bounds: 0..1", col);
|
||||
|
||||
match col {
|
||||
0 => { self.x00 = col_content.x(); self.x10 = col_content.y(); },
|
||||
1 => { self.x01 = col_content.x(); self.x11 = col_content.y(); },
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn det(&self) -> f32 {
|
||||
self.x00 * self.x11
|
||||
- self.x01 * self.x10
|
||||
|
|
@ -108,6 +134,12 @@ impl Mat2 {
|
|||
self.set_row(row1, self.get_row(row2).expect(format!("This row ({}) is out of bounds! Bounds: 0..1", row2).as_str()));
|
||||
self.set_row(row2, tmp);
|
||||
}
|
||||
|
||||
pub fn swap_cols(&mut self, col1: usize, col2: usize) {
|
||||
let tmp = self.get_col(col1).expect(format!("This row ({}) is out of bounds! Bounds: 0..1", col1).as_str());
|
||||
self.set_col(col1, self.get_col(col2).expect(format!("This row ({}) is out of bounds! Bounds: 0..1", col2).as_str()));
|
||||
self.set_col(col2, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Mat2> for Mat2 {
|
||||
|
|
@ -187,11 +219,12 @@ impl Div<f32> for Mat2 {
|
|||
}
|
||||
}
|
||||
|
||||
/// [WARN]: This will return a column-major array for wgpu use!
|
||||
impl Into<[[f32; 2]; 2]> for Mat2 {
|
||||
fn into(self) -> [[f32; 2]; 2] {
|
||||
[
|
||||
[self.x00, self.x01],
|
||||
[self.x10, self.x11],
|
||||
[self.x00, self.x10],
|
||||
[self.x01, self.x11],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -235,7 +268,7 @@ impl Mat3 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_vec(row1: Vec3, row2: Vec3, row3: Vec3) -> Self {
|
||||
pub fn from_rows(row1: Vec3, row2: Vec3, row3: Vec3) -> Self {
|
||||
Self {
|
||||
x00: row1.x(), x01: row1.y(), x02: row1.z(),
|
||||
x10: row2.x(), x11: row2.y(), x12: row2.z(),
|
||||
|
|
@ -243,6 +276,14 @@ impl Mat3 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_cols(col1: Vec3, col2: Vec3, col3: Vec3) -> Self {
|
||||
Self {
|
||||
x00: col1.x(), x01: col2.x(), x02: col3.x(),
|
||||
x10: col1.y(), x11: col2.y(), x12: col3.y(),
|
||||
x20: col1.z(), x21: col2.z(), x22: col3.z()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, row: usize, col: usize) -> Option<f32> {
|
||||
assert!(row <= 2, "This row ({}) is out of bounds! Bounds: 0..2", row);
|
||||
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
|
||||
|
|
@ -270,6 +311,16 @@ impl Mat3 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_col(&self, col: usize) -> Option<Vec3> {
|
||||
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
|
||||
match col {
|
||||
0 => Some(Vec3::new(self.x00, self.x10, self.x20)),
|
||||
1 => Some(Vec3::new(self.x01, self.x11, self.x21)),
|
||||
2 => Some(Vec3::new(self.x02, self.x12, self.x22)),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&mut self, row: usize, col: usize, element: f32) {
|
||||
assert!(row <= 2, "This row ({}) is out of bounds! Bounds: 0..2", row);
|
||||
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
|
||||
|
|
@ -298,6 +349,16 @@ impl Mat3 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_col(&mut self, col: usize, col_content: Vec3) {
|
||||
assert!(col <= 2, "This row ({}) is out of bounds! Bounds: 0..2", col);
|
||||
match col {
|
||||
0 => { self.x00 = col_content.x; self.x10 = col_content.y; self.x20 = col_content.z; },
|
||||
1 => { self.x01 = col_content.x; self.x11 = col_content.y; self.x21 = col_content.z; },
|
||||
2 => { self.x02 = col_content.x; self.x12 = col_content.y; self.x22 = col_content.z; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn det(&self) -> f32 {
|
||||
self.x00 * self.x11 * self.x22
|
||||
+ self.x01 * self.x12 * self.x20
|
||||
|
|
@ -320,6 +381,12 @@ impl Mat3 {
|
|||
self.set_row(row1, self.get_row(row2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", row2).as_str()));
|
||||
self.set_row(row2, tmp);
|
||||
}
|
||||
|
||||
pub fn swap_cols(&mut self, col1: usize, col2: usize) {
|
||||
let tmp = self.get_col(col1).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col1).as_str());
|
||||
self.set_col(col1, self.get_col(col2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col2).as_str()));
|
||||
self.set_col(col2, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Mat3> for Mat3 {
|
||||
|
|
@ -412,9 +479,9 @@ impl Div<f32> for Mat3 {
|
|||
impl Into<[[f32; 3]; 3]> for Mat3 {
|
||||
fn into(self) -> [[f32; 3]; 3] {
|
||||
[
|
||||
[self.x00, self.x01, self.x02],
|
||||
[self.x10, self.x11, self.x12],
|
||||
[self.x20, self.x21, self.x22],
|
||||
[self.x00, self.x10, self.x20],
|
||||
[self.x01, self.x11, self.x21],
|
||||
[self.x02, self.x12, self.x22],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -470,7 +537,7 @@ impl Mat4 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_vec(row1: Vec4, row2: Vec4, row3: Vec4, row4: Vec4) -> Self {
|
||||
pub fn from_rows(row1: Vec4, row2: Vec4, row3: Vec4, row4: Vec4) -> Self {
|
||||
Self {
|
||||
x00: row1.x(), x01: row1.y(), x02: row1.z(), x03: row1.w(),
|
||||
x10: row2.x(), x11: row2.y(), x12: row2.z(), x13: row2.w(),
|
||||
|
|
@ -479,6 +546,15 @@ impl Mat4 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_cols(col1: Vec4, col2: Vec4, col3: Vec4, col4: Vec4) -> Self {
|
||||
Self {
|
||||
x00: col1.x(), x01: col2.x(), x02: col3.x(), x03: col4.x(),
|
||||
x10: col1.y(), x11: col2.y(), x12: col3.y(), x13: col4.y(),
|
||||
x20: col1.z(), x21: col2.z(), x22: col3.z(), x23: col4.z(),
|
||||
x30: col1.w(), x31: col2.w(), x32: col3.w(), x33: col4.w()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rh_look_to(camera: Point3, dir: Vec3, up: Vec3) -> Self {
|
||||
let f = dir.normalize();
|
||||
let s = cross(f, up).normalize();
|
||||
|
|
@ -486,19 +562,19 @@ impl Mat4 {
|
|||
let cam = camera.to_vec();
|
||||
|
||||
|
||||
/*Mat4::new(
|
||||
Mat4::new(
|
||||
s.x().clone(), u.x().clone(), -f.x().clone(), 0.0,
|
||||
s.y().clone(), u.y().clone(), -f.y().clone(), 0.0,
|
||||
s.z().clone(), u.z().clone(), -f.z().clone(), 0.0,
|
||||
-dot(&cam, &s), -dot(&cam, &u), dot(&cam, &f),1.0
|
||||
)*/
|
||||
)
|
||||
|
||||
Mat4::new(
|
||||
/*Mat4::new(
|
||||
s.x().clone(), s.y().clone(), s.z().clone(), 0.0,
|
||||
u.x().clone(), u.y().clone(), u.z().clone(), 0.0,
|
||||
-f.x().clone(), -f.y().clone(), -f.z().clone(), 0.0,
|
||||
-dot(&cam, &s), -dot(&cam, &u), dot(&cam, &f), 1.0
|
||||
)
|
||||
)*/
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -524,22 +600,31 @@ impl Mat4 {
|
|||
let bottom = -ymax;
|
||||
let top = ymax;
|
||||
|
||||
/*Mat4::new(
|
||||
Mat4::new(
|
||||
(2.0 * near) / (right - left), 0.0, (right + left) / (right - left), 0.0,
|
||||
0.0, (2.0 * near) / (top - bottom), (top + bottom) / (top - bottom), 0.0,
|
||||
0.0, 0.0, -(far + near) / (far - near), -(2.0 * far * near) / (far - near),
|
||||
0.0, 0.0, -1.0, 0.0
|
||||
)*/
|
||||
)
|
||||
|
||||
Mat4::new(
|
||||
/*Mat4::new(
|
||||
(2.0 * near) / (right - left), 0.0, 0.0, 0.0,
|
||||
0.0, (2.0 * near) / (top - bottom), 0.0, 0.0,
|
||||
(right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1.0,
|
||||
0.0, 0.0, -(2.0 * far * near) / (far - near), 0.0
|
||||
)
|
||||
)*/
|
||||
|
||||
}
|
||||
|
||||
pub fn orthographic_matrix(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Self {
|
||||
Mat4::new(
|
||||
2.0 / (right - left), 0.0, 0.0, 0.0,
|
||||
0.0, 2.0 / (top - bottom), 0.0, 0.0,
|
||||
0.0, 0.0, -2.0 / (far - near), 0.0,
|
||||
-(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1.0
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get(&self, row: usize, col: usize) -> Option<f32> {
|
||||
assert!(row <= 3, "This row ({}) is out of bounds! Bounds: 0..3", row);
|
||||
assert!(col <= 3, "This row ({}) is out of bounds! Bounds: 0..3", col);
|
||||
|
|
@ -575,6 +660,17 @@ impl Mat4 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_col(&self, col: usize) -> Option<Vec4> {
|
||||
assert!(col <= 3, "This row ({}) is out of bounds! Bounds: 0..3", col);
|
||||
match col {
|
||||
0 => Some(Vec4::new(self.x00, self.x10, self.x20, self.x30)),
|
||||
1 => Some(Vec4::new(self.x01, self.x11, self.x21, self.x31)),
|
||||
2 => Some(Vec4::new(self.x02, self.x12, self.x22, self.x32)),
|
||||
3 => Some(Vec4::new(self.x03, self.x13, self.x23, self.x33)),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&mut self, row: usize, col: usize, element: f32) {
|
||||
assert!(row <= 3, "The given row ({}) is out of bounds! Bounds: 0..3", row);
|
||||
assert!(col <= 3, "The given column ({}) is out of bounds! Bounds: 0..3", col);
|
||||
|
|
@ -610,6 +706,17 @@ impl Mat4 {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_col(&mut self, col: usize, col_content: Vec4) {
|
||||
assert!(col <= 3, "This column ({}) is out of bounds! Bounds: 0..3", col);
|
||||
match col {
|
||||
0 => { self.x00 = col_content.x(); self.x10 = col_content.y(); self.x20 = col_content.z(); self.x30 = col_content.w(); },
|
||||
1 => { self.x01 = col_content.x(); self.x11 = col_content.y(); self.x21 = col_content.z(); self.x31 = col_content.w(); },
|
||||
2 => { self.x02 = col_content.x(); self.x12 = col_content.y(); self.x22 = col_content.z(); self.x32 = col_content.w(); },
|
||||
3 => { self.x03 = col_content.x(); self.x13 = col_content.y(); self.x23 = col_content.z(); self.x33 = col_content.w(); }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn det(&self) -> f32 {
|
||||
self.x00 * (self.x11 * (self.x22* self.x33 - self.x23 * self.x32)
|
||||
- self.x21 * (self.x12 * self.x33 - self.x13 * self.x32)
|
||||
|
|
@ -633,6 +740,18 @@ impl Mat4 {
|
|||
x30: self.x03, x31: self.x13, x32: self.x23, x33: self.x33
|
||||
}
|
||||
}
|
||||
|
||||
pub fn swap_rows(&mut self, row1: usize, row2: usize) {
|
||||
let tmp = self.get_row(row1).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", row1).as_str());
|
||||
self.set_row(row1, self.get_row(row2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", row2).as_str()));
|
||||
self.set_row(row2, tmp);
|
||||
}
|
||||
|
||||
pub fn swap_cols(&mut self, col1: usize, col2: usize) {
|
||||
let tmp = self.get_col(col1).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col1).as_str());
|
||||
self.set_col(col1, self.get_col(col2).expect(format!("This row ({}) is out of bounds! Bounds: 0..2", col2).as_str()));
|
||||
self.set_col(col2, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Mat4> for Mat4 {
|
||||
|
|
@ -715,10 +834,10 @@ impl Mul<Mat4> for Mat4 {
|
|||
impl Into<[[f32; 4]; 4]> for Mat4 {
|
||||
fn into(self) -> [[f32; 4]; 4] {
|
||||
[
|
||||
[self.x00, self.x01, self.x02, self.x03],
|
||||
[self.x10, self.x11, self.x12, self.x13],
|
||||
[self.x20, self.x21, self.x22, self.x23],
|
||||
[self.x30, self.x31, self.x32, self.x33],
|
||||
[self.x00, self.x10, self.x20, self.x30],
|
||||
[self.x01, self.x11, self.x21, self.x31],
|
||||
[self.x02, self.x12, self.x22, self.x32],
|
||||
[self.x03, self.x13, self.x23, self.x33],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue