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);
// 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();

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

@ -57,22 +57,11 @@ namespace
void fillTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriStripsData& data, const osg::Matrixf& transform)
{
const std::vector<osg::Vec3f>& vertices = data.vertices;
const std::vector<std::vector<unsigned short>>& strips = data.strips;
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));
mesh.preallocateVertices(static_cast<int>(data.vertices.size()));
mesh.preallocateIndices(static_cast<int>(data.mNumTriangles));
// It's triangulation time. Totally not a NifSkope spell ripoff.
for (const std::vector<unsigned short>& strip : strips)
for (const std::vector<unsigned short>& 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 (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(Misc::Convert::toBullet(vertices[a] * transform),
Misc::Convert::toBullet(vertices[b] * transform),
Misc::Convert::toBullet(vertices[c] * transform));
}
mesh.addTriangle(vertexA, vertexB, vertexC);
else
{
mesh.addTriangle(Misc::Convert::toBullet(vertices[a] * transform),
Misc::Convert::toBullet(vertices[c] * transform),
Misc::Convert::toBullet(vertices[b] * transform));
}
}
mesh.addTriangle(vertexA, vertexC, vertexB);
}
}
}

Loading…
Cancel
Save