Begin implementing obj rotation

This commit is contained in:
Jonathan Flueren 2022-09-27 17:19:21 +02:00
parent 637abb06ad
commit fd9cbea2f6

View file

@ -290,24 +290,38 @@ fn from_obj(path: &str) -> HittableList {
let material = Arc::new(Metal::new(&Color::new(0.7, 0.6, 0.5), 0.0));
//let material = Arc::new(Rainbow::new());
let rotate_obj = 0; // rotate clockwise, 0: 0, 1: 90, 2: 180, 3: 270
let cornell_box = tobj::load_obj(path, &tobj::OFFLINE_RENDERING_LOAD_OPTIONS);
let (models, materials) = cornell_box.expect("Failed to load OBJ file");
let materials = materials.expect("Failed to load MTL file");
let mut new_x = 0;
let mut new_y = 1;
let mut new_z = 2;
match rotate_obj {
1 => {
new_x = 0;
new_y = 2;
new_z = 1;
},
2 => {
new_x = 0;
new_y = 2;
new_z = 1;
},
3 => {
new_x = 0;
new_y = 2;
new_z = 1;
},
_ => {}
}
for (i, m) in models.iter().enumerate() {
let mesh = &m.mesh;
/* Mesh vector lengths
println!("positions: {}", mesh.positions.len());
println!("vertex_color: {}", mesh.vertex_color.len());
println!("normals: {}", mesh.normals.len());
println!("texcoords: {}", mesh.texcoords.len());
println!("indices: {}", mesh.indices.len());
println!("face_arities: {}", mesh.face_arities.len());
println!("texcoord_indices: {}", mesh.texcoord_indices.len());
println!("normal_indices: {}", mesh.normal_indices.len());
*/
let mut next_face = 0;
for f in 0..mesh.face_arities.len() {
let end = next_face + mesh.face_arities[f] as usize;
@ -318,25 +332,25 @@ fn from_obj(path: &str) -> HittableList {
for v in 0..mesh.indices.len() / 3 {
let index_a = mesh.indices[3 * v] as usize;
let index_b = mesh.indices[3 * v + 2] as usize;
let index_c = mesh.indices[3 * v + 1] as usize;
let index_b = mesh.indices[3 * v + 1] as usize;
let index_c = mesh.indices[3 * v + 2] as usize;
let index_normal_a = mesh.normal_indices[3 * v] as usize;
let index_normal_b = mesh.normal_indices[3 * v + 1] as usize;
let index_normal_c = mesh.normal_indices[3 * v + 2] as usize;
let normal_avg = Vec3::unit_vector(
Vec3::new(
mesh.positions[3 * index_normal_a] as f64,
mesh.positions[3 * index_normal_a + 2] as f64,
mesh.positions[3 * index_normal_a + 1] as f64,
mesh.positions[3 * index_normal_a + new_x] as f64,
mesh.positions[3 * index_normal_a + new_y] as f64,
mesh.positions[3 * index_normal_a + new_z] as f64,
) + Vec3::new(
mesh.positions[3 * index_normal_b] as f64,
mesh.positions[3 * index_normal_b + 2] as f64,
mesh.positions[3 * index_normal_b + 1] as f64,
mesh.positions[3 * index_normal_b + new_x] as f64,
mesh.positions[3 * index_normal_b + new_y] as f64,
mesh.positions[3 * index_normal_b + new_z] as f64,
) + Vec3::new(
mesh.positions[3 * index_normal_c] as f64,
mesh.positions[3 * index_normal_c + 2] as f64,
mesh.positions[3 * index_normal_c + 1] as f64,
mesh.positions[3 * index_normal_c + new_x] as f64,
mesh.positions[3 * index_normal_c + new_y] as f64,
mesh.positions[3 * index_normal_c + new_z] as f64,
),
);
@ -355,19 +369,19 @@ fn from_obj(path: &str) -> HittableList {
world.add(Box::<Triangle>::new(Triangle::new(
Point3::new(
mesh.positions[3 * index_a] as f64,
mesh.positions[3 * index_a + 2] as f64,
mesh.positions[3 * index_a + 1] as f64,
mesh.positions[3 * index_a + new_x] as f64,
mesh.positions[3 * index_a + new_y] as f64,
mesh.positions[3 * index_a + new_z] as f64,
),
Point3::new(
mesh.positions[3 * index_b] as f64,
mesh.positions[3 * index_b + 2] as f64,
mesh.positions[3 * index_b + 1] as f64,
mesh.positions[3 * index_b + new_x] as f64,
mesh.positions[3 * index_b + new_y] as f64,
mesh.positions[3 * index_b + new_z] as f64,
),
Point3::new(
mesh.positions[3 * index_c] as f64,
mesh.positions[3 * index_c + 2] as f64,
mesh.positions[3 * index_c + 1] as f64,
mesh.positions[3 * index_c + new_x] as f64,
mesh.positions[3 * index_c + new_y] as f64,
mesh.positions[3 * index_c + new_z] as f64,
),
normal_avg,
material.clone(),