mirror of
https://github.com/IllusionMan1212/3dobs.git
synced 2026-10-24 10:12:35 +00:00
importer: clean up commented code
This commit is contained in:
+1
-1
@@ -229,7 +229,7 @@ pub fn load_stl(mut file: std::fs::File) -> Result<Object, Box<dyn std::error::E
|
||||
parse_binary_stl(file)?
|
||||
};
|
||||
let elapsed = now.elapsed();
|
||||
println!("STL file loaded in {} µs", elapsed.as_micros());
|
||||
println!("Loaded in {} ms", elapsed.as_millis());
|
||||
|
||||
Ok(obj)
|
||||
}
|
||||
|
||||
+20
-66
@@ -1,51 +1,7 @@
|
||||
use glad_gl::gl;
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::{shader::Shader, utils, importer::Material};
|
||||
|
||||
fn decompose_mat(matrix: &mut glm::Mat4) -> (glm::Vec3, glm::Vec3, glm::Vec3) {
|
||||
let pos = glm::vec3(matrix.c0.w, matrix.c1.w, matrix.c2.w);
|
||||
matrix.c3.x = 0.0;
|
||||
matrix.c3.y = 0.0;
|
||||
matrix.c3.z = 0.0;
|
||||
|
||||
let scale_x = glm::length(glm::vec3(matrix.c0.x, matrix.c1.x, matrix.c2.x));
|
||||
let scale_y = glm::length(glm::vec3(matrix.c0.y, matrix.c1.y, matrix.c2.y));
|
||||
let scale_z = glm::length(glm::vec3(matrix.c0.z, matrix.c1.z, matrix.c2.z));
|
||||
let scale = glm::vec3(scale_x, scale_y, scale_z);
|
||||
|
||||
let matrix = glm::mat3(
|
||||
matrix[0][0] / scale_x, matrix[0][1] / scale_y, matrix[0][2] / scale_z,
|
||||
matrix[1][0] / scale_x, matrix[1][1] / scale_y, matrix[1][2] / scale_z,
|
||||
matrix[2][0] / scale_x, matrix[2][1] / scale_y, matrix[2][2] / scale_z,
|
||||
);
|
||||
|
||||
let mut pitch = 0.0;
|
||||
let mut yaw = 0.0;
|
||||
let mut roll = 0.0;
|
||||
|
||||
// source: https://www.geometrictools.com/Documentation/EulerAngles.pdf
|
||||
if matrix[0][2] < 1.0 {
|
||||
if matrix[0][2] > -1.0 {
|
||||
pitch = (-matrix[1][2]).atan2(matrix[2][2]).to_degrees();
|
||||
yaw = matrix[0][2].asin().to_degrees();
|
||||
roll = (-matrix[0][1]).atan2(matrix[0][0]).to_degrees();
|
||||
} else {
|
||||
pitch = -(matrix[1][0].atan2(matrix[1][1])).to_degrees();
|
||||
yaw = -(std::f32::consts::FRAC_PI_2).to_degrees();
|
||||
roll = 0.0;
|
||||
}
|
||||
} else {
|
||||
pitch = matrix[1][0].atan2(matrix[1][1]).to_degrees();
|
||||
yaw = std::f32::consts::FRAC_PI_2.to_degrees();
|
||||
roll = 0.0;
|
||||
}
|
||||
|
||||
let rotation = glm::vec3(pitch, yaw, roll);
|
||||
|
||||
(pos, rotation, scale)
|
||||
}
|
||||
|
||||
fn create_rotation_matrix(pitch: f32, yaw: f32, roll: f32, pivot: glm::Vec3) -> glm::Mat4 {
|
||||
let pitch = pitch.to_radians();
|
||||
let yaw = yaw.to_radians();
|
||||
@@ -122,8 +78,6 @@ impl Mesh {
|
||||
gl::BindVertexArray(0);
|
||||
}
|
||||
|
||||
// let (position, rotation, scale) = decompose_mat(transformation);
|
||||
|
||||
Mesh {
|
||||
name: name.to_string(),
|
||||
vertices,
|
||||
@@ -225,26 +179,26 @@ impl Vertex {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Texture {
|
||||
pub id: u32,
|
||||
pub typ: russimp::material::TextureType,
|
||||
pub path: std::path::PathBuf,
|
||||
}
|
||||
// #[derive(Clone, Debug)]
|
||||
// pub struct Texture {
|
||||
// pub id: u32,
|
||||
// pub typ: russimp::material::TextureType,
|
||||
// pub path: std::path::PathBuf,
|
||||
// }
|
||||
|
||||
impl Texture {
|
||||
pub fn new(path: std::path::PathBuf, typ: russimp::material::TextureType) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let path_str = match path.to_str() {
|
||||
Some(path) => path,
|
||||
None => return Err("Failed to convert texture path to string".into()),
|
||||
};
|
||||
let id = utils::load_texture(path_str)?;
|
||||
// impl Texture {
|
||||
// pub fn new(path: std::path::PathBuf, typ: russimp::material::TextureType) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
// let path_str = match path.to_str() {
|
||||
// Some(path) => path,
|
||||
// None => return Err("Failed to convert texture path to string".into()),
|
||||
// };
|
||||
// let id = utils::load_texture(path_str)?;
|
||||
|
||||
Ok(Texture {
|
||||
id,
|
||||
typ,
|
||||
path,
|
||||
})
|
||||
}
|
||||
}
|
||||
// Ok(Texture {
|
||||
// id,
|
||||
// typ,
|
||||
// path,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
+9
-228
@@ -1,12 +1,4 @@
|
||||
use russimp;
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
use crate::{mesh::{Mesh, Vertex, Texture, apply_rotation}, shader::Shader, utils, ui::ui, log, aabb, importer};
|
||||
|
||||
const SUPPORTED_TEXTURE_TYPES: [russimp::material::TextureType; 2] = [
|
||||
russimp::material::TextureType::Diffuse,
|
||||
russimp::material::TextureType::Specular,
|
||||
];
|
||||
use crate::{mesh::{Mesh, apply_rotation}, shader::Shader, utils, ui::ui, aabb, importer};
|
||||
|
||||
const SCALING_FACTOR: f32 = 8.0;
|
||||
|
||||
@@ -19,180 +11,7 @@ pub struct Model {
|
||||
pub scaling_factor: f32,
|
||||
}
|
||||
|
||||
// fn process_node<'a>(
|
||||
// node: &russimp::node::Node,
|
||||
// scene: &'a russimp::scene::Scene,
|
||||
// meshes: &mut Vec<Mesh>,
|
||||
// dir: &std::path::PathBuf,
|
||||
// loaded_textures: &mut Vec<Texture>,
|
||||
// init_trans: &glm::Mat4,
|
||||
// aabb: &mut aabb::AABB,
|
||||
// scale: &mut f32,
|
||||
// ) -> Vec<Box<dyn std::error::Error>> {
|
||||
// let mut errors = vec![];
|
||||
|
||||
// let node_trans = glm::mat4(
|
||||
// node.transformation.a1, node.transformation.a2, node.transformation.a3, node.transformation.a4,
|
||||
// node.transformation.b1, node.transformation.b2, node.transformation.b3, node.transformation.b4,
|
||||
// node.transformation.c1, node.transformation.c2, node.transformation.c3, node.transformation.c4,
|
||||
// node.transformation.d1, node.transformation.d2, node.transformation.d3, node.transformation.d4,
|
||||
// );
|
||||
// let mut new_trans = *init_trans * node_trans;
|
||||
|
||||
// for i in 0..node.meshes.len() {
|
||||
// let mesh = &scene.meshes[node.meshes[i] as usize];
|
||||
// let (processed_mesh, mut errs) = process_mesh(mesh, scene, dir, loaded_textures, &mut new_trans, aabb);
|
||||
// errors.append(&mut errs);
|
||||
// meshes.push(processed_mesh);
|
||||
// }
|
||||
|
||||
// for child in node.children.borrow().clone().into_iter() {
|
||||
// let mut errs = process_node(&child, scene, meshes, dir, loaded_textures, &new_trans, aabb, scale);
|
||||
// errors.append(&mut errs);
|
||||
// }
|
||||
|
||||
// let scale_factor_x = SCALING_FACTOR / (aabb.max.x - aabb.min.x);
|
||||
// let scale_factor_y = SCALING_FACTOR / (aabb.max.y - aabb.min.y);
|
||||
// let scale_factor_z = SCALING_FACTOR / (aabb.max.z - aabb.min.z);
|
||||
|
||||
// // Use the minimum scaling factor to maintain proportions
|
||||
// *scale = scale_factor_x.min(scale_factor_y).min(scale_factor_z);
|
||||
|
||||
// return errors;
|
||||
// }
|
||||
|
||||
// fn process_mesh(
|
||||
// mesh: &russimp::mesh::Mesh,
|
||||
// scene: &russimp::scene::Scene,
|
||||
// dir: &std::path::PathBuf,
|
||||
// loaded_textures: &mut Vec<Texture>,
|
||||
// transformation: &mut glm::Mat4,
|
||||
// aabb: &mut aabb::AABB
|
||||
// ) -> (Mesh, Vec<Box<dyn std::error::Error>>) {
|
||||
// let mut vertices = vec![];
|
||||
// let mut indices = vec![];
|
||||
// let mut textures = vec![];
|
||||
|
||||
// for i in 0..mesh.vertices.len() {
|
||||
// let pos = glm::vec4(mesh.vertices[i].x, mesh.vertices[i].y, mesh.vertices[i].z, 1.0);
|
||||
|
||||
// let norm = match mesh.normals.len() {
|
||||
// 0 => glm::vec3(0.0, 0.0, 0.0),
|
||||
// _ => glm::vec3(mesh.normals[i].x, mesh.normals[i].y, mesh.normals[i].z),
|
||||
// };
|
||||
|
||||
// let t = &mesh.texture_coords[0];
|
||||
// let tex_coords = match t {
|
||||
// Some(tex) => {
|
||||
// glm::vec2(tex[i].x, tex[i].y)
|
||||
// },
|
||||
// None => glm::vec2(0.0, 0.0)
|
||||
// };
|
||||
|
||||
// let vertex = Vertex::new(pos.truncate(3), norm, tex_coords);
|
||||
|
||||
// if vertex.position.x < aabb.min.x {
|
||||
// aabb.min.x = vertex.position.x;
|
||||
// }
|
||||
|
||||
// if vertex.position.y < aabb.min.y {
|
||||
// aabb.min.y = vertex.position.y;
|
||||
// }
|
||||
|
||||
// if vertex.position.z < aabb.min.z {
|
||||
// aabb.min.z = vertex.position.z;
|
||||
// }
|
||||
|
||||
// if vertex.position.x > aabb.max.x {
|
||||
// aabb.max.x = vertex.position.x;
|
||||
// }
|
||||
|
||||
// if vertex.position.y > aabb.max.y {
|
||||
// aabb.max.y = vertex.position.y;
|
||||
// }
|
||||
|
||||
// if vertex.position.z > aabb.max.z {
|
||||
// aabb.max.z = vertex.position.z;
|
||||
// }
|
||||
|
||||
// vertices.push(vertex);
|
||||
// }
|
||||
|
||||
// for i in 0..mesh.faces.len() {
|
||||
// for j in 0..mesh.faces[i].0.len() {
|
||||
// indices.push(mesh.faces[i].0[j]);
|
||||
// }
|
||||
// }
|
||||
|
||||
// let mat = &scene.materials[mesh.material_index as usize];
|
||||
|
||||
// let material = process_material(mat);
|
||||
|
||||
// let (mut found_textures, errs) = load_material_textures(mat, dir, loaded_textures);
|
||||
// textures.append(&mut found_textures);
|
||||
|
||||
// let mesh = Mesh::new(mesh.name.as_str(), vertices, indices, textures, material, transformation);
|
||||
// return (mesh, errs);
|
||||
// }
|
||||
|
||||
// fn process_material(mat: &russimp::material::Material) -> Material {
|
||||
// let mat_name = String::from("Default_Mat");
|
||||
// let ambient = glm::vec3(0.2, 0.2, 0.2);
|
||||
// let diffuse = glm::vec3(0.7, 0.7, 0.7);
|
||||
// let specular = glm::vec3(0.1, 0.1, 0.1);
|
||||
// let shininess = 32.0;
|
||||
|
||||
// // TODO: better way of mapping properties
|
||||
// // for property in mat.properties.iter() {
|
||||
// // match property.key.as_str() {
|
||||
// // "$clr.ambient" => {
|
||||
// // ambient = match &property.data {
|
||||
// // russimp::material::PropertyTypeInfo::FloatArray(a) => {
|
||||
// // glm::vec3(a[0], a[1], a[2])
|
||||
// // },
|
||||
// // _ => panic!("Property should not be this type: {}", property.key)
|
||||
// // };
|
||||
// // },
|
||||
// // "$clr.diffuse" => {
|
||||
// // diffuse = match &property.data {
|
||||
// // russimp::material::PropertyTypeInfo::FloatArray(a) => {
|
||||
// // glm::vec3(a[0], a[1], a[2])
|
||||
// // },
|
||||
// // _ => panic!("Property should not be this type: {}", property.key)
|
||||
// // };
|
||||
// // },
|
||||
// // "$clr.specular" => {
|
||||
// // specular = match &property.data {
|
||||
// // russimp::material::PropertyTypeInfo::FloatArray(a) => {
|
||||
// // glm::vec3(a[0], a[1], a[2])
|
||||
// // },
|
||||
// // _ => panic!("Property should not be this type: {}", property.key)
|
||||
// // }
|
||||
// // }
|
||||
// // "?mat.name" => {
|
||||
// // mat_name = match &property.data {
|
||||
// // russimp::material::PropertyTypeInfo::String(s) => {
|
||||
// // s.to_string()
|
||||
// // },
|
||||
// // _ => panic!("Property should not be this type: {}", property.key)
|
||||
// // };
|
||||
// // }
|
||||
// // "$mat.shininess" => {
|
||||
// // shininess = match &property.data {
|
||||
// // russimp::material::PropertyTypeInfo::FloatArray(a) => {
|
||||
// // a[0]
|
||||
// // },
|
||||
// // _ => panic!("Property should not be this type: {}", property.key)
|
||||
// // };
|
||||
// // }
|
||||
// // _ => {},
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// Material::new(mat_name, ambient, diffuse, specular, shininess)
|
||||
// }
|
||||
|
||||
// // TODO: might nuke this cuz we don't need textures I think
|
||||
// TODO: texture support, kept for reference
|
||||
// fn load_material_textures(
|
||||
// mat: &russimp::material::Material,
|
||||
// dir: &std::path::PathBuf,
|
||||
@@ -243,45 +62,15 @@ pub struct Model {
|
||||
|
||||
impl Model {
|
||||
pub fn new(obj: importer::Object, state: &mut ui::State) -> Model {
|
||||
// let scene = russimp::scene::Scene::from_file(path,
|
||||
// vec![
|
||||
// russimp::scene::PostProcess::Triangulate,
|
||||
// russimp::scene::PostProcess::GenerateNormals,
|
||||
// russimp::scene::PostProcess::FlipUVs,
|
||||
// ])
|
||||
// .map_err(|e| {
|
||||
// let e = match e {
|
||||
// russimp::RussimpError::TextureNotFound => anyhow!("Texture not found"),
|
||||
// _ => anyhow!("{}", e)
|
||||
// };
|
||||
// return e;
|
||||
// })?;
|
||||
|
||||
// let root_node = match &scene.root {
|
||||
// Some(root) => root,
|
||||
// None => return Err("Scene has no root node".into()),
|
||||
// };
|
||||
|
||||
// if scene.flags & russimp::sys::AI_SCENE_FLAGS_INCOMPLETE == 1 {
|
||||
// return Err("Scene is incomplete")?;
|
||||
// }
|
||||
|
||||
// let directory = match std::path::Path::new(path).parent() {
|
||||
// Some(dir) => dir,
|
||||
// None => return Err("Model path has no parent directory".into()),
|
||||
// }.to_path_buf();
|
||||
|
||||
// let mut loaded_textures = vec![];
|
||||
// let mut meshes = vec![];
|
||||
// let init_trans_mat = utils::mat_ident();
|
||||
let mut scale: f32 = 1.0;
|
||||
// let errors = process_node(&root_node, &scene, &mut meshes, &directory, &mut loaded_textures, &init_trans_mat, &mut aabb, &mut scale);
|
||||
|
||||
// for err in errors {
|
||||
// state.logger.history.push(log::LogMessage::new(log::LogLevel::Warning, &err.to_string()));
|
||||
// }
|
||||
let mut meshes = Vec::new();
|
||||
|
||||
let scale_factor_x = SCALING_FACTOR / (obj.aabb.max.x - obj.aabb.min.x);
|
||||
let scale_factor_y = SCALING_FACTOR / (obj.aabb.max.y - obj.aabb.min.y);
|
||||
let scale_factor_z = SCALING_FACTOR / (obj.aabb.max.z - obj.aabb.min.z);
|
||||
|
||||
// Use the minimum scaling factor to maintain proportions
|
||||
let scale = scale_factor_x.min(scale_factor_y).min(scale_factor_z);
|
||||
|
||||
for mesh in obj.meshes.into_iter() {
|
||||
meshes.push(Mesh::new(&mesh.name, mesh.vertices, mesh.indices, mesh.material));
|
||||
}
|
||||
@@ -293,14 +82,6 @@ impl Model {
|
||||
scaling_factor: scale,
|
||||
meshes,
|
||||
}
|
||||
// Ok(Model {
|
||||
// id: state.get_next_id(),
|
||||
// name: root_node.name.to_owned(),
|
||||
// aabb_mesh: aabb::AABBMesh::new(&aabb),
|
||||
// aabb,
|
||||
// scaling_factor: scale,
|
||||
// meshes,
|
||||
// })
|
||||
}
|
||||
|
||||
pub fn draw(&self, shader: &Shader, draw_aabb: bool) {
|
||||
|
||||
Reference in New Issue
Block a user