feat(math): Étape 15.A primitives — cube, plane, uv_sphere, icosphere, cylinder, cone, torus + tests ; factorisation cube_geometry (cube, spot_test)
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
use glam::{Quat, Vec3};
|
||||
use wsg_lib::AppHandler;
|
||||
use wsg_lib::app::AppBuilder;
|
||||
use wsg_lib::resources::Geometry;
|
||||
use wsg_lib::math::cube;
|
||||
use wsg_lib::utils::WsgError;
|
||||
|
||||
/// Handler de test : cube qui tourne lentement sur deux axes, éclairé **uniquement** par une spot.
|
||||
@@ -21,58 +21,6 @@ struct SpotTest {
|
||||
angle_y: f32,
|
||||
}
|
||||
|
||||
/// Cube unitaire centré à l'origine (mêmes 24 sommets / 36 indices que l'exemple `cube`).
|
||||
fn cube_geometry() -> Geometry {
|
||||
let s = 0.5;
|
||||
let faces: [([f32; 3], [[f32; 3]; 4]); 6] = [
|
||||
(
|
||||
[0.0, 0.0, 1.0],
|
||||
[[-s, -s, s], [s, -s, s], [s, s, s], [-s, s, s]],
|
||||
), // +Z
|
||||
(
|
||||
[0.0, 0.0, -1.0],
|
||||
[[s, -s, -s], [-s, -s, -s], [-s, s, -s], [s, s, -s]],
|
||||
), // -Z
|
||||
(
|
||||
[1.0, 0.0, 0.0],
|
||||
[[s, -s, -s], [s, s, -s], [s, s, s], [s, -s, s]],
|
||||
), // +X
|
||||
(
|
||||
[-1.0, 0.0, 0.0],
|
||||
[[-s, -s, s], [-s, s, s], [-s, s, -s], [-s, -s, -s]],
|
||||
), // -X
|
||||
(
|
||||
[0.0, 1.0, 0.0],
|
||||
[[-s, s, -s], [s, s, -s], [s, s, s], [-s, s, s]],
|
||||
), // +Y
|
||||
(
|
||||
[0.0, -1.0, 0.0],
|
||||
[[-s, -s, s], [s, -s, s], [s, -s, -s], [-s, -s, -s]],
|
||||
), // -Y
|
||||
];
|
||||
|
||||
let mut positions = Vec::with_capacity(24);
|
||||
let mut normals = Vec::with_capacity(24);
|
||||
let mut uvs = Vec::with_capacity(24);
|
||||
let quad_uvs = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
|
||||
for (normal, corners) in faces {
|
||||
for (i, corner) in corners.iter().enumerate() {
|
||||
positions.push(*corner);
|
||||
normals.push(normal);
|
||||
uvs.push(quad_uvs[i]);
|
||||
}
|
||||
}
|
||||
let mut indices = Vec::with_capacity(36);
|
||||
for face in 0..6u16 {
|
||||
let b = face * 4;
|
||||
indices.extend_from_slice(&[b, b + 1, b + 2, b, b + 2, b + 3]);
|
||||
}
|
||||
Geometry::new(positions)
|
||||
.with_normals(normals)
|
||||
.with_uvs(uvs)
|
||||
.with_indices(indices)
|
||||
}
|
||||
|
||||
impl AppHandler for SpotTest {
|
||||
fn setup(&mut self, app: &mut wsg_lib::App) {
|
||||
app.scene
|
||||
@@ -80,7 +28,7 @@ impl AppHandler for SpotTest {
|
||||
.unwrap();
|
||||
app.scene.add_material_shader("mat", "standard").unwrap();
|
||||
app.scene
|
||||
.create_mesh("cube_mesh", cube_geometry(), Some("mat"))
|
||||
.create_mesh("cube_mesh", cube(1.0), Some("mat"))
|
||||
.unwrap();
|
||||
app.scene.add_entity("cube", "cube_mesh").unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user