Skip to content

Commit

Permalink
refactor!: replaced every [f32; 2] with Vector2
Browse files Browse the repository at this point in the history
Also modified the ./primitive_shapes codes to use directly Vector3 & Vector2 directly instead of casting arrays to their respectives Vector.
  • Loading branch information
Gipson62 committed Jan 11, 2025
1 parent c648c14 commit 4dbf03c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 118 deletions.
11 changes: 7 additions & 4 deletions crates/blue_engine_core/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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::<Vector3>() as wgpu::BufferAddress`
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
},
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
159 changes: 81 additions & 78 deletions crates/blue_engine_core/src/primitive_shapes/three_dimensions.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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![
Expand Down Expand Up @@ -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),
});
}
}
Expand Down
74 changes: 39 additions & 35 deletions crates/blue_engine_core/src/primitive_shapes/two_dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand Down
Loading

0 comments on commit 4dbf03c

Please sign in to comment.