Clean up fillTriangleMesh for NiTriStrips

7220-lua-add-a-general-purpose-lexical-parser
Alexei Kotov 2 years ago
parent a41cbfb349
commit c8430ee6c8

@ -149,8 +149,8 @@ namespace Nif
{ {
NiGeometryData::read(nif); NiGeometryData::read(nif);
// Every strip with n points defines n-2 triangles, so this should be unnecessary. mNumTriangles = nif->getUShort();
/*int tris =*/nif->getUShort();
// Number of triangle strips // Number of triangle strips
int numStrips = nif->getUShort(); int numStrips = nif->getUShort();

@ -53,6 +53,8 @@ namespace Nif
struct NiTriStripsData : public NiGeometryData struct NiTriStripsData : public NiGeometryData
{ {
size_t mNumTriangles;
// Triangle strips, series of vertex indices. // Triangle strips, series of vertex indices.
std::vector<std::vector<unsigned short>> strips; std::vector<std::vector<unsigned short>> strips;

@ -57,22 +57,11 @@ namespace
void fillTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriStripsData& data, const osg::Matrixf& transform) void fillTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriStripsData& data, const osg::Matrixf& transform)
{ {
const std::vector<osg::Vec3f>& vertices = data.vertices; mesh.preallocateVertices(static_cast<int>(data.vertices.size()));
const std::vector<std::vector<unsigned short>>& strips = data.strips; mesh.preallocateIndices(static_cast<int>(data.mNumTriangles));
mesh.preallocateVertices(static_cast<int>(vertices.size()));
int numTriangles = 0;
for (const std::vector<unsigned short>& strip : strips)
{
// Each strip with N points contains information about N-2 triangles.
if (strip.size() >= 3)
numTriangles += static_cast<int>(strip.size() - 2);
}
mesh.preallocateIndices(static_cast<int>(numTriangles));
// It's triangulation time. Totally not a NifSkope spell ripoff. for (const std::vector<unsigned short>& strip : data.strips)
for (const std::vector<unsigned short>& strip : strips)
{ {
// Can't make a triangle from less than 3 points.
if (strip.size() < 3) if (strip.size() < 3)
continue; continue;
@ -84,21 +73,15 @@ namespace
a = b; a = b;
b = c; b = c;
c = strip[i]; c = strip[i];
if (a != b && b != c && a != c) if (a == b || b == c || a == c)
{ continue;
if (i % 2 == 0) const btVector3 vertexA = Misc::Convert::toBullet(data.vertices[a] * transform);
{ const btVector3 vertexB = Misc::Convert::toBullet(data.vertices[b] * transform);
mesh.addTriangle(Misc::Convert::toBullet(vertices[a] * transform), const btVector3 vertexC = Misc::Convert::toBullet(data.vertices[c] * transform);
Misc::Convert::toBullet(vertices[b] * transform), if (i % 2 == 0)
Misc::Convert::toBullet(vertices[c] * transform)); mesh.addTriangle(vertexA, vertexB, vertexC);
} else
else mesh.addTriangle(vertexA, vertexC, vertexB);
{
mesh.addTriangle(Misc::Convert::toBullet(vertices[a] * transform),
Misc::Convert::toBullet(vertices[c] * transform),
Misc::Convert::toBullet(vertices[b] * transform));
}
}
} }
} }
} }

Loading…
Cancel
Save