1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00

Modernize NiGeometryData

This commit is contained in:
Alexei Kotov 2023-09-05 00:56:20 +03:00
parent 50a5d53b41
commit 89774716fb
5 changed files with 84 additions and 62 deletions

View File

@ -323,19 +323,19 @@ namespace
init(mController); init(mController);
mNiTriShapeData.recType = Nif::RC_NiTriShapeData; mNiTriShapeData.recType = Nif::RC_NiTriShapeData;
mNiTriShapeData.vertices = { osg::Vec3f(0, 0, 0), osg::Vec3f(1, 0, 0), osg::Vec3f(1, 1, 0) }; mNiTriShapeData.mVertices = { osg::Vec3f(0, 0, 0), osg::Vec3f(1, 0, 0), osg::Vec3f(1, 1, 0) };
mNiTriShapeData.mNumTriangles = 1; mNiTriShapeData.mNumTriangles = 1;
mNiTriShapeData.mTriangles = { 0, 1, 2 }; mNiTriShapeData.mTriangles = { 0, 1, 2 };
mNiTriShape.data = Nif::NiGeometryDataPtr(&mNiTriShapeData); mNiTriShape.data = Nif::NiGeometryDataPtr(&mNiTriShapeData);
mNiTriShapeData2.recType = Nif::RC_NiTriShapeData; mNiTriShapeData2.recType = Nif::RC_NiTriShapeData;
mNiTriShapeData2.vertices = { osg::Vec3f(0, 0, 1), osg::Vec3f(1, 0, 1), osg::Vec3f(1, 1, 1) }; mNiTriShapeData2.mVertices = { osg::Vec3f(0, 0, 1), osg::Vec3f(1, 0, 1), osg::Vec3f(1, 1, 1) };
mNiTriShapeData2.mNumTriangles = 1; mNiTriShapeData2.mNumTriangles = 1;
mNiTriShapeData2.mTriangles = { 0, 1, 2 }; mNiTriShapeData2.mTriangles = { 0, 1, 2 };
mNiTriShape2.data = Nif::NiGeometryDataPtr(&mNiTriShapeData2); mNiTriShape2.data = Nif::NiGeometryDataPtr(&mNiTriShapeData2);
mNiTriStripsData.recType = Nif::RC_NiTriStripsData; mNiTriStripsData.recType = Nif::RC_NiTriStripsData;
mNiTriStripsData.vertices mNiTriStripsData.mVertices
= { osg::Vec3f(0, 0, 0), osg::Vec3f(1, 0, 0), osg::Vec3f(1, 1, 0), osg::Vec3f(0, 1, 0) }; = { osg::Vec3f(0, 0, 0), osg::Vec3f(1, 0, 0), osg::Vec3f(1, 1, 0), osg::Vec3f(0, 1, 0) };
mNiTriStripsData.mNumTriangles = 2; mNiTriStripsData.mNumTriangles = 2;
mNiTriStripsData.mStrips = { { 0, 1, 2, 3 } }; mNiTriStripsData.mStrips = { { 0, 1, 2, 3 } };

View File

@ -11,72 +11,79 @@ namespace Nif
void NiGeometryData::read(NIFStream* nif) void NiGeometryData::read(NIFStream* nif)
{ {
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 114)) if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 114))
nif->getInt(); // Group ID. (Almost?) always 0. nif->read(mGroupId);
int verts = nif->getUShort(); // Note: has special meaning for NiPSysData
nif->read(mNumVertices);
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0)) if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0))
nif->skip(2); // Keep flags and compress flags {
nif->read(mKeepFlags);
nif->read(mCompressFlags);
}
if (nif->getBoolean()) bool hasVertices;
nif->readVector(vertices, verts); nif->read(hasVertices);
if (hasVertices)
nif->readVector(mVertices, mNumVertices);
unsigned int dataFlags = 0;
if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0)) if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0))
dataFlags = nif->getUShort(); nif->read(mDataFlags);
if (nif->getVersion() == NIFFile::NIFVersion::VER_BGS if (nif->getVersion() == NIFFile::NIFVersion::VER_BGS
&& nif->getBethVersion() > NIFFile::BethVersion::BETHVER_FO3) && nif->getBethVersion() > NIFFile::BethVersion::BETHVER_FO3)
nif->getUInt(); // Material CRC nif->read(mMaterialHash);
if (nif->getBoolean()) bool hasNormals;
nif->read(hasNormals);
if (hasNormals)
{ {
nif->readVector(normals, verts); nif->readVector(mNormals, mNumVertices);
if (dataFlags & 0x1000) if (mDataFlags & DataFlag_HasTangents)
{ {
nif->readVector(tangents, verts); nif->readVector(mTangents, mNumVertices);
nif->readVector(bitangents, verts); nif->readVector(mBitangents, mNumVertices);
} }
} }
center = nif->getVector3(); nif->read(mCenter);
radius = nif->getFloat(); nif->read(mRadius);
if (nif->getBoolean()) bool hasColors;
nif->readVector(colors, verts); nif->read(hasColors);
if (hasColors)
nif->readVector(mColors, mNumVertices);
unsigned int numUVs = dataFlags;
if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0)) if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0))
numUVs = nif->getUShort(); nif->read(mDataFlags);
// In Morrowind this field only corresponds to the number of UV sets. // In 4.0.0.2 the flags field corresponds to the number of UV sets.
// In later games only the first 6 bits are used as a count and the rest are flags. // In later revisions the part that corresponds to the number is narrower.
uint16_t numUVs = mDataFlags;
if (nif->getVersion() > NIFFile::NIFVersion::VER_MW) if (nif->getVersion() > NIFFile::NIFVersion::VER_MW)
{ {
numUVs &= 0x3f; numUVs &= DataFlag_NumUVsMask;
if (nif->getVersion() == NIFFile::NIFVersion::VER_BGS && nif->getBethVersion() > 0) if (nif->getVersion() == NIFFile::NIFVersion::VER_BGS && nif->getBethVersion() > 0)
numUVs &= 0x1; numUVs &= DataFlag_HasUV;
} }
bool hasUVs = true; bool hasUVs = true;
if (nif->getVersion() <= NIFFile::NIFVersion::VER_MW) if (nif->getVersion() <= NIFFile::NIFVersion::VER_MW)
hasUVs = nif->getBoolean(); nif->read(hasUVs);
if (hasUVs) if (hasUVs)
{ {
uvlist.resize(numUVs); mUVList.resize(numUVs);
for (unsigned int i = 0; i < numUVs; i++) for (std::vector<osg::Vec2f>& list : mUVList)
{ {
nif->readVector(uvlist[i], verts); nif->readVector(list, mNumVertices);
// flip the texture coordinates to convert them to the OpenGL convention of bottom-left image origin // flip the texture coordinates to convert them to the OpenGL convention of bottom-left image origin
for (unsigned int uv = 0; uv < uvlist[i].size(); ++uv) for (osg::Vec2f& uv : list)
{ uv.y() = 1.f - uv.y();
uvlist[i][uv] = osg::Vec2f(uvlist[i][uv].x(), 1.f - uvlist[i][uv].y());
}
} }
} }
if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0)) if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0))
nif->getUShort(); // Consistency flags nif->read(mConsistencyType);
if (nif->getVersion() >= NIFStream::generateVersion(20, 0, 0, 4)) if (nif->getVersion() >= NIFStream::generateVersion(20, 0, 0, 4))
nif->skip(4); // Additional data nif->skip(4); // Additional data
@ -130,14 +137,13 @@ namespace Nif
{ {
NiGeometryData::read(nif); NiGeometryData::read(nif);
size_t num = vertices.size();
std::vector<uint8_t> flags; std::vector<uint8_t> flags;
nif->readVector(flags, num); nif->readVector(flags, mNumVertices);
// Can't construct a line from a single vertex. // Can't construct a line from a single vertex.
if (num < 2) if (mNumVertices < 2)
return; return;
// Convert connectivity flags into usable geometry. The last element needs special handling. // Convert connectivity flags into usable geometry. The last element needs special handling.
for (size_t i = 0; i < num - 1; ++i) for (uint16_t i = 0; i < mNumVertices - 1; ++i)
{ {
if (flags[i] & 1) if (flags[i] & 1)
{ {
@ -146,9 +152,9 @@ namespace Nif
} }
} }
// If there are just two vertices, they can be connected twice. Probably isn't critical. // If there are just two vertices, they can be connected twice. Probably isn't critical.
if (flags[num - 1] & 1) if (flags[mNumVertices - 1] & 1)
{ {
mLines.emplace_back(num - 1); mLines.emplace_back(mNumVertices - 1);
mLines.emplace_back(0); mLines.emplace_back(0);
} }
} }
@ -164,21 +170,21 @@ namespace Nif
if (nif->getVersion() <= NIFStream::generateVersion(10, 0, 1, 0)) if (nif->getVersion() <= NIFStream::generateVersion(10, 0, 1, 0))
std::fill(particleRadii.begin(), particleRadii.end(), nif->getFloat()); std::fill(particleRadii.begin(), particleRadii.end(), nif->getFloat());
else if (nif->getBoolean()) else if (nif->getBoolean())
nif->readVector(particleRadii, vertices.size()); nif->readVector(particleRadii, mNumVertices);
activeCount = nif->getUShort(); activeCount = nif->getUShort();
// Particle sizes // Particle sizes
if (nif->getBoolean()) if (nif->getBoolean())
nif->readVector(sizes, vertices.size()); nif->readVector(sizes, mNumVertices);
if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0) && nif->getBoolean()) if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0) && nif->getBoolean())
nif->readVector(rotations, vertices.size()); nif->readVector(rotations, mNumVertices);
if (nif->getVersion() >= NIFStream::generateVersion(20, 0, 0, 4)) if (nif->getVersion() >= NIFStream::generateVersion(20, 0, 0, 4))
{ {
if (nif->getBoolean()) if (nif->getBoolean())
nif->readVector(rotationAngles, vertices.size()); nif->readVector(rotationAngles, mNumVertices);
if (nif->getBoolean()) if (nif->getBoolean())
nif->readVector(rotationAxes, vertices.size()); nif->readVector(rotationAxes, mNumVertices);
} }
} }
@ -192,7 +198,7 @@ namespace Nif
bool hasRotations; bool hasRotations;
nif->read(hasRotations); nif->read(hasRotations);
if (hasRotations) if (hasRotations)
nif->readVector(rotations, vertices.size()); nif->readVector(rotations, mNumVertices);
} }
void NiPosData::read(NIFStream* nif) void NiPosData::read(NIFStream* nif)

View File

@ -35,11 +35,27 @@ namespace Nif
// Common ancestor for several data classes // Common ancestor for several data classes
struct NiGeometryData : public Record struct NiGeometryData : public Record
{ {
std::vector<osg::Vec3f> vertices, normals, tangents, bitangents; // Interpretation of Flags field differs depending on the version
std::vector<osg::Vec4f> colors; enum DataFlags
std::vector<std::vector<osg::Vec2f>> uvlist; {
osg::Vec3f center; DataFlag_HasUV = 0x0001,
float radius; DataFlag_NumUVsMask = 0x003F,
DataFlag_HasTangents = 0x1000,
};
int32_t mGroupId{ 0 };
uint16_t mNumVertices;
uint8_t mKeepFlags{ 0 };
uint8_t mCompressFlags{ 0 };
std::vector<osg::Vec3f> mVertices;
uint16_t mDataFlags{ 0 };
uint32_t mMaterialHash;
std::vector<osg::Vec3f> mNormals, mTangents, mBitangents;
osg::Vec3f mCenter;
float mRadius;
std::vector<osg::Vec4f> mColors;
std::vector<std::vector<osg::Vec2f>> mUVList;
uint16_t mConsistencyType;
void read(NIFStream* nif) override; void read(NIFStream* nif) override;
}; };

View File

@ -35,7 +35,7 @@ namespace
void prepareTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriBasedGeomData& data) void prepareTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriBasedGeomData& data)
{ {
// FIXME: copying vertices/indices individually is unreasonable // FIXME: copying vertices/indices individually is unreasonable
const std::vector<osg::Vec3f>& vertices = data.vertices; const std::vector<osg::Vec3f>& vertices = data.mVertices;
mesh.preallocateVertices(static_cast<int>(vertices.size())); mesh.preallocateVertices(static_cast<int>(vertices.size()));
for (const osg::Vec3f& vertex : vertices) for (const osg::Vec3f& vertex : vertices)
mesh.findOrAddVertex(Misc::Convert::toBullet(vertex), false); mesh.findOrAddVertex(Misc::Convert::toBullet(vertex), false);
@ -381,7 +381,7 @@ namespace NifBullet
if (args.mHasMarkers && Misc::StringUtils::ciStartsWith(niGeometry.name, "EditorMarker")) if (args.mHasMarkers && Misc::StringUtils::ciStartsWith(niGeometry.name, "EditorMarker"))
return; return;
if (niGeometry.data.empty() || niGeometry.data->vertices.empty()) if (niGeometry.data.empty() || niGeometry.data->mVertices.empty())
return; return;
if (!niGeometry.skin.empty()) if (!niGeometry.skin.empty())

View File

@ -1146,7 +1146,7 @@ namespace NifOsg
if (particle.lifespan <= 0) if (particle.lifespan <= 0)
continue; continue;
if (particle.vertex >= particledata->vertices.size()) if (particle.vertex >= particledata->mVertices.size())
continue; continue;
ParticleAgeSetter particletemplate(std::max(0.f, particle.lifetime)); ParticleAgeSetter particletemplate(std::max(0.f, particle.lifetime));
@ -1158,7 +1158,7 @@ namespace NifOsg
// which can not be done in this loader since we are not attached to the scene yet. Will be fixed up // which can not be done in this loader since we are not attached to the scene yet. Will be fixed up
// post-load in the SceneManager. // post-load in the SceneManager.
created->setVelocity(particle.velocity); created->setVelocity(particle.velocity);
const osg::Vec3f& position = particledata->vertices[particle.vertex]; const osg::Vec3f& position = particledata->mVertices[particle.vertex];
created->setPosition(position); created->setPosition(position);
created->setColorRange(osgParticle::rangev4(partctrl->color, partctrl->color)); created->setColorRange(osgParticle::rangev4(partctrl->color, partctrl->color));
@ -1173,7 +1173,7 @@ namespace NifOsg
} }
// radius may be used to force a larger bounding box // radius may be used to force a larger bounding box
box.expandBy(osg::BoundingSphere(osg::Vec3(0, 0, 0), particledata->radius)); box.expandBy(osg::BoundingSphere(osg::Vec3(0, 0, 0), particledata->mRadius));
partsys->setInitialBound(box); partsys->setInitialBound(box);
} }
@ -1346,9 +1346,9 @@ namespace NifOsg
void handleNiGeometryData(osg::Geometry* geometry, const Nif::NiGeometryData* data, void handleNiGeometryData(osg::Geometry* geometry, const Nif::NiGeometryData* data,
const std::vector<unsigned int>& boundTextures, const std::string& name) const std::vector<unsigned int>& boundTextures, const std::string& name)
{ {
const auto& vertices = data->vertices; const auto& vertices = data->mVertices;
const auto& normals = data->normals; const auto& normals = data->mNormals;
const auto& colors = data->colors; const auto& colors = data->mColors;
if (!vertices.empty()) if (!vertices.empty())
geometry->setVertexArray(new osg::Vec3Array(vertices.size(), vertices.data())); geometry->setVertexArray(new osg::Vec3Array(vertices.size(), vertices.data()));
if (!normals.empty()) if (!normals.empty())
@ -1357,7 +1357,7 @@ namespace NifOsg
if (!colors.empty()) if (!colors.empty())
geometry->setColorArray(new osg::Vec4Array(colors.size(), colors.data()), osg::Array::BIND_PER_VERTEX); geometry->setColorArray(new osg::Vec4Array(colors.size(), colors.data()), osg::Array::BIND_PER_VERTEX);
const auto& uvlist = data->uvlist; const auto& uvlist = data->mUVList;
int textureStage = 0; int textureStage = 0;
for (std::vector<unsigned int>::const_iterator it = boundTextures.begin(); it != boundTextures.end(); for (std::vector<unsigned int>::const_iterator it = boundTextures.begin(); it != boundTextures.end();
++it, ++textureStage) ++it, ++textureStage)
@ -1473,7 +1473,7 @@ namespace NifOsg
// above the actual renderable would be tedious. // above the actual renderable would be tedious.
std::vector<const Nif::Property*> drawableProps; std::vector<const Nif::Property*> drawableProps;
collectDrawableProperties(nifNode, parent, drawableProps); collectDrawableProperties(nifNode, parent, drawableProps);
applyDrawableProperties(parentNode, drawableProps, composite, !niGeometryData->colors.empty(), animflags); applyDrawableProperties(parentNode, drawableProps, composite, !niGeometryData->mColors.empty(), animflags);
} }
void handleGeometry(const Nif::Node* nifNode, const Nif::Parent* parent, osg::Group* parentNode, void handleGeometry(const Nif::Node* nifNode, const Nif::Parent* parent, osg::Group* parentNode,