From 895965750fbede1b6894235830a3754e1b5aabc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Bousqui=C3=A9?= Date: Sat, 1 Aug 2026 21:25:43 +0200 Subject: [PATCH] =?UTF-8?q?pr=C3=A9paration=20phase=201.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 8 +++++ docs/ROADMAP.md | 6 ++-- docs/{ => rules}/OKF_SPEC.md | 0 lib/Cargo.toml | 2 ++ lib/src/lib.rs | 1 + lib/src/math/geometry.rs | 28 +++++++++++++++++ lib/src/math/mod.rs | 23 ++++++++++++++ lib/src/math/transform.rs | 45 ++++++++++++++++++++++++++++ lib/src/resources/camera.rs | 58 ++++++++++++++++++++++++++++++++++++ 9 files changed, 168 insertions(+), 3 deletions(-) rename docs/{ => rules}/OKF_SPEC.md (100%) create mode 100644 lib/src/math/geometry.rs create mode 100644 lib/src/math/mod.rs create mode 100644 lib/src/math/transform.rs create mode 100644 lib/src/resources/camera.rs diff --git a/Cargo.lock b/Cargo.lock index 3d1b6c5..83d1108 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -501,6 +501,12 @@ dependencies = [ "xml-rs", ] +[[package]] +name = "glam" +version = "0.33.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f22fb22f065b308be0d8724e3706c7fa3fc2a6c7d6899df4cad7860e7a75436" + [[package]] name = "glow" version = "0.17.0" @@ -2323,7 +2329,9 @@ name = "wsg-lib" version = "0.1.0" dependencies = [ "bytemuck", + "glam", "pollster", + "slotmap", "thiserror 2.0.18", "wgpu", "winit", diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 67ffb6a..423ca32 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -19,16 +19,16 @@ generated: { by: human:jerome, at: 2026-07-31T00:00:00Z } **Objectif** : Afficher un cube (ou autre mesh) 3D avec un éclairage Phong basique. ### 1.1 Dépendances & Mathématiques -- [ ] Ajouter `glam = "0.29"` en dépendance (`lib/Cargo.toml`) +- [ ] Ajouter `glam = "0.33"` en dépendance (`lib/Cargo.toml`) - [ ] Ajouter `slotmap = "1.0"` en dépendance - [ ] Créer module `math/` (ou `transform.rs`) : - [ ] Struct `Transform { translation: Vec3, rotation: Quat, scale: Vec3 }` - [ ] Méthode `to_matrix() -> Mat4` pour calculer la matrice locale - - [ ] Struct `Camera { position: Vec3, target: Vec3, up: Vec3 }` + - [ ] Struct `Camera { position: Vec3, target: Vec3, up: Vec3 }` : resources/camera.rs - [ ] Fonctions `view_matrix()` et `projection_matrix(fov, aspect, near, far)` ### 1.2 Geometry & Mesh -- [ ] Créer struct `Geometry` : +- [ ] Créer struct `Geometry` (math/geometry.rs) : - [ ] `positions: Vec<[f32; 3]>` (obligatoire) - [ ] `indices: Option>` (optionnel) - [ ] `normals: Option>` (pour Phong) diff --git a/docs/OKF_SPEC.md b/docs/rules/OKF_SPEC.md similarity index 100% rename from docs/OKF_SPEC.md rename to docs/rules/OKF_SPEC.md diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 0cec851..a3f851e 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -11,6 +11,8 @@ wgpu = "30.0.0" # Vérifiez la version la plus récente winit = "0.29" # For window management — pinned to match examples thiserror = "2" bytemuck = { version = "1.25.0", features = ["derive"] } +glam = "0.33" +slotmap = "1.0" [dev-dependencies] pollster = { version="0.4.0", features = ["macro"] } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 4d2498d..c79d387 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -32,6 +32,7 @@ pub mod pipeline; pub mod resources; pub mod scene; pub mod utils; +pub mod math; /// Re-export of the high-level application facade for convenient top-level access. /// Users create App instances via `AppBuilder`, then call `.run(handler)` to start the application. diff --git a/lib/src/math/geometry.rs b/lib/src/math/geometry.rs new file mode 100644 index 0000000..a677903 --- /dev/null +++ b/lib/src/math/geometry.rs @@ -0,0 +1,28 @@ +//! # Geometry Module +//! +//! Defines the `Geometry` struct for storing vertex data of 3D meshes. +//! This module handles the core geometric representation used by meshes. +//! +//! ## Usage +//! - Stores vertex attributes (positions, normals, UVs) +//! - Used by `Mesh` to define its vertex data +//! - Passed to shaders for rendering +//! +//! ## Related Types +//! - `Geometry`: Main struct for vertex data storage +//! - Fields: positions, normals, uvs, indices + +/// Represents the geometric data of a 3D mesh. +/// +/// This struct stores the core vertex attributes that define a mesh's shape. +#[derive(Debug, Clone)] +pub struct Geometry { + /// Vertex positions as an array of 3D coordinates + pub positions: Vec<[f32; 3]>, + /// Optional vertex normals for lighting calculations + pub normals: Option>, + /// Optional texture coordinates for UV mapping + pub uvs: Option>, + /// Optional indices for indexed rendering + pub indices: Option>, +} \ No newline at end of file diff --git a/lib/src/math/mod.rs b/lib/src/math/mod.rs new file mode 100644 index 0000000..ec464d4 --- /dev/null +++ b/lib/src/math/mod.rs @@ -0,0 +1,23 @@ +//! # Math Module — Geometric and Transformation Utilities +//! +//! Provides core mathematical types and utilities for 3D graphics operations, including: +//! - `Transform` for object positioning, rotation, and scaling +//! - `Camera` for view and projection matrix calculations +//! - `Geometry` for mesh vertex data representation +//! +//! ## Interaction with Other Modules +//! - `scene::Scene` uses `Transform` to manage entity positions +//! - `renderer::Renderer` uses `Transform` and `Camera` to compute matrices for shaders +//! - `resources::Mesh` stores vertex data in `Geometry` format +//! +//! ## Files +//! - `transform.rs`: Defines the `Transform` struct and its conversion to matrix form +//! - `geometry.rs`: Defines the `Geometry` struct for mesh data storage +//! - `camera.rs`: Defines the `Camera` struct and view/projection matrix calculations + +pub mod transform; +pub mod geometry; + +// Re-exports +pub use transform::Transform; +pub use geometry::Geometry; \ No newline at end of file diff --git a/lib/src/math/transform.rs b/lib/src/math/transform.rs new file mode 100644 index 0000000..a498af8 --- /dev/null +++ b/lib/src/math/transform.rs @@ -0,0 +1,45 @@ +//! # Transform Module +//! +//! Defines the `Transform` struct for representing object transformations in 3D space, +//! including translation, rotation, and scale. Also provides functionality to convert +//! the transform into a 4x4 matrix for use in shaders. +//! +//! ## Usage +//! - Used by `Scene` entities to define their position in the world +//! - Converted to `Mat4` for MVP matrix calculations in the renderer +//! +//! ## Related Types +//! - `Transform`: Core struct for position/rotation/scale +//! - `to_matrix()`: Converts transform to a 4x4 matrix + +use glam::{Vec3, Quat, Mat4}; + +/// Represents a 3D transformation with translation, rotation, and scale. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Transform { + /// Translation vector in 3D space + pub translation: Vec3, + /// Rotation as a quaternion + pub rotation: Quat, + /// Scale factors along X, Y, Z axes + pub scale: Vec3, +} + +impl Transform { + /// Creates a new identity transform. + pub fn identity() -> Self { + Self { + translation: Vec3::ZERO, + rotation: Quat::IDENTITY, + scale: Vec3::ONE, + } + } + + /// Converts the transform into a 4x4 transformation matrix. + /// + /// # Returns + /// A `Mat4` representing the transformation matrix + pub fn to_matrix(&self) -> Mat4 { + Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation) + } +} \ No newline at end of file diff --git a/lib/src/resources/camera.rs b/lib/src/resources/camera.rs new file mode 100644 index 0000000..61727d4 --- /dev/null +++ b/lib/src/resources/camera.rs @@ -0,0 +1,58 @@ +//! # Camera Module +//! +//! Defines the `Camera` struct and related functionality for 3D viewing. +//! Supports different camera types and projection configurations. +//! +//! ## Usage +//! - Used by `Renderer` to compute view and projection matrices +//! - Configurable for perspective and orthographic projections +//! - Supports FPS-style and orbital movement patterns +//! +//! ## Related Types +//! - `Camera`: Main struct for camera configuration +//! - `view_matrix()`: Computes the view matrix +//! - `projection_matrix()`: Computes the projection matrix + +use glam::{Vec3, Mat4}; + +/// Represents a 3D camera for viewing the scene. +/// +/// The camera defines the viewpoint and projection settings for rendering. +#[derive(Debug, Clone)] +pub struct Camera { + /// Position of the camera in world space + pub position: Vec3, + /// Target point the camera is looking at + pub target: Vec3, + /// Up vector defining the camera's orientation + pub up: Vec3, +} + +impl Camera { + /// Creates a new camera with specified position, target, and up vector. + pub fn new(position: Vec3, target: Vec3, up: Vec3) -> Self { + Self { position, target, up } + } + + /// Computes the view matrix for this camera. + /// + /// # Returns + /// A `Mat4` representing the view transformation matrix + pub fn view_matrix(&self) -> Mat4 { + Mat4::look_at_rh(self.position, self.target, self.up) + } + + /// Computes the projection matrix for this camera. + /// + /// # Parameters + /// - `fov`: Field of view in radians + /// - `aspect`: Aspect ratio of the viewport + /// - `near`: Near clipping plane distance + /// - `far`: Far clipping plane distance + /// + /// # Returns + /// A `Mat4` representing the projection transformation matrix + pub fn projection_matrix(&self, fov: f32, aspect: f32, near: f32, far: f32) -> Mat4 { + Mat4::perspective_rh_gl(fov, aspect, near, far) + } +} \ No newline at end of file