From 4dbf03cb112605ced62afbc167fd4edcdbab6ab6 Mon Sep 17 00:00:00 2001 From: Julien Higginson Date: Sat, 11 Jan 2025 18:43:13 +0100 Subject: [PATCH] refactor!: replaced every [f32; 2] with Vector2 Also modified the ./primitive_shapes codes to use directly Vector3 & Vector2 directly instead of casting arrays to their respectives Vector. --- crates/blue_engine_core/src/header.rs | 11 +- .../src/primitive_shapes/three_dimensions.rs | 159 +++++++++--------- .../src/primitive_shapes/two_dimensions.rs | 74 ++++---- crates/blue_engine_core/src/vector.rs | 4 +- 4 files changed, 130 insertions(+), 118 deletions(-) diff --git a/crates/blue_engine_core/src/header.rs b/crates/blue_engine_core/src/header.rs index 4bca910..73ca191 100644 --- a/crates/blue_engine_core/src/header.rs +++ b/crates/blue_engine_core/src/header.rs @@ -61,9 +61,9 @@ pub struct Vertex { /// Contains position data for the vertex in 3D space pub position: Vector3, /// Contains uv position data for the vertex - pub uv: [f32; 2], + pub uv: Vector2, /// Contains the normal face of the vertex - pub normal: [f32; 3], + pub normal: Vector3, } impl Vertex { pub(crate) fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { @@ -77,7 +77,8 @@ impl Vertex { format: wgpu::VertexFormat::Float32x3, }, wgpu::VertexAttribute { - offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, + // This should be replaced with `std::mem::size_of::() as wgpu::BufferAddress` + offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, shader_location: 1, format: wgpu::VertexFormat::Float32x2, }, @@ -778,6 +779,7 @@ impl_deref_field!( ); #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default, Zeroable)] +#[repr(C)] /// General purposes 3D vector pub struct Vector3 { /// X coordinate in 3D space @@ -789,9 +791,10 @@ pub struct Vector3 { } #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default, Zeroable)] +#[repr(C)] /// General purposes 2D vector pub struct Vector2 { - /// X coordinate in 3D space + /// X coordinate in 2D space pub x: f32, /// Y coordinate in 2D space pub y: f32, diff --git a/crates/blue_engine_core/src/primitive_shapes/three_dimensions.rs b/crates/blue_engine_core/src/primitive_shapes/three_dimensions.rs index 16006b5..7aba35d 100644 --- a/crates/blue_engine_core/src/primitive_shapes/three_dimensions.rs +++ b/crates/blue_engine_core/src/primitive_shapes/three_dimensions.rs @@ -1,4 +1,7 @@ -use crate::{ObjectSettings, ObjectStorage, Renderer, StringBuffer, UnsignedIntType, Vertex}; +use crate::{ + ObjectSettings, ObjectStorage, Renderer, StringBuffer, UnsignedIntType, Vector2, Vector3, + Vertex, +}; use std::f32::consts::PI; /// Creates a 3D cube @@ -8,129 +11,129 @@ pub fn cube(name: impl StringBuffer, renderer: &mut Renderer, objects: &mut Obje vec![ // Front Face Vertex { - position: [-1.0, -1.0, 1.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, 1.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, -1.0, 1.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, 1.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, 1.0, 1.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, 1.0, 1.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, 1.0, 1.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, 1.0, 1.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, // Back Face Vertex { - position: [-1.0, 1.0, -1.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, 1.0, -1.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, 1.0, -1.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, 1.0, -1.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, -1.0, -1.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, -1.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, -1.0, -1.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, -1.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, // Right face Vertex { - position: [1.0, -1.0, -1.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, -1.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, 1.0, -1.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, 1.0, -1.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, 1.0, 1.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, 1.0, 1.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, -1.0, 1.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, 1.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, - // Left Face + // Left face Vertex { - position: [-1.0, -1.0, 1.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, -1.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, 1.0, 1.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, 1.0, -1.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, 1.0, -1.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, 1.0, 1.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, -1.0, -1.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, 1.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, - // Top Face + // Top face Vertex { - position: [1.0, 1.0, -1.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, 1.0, -1.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, 1.0, -1.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, 1.0, -1.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, 1.0, 1.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, 1.0, 1.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, 1.0, 1.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, 1.0, 1.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, - // Bottom Face + // Bottom face Vertex { - position: [1.0, -1.0, 1.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, -1.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, -1.0, 1.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, -1.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, -1.0, -1.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, 1.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, -1.0, -1.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, 1.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, ], vec![ @@ -179,8 +182,8 @@ pub fn uv_sphere( vertices.push(Vertex { position: [x, y, z].into(), - uv: [(j as f32) / sectors, (i as f32) / stacks], - normal: [x * length_inv, y * length_inv, z * length_inv], + uv: Vector2::new((j as f32) / sectors, (i as f32) / stacks), + normal: Vector3::new(x * length_inv, y * length_inv, z * length_inv), }); } } diff --git a/crates/blue_engine_core/src/primitive_shapes/two_dimensions.rs b/crates/blue_engine_core/src/primitive_shapes/two_dimensions.rs index ec7ee46..7f0ee0c 100644 --- a/crates/blue_engine_core/src/primitive_shapes/two_dimensions.rs +++ b/crates/blue_engine_core/src/primitive_shapes/two_dimensions.rs @@ -2,11 +2,15 @@ * Blue Engine by Elham Aryanpur * * The license is same as the one on the root. -*/ + */ + +/* + * For the sake of example we never use Vector3::default() or Vector3::x_axis() or any axis. + */ use crate::{ header::{ObjectSettings, Vertex}, - ObjectStorage, Renderer, StringBuffer, + ObjectStorage, Renderer, StringBuffer, Vector2, Vector3, }; /// Creates a 2D triangle @@ -20,19 +24,19 @@ pub fn triangle( name.clone(), vec![ Vertex { - position: [0.0, 1.0, 0.0].into(), - uv: [0.5, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(0.0, 1.0, 0.0), + uv: Vector2::new(0.5, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, -1.0, 0.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, 0.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, -1.0, 0.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, 0.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, ], vec![0, 1, 2], @@ -52,24 +56,24 @@ pub fn square( name.clone(), vec![ Vertex { - position: [1.0, 1.0, 0.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, 1.0, 0.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [1.0, -1.0, 0.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(1.0, -1.0, 0.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, -1.0, 0.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, -1.0, 0.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-1.0, 1.0, 0.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-1.0, 1.0, 0.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, ], vec![2, 1, 0, 2, 0, 3], @@ -91,24 +95,24 @@ pub fn rectangle( name.clone(), vec![ Vertex { - position: [width / 2.0, height / 2.0, 0.0].into(), - uv: [1.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(width / 2.0, height / 2.0, 0.0), + uv: Vector2::new(1.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [width / 2.0, -height / 2.0, 0.0].into(), - uv: [1.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(width / 2.0, -height / 2.0, 0.0), + uv: Vector2::new(1.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-width / 2.0, -height / 2.0, 0.0].into(), - uv: [0.0, 1.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-width / 2.0, -height / 2.0, 0.0), + uv: Vector2::new(0.0, 1.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, Vertex { - position: [-width / 2.0, height / 2.0, 0.0].into(), - uv: [0.0, 0.0], - normal: [0.0, 0.0, 0.0], + position: Vector3::new(-width / 2.0, height / 2.0, 0.0), + uv: Vector2::new(0.0, 0.0), + normal: Vector3::new(0.0, 0.0, 0.0), }, ], vec![2, 1, 0, 2, 0, 3], diff --git a/crates/blue_engine_core/src/vector.rs b/crates/blue_engine_core/src/vector.rs index c6a8f3f..d4f160c 100644 --- a/crates/blue_engine_core/src/vector.rs +++ b/crates/blue_engine_core/src/vector.rs @@ -520,6 +520,8 @@ impl Vector2 { unsafe impl Send for Vector2 {} unsafe impl Sync for Vector2 {} +unsafe impl Pod for Vector2 {} + impl Add for Vector2 { type Output = Self; @@ -724,4 +726,4 @@ impl Neg for Vector2 { y: -self.y, } } -} \ No newline at end of file +}