From 1a1e387dd99a3232631009471bb240d11f3f0a50 Mon Sep 17 00:00:00 2001 From: Berkus Decker Date: Sat, 30 Mar 2024 15:37:38 +0200 Subject: [PATCH] wip: model loading --- src/main.rs | 19 +++++++++++++---- src/support/brender/model.rs | 40 +++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 04c919c..656b002 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,8 +18,12 @@ use { carma::{ assets::car_asset::{CarAsset, CarAssetLoader}, support::{ - brender::model::Model, camera::CameraState, car::Car, logger, - render_manager::RenderManager, visitor::visit_files, + brender::{material::Material, model::Model}, + camera::CameraState, + car::Car, + logger, + render_manager::RenderManager, + visitor::visit_files, }, }, cgmath::Vector3, @@ -59,8 +63,15 @@ fn setup_cars( // bevy @todo: load all textures into the texture atlas // TextureAtlasBuilder - let models = Model::load_many("assets/DecodedData/DATA/MODELS/NETCITYA.DAT".into()) - .expect("Load models"); + let mats = + Material::load_many("assets/DecodedData/DATA/MATERIAL/CITYA.MAT").expect("Load materials"); + // for mat in mats { + // debug!("{:?}", mat); + // } + + // models refer to materials to load! check that + let models = + Model::load_many("assets/DecodedData/DATA/MODELS/NETCITYA.DAT").expect("Load models"); // models.extend( // Model::load_many("assets/DecodedData/DATA/MODELS/CITYA.DAT".into()) diff --git a/src/support/brender/model.rs b/src/support/brender/model.rs index e6edb79..e177631 100644 --- a/src/support/brender/model.rs +++ b/src/support/brender/model.rs @@ -5,10 +5,9 @@ use { VerticesChunk, }, crate::support::Error, - bevy::render::mesh::VertexAttributeValues, + bevy::{prelude::*, render::mesh::VertexAttributeValues}, byteorder::ReadBytesExt, carma_derive::ResourceTag, - cgmath::{InnerSpace, Vector3}, culpa::{throw, throws}, std::{ fs::File, @@ -73,7 +72,7 @@ impl Model { } pub fn bevy_mesh(&self) -> bevy::render::mesh::Mesh { - use bevy::render::mesh::{Mesh, PrimitiveTopology}; + use bevy::render::mesh::PrimitiveTopology; let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); @@ -198,15 +197,18 @@ impl FromStream for Model { impl Model { // Single model file may contain multiple models #[throws] - pub fn load_many(fname: String) -> Vec> { - let file = File::open(fname)?; - let mut file = BufReader::new(file); + pub fn load_many + std::fmt::Debug>(filename: P) -> Vec> { + debug!("Loading many Models from {:?}", filename); + let mut file = BufReader::new(File::open(filename)?); let mut models = Vec::<_>::new(); loop { let m = Model::from_stream(&mut file); match m { Err(_) => break, // fixme: allow only Eof here - Ok(m) => models.push(m), + Ok(m) => { + debug!(".. Loaded {}", m.name); + models.push(m) + } } } models @@ -238,7 +240,7 @@ impl Model { #[cfg(test)] mod tests { - use {super::*, cgmath::Zero, std::io::Cursor}; + use {super::* /* , cgmath::Zero*/, std::io::Cursor}; #[test] fn test_load_model() { @@ -259,15 +261,15 @@ mod tests { } // test that normals to unit vectors will be the third unit vector - #[test] - fn test_calc_normal() { - assert_eq!( - calc_plane_normal(Vector3::unit_y(), Vector3::zero(), Vector3::unit_x()), - Vector3::unit_z() - ); - assert_eq!( - calc_plane_normal(Vector3::unit_x(), Vector3::zero(), Vector3::unit_y()), - -Vector3::unit_z() - ); - } + // #[test] + // fn test_calc_normal() { + // assert_eq!( + // calc_plane_normal(Vector3::unit_y(), Vector3::zero(), Vector3::unit_x()), + // Vector3::unit_z() + // ); + // assert_eq!( + // calc_plane_normal(Vector3::unit_x(), Vector3::zero(), Vector3::unit_y()), + // -Vector3::unit_z() + // ); + // } } // tests mod