diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-08-08 20:11:06 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-08-08 20:11:06 -0700 |
| commit | 4b877079872248d3a4c17312b18599c6084e1e2d (patch) | |
| tree | 1c764f0e7700e3cfec3aad64bf8bf3e1fa7b0261 /src/files | |
| parent | d56eb5e4c223f5c89117cda8dfb1527e02b6a90c (diff) | |
move the obj loader to a more logical place
Force-Push: yes Change-Id: I0d506fa517daeacf63b52fdd5900f1bbee9c22b7
Diffstat (limited to 'src/files')
| -rw-r--r-- | src/files/mod.rs | 3 | ||||
| -rw-r--r-- | src/files/obj.rs | 44 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/files/mod.rs b/src/files/mod.rs new file mode 100644 index 0000000..8bc7539 --- /dev/null +++ b/src/files/mod.rs @@ -0,0 +1,3 @@ +#![deny(unsafe_code)] + +pub mod obj; diff --git a/src/files/obj.rs b/src/files/obj.rs new file mode 100644 index 0000000..c45ffbf --- /dev/null +++ b/src/files/obj.rs @@ -0,0 +1,44 @@ +#![forbid(unsafe_code)] +use crate::error::*; +use crate::linear_algebra::{ Vec2, Vec3 }; +use crate::shader_data::Vertex; + +use std::io::Cursor; + +use tobj::{ load_obj_buf, LoadOptions }; + + +pub fn load_obj() -> Result<(Vec<Vertex<f32>>, Vec<u32>)> { + let obj = include_bytes!("../../models/teapot.obj"); + + let load_options = LoadOptions { + single_index: true, + triangulate: true, + ..Default::default() + }; + let (models, materials) = load_obj_buf(&mut Cursor::new(obj), &load_options, + |_| Ok(Default::default()))?; + + let _ = materials?; + + let mut vertices = Vec::new(); + let mut indices = Vec::new(); + + for model in models { + for index in 0 .. model.mesh.positions.len() / 3 { + vertices.push(Vertex::new(Vec3::new(model.mesh.positions[index*3], + model.mesh.positions[index*3 + 1], + model.mesh.positions[index*3 + 2]), + Vec3::new(1.0, 1.0, 1.0), + Vec2::new(model.mesh.texcoords[index*2], + model.mesh.texcoords[index*2 + 1]))) + } + + for index in model.mesh.indices { + indices.push(index); + } + } + + Ok((vertices, indices)) +} + |