Skip to content

Commit

Permalink
feat!: added Position3D for all of the position: [f32; 3]
Browse files Browse the repository at this point in the history
This should resolve #71 as all of the function that set a position taks `impl Into<Position3D>` as input and you can trivially cast a Position3D to a nalgebra_glm::Vec3 with `.into()` when needed
  • Loading branch information
Gipson62 committed Jan 11, 2025
1 parent 3469c3e commit eff4e4e
Show file tree
Hide file tree
Showing 8 changed files with 3,607 additions and 85 deletions.
3,330 changes: 3,330 additions & 0 deletions crates/blue_engine_core/Cargo.lock

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions crates/blue_engine_core/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub use uniform_buffer::*;

use downcast::{downcast, Any};

use crate::position::Position3D;

/// The uint type used for indices and more
#[cfg(feature = "u16")]
pub type UnsignedIntType = u16;
Expand Down Expand Up @@ -59,7 +61,7 @@ macro_rules! impl_deref_field {
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
/// Contains position data for the vertex in 3D space
pub position: [f32; 3],
pub position: Position3D,
/// Contains uv position data for the vertex
pub uv: [f32; 2],
/// Contains the normal face of the vertex
Expand Down Expand Up @@ -115,7 +117,7 @@ pub struct Object {
/// Dictates the size of your object in relation to the world
pub size: glm::Vec3,
/// Dictates the position of your object in pixels
pub position: glm::Vec3,
pub position: Position3D,
/// Dictates the rotation of your object
pub rotation: glm::Vec3,
// flags the object to be updated until next frame
Expand Down Expand Up @@ -434,11 +436,11 @@ pub enum Projection {
#[derive(Debug)]
pub struct Camera {
/// The position of the camera in 3D space
pub position: nalgebra_glm::Vec3,
pub position: Position3D,
/// The target at which the camera should be looking
pub target: nalgebra_glm::Vec3,
pub target: Position3D,
/// The up vector of the camera. This defines the elevation of the camera
pub up: nalgebra_glm::Vec3,
pub up: Position3D,
/// The resolution of the camera view
pub resolution: (f32, f32),
/// The projection of the camera
Expand Down Expand Up @@ -558,7 +560,7 @@ pub struct InstanceRaw {
#[derive(Debug, Clone, Copy)]
pub struct Instance {
/// The position of the instance
pub position: nalgebra_glm::Vec3,
pub position: Position3D,
/// The rotation of the instance
pub rotation: nalgebra_glm::Vec3,
/// The scale of the instance
Expand Down
2 changes: 2 additions & 0 deletions crates/blue_engine_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub mod error;
pub mod header;
/// contains the definition for Object type, which is a type that make it easier to manage data for rendering.
pub mod objects;
/// contains definition for position and rotation in 2D and 3D space.
pub mod position;
/// contains definition for some 2D and 3D shapes. They are basic shapes and
/// can be used as examples of how to create your own content.
pub mod primitive_shapes;
Expand Down
40 changes: 18 additions & 22 deletions crates/blue_engine_core/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::header::{
glm, pixel_to_cartesian, uniform_type, Instance, InstanceRaw, Object, ObjectSettings, Pipeline,
PipelineData, Renderer, RotateAxis, TextureData, Textures, Vertex,
};
use crate::position::Position3D;
use crate::uniform_type::{Array4, Matrix};
use crate::utils::default_resources::{DEFAULT_MATRIX_4, DEFAULT_SHADER, DEFAULT_TEXTURE};
use crate::{ObjectStorage, RotateAmount, StringBuffer, UnsignedIntType};
Expand Down Expand Up @@ -60,7 +61,7 @@ impl Renderer {
)?;

let instance = Instance::new(
[0f32, 0f32, 0f32].into(),
[0f32, 0f32, 0f32],
[0f32, 0f32, 0f32].into(),
[1f32, 1f32, 1f32].into(),
);
Expand All @@ -81,7 +82,7 @@ impl Renderer {
instance_buffer,
uniform_layout: uniform.1,
size: glm::vec3(1f32, 1f32, 1f32),
position: glm::vec3(0f32, 0f32, 0f32),
position: Position3D::default(),
rotation: glm::vec3(0f32, 0f32, 0f32),
changed: false,
position_matrix: DEFAULT_MATRIX_4.to_im(),
Expand Down Expand Up @@ -319,13 +320,11 @@ impl Object {
}

/// Moves the object by the amount you specify in the axis you specify
pub fn set_translation(&mut self, x: f32, y: f32, z: f32) -> &mut Self {
self.position.x -= x;
self.position.y -= y;
self.position.z -= z;
pub fn set_translation(&mut self, new_pos: impl Into<Position3D>) -> &mut Self {
self.position -= new_pos.into();

let mut position_matrix = self.position_matrix;
position_matrix = nalgebra_glm::translate(&position_matrix, &nalgebra_glm::vec3(x, y, z));
position_matrix = nalgebra_glm::translate(&position_matrix, &self.position.into());
self.position_matrix = position_matrix;

self.inverse_matrices();
Expand All @@ -334,16 +333,13 @@ impl Object {
}

/// Sets the position of the object in 3D space relative to the window
pub fn set_position(&mut self, x: f32, y: f32, z: f32) -> &mut Self {
self.set_translation(
(self.position.x - x) * -1f32,
(self.position.y - y) * -1f32,
(self.position.z - z) * -1f32,
);
pub fn set_position(&mut self, new_pos: impl Into<Position3D>) -> &mut Self {
let new_pos = new_pos.into();
self.set_translation((self.position - new_pos) * -1f32);

self.position.x = x;
self.position.y = y;
self.position.z = z;
self.position.x = new_pos.x;
self.position.y = new_pos.y;
self.position.z = new_pos.z;
self
}

Expand Down Expand Up @@ -628,17 +624,17 @@ impl ShaderBuilder {

impl Instance {
/// Creates a new instance
pub fn new(position: glm::Vec3, rotation: glm::Vec3, scale: glm::Vec3) -> Self {
pub fn new(position: impl Into<Position3D>, rotation: glm::Vec3, scale: glm::Vec3) -> Self {
Self {
position,
position: position.into(),
rotation,
scale,
}
}

/// Gathers all information and builds a Raw Instance to be sent to GPU
pub fn to_raw(&self) -> InstanceRaw {
let position_matrix = glm::translate(&DEFAULT_MATRIX_4.to_im(), &self.position);
let position_matrix = glm::translate(&DEFAULT_MATRIX_4.to_im(), &self.position.into());
let rotation_matrix = nalgebra_glm::rotate(&DEFAULT_MATRIX_4.to_im(), 0f32, &self.rotation);
let scale_matrix = glm::scale(&DEFAULT_MATRIX_4.to_im(), &self.scale);
InstanceRaw {
Expand All @@ -647,8 +643,8 @@ impl Instance {
}

/// Sets the position
pub fn set_position(&mut self, position: glm::Vec3) {
self.position = position;
pub fn set_position(&mut self, position: impl Into<Position3D>) {
self.position = position.into();
}

/// Sets the rotation
Expand All @@ -665,7 +661,7 @@ impl Instance {
impl Default for Instance {
fn default() -> Self {
Self {
position: glm::Vec3::new(0.0, 0.0, 0.0),
position: Position3D::default(),
rotation: glm::Vec3::new(0.0, 0.0, 0.0),
scale: glm::Vec3::new(1.0, 1.0, 1.0),
}
Expand Down
193 changes: 193 additions & 0 deletions crates/blue_engine_core/src/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
use std::ops::{Add, Mul, Sub, SubAssign};

use bytemuck::{Pod, Zeroable};

/// 3D position
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default, Zeroable)]
pub struct Position3D {
/// X coordinate in 3D space
pub x: f32,
/// Y coordinate in 3D space
pub y: f32,
/// Z coordinate in 3D space
pub z: f32,
}

impl Position3D {
/// Create a new 3D position with the given coordinates
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
}

unsafe impl Send for Position3D {}
unsafe impl Sync for Position3D {}

unsafe impl Pod for Position3D {}

impl Add for Position3D {
type Output = Self;

fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}

impl Sub for Position3D {
type Output = Self;

fn sub(self, other: Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}

impl SubAssign for Position3D {
fn sub_assign(&mut self, other: Self) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
}
}

impl Mul<f32> for Position3D {
type Output = Self;

fn mul(self, scalar: f32) -> Self {
Self {
x: self.x * scalar,
y: self.y * scalar,
z: self.z * scalar,
}
}
}

impl From<[f32; 3]> for Position3D {
fn from(pos: [f32; 3]) -> Self {
Self {
x: pos[0],
y: pos[1],
z: pos[2],
}
}
}

impl From<Position3D> for [f32; 3] {
fn from(pos: Position3D) -> Self {
[pos.x, pos.y, pos.z]
}
}

impl From<(f32, f32, f32)> for Position3D {
fn from(pos: (f32, f32, f32)) -> Self {
Self {
x: pos.0,
y: pos.1,
z: pos.2,
}
}
}

impl From<Position3D> for (f32, f32, f32) {
fn from(pos: Position3D) -> Self {
(pos.x, pos.y, pos.z)
}
}

impl From<nalgebra_glm::Vec3> for Position3D {
fn from(pos: nalgebra_glm::Vec3) -> Self {
Self {
x: pos.x,
y: pos.y,
z: pos.z,
}
}
}

impl From<Position3D> for nalgebra_glm::Vec3 {
fn from(pos: Position3D) -> Self {
nalgebra_glm::vec3(pos.x, pos.y, pos.z)
}
}

/// 2D position
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
pub struct Position2D {
/// X coordinate in 2D space
pub x: f32,
/// Y coordinate in 2D space
pub y: f32,
}

impl Position2D {
/// Create a new 2D position with the given coordinates
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}

unsafe impl Send for Position2D {}
unsafe impl Sync for Position2D {}

impl Add for Position2D {
type Output = Self;

fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}

impl From<[f32; 2]> for Position2D {
fn from(pos: [f32; 2]) -> Self {
Self {
x: pos[0],
y: pos[1],
}
}
}

impl From<Position2D> for [f32; 2] {
fn from(pos: Position2D) -> Self {
[pos.x, pos.y]
}
}

impl From<(f32, f32)> for Position2D {
fn from(pos: (f32, f32)) -> Self {
Self {
x: pos.0,
y: pos.1,
}
}
}

impl From<Position2D> for (f32, f32) {
fn from(pos: Position2D) -> Self {
(pos.x, pos.y)
}
}

impl From<nalgebra_glm::Vec2> for Position2D {
fn from(pos: nalgebra_glm::Vec2) -> Self {
Self {
x: pos.x,
y: pos.y,
}
}
}

impl From<Position2D> for nalgebra_glm::Vec2 {
fn from(pos: Position2D) -> Self {
nalgebra_glm::vec2(pos.x, pos.y)
}
}
Loading

0 comments on commit eff4e4e

Please sign in to comment.