Skip to content

Commit

Permalink
Add Loader API & clean up tests/examples to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 15, 2024
1 parent aa9e65f commit 0adade3
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 181 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ workspace = true

[workspace]
resolver = "2"
members = ["example"]
members = [
"examples/kiss3d",
]

# This table is shared by projects under https://github.com/taiki-e.
# It is not intended for manual editing.
Expand Down
153 changes: 0 additions & 153 deletions example/examples/kiss3d.rs

This file was deleted.

8 changes: 4 additions & 4 deletions example/Cargo.toml → examples/kiss3d/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "example"
name = "kiss3d-example"
version = "0.0.0"
edition = "2021"
publish = false

[dev-dependencies]
mesh-loader = { path = ".." }
[dependencies]
mesh-loader = { path = "../.." }

anyhow = "1"
clap = { version = "3", features = ["derive"] }
kiss3d = { git = "https://github.com/sebcrozet/kiss3d.git", rev = "da70cd0", features = ["vertex_index_u32"] }
lexopt = "0.3"

[lints]
workspace = true
114 changes: 114 additions & 0 deletions examples/kiss3d/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use std::{
cell::RefCell,
path::{Path, PathBuf},
rc::Rc,
};

use anyhow::Result;
use kiss3d::{light::Light, nalgebra as na, scene::SceneNode, window::Window};
use lexopt::prelude::*;
use na::{Translation3, UnitQuaternion, Vector3};

const DEFAULT_SCALE: f32 = 0.1;

#[derive(Debug)]
struct Args {
path: PathBuf,
scale: f32,
}

impl Args {
fn parse() -> Result<Self> {
let mut parser = lexopt::Parser::from_env();
let mut path = None;
let mut scale = None;
while let Some(arg) = parser.next()? {
match arg {
Value(v) => path = Some(v.into()),
Long("scale") => scale = Some(parser.value()?.parse()?),
Short('h') | Long("help") => {
path = None;
break;
}
arg => return Err(arg.unexpected().into()),
}
}
let Some(path) = path else {
println!(
"Usage: cargo run --bin {} -- <PATH> [--scale <SCALE={DEFAULT_SCALE}>]",
env!("CARGO_BIN_NAME")
);
std::process::exit(1);
};
Ok(Self {
path,
scale: scale.unwrap_or(DEFAULT_SCALE),
})
}
}

fn main() -> Result<()> {
let args = Args::parse()?;
eprintln!("args={args:?}");
let path = &args.path;
let scale = Vector3::new(args.scale, args.scale, args.scale);

let mut window = Window::new(&format!("{} ー mesh-loader example", args.path.display()));

let mut base = add_mesh(&mut window, path, scale)?;
base.set_local_scale(args.scale, args.scale, args.scale);

base.append_translation(&Translation3::new(0.0, -0.05, -0.2));

window.set_light(Light::StickToCamera);

let rot_triangle = UnitQuaternion::from_axis_angle(&Vector3::z_axis(), 0.014);

let eye = na::Point3::new(3.0f32, 1.0, 1.0);
let at = na::Point3::new(0.0f32, 0.0, 0.0);
let mut camera = kiss3d::camera::ArcBall::new(eye, at);
camera.set_up_axis(na::Vector3::z());
camera.set_dist_step(0.5);
while window.render_with_camera(&mut camera) {
base.prepend_to_local_rotation(&rot_triangle);
}

Ok(())
}

fn add_mesh(window: &mut Window, path: &Path, scale: na::Vector3<f32>) -> Result<SceneNode> {
let loader = mesh_loader::Loader::default().merge_meshes(true);
let mut scene = loader.load(path)?;
assert_eq!(scene.meshes.len(), 1); // merge_meshes guarantees this.
let mesh = scene.meshes.pop().unwrap();
eprintln!("mesh={mesh:?}");
let coords = mesh.vertices.into_iter().map(Into::into).collect();
let faces = mesh
.faces
.into_iter()
.map(|f| na::Point3::new(f[0], f[1], f[2]))
.collect();
let normals = if mesh.normals.is_empty() {
None
} else {
Some(mesh.normals.into_iter().map(Into::into).collect())
};
let uvs = if mesh.texcoords[0].is_empty() {
None
} else {
Some(mesh.texcoords[0].iter().copied().map(Into::into).collect())
};
let kiss3d_mesh = Rc::new(RefCell::new(kiss3d::resource::Mesh::new(
coords, faces, normals, uvs, false,
)));
let kiss3d_scene = window.add_mesh(kiss3d_mesh, scale);
// TODO(material)
// if let Some(color) = material.diffuse_color() {
// kiss3d_scene.set_color(color[0], color[1], color[2]);
// }
// if let Some(path) = materials.get(0) {
// kiss3d_scene.set_texture_from_file(path, path.to_str().unwrap());
// }

Ok(kiss3d_scene)
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod error;
#[cfg(any(feature = "collada", feature = "stl"))]
mod utils;

mod loader;
pub use loader::*;
mod common;
pub use common::*;

Expand Down
Loading

0 comments on commit 0adade3

Please sign in to comment.