diff --git a/components/nif/data.cpp b/components/nif/data.cpp index 7a6ea7b540..02e78b599a 100644 --- a/components/nif/data.cpp +++ b/components/nif/data.cpp @@ -149,8 +149,8 @@ namespace Nif { NiGeometryData::read(nif); - // Every strip with n points defines n-2 triangles, so this should be unnecessary. - /*int tris =*/nif->getUShort(); + mNumTriangles = nif->getUShort(); + // Number of triangle strips int numStrips = nif->getUShort(); diff --git a/components/nif/data.hpp b/components/nif/data.hpp index 2d55e6cbef..563007f73f 100644 --- a/components/nif/data.hpp +++ b/components/nif/data.hpp @@ -53,6 +53,8 @@ namespace Nif struct NiTriStripsData : public NiGeometryData { + size_t mNumTriangles; + // Triangle strips, series of vertex indices. std::vector> strips; diff --git a/components/nifbullet/bulletnifloader.cpp b/components/nifbullet/bulletnifloader.cpp index f276df0863..aa5922e79c 100644 --- a/components/nifbullet/bulletnifloader.cpp +++ b/components/nifbullet/bulletnifloader.cpp @@ -57,22 +57,11 @@ namespace void fillTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriStripsData& data, const osg::Matrixf& transform) { - const std::vector& vertices = data.vertices; - const std::vector>& strips = data.strips; - mesh.preallocateVertices(static_cast(vertices.size())); - int numTriangles = 0; - for (const std::vector& strip : strips) - { - // Each strip with N points contains information about N-2 triangles. - if (strip.size() >= 3) - numTriangles += static_cast(strip.size() - 2); - } - mesh.preallocateIndices(static_cast(numTriangles)); + mesh.preallocateVertices(static_cast(data.vertices.size())); + mesh.preallocateIndices(static_cast(data.mNumTriangles)); - // It's triangulation time. Totally not a NifSkope spell ripoff. - for (const std::vector& strip : strips) + for (const std::vector& strip : data.strips) { - // Can't make a triangle from less than 3 points. if (strip.size() < 3) continue; @@ -84,21 +73,15 @@ namespace a = b; b = c; c = strip[i]; - if (a != b && b != c && a != c) - { - if (i % 2 == 0) - { - mesh.addTriangle(Misc::Convert::toBullet(vertices[a] * transform), - Misc::Convert::toBullet(vertices[b] * transform), - Misc::Convert::toBullet(vertices[c] * transform)); - } - else - { - mesh.addTriangle(Misc::Convert::toBullet(vertices[a] * transform), - Misc::Convert::toBullet(vertices[c] * transform), - Misc::Convert::toBullet(vertices[b] * transform)); - } - } + if (a == b || b == c || a == c) + continue; + const btVector3 vertexA = Misc::Convert::toBullet(data.vertices[a] * transform); + const btVector3 vertexB = Misc::Convert::toBullet(data.vertices[b] * transform); + const btVector3 vertexC = Misc::Convert::toBullet(data.vertices[c] * transform); + if (i % 2 == 0) + mesh.addTriangle(vertexA, vertexB, vertexC); + else + mesh.addTriangle(vertexA, vertexC, vertexB); } } }