diff --git a/src/main.rs b/src/main.rs index d49b7cf..348ace8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,9 @@ use tga::*; mod point; use point::*; +mod model; +use model::*; + const WHITE: TGAColor = TGAColor { bgra: [255, 255, 255, 255], }; @@ -29,12 +32,12 @@ const YELLOW: TGAColor = TGAColor { }; fn main() { - let w = 64; - let h = 64; + let w = 800; + let h = 800; let mut framebuffer = TGAImage::new(w, h, TGAFormat::RGB); + let model = Model::from_obj("diablo3_pose.obj"); - //bench(&mut framebuffer); - baseline(&mut framebuffer); + dbg!(model); framebuffer .write_file("framebuffer.tga", true, true) diff --git a/src/model.rs b/src/model.rs new file mode 100644 index 0000000..f4bf407 --- /dev/null +++ b/src/model.rs @@ -0,0 +1,62 @@ +#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd)] +pub struct Point3 { + store: [f32; 3], +} + +impl From<[f32; 3]> for Point3 { + fn from(value: [f32; 3]) -> Self { + Self { store: value } + } +} + +#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd)] +pub struct Face { + store: [usize; 3], +} + +impl From<[usize; 3]> for Face { + fn from(value: [usize; 3]) -> Self { + Self { store: value } + } +} + +#[derive(Default, Debug, Clone, PartialEq, PartialOrd)] +pub struct Model { + pub verts: Vec, + pub faces: Vec, +} + +impl Model { + pub fn from_obj(file: &str) -> Self { + let lines = std::fs::read_to_string(file).unwrap(); + let lines = lines.lines(); + + let mut verts = Vec::with_capacity(lines.size_hint().1.unwrap_or(1 << 16)); + let mut faces = Vec::new(); + + for line in lines { + let line: Vec<_> = line.split_whitespace().collect(); + if let Some(t) = line.get(0) { + if *t == "v" { + let v: Vec<_> = line[1..4] + .iter() + .map(|v| v.parse::().unwrap()) + .collect(); + + let v: [f32; 3] = v.try_into().unwrap(); + verts.push(v.into()); + } + if *t == "f" { + let f: Vec = line[1..4] + .iter() + .map(|l| l.split('/').take(1).next().unwrap().parse().unwrap()) + .collect(); + let f: [usize; 3] = f.try_into().unwrap(); + faces.push(f.into()); + } + } + } + + Self { verts, faces } + } +}