From cfd37dbcc63c3cc0365ea0c1cc4ce207e09655e5 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Tue, 5 Sep 2023 00:08:28 +0300 Subject: [PATCH] Modernize NiTriShapeData --- .../nifloader/testbulletnifloader.cpp | 6 ++--- components/nif/data.cpp | 22 ++++++------------- components/nif/data.hpp | 3 ++- components/nifbullet/bulletnifloader.cpp | 4 ++-- components/nifosg/nifloader.cpp | 2 +- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/apps/openmw_test_suite/nifloader/testbulletnifloader.cpp b/apps/openmw_test_suite/nifloader/testbulletnifloader.cpp index 3560c1cd61..022ba5dbbb 100644 --- a/apps/openmw_test_suite/nifloader/testbulletnifloader.cpp +++ b/apps/openmw_test_suite/nifloader/testbulletnifloader.cpp @@ -325,13 +325,13 @@ namespace mNiTriShapeData.recType = Nif::RC_NiTriShapeData; mNiTriShapeData.vertices = { osg::Vec3f(0, 0, 0), osg::Vec3f(1, 0, 0), osg::Vec3f(1, 1, 0) }; mNiTriShapeData.mNumTriangles = 1; - mNiTriShapeData.triangles = { 0, 1, 2 }; + mNiTriShapeData.mTriangles = { 0, 1, 2 }; mNiTriShape.data = Nif::NiGeometryDataPtr(&mNiTriShapeData); mNiTriShapeData2.recType = Nif::RC_NiTriShapeData; mNiTriShapeData2.vertices = { osg::Vec3f(0, 0, 1), osg::Vec3f(1, 0, 1), osg::Vec3f(1, 1, 1) }; mNiTriShapeData2.mNumTriangles = 1; - mNiTriShapeData2.triangles = { 0, 1, 2 }; + mNiTriShapeData2.mTriangles = { 0, 1, 2 }; mNiTriShape2.data = Nif::NiGeometryDataPtr(&mNiTriShapeData2); mNiTriStripsData.recType = Nif::RC_NiTriStripsData; @@ -978,7 +978,7 @@ namespace for_tri_shape_child_node_with_empty_data_triangles_should_return_shape_with_null_collision_shape) { auto data = static_cast(mNiTriShape.data.getPtr()); - data->triangles.clear(); + data->mTriangles.clear(); mNiTriShape.parents.push_back(&mNiNode); mNiNode.children = Nif::NodeList(std::vector({ Nif::NodePtr(&mNiTriShape) })); diff --git a/components/nif/data.cpp b/components/nif/data.cpp index ed17de9464..aed18619e1 100644 --- a/components/nif/data.cpp +++ b/components/nif/data.cpp @@ -93,25 +93,17 @@ namespace Nif { NiTriBasedGeomData::read(nif); - // We have three times as many vertices as triangles, so this - // is always equal to mNumTriangles * 3. - int cnt = nif->getInt(); + uint32_t numIndices; + nif->read(numIndices); bool hasTriangles = true; if (nif->getVersion() > NIFFile::NIFVersion::VER_OB_OLD) - hasTriangles = nif->getBoolean(); + nif->read(hasTriangles); if (hasTriangles) - nif->readVector(triangles, cnt); + nif->readVector(mTriangles, numIndices); - // Read the match list, which lists the vertices that are equal to - // vertices. We don't actually need need this for anything, so - // just skip it. - unsigned short verts = nif->getUShort(); - for (unsigned short i = 0; i < verts; i++) - { - // Number of vertices matching vertex 'i' - int num = nif->getUShort(); - nif->skip(num * sizeof(short)); - } + mMatchGroups.resize(nif->get()); + for (auto& group : mMatchGroups) + nif->readVector(group, nif->get()); } void NiTriStripsData::read(NIFStream* nif) diff --git a/components/nif/data.hpp b/components/nif/data.hpp index 0b62071a98..eaf8120e81 100644 --- a/components/nif/data.hpp +++ b/components/nif/data.hpp @@ -55,7 +55,8 @@ namespace Nif struct NiTriShapeData : public NiTriBasedGeomData { // Triangles, three vertex indices per triangle - std::vector triangles; + std::vector mTriangles; + std::vector> mMatchGroups; void read(NIFStream* nif) override; }; diff --git a/components/nifbullet/bulletnifloader.cpp b/components/nifbullet/bulletnifloader.cpp index 199a6f749d..51f8cae6c3 100644 --- a/components/nifbullet/bulletnifloader.cpp +++ b/components/nifbullet/bulletnifloader.cpp @@ -46,7 +46,7 @@ namespace void fillTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriShapeData& data) { prepareTriangleMesh(mesh, data); - const std::vector& triangles = data.triangles; + const std::vector& triangles = data.mTriangles; for (std::size_t i = 0; i < triangles.size(); i += 3) mesh.addTriangleIndices(triangles[i + 0], triangles[i + 1], triangles[i + 2]); } @@ -87,7 +87,7 @@ namespace return {}; auto data = static_cast(geometry.data.getPtr()); - if (data->triangles.empty()) + if (data->mTriangles.empty()) return {}; return function(static_cast(*data)); diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index d9e3dc1439..87d6d19ab0 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -1430,7 +1430,7 @@ namespace NifOsg if (niGeometryData->recType != Nif::RC_NiTriShapeData) return; auto data = static_cast(niGeometryData); - const std::vector& triangles = data->triangles; + const std::vector& triangles = data->mTriangles; if (triangles.empty()) return; geometry->addPrimitiveSet(