Skip to content

Commit

Permalink
wip: model loading
Browse files Browse the repository at this point in the history
  • Loading branch information
berkus committed Mar 30, 2024
1 parent 1f6c434 commit 1a1e387
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 =

Check warning on line 66 in src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

unused variable: `mats`

warning: unused variable: `mats` --> src/main.rs:66:9 | 66 | let mats = | ^^^^ help: if this is intentional, prefix it with an underscore: `_mats` | = note: `#[warn(unused_variables)]` on by default
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())
Expand Down
40 changes: 21 additions & 19 deletions src/support/brender/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<Box<Model>> {
let file = File::open(fname)?;
let mut file = BufReader::new(file);
pub fn load_many<P: AsRef<std::path::Path> + std::fmt::Debug>(filename: P) -> Vec<Box<Model>> {
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
Expand Down Expand Up @@ -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() {
Expand All @@ -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

0 comments on commit 1a1e387

Please sign in to comment.