primitive meshes

This commit is contained in:
Jérôme Bousquié
2026-09-24 14:25:44 +02:00
parent 805babe53d
commit ab3f056dbb
36 changed files with 1531 additions and 808 deletions
+1 -1
View File
@@ -13,7 +13,7 @@
use glam::{Quat, Vec3};
use wsg_lib::AppHandler;
use wsg_lib::app::AppBuilder;
use wsg_lib::math::cube;
use wsg_lib::mesh::cube;
use wsg_lib::resources::Texture;
use wsg_lib::utils::WsgError;
+2 -1
View File
@@ -32,7 +32,8 @@ use winit::keyboard::KeyCode;
use wsg_lib::AppHandler;
use wsg_lib::app::AppBuilder;
use wsg_lib::core::ToneMapper;
use wsg_lib::math::{Transform, cone, cube, cylinder, icosphere, plane, torus, uv_sphere};
use wsg_lib::core::Transform;
use wsg_lib::mesh::{cone, cube, cylinder, icosphere, plane, torus, uv_sphere};
use wsg_lib::resources::{CameraController, Texture};
use wsg_lib::utils::WsgError;
+66
View File
@@ -0,0 +1,66 @@
//! # Example: File Import (OBJ)
//!
//! Demonstrates loading a Wavefront OBJ file with `wsg_lib::mesh::load_obj`.
//! Parses the file and prints geometry statistics.
//!
//! ## Build & Run
//! ```sh
//! cargo run -p wsg-lib --example import --features import-obj -- /path/to/model.obj
//! ```
//!
//! Without a file argument, parses a built-in sample triangle.
use wsg_lib::mesh::import::parse_obj;
use wsg_lib::mesh::load_obj;
fn main() {
let args: Vec<String> = std::env::args().collect();
let content = if args.len() > 1 {
let path = &args[1];
eprintln!("Loading: {path}");
match load_obj(path) {
Ok(geom) => {
print_stats(&geom);
return;
}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
} else {
eprintln!("No file argument — parsing a built-in sample.");
eprintln!("Usage: import <model.obj>");
// Built-in sample: a simple triangle with UVs and normals
"v 0.0 0.0 0.0\nv 1.0 0.0 0.0\nv 0.5 1.0 0.0\nvn 0 0 1\nvt 0.0 0.0\nvt 1.0 0.0\nvt 0.5 1.0\nf 1/1/1 2/2/1 3/3/1\n"
};
let geom = parse_obj(content).expect("sample should parse");
print_stats(&geom);
}
fn print_stats(geom: &wsg_lib::Geometry) {
println!("\n=== Geometry Statistics ===");
println!(" Vertices: {}", geom.positions.len());
if let Some(n) = &geom.normals {
println!(" Normals: {}", n.len());
}
if let Some(uv) = &geom.uvs {
println!(" UVs: {}", uv.len());
}
if let Some(idx) = &geom.indices {
println!(" Indices: {} ({} triangles)", idx.len(), idx.len() / 3);
}
if let Err(e) = geom.validate() {
println!(" Validation FAILED: {e}");
} else {
println!(" Validation: OK");
}
// Bounding box
if let Some(bbox) = geom.bbox() {
println!(" BBox min: {:?}", bbox.min);
println!(" BBox max: {:?}", bbox.max);
}
println!();
}
+2 -2
View File
@@ -104,7 +104,7 @@ impl wsg_lib::AppHandler for ShadowTest {
.add_entity_with_transform(
"ground",
"ground_mesh",
wsg_lib::math::Transform::identity(),
wsg_lib::core::Transform::identity(),
)
.unwrap();
@@ -112,7 +112,7 @@ impl wsg_lib::AppHandler for ShadowTest {
app.scene
.create_mesh("cube_mesh", box_geometry(0.5, 0.5, 0.5), Some("mat"))
.unwrap();
let mut cube_tf = wsg_lib::math::Transform::identity();
let mut cube_tf = wsg_lib::core::Transform::identity();
cube_tf.translation = Vec3::new(0.0, 0.5, 0.0);
app.scene
.add_entity_with_transform("cube", "cube_mesh", cube_tf)
+1 -1
View File
@@ -12,7 +12,7 @@
use glam::{Quat, Vec3};
use wsg_lib::AppHandler;
use wsg_lib::app::AppBuilder;
use wsg_lib::math::cube;
use wsg_lib::mesh::cube;
use wsg_lib::utils::WsgError;
/// Test handler: cube rotating slowly on two axes, lit **only** by a spot.