]> git.nega.tv - josh/narcissus/commitdiff
Add shorthand functions for vector and point new
authorJoshua Simmons <josh@nega.tv>
Sun, 13 Nov 2022 22:51:52 +0000 (23:51 +0100)
committerJoshua Simmons <josh@nega.tv>
Sun, 13 Nov 2022 22:51:52 +0000 (23:51 +0100)
narcissus-maths/src/lib.rs
narcissus-maths/src/point2.rs
narcissus-maths/src/point3.rs
narcissus-maths/src/vec2.rs
narcissus-maths/src/vec3.rs
narcissus-maths/src/vec4.rs
narcissus/src/main.rs

index e653a05fbe84e41485b075edf0f0a8d288bb7345..549b9a607aec355a846853dac80523c8370633d9 100644 (file)
@@ -19,14 +19,14 @@ pub use mat2::Mat2;
 pub use mat3::Mat3;
 pub use mat4::Mat4;
 pub use next_after_f32::next_after_f32;
-pub use point2::Point2;
-pub use point3::Point3;
+pub use point2::{point2, Point2};
+pub use point3::{point3, Point3};
 pub use quat::Quat;
 pub use sin_cos_pi::sin_cos_pi_f32;
 pub use tan_pi::tan_pi_f32;
-pub use vec2::Vec2;
-pub use vec3::Vec3;
-pub use vec4::Vec4;
+pub use vec2::{vec2, Vec2};
+pub use vec3::{vec3, Vec3};
+pub use vec4::{vec4, Vec4};
 
 /// Unit type for an angle expressed in radians.
 #[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
index ed4eac6fba87c0fd31a6805a47b47d7b29d446ad..2d5ae7154765af314c2d3981d3bbd2c59e725ca0 100644 (file)
@@ -8,6 +8,10 @@ pub struct Point2 {
     pub y: f32,
 }
 
+pub const fn point2(x: f32, y: f32) -> Point2 {
+    Point2 { x, y }
+}
+
 impl_shared!(Point2, f32, 2);
 impl_affine!(Point2, f32, 2);
 
index f5a7e07f1ed596081bbd8c1c1c2886a693718a1f..e6379baf8a05a154be27729105756ebaaa88290a 100644 (file)
@@ -9,6 +9,10 @@ pub struct Point3 {
     pub z: f32,
 }
 
+pub const fn point3(x: f32, y: f32, z: f32) -> Point3 {
+    Point3 { x, y, z }
+}
+
 impl_shared!(Point3, f32, 3);
 impl_affine!(Point3, f32, 3);
 
index e0b94302109ef463aa9456eaadcbcc508949828c..898fee5def1febf38c5654a7bdfa33c6eeaca752 100644 (file)
@@ -7,6 +7,10 @@ pub struct Vec2 {
     pub y: f32,
 }
 
+pub const fn vec2(x: f32, y: f32) -> Vec2 {
+    Vec2 { x, y }
+}
+
 impl_shared!(Vec2, f32, 2);
 impl_vector!(Vec2, f32, 2);
 
index 24592e2c3222bfb8601ae057701ce2aaf61eed52..975d77955c5f4068b2df1ad4f1c3ed2ae5e302f0 100644 (file)
@@ -11,6 +11,10 @@ pub struct Vec3 {
 impl_shared!(Vec3, f32, 3);
 impl_vector!(Vec3, f32, 3);
 
+pub const fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
+    Vec3 { x, y, z }
+}
+
 impl Vec3 {
     pub const X: Vec3 = Vec3::new(1.0, 0.0, 0.0);
     pub const Y: Vec3 = Vec3::new(0.0, 1.0, 0.0);
index c3d7a039af84dda4322029821cd6f89c08ec90c4..9ce2140b7fb74eb3a79f11a4bf5fc3092e56f9ee 100644 (file)
@@ -9,6 +9,10 @@ pub struct Vec4 {
     pub w: f32,
 }
 
+pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
+    Vec4 { x, y, z, w }
+}
+
 impl_shared!(Vec4, f32, 4);
 impl_vector!(Vec4, f32, 4);
 
index 5452fbccf74f0b35af5347f87ed93d8872244780..8b65a62ac24861a33f554595a9fd714408ca44c1 100644 (file)
@@ -11,7 +11,7 @@ use narcissus_gpu::{
     TypedBind, Viewport,
 };
 use narcissus_maths::{
-    sin_cos_pi_f32, Affine3, Deg, HalfTurn, Mat3, Mat4, Point3, Vec2, Vec3, Vec4,
+    sin_cos_pi_f32, vec2, vec3, vec4, Affine3, Deg, HalfTurn, Mat3, Mat4, Point3, Vec2, Vec3,
 };
 
 const MAX_SHARKS: usize = 262_144;
@@ -53,15 +53,15 @@ fn load_obj<P: AsRef<Path>>(path: P) -> (Vec<Vertex>, Vec<u16>) {
 
     impl obj::Visitor for ObjVisitor {
         fn visit_position(&mut self, x: f32, y: f32, z: f32, _w: f32) {
-            self.positions.push(Vec3::new(x, y, z))
+            self.positions.push(vec3(x, y, z))
         }
 
         fn visit_texcoord(&mut self, u: f32, v: f32, _w: f32) {
-            self.texcoords.push(Vec2::new(u, v));
+            self.texcoords.push(vec2(u, v));
         }
 
         fn visit_normal(&mut self, x: f32, y: f32, z: f32) {
-            self.normals.push(Vec3::new(x, y, z))
+            self.normals.push(vec3(x, y, z))
         }
 
         fn visit_face(&mut self, indices: &[(i32, i32, i32)]) {
@@ -94,9 +94,9 @@ fn load_obj<P: AsRef<Path>>(path: P) -> (Vec<Vertex>, Vec<u16>) {
             let texcoord = visitor.texcoords[texcoord_index as usize - 1];
             (
                 Vertex {
-                    position: Vec4::new(position.x, position.y, position.z, 0.0).into(),
-                    normal: Vec4::new(normal.x, normal.y, normal.z, 0.0).into(),
-                    texcoord: Vec4::new(texcoord.x, texcoord.y, 0.0, 0.0).into(),
+                    position: vec4(position.x, position.y, position.z, 0.0).into(),
+                    normal: vec4(normal.x, normal.y, normal.z, 0.0).into(),
+                    texcoord: vec4(texcoord.x, texcoord.y, 0.0, 0.0).into(),
                 },
                 index as u16,
             )
@@ -318,7 +318,7 @@ pub fn main() {
             let z = z as f32 * shark_distance - NUM_SHARKS as f32 / 2.0 * shark_distance;
             shark_transforms.push(Affine3 {
                 matrix: Mat3::from_axis_rotation(Vec3::Y, HalfTurn::new(rng.next_f32())),
-                translate: Vec3::new(x, 0.0, z),
+                translate: vec3(x, 0.0, z),
             })
         }
     }