summary refs log tree commit diff
path: root/src/model_loader.rs
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-07-20 20:43:48 -0700
committerIrene Knapp <ireneista@irenes.space>2026-07-20 20:43:48 -0700
commit3ac1e5e5a9ebcb4beb2bfe149dd5095f2250f60d (patch)
treed409eb8dfcca1b56dffe8c143a2c9028818d6d5d /src/model_loader.rs
parent365c2f8bdc217f33264e3aed394f67840a4c075d (diff)
load a nontrivial model and render it with a texture HEAD main
yay

Force-Push: yes
Change-Id: Icb715692286c8560532ab73d32a49be8f485d790
Diffstat (limited to 'src/model_loader.rs')
-rw-r--r--src/model_loader.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/model_loader.rs b/src/model_loader.rs
new file mode 100644
index 0000000..fea5ab5
--- /dev/null
+++ b/src/model_loader.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_model() -> 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))
+}
+