Skip to content

Commit

Permalink
fixed crashes, infinite loops, and minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Aug 27, 2023
1 parent fbf8e72 commit ccceff4
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/ofbx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,6 @@ struct GeometryData {
std::vector<Vec3> tangents;
std::vector<int> materials;
std::vector<int> indices;
std::vector<int> to_old_vertices;
std::vector<NewVertex> to_new_vertices;
};

Expand Down Expand Up @@ -2091,7 +2090,7 @@ struct AnimationLayerImpl : AnimationLayer
{
for (const AnimationCurveNodeImpl* node : curve_nodes)
{
if (node->bone_link_property == prop && node->bone == &bone) return node;
if (node->bone_link_property.begin && node->bone_link_property == prop && node->bone == &bone) return node;
}
return nullptr;
}
Expand Down Expand Up @@ -2259,29 +2258,30 @@ static void buildGeometryVertexData(
bool triangulationEnabled,
Allocator::MTAllocator& allocator)
{
std::vector<int> to_old_vertices;

if (triangulationEnabled) {
triangulate(original_indices, &geom->to_old_vertices, &to_old_indices);
geom->vertices.resize(geom->to_old_vertices.size());
triangulate(original_indices, &to_old_vertices, &to_old_indices);
geom->vertices.resize(to_old_vertices.size());
geom->indices.resize(geom->vertices.size());
for (int i = 0, c = (int)geom->to_old_vertices.size(); i < c; ++i)
for (int i = 0, c = (int)to_old_vertices.size(); i < c; ++i)
{
geom->vertices[i] = vertices[geom->to_old_vertices[i]];
geom->vertices[i] = vertices[to_old_vertices[i]];
geom->indices[i] = codeIndex(i, i % 3 == 2);
}
} else {
geom->vertices = vertices;
geom->to_old_vertices.resize(original_indices.size());
to_old_vertices.resize(original_indices.size());
for (size_t i = 0; i < original_indices.size(); ++i) {
geom->to_old_vertices[i] = decodeIndex(original_indices[i]);
to_old_vertices[i] = decodeIndex(original_indices[i]);
}
geom->indices = original_indices;
to_old_indices.resize(original_indices.size());
iota(to_old_indices.begin(), to_old_indices.end(), 0);
}

geom->to_new_vertices.resize(vertices.size()); // some vertices can be unused, so this isn't necessarily the same size as to_old_vertices.
const int* to_old_vertices = geom->to_old_vertices.empty() ? nullptr : &geom->to_old_vertices[0];
for (int i = 0, c = (int)geom->to_old_vertices.size(); i < c; ++i)
for (int i = 0, c = (int)to_old_vertices.size(); i < c; ++i)
{
int old = to_old_vertices[i];
add(geom->to_new_vertices[old], i, allocator);
Expand Down Expand Up @@ -3319,7 +3319,7 @@ bool ShapeImpl::postprocess(GeometryImpl* geom, Allocator& allocator)
allocator.vec3_tmp2.clear(); // old normals
allocator.int_tmp.clear(); // old indices
if (!parseDoubleVecData(*vertices_element->first_property, &allocator.vec3_tmp, &allocator.tmp)) return true;
if (!parseDoubleVecData(*normals_element->first_property, &allocator.vec3_tmp2, &allocator.tmp)) return true;
if (normals_element && !parseDoubleVecData(*normals_element->first_property, &allocator.vec3_tmp2, &allocator.tmp)) return true;
if (!parseBinaryArray(*indexes_element->first_property, &allocator.int_tmp)) return true;

if (allocator.vec3_tmp.size() != allocator.int_tmp.size() || allocator.vec3_tmp2.size() != allocator.int_tmp.size()) return false;
Expand All @@ -3328,7 +3328,7 @@ bool ShapeImpl::postprocess(GeometryImpl* geom, Allocator& allocator)
normals = geom->normals;

Vec3* vr = &allocator.vec3_tmp[0];
Vec3* nr = &allocator.vec3_tmp2[0];
Vec3* nr = normals_element ? &allocator.vec3_tmp2[0] : nullptr;
int* ir = &allocator.int_tmp[0];
for (int i = 0, c = (int)allocator.int_tmp.size(); i < c; ++i)
{
Expand All @@ -3338,7 +3338,7 @@ bool ShapeImpl::postprocess(GeometryImpl* geom, Allocator& allocator)
while (n)
{
vertices[n->index] = vertices[n->index] + vr[i];
normals[n->index] = normals[n->index] + nr[i];
if (normals_element) normals[n->index] = normals[n->index] + nr[i];
n = n->next;
}
}
Expand Down Expand Up @@ -4189,8 +4189,7 @@ Object* Object::getParent() const
if (connection.from_object == id)
{
Object* obj = scene.m_object_map.find(connection.to_object)->second.object;
if (obj && obj->is_node && obj != this)
{
if (obj && obj->is_node && obj != this && connection.type == Scene::Connection::OBJECT_OBJECT) {
assert(parent == nullptr);
parent = obj;
}
Expand Down

0 comments on commit ccceff4

Please sign in to comment.