add from_path to lib

This commit is contained in:
Robin Appelman 2023-12-17 14:59:46 +01:00
commit 92449728fb
3 changed files with 22 additions and 31 deletions

View file

@ -15,8 +15,8 @@ use gltf_json::Index;
use std::borrow::Cow; use std::borrow::Cow;
use std::env::args_os; use std::env::args_os;
use std::io::Write; use std::io::Write;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use vmdl::{Mdl, Model, Vtx, Vvd}; use vmdl::Model;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum Output { enum Output {
@ -141,20 +141,9 @@ fn export(model: Model, output: Output) -> Result<(), Error> {
Ok(()) Ok(())
} }
fn load(path: &Path) -> Result<Model, vmdl::ModelError> {
let data = fs::read(path)?;
let mdl = Mdl::read(&data)?;
let data = fs::read(path.with_extension("dx90.vtx"))?;
let vtx = Vtx::read(&data)?;
let data = fs::read(path.with_extension("vvd"))?;
let vvd = Vvd::read(&data)?;
Ok(Model::from_parts(mdl, vtx, vvd))
}
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let path = PathBuf::from(args_os().nth(1).expect("No model file provided")); let path = PathBuf::from(args_os().nth(1).expect("No model file provided"));
let source_model = load(&path)?; let source_model = Model::from_path(&path)?;
export(source_model, Output::Binary)?; export(source_model, Output::Binary)?;
Ok(()) Ok(())

View file

@ -4,14 +4,10 @@ mod material;
use crate::loader::{LoadError, Loader}; use crate::loader::{LoadError, Loader};
use crate::material::load_material_fallback; use crate::material::load_material_fallback;
use std::env::args_os; use std::env::args_os;
use std::fs; use std::path::PathBuf;
use std::path::{Path, PathBuf};
use thiserror::Error; use thiserror::Error;
use three_d::*; use three_d::*;
use tracing::error; use tracing::error;
use vmdl::mdl::Mdl;
use vmdl::vtx::Vtx;
use vmdl::vvd::Vvd;
use vmdl::{Model, Vector}; use vmdl::{Model, Vector};
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -51,7 +47,7 @@ fn main() -> Result<(), Error> {
let mut args = args_os(); let mut args = args_os();
let _ = args.next(); let _ = args.next();
let path = PathBuf::from(args.next().expect("No demo file provided")); let path = PathBuf::from(args.next().expect("No demo file provided"));
let source_model = load(&path).unwrap(); let source_model = Model::from_path(&path)?;
let window = Window::new(WindowSettings { let window = Window::new(WindowSettings {
title: path.display().to_string(), title: path.display().to_string(),
@ -247,17 +243,6 @@ fn main() -> Result<(), Error> {
Ok(()) Ok(())
} }
fn load(path: &Path) -> Result<Model, vmdl::ModelError> {
let data = fs::read(path)?;
let mdl = Mdl::read(&data)?;
let data = fs::read(path.with_extension("dx90.vtx"))?;
let vtx = Vtx::read(&data)?;
let data = fs::read(path.with_extension("vvd"))?;
let vvd = Vvd::read(&data)?;
Ok(Model::from_parts(mdl, vtx, vvd))
}
// 1 hammer unit is ~1.905cm // 1 hammer unit is ~1.905cm
const UNIT_SCALE: f32 = 1.0 / (1.905 * 100.0); const UNIT_SCALE: f32 = 1.0 / (1.905 * 100.0);

View file

@ -15,7 +15,9 @@ pub use error::*;
pub use handle::Handle; pub use handle::Handle;
pub use shared::*; pub use shared::*;
use std::any::type_name; use std::any::type_name;
use std::fs;
use std::mem::size_of; use std::mem::size_of;
use std::path::Path;
pub struct Model { pub struct Model {
#[allow(dead_code)] #[allow(dead_code)]
@ -29,6 +31,21 @@ impl Model {
Model { mdl, vtx, vvd } Model { mdl, vtx, vvd }
} }
/// Load the model from path
///
/// Requires a path to the `.mdl` file and the `.dx90.vtx` and `.vvd` files for the model to be in the same directory.
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, ModelError> {
let path = path.as_ref();
let data = fs::read(path)?;
let mdl = Mdl::read(&data)?;
let data = fs::read(path.with_extension("dx90.vtx"))?;
let vtx = Vtx::read(&data)?;
let data = fs::read(path.with_extension("vvd"))?;
let vvd = Vvd::read(&data)?;
Ok(Model::from_parts(mdl, vtx, vvd))
}
pub fn vertices(&self) -> &[Vertex] { pub fn vertices(&self) -> &[Vertex] {
&self.vvd.vertices &self.vvd.vertices
} }