Skip to content

Commit

Permalink
Correct OBJ export for multiple model groups, show warning when the v…
Browse files Browse the repository at this point in the history
…ertex output exceeds the limit
  • Loading branch information
coderbot16 committed Nov 18, 2018
1 parent a5818e6 commit ea21d7d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
21 changes: 13 additions & 8 deletions src/collada_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,8 @@ impl<'n> fmt::Display for Geometry<'n> {
}
}

pub fn convert(cem: Scene<V2>) -> String {
let mut string = String::new();
let model = cem.model;
let name = "scene_root";
fn write_meshes(name: &str, model: &V2, string: &mut String) {
let triangle_data = &model.lod_levels[0];

string.push_str(HEADER);

let mut polygons = vec![0; model.lod_levels[0].len() * 3];

for &v2::Material { ref name, texture, ref triangles, vertex_offset, vertex_count: _vertex_count, ref texture_name } in &model.materials {
Expand Down Expand Up @@ -143,11 +137,22 @@ pub fn convert(cem: Scene<V2>) -> String {

writeln!(string, "{}", geometry).unwrap();
}
}

pub fn convert(cem: Scene<V2>) -> String {
let mut string = String::new();

string.push_str(HEADER);

write_meshes("scene_root", &cem.model, &mut string);

string.push_str(" </library_geometries>\n");
string.push_str(" <library_controllers>\n");

if model.frames.len() > 1 {
let name = "scene_root"; // TODO
let model = &cem.model;

if cem.model.frames.len() > 1 {
writeln!(string, " <controller id=\"{0}-morph\" name=\"{0}-morph\">", name);
writeln!(string, " <morph source=\"#{}-mesh\" method=\"NORMALIZED\">", format!("{}_frame{}", name, 0));

Expand Down
39 changes: 24 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn convert<I, O>(mut i: I, mut o: O, input_format: Format, format: Format) -> io
|parse| io::Error::new(io::ErrorKind::InvalidData, format!("Error in OBJ file on line {}: {}", parse.line_number, parse.message))
)?;

let model = obj_to_cem(&obj.objects[0]);
let model = obj_to_cem(&obj.objects);

Scene::root(model).write(&mut o)
},
Expand Down Expand Up @@ -184,7 +184,7 @@ fn convert<I, O>(mut i: I, mut o: O, input_format: Format, format: Format) -> io
}
}

fn obj_to_cem(i: &Object) -> V2 {
fn obj_to_cem(i: &[Object]) -> V2 {
let mut triangles = Vec::new();
let mut vertices = Vec::new();

Expand All @@ -193,8 +193,8 @@ fn obj_to_cem(i: &Object) -> V2 {
{
let mut vertex_associations = HashMap::new();

let mut resolve_index = |v: VTNIndex| {
*vertex_associations.entry(v).or_insert_with(|| {
let mut resolve_index = |i: &Object, idx: usize, v: VTNIndex| {
*vertex_associations.entry((idx, v)).or_insert_with(|| {
let index = vertices.len();

let position = i.vertices[v.0];
Expand All @@ -217,22 +217,31 @@ fn obj_to_cem(i: &Object) -> V2 {
})
};

for geometry in &i.geometry {
for primitive in geometry.shapes.iter().map(|shape| shape.primitive) {
match primitive {
Primitive::Triangle(v0, v1, v2) => {
triangles.push((
resolve_index(v0) as u32,
resolve_index(v1) as u32,
resolve_index(v2) as u32
));
},
_ => () // Skip lines and points, not supported.
for (idx, i) in i.iter().enumerate() {
for geometry in &i.geometry {
for primitive in geometry.shapes.iter().map(|shape| shape.primitive) {
match primitive {
Primitive::Triangle(v0, v1, v2) => {
triangles.push((
resolve_index(i, idx, v0) as u32,
resolve_index(i, idx, v1) as u32,
resolve_index(i, idx, v2) as u32
));
},
_ => () // Skip lines and points, not supported.
}
}
}
}
}

// It appears that there is some limit on the vertex count. This needs to be investigated further, but it appears that
// adding more than 2442 instantly crashes the game on model load.
if vertices.len() > 2442 {
eprintln!("warning: Vertex count exceeds 2442, this will most likely crash the game. You have been warned.");
eprintln!("{} vertices, {} triangles", vertices.len(), triangles.len());
}

// Create the model

let mut center_builder = ::cem::collider::CenterBuilder::begin();
Expand Down

0 comments on commit ea21d7d

Please sign in to comment.