Added merge method in model
This commit is contained in:
parent
7a73be82ab
commit
69606e4d56
|
@ -4,6 +4,7 @@ pub mod face;
|
|||
pub mod material;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry;
|
||||
|
||||
use glium::VertexBuffer;
|
||||
|
||||
|
@ -121,6 +122,50 @@ impl Model {
|
|||
}
|
||||
}
|
||||
|
||||
/// Merges the model passed as parameter into the current model.
|
||||
pub fn merge(&mut self, other: Model) {
|
||||
let mut other = other;
|
||||
|
||||
let vertex_offset = self.vertices.len();
|
||||
let tex_coord_offset = self.texture_coordinates.len();
|
||||
let normal_offset = self.normals.len();
|
||||
let face_offset = self.faces.len();
|
||||
|
||||
self.vertices.append(&mut other.vertices);
|
||||
self.texture_coordinates.append(&mut other.texture_coordinates);
|
||||
self.normals.append(&mut other.normals);
|
||||
|
||||
for part in &mut other.parts {
|
||||
let mut new_part = Part::new(part.material_name.clone());
|
||||
for face in &mut part.faces {
|
||||
for fv in &mut [&mut face.a, &mut face.b, &mut face.c] {
|
||||
fv.vertex += vertex_offset;
|
||||
if let Some(tex_coord) = fv.texture_coordinate {
|
||||
fv.texture_coordinate = Some(tex_coord + tex_coord_offset);
|
||||
}
|
||||
if let Some(normal) = fv.normal {
|
||||
fv.normal = Some(normal + normal_offset);
|
||||
}
|
||||
}
|
||||
if let Some(id) = face.id {
|
||||
face.id = Some(id + face_offset);
|
||||
}
|
||||
new_part.faces.push(face.clone());
|
||||
self.faces.push(face.clone());
|
||||
}
|
||||
self.parts.push(new_part);
|
||||
}
|
||||
|
||||
for (name, material) in other.materials {
|
||||
let entry = self.materials.entry(name);
|
||||
if let Entry::Occupied(_) = entry {
|
||||
// Return error
|
||||
} else {
|
||||
entry.or_insert(material);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a model from a file.
|
||||
pub fn from(path: &str) -> Result<Model, ParserError> {
|
||||
parse(path)
|
||||
|
|
Loading…
Reference in New Issue