first steps at loading an obj
This commit is contained in:
parent
d7e87bdb1e
commit
85f945aef7
2 changed files with 69 additions and 4 deletions
11
src/main.rs
11
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)
|
||||
|
|
62
src/model.rs
Normal file
62
src/model.rs
Normal file
|
@ -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<Point3>,
|
||||
pub faces: Vec<Face>,
|
||||
}
|
||||
|
||||
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::<f32>().unwrap())
|
||||
.collect();
|
||||
|
||||
let v: [f32; 3] = v.try_into().unwrap();
|
||||
verts.push(v.into());
|
||||
}
|
||||
if *t == "f" {
|
||||
let f: Vec<usize> = 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 }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue