Skip to content

Instantly share code, notes, and snippets.

@sajattack
Created July 11, 2022 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajattack/1f8961294f75df795473bc1231c4ce5b to your computer and use it in GitHub Desktop.
Save sajattack/1f8961294f75df795473bc1231c4ce5b to your computer and use it in GitHub Desktop.
Wavefront OBJ to raw vertices, used for importing models to PSP, very primitive
[package]
name = "obj2verts"
version = "0.1.0"
authors = ["Paul Sajna <sajattack@gmail.com>"]
edition = "2018"
[dependencies]
wavefront_obj = "8.0.0"
byteorder = "1.3.4"
use wavefront_obj::obj::{self, Primitive};
use std::fs;
use byteorder::{LittleEndian, WriteBytesExt};
fn main() -> Result<(), std::io::Error> {
let obj_string = fs::read_to_string("rustacean1.obj")?;
let objects = obj::parse(obj_string).unwrap();
let object = objects.objects[0].clone();
let vertices_indexed = object.vertices;
let normals_indexed = object.normals;
let mut output_file = fs::File::create("rustacean1.raw")?;
for shape in object.geometry[0].shapes.clone() {
let primitive = shape.primitive;
if let Primitive::Triangle(x, y, z) = primitive {
// x.nx
output_file.write_f32::<LittleEndian>(normals_indexed[x.2.unwrap()].x as f32)?;
// x.ny
output_file.write_f32::<LittleEndian>(normals_indexed[x.2.unwrap()].y as f32)?;
// x.nz
output_file.write_f32::<LittleEndian>(normals_indexed[x.2.unwrap()].z as f32)?;
// x.px
output_file.write_f32::<LittleEndian>(vertices_indexed[x.0].x as f32)?;
// x.py
output_file.write_f32::<LittleEndian>(vertices_indexed[x.0].y as f32)?;
// x.pz
output_file.write_f32::<LittleEndian>(vertices_indexed[x.0].z as f32)?;
// y.nx
output_file.write_f32::<LittleEndian>(normals_indexed[y.2.unwrap()].x as f32)?;
// y.ny
output_file.write_f32::<LittleEndian>(normals_indexed[y.2.unwrap()].y as f32)?;
// y.nz
output_file.write_f32::<LittleEndian>(normals_indexed[y.2.unwrap()].z as f32)?;
// y.px
output_file.write_f32::<LittleEndian>(vertices_indexed[y.0].x as f32)?;
// y.py
output_file.write_f32::<LittleEndian>(vertices_indexed[y.0].y as f32)?;
// y.pz
output_file.write_f32::<LittleEndian>(vertices_indexed[y.0].z as f32)?;
// z.nx
output_file.write_f32::<LittleEndian>(normals_indexed[z.2.unwrap()].x as f32)?;
// z.ny
output_file.write_f32::<LittleEndian>(normals_indexed[z.2.unwrap()].y as f32)?;
// z.nz
output_file.write_f32::<LittleEndian>(normals_indexed[z.2.unwrap()].z as f32)?;
// z.px
output_file.write_f32::<LittleEndian>(vertices_indexed[z.0].x as f32)?;
// z.py
output_file.write_f32::<LittleEndian>(vertices_indexed[z.0].y as f32)?;
// z.pz
output_file.write_f32::<LittleEndian>(vertices_indexed[z.0].z as f32)?;
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment