1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 18:59:57 +00:00
openmw/components/nif/data.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

607 lines
18 KiB
C++
Raw Normal View History

#include "data.hpp"
2023-09-03 14:41:52 +00:00
#include <components/debug/debuglog.hpp>
#include "exception.hpp"
2022-07-21 11:51:34 +00:00
#include "nifkey.hpp"
#include "node.hpp"
namespace Nif
{
2021-11-10 16:40:02 +00:00
void NiGeometryData::read(NIFStream* nif)
{
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 114))
2023-09-04 21:56:20 +00:00
nif->read(mGroupId);
2021-11-10 16:40:02 +00:00
2023-09-04 21:56:20 +00:00
nif->read(mNumVertices);
2023-09-21 23:19:32 +00:00
bool isPSysData = false;
switch (recType)
{
case RC_NiPSysData:
case RC_NiMeshPSysData:
2023-09-21 23:19:32 +00:00
case RC_BSStripPSysData:
isPSysData = true;
break;
default:
break;
}
bool hasData = !isPSysData || nif->getBethVersion() < NIFFile::BethVersion::BETHVER_FO3;
2022-09-22 18:26:05 +00:00
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0))
2023-09-04 21:56:20 +00:00
{
nif->read(mKeepFlags);
nif->read(mCompressFlags);
}
2022-09-22 18:26:05 +00:00
if (nif->get<bool>() && hasData)
2023-09-04 21:56:20 +00:00
nif->readVector(mVertices, mNumVertices);
if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0))
{
2023-09-04 21:56:20 +00:00
nif->read(mDataFlags);
if (nif->getVersion() == NIFFile::NIFVersion::VER_BGS
&& nif->getBethVersion() > NIFFile::BethVersion::BETHVER_FO3)
nif->read(mMaterialHash);
}
if (nif->get<bool>() && hasData)
2022-09-22 18:26:05 +00:00
{
2023-09-04 21:56:20 +00:00
nif->readVector(mNormals, mNumVertices);
if (mDataFlags & DataFlag_HasTangents)
2022-09-22 18:26:05 +00:00
{
2023-09-04 21:56:20 +00:00
nif->readVector(mTangents, mNumVertices);
nif->readVector(mBitangents, mNumVertices);
2022-09-22 18:26:05 +00:00
}
}
nif->read(mBoundingSphere);
if (nif->get<bool>() && hasData)
2023-09-04 21:56:20 +00:00
nif->readVector(mColors, mNumVertices);
if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0))
2023-09-04 21:56:20 +00:00
nif->read(mDataFlags);
2022-09-22 18:26:05 +00:00
2023-09-04 21:56:20 +00:00
// In 4.0.0.2 the flags field corresponds to the number of UV sets.
// In later revisions the part that corresponds to the number is narrower.
uint16_t numUVs = mDataFlags;
if (nif->getVersion() > NIFFile::NIFVersion::VER_MW)
{
2023-09-04 21:56:20 +00:00
numUVs &= DataFlag_NumUVsMask;
if (nif->getVersion() == NIFFile::NIFVersion::VER_BGS && nif->getBethVersion() > 0)
2023-09-04 21:56:20 +00:00
numUVs &= DataFlag_HasUV;
}
else if (!nif->get<bool>())
numUVs = 0;
2022-09-22 18:26:05 +00:00
if (hasData)
2022-09-22 18:26:05 +00:00
{
mUVList.resize(numUVs);
for (std::vector<osg::Vec2f>& list : mUVList)
{
nif->readVector(list, mNumVertices);
// flip the texture coordinates to convert them to the OpenGL convention of bottom-left image origin
for (osg::Vec2f& uv : list)
uv.y() = 1.f - uv.y();
}
}
if (nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 0))
{
2023-09-04 21:56:20 +00:00
nif->read(mConsistencyType);
if (nif->getVersion() >= NIFStream::generateVersion(20, 0, 0, 4))
nif->skip(4); // Additional data
}
2022-09-22 18:26:05 +00:00
}
2023-02-10 20:06:56 +00:00
void NiTriBasedGeomData::read(NIFStream* nif)
2022-09-22 18:26:05 +00:00
{
NiGeometryData::read(nif);
2023-09-03 12:21:42 +00:00
nif->read(mNumTriangles);
2023-02-10 20:06:56 +00:00
}
2022-09-22 18:26:05 +00:00
2023-02-10 20:06:56 +00:00
void NiTriShapeData::read(NIFStream* nif)
{
NiTriBasedGeomData::read(nif);
2022-09-22 18:26:05 +00:00
2023-09-04 21:08:28 +00:00
uint32_t numIndices;
nif->read(numIndices);
2023-09-05 22:12:29 +00:00
if (nif->getVersion() > NIFFile::NIFVersion::VER_OB_OLD && !nif->get<bool>())
numIndices = 0;
nif->readVector(mTriangles, numIndices);
2023-09-04 21:08:28 +00:00
mMatchGroups.resize(nif->get<uint16_t>());
for (auto& group : mMatchGroups)
nif->readVector(group, nif->get<uint16_t>());
2022-09-22 18:26:05 +00:00
}
void NiTriStripsData::read(NIFStream* nif)
{
2023-02-10 20:06:56 +00:00
NiTriBasedGeomData::read(nif);
2023-09-03 14:41:52 +00:00
uint16_t numStrips;
nif->read(numStrips);
std::vector<uint16_t> lengths;
2023-07-09 23:07:51 +00:00
nif->readVector(lengths, numStrips);
2023-09-05 22:12:29 +00:00
if (nif->getVersion() > NIFFile::NIFVersion::VER_OB_OLD && !nif->get<bool>())
numStrips = 0;
2023-09-03 14:41:52 +00:00
mStrips.resize(numStrips);
for (int i = 0; i < numStrips; i++)
2023-09-03 14:41:52 +00:00
nif->readVector(mStrips[i], lengths[i]);
}
void NiLinesData::read(NIFStream* nif)
{
NiGeometryData::read(nif);
2023-09-03 12:21:42 +00:00
2023-07-09 23:07:51 +00:00
std::vector<uint8_t> flags;
2023-09-04 21:56:20 +00:00
nif->readVector(flags, mNumVertices);
// Can't construct a line from a single vertex.
2023-09-04 21:56:20 +00:00
if (mNumVertices < 2)
2022-09-22 18:26:05 +00:00
return;
2023-09-06 20:26:43 +00:00
// There can't be more than 2 indices for each vertex
mLines.reserve(mNumVertices * 2);
// Convert connectivity flags into usable geometry. The last element needs special handling.
2023-09-04 21:56:20 +00:00
for (uint16_t i = 0; i < mNumVertices - 1; ++i)
{
if (flags[i] & 1)
{
mLines.emplace_back(i);
mLines.emplace_back(i + 1);
}
}
2020-05-30 14:59:36 +00:00
// If there are just two vertices, they can be connected twice. Probably isn't critical.
2023-09-04 21:56:20 +00:00
if (flags[mNumVertices - 1] & 1)
2022-09-22 18:26:05 +00:00
{
2023-09-04 21:56:20 +00:00
mLines.emplace_back(mNumVertices - 1);
mLines.emplace_back(0);
2022-09-22 18:26:05 +00:00
}
2023-09-06 20:26:43 +00:00
mLines.shrink_to_fit();
}
2019-10-05 12:02:42 +00:00
void NiPosData::read(NIFStream* nif)
2022-09-22 18:26:05 +00:00
{
mKeyList = std::make_shared<Vector3KeyMap>();
2019-10-05 12:02:42 +00:00
mKeyList->read(nif);
}
2020-05-30 14:59:36 +00:00
void NiUVData::read(NIFStream* nif)
{
for (FloatKeyMapPtr& keys : mKeyList)
2020-05-30 14:59:36 +00:00
{
keys = std::make_shared<FloatKeyMap>();
keys->read(nif);
2020-05-30 14:59:36 +00:00
}
}
2022-09-22 18:26:05 +00:00
2020-05-30 14:59:36 +00:00
void NiFloatData::read(NIFStream* nif)
{
mKeyList = std::make_shared<FloatKeyMap>();
mKeyList->read(nif);
}
2023-09-03 17:49:09 +00:00
void NiPixelFormat::read(NIFStream* nif)
{
2023-09-03 17:49:09 +00:00
mFormat = static_cast<Format>(nif->get<uint32_t>());
if (nif->getVersion() <= NIFStream::generateVersion(10, 4, 0, 1))
2022-09-22 18:26:05 +00:00
{
2023-09-03 17:49:09 +00:00
nif->readArray(mColorMasks);
nif->read(mBitsPerPixel);
nif->readArray(mCompareBits);
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0))
2023-09-03 17:49:09 +00:00
nif->read(mPixelTiling);
2022-09-22 18:26:05 +00:00
}
2023-09-03 17:49:09 +00:00
else
2022-09-22 18:26:05 +00:00
{
2023-09-03 17:49:09 +00:00
mBitsPerPixel = nif->get<uint8_t>();
nif->read(mRendererHint);
nif->read(mExtraData);
nif->read(mFlags);
nif->read(mPixelTiling);
if (nif->getVersion() >= NIFStream::generateVersion(20, 3, 0, 4))
2023-09-03 17:49:09 +00:00
nif->read(mUseSrgb);
for (int i = 0; i < 4; i++)
mChannels[i].read(nif);
2022-09-22 18:26:05 +00:00
}
2023-09-03 17:49:09 +00:00
}
2023-09-03 17:49:09 +00:00
void NiPixelFormat::ChannelData::read(NIFStream* nif)
{
mType = static_cast<Type>(nif->get<uint32_t>());
mConvention = static_cast<Convention>(nif->get<uint32_t>());
nif->read(mBitsPerChannel);
nif->read(mSigned);
}
2023-09-03 17:49:09 +00:00
void NiPixelData::read(NIFStream* nif)
{
mPixelFormat.read(nif);
mPalette.read(nif);
mMipmaps.resize(nif->get<uint32_t>());
nif->read(mBytesPerPixel);
for (Mipmap& mip : mMipmaps)
2022-09-22 18:26:05 +00:00
{
2023-09-03 17:49:09 +00:00
nif->read(mip.mWidth);
nif->read(mip.mHeight);
nif->read(mip.mOffset);
}
2023-09-03 17:49:09 +00:00
uint32_t numPixels;
nif->read(numPixels);
if (nif->getVersion() >= NIFStream::generateVersion(10, 4, 0, 2))
nif->read(mNumFaces);
nif->readVector(mData, numPixels * mNumFaces);
}
void NiPixelData::post(Reader& nif)
{
2023-09-03 17:49:09 +00:00
mPalette.post(nif);
}
void NiColorData::read(NIFStream* nif)
{
mKeyMap = std::make_shared<Vector4KeyMap>();
mKeyMap->read(nif);
}
2022-09-22 18:26:05 +00:00
void NiVisData::read(NIFStream* nif)
{
mKeys = std::make_shared<std::map<float, bool>>();
uint32_t numKeys;
nif->read(numKeys);
for (size_t i = 0; i < numKeys; i++)
2022-09-22 18:26:05 +00:00
{
float time;
char value;
nif->read(time);
nif->read(value);
(*mKeys)[time] = (value != 0);
2022-09-22 18:26:05 +00:00
}
}
2023-09-03 12:21:42 +00:00
void NiSkinInstance::read(NIFStream* nif)
{
mData.read(nif);
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 101))
mPartitions.read(nif);
mRoot.read(nif);
readRecordList(nif, mBones);
}
void NiSkinInstance::post(Reader& nif)
{
mData.post(nif);
mPartitions.post(nif);
mRoot.post(nif);
postRecordList(nif, mBones);
if (mData.empty() || mRoot.empty())
throw Nif::Exception("NiSkinInstance missing root or data", nif.getFilename());
if (mBones.size() != mData->mBones.size())
2023-09-03 12:21:42 +00:00
throw Nif::Exception("Mismatch in NiSkinData bone count", nif.getFilename());
for (auto& bone : mBones)
{
if (bone.empty())
throw Nif::Exception("Oops: Missing bone! Don't know how to handle this.", nif.getFilename());
bone->setBone();
}
}
const Nif::NiSkinPartition* NiSkinInstance::getPartitions() const
{
const Nif::NiSkinPartition* partitions = nullptr;
if (!mPartitions.empty())
partitions = mPartitions.getPtr();
else if (!mData.empty() && !mData->mPartitions.empty())
partitions = mData->mPartitions.getPtr();
return partitions;
}
2023-09-03 12:21:42 +00:00
void BSDismemberSkinInstance::read(NIFStream* nif)
{
NiSkinInstance::read(nif);
mParts.resize(nif->get<uint32_t>());
for (BodyPart& part : mParts)
{
nif->read(part.mFlags);
nif->read(part.mType);
}
}
2023-09-27 09:17:44 +00:00
void BSSkinInstance::read(NIFStream* nif)
{
mRoot.read(nif);
mData.read(nif);
readRecordList(nif, mBones);
nif->readVector(mScales, nif->get<uint32_t>());
}
void BSSkinInstance::post(Reader& nif)
{
mRoot.post(nif);
mData.post(nif);
postRecordList(nif, mBones);
if (mData.empty() || mRoot.empty())
throw Nif::Exception("BSSkin::Instance missing root or data", nif.getFilename());
if (mBones.size() != mData->mBones.size())
throw Nif::Exception("Mismatch in BSSkin::BoneData bone count", nif.getFilename());
for (auto& bone : mBones)
{
if (bone.empty())
throw Nif::Exception("Oops: Missing bone! Don't know how to handle this.", nif.getFilename());
bone->setBone();
}
}
void NiSkinData::read(NIFStream* nif)
{
nif->read(mTransform);
uint32_t numBones;
nif->read(numBones);
2023-05-22 21:58:59 +00:00
bool hasVertexWeights = true;
if (nif->getVersion() >= NIFFile::NIFVersion::VER_MW)
{
if (nif->getVersion() <= NIFStream::generateVersion(10, 1, 0, 0))
mPartitions.read(nif);
if (nif->getVersion() >= NIFStream::generateVersion(4, 2, 1, 0))
nif->read(hasVertexWeights);
}
mBones.resize(numBones);
for (BoneInfo& bi : mBones)
2022-09-22 18:26:05 +00:00
{
nif->read(bi.mTransform);
nif->read(bi.mBoundSphere);
2022-09-22 18:26:05 +00:00
uint16_t numVertices;
nif->read(numVertices);
2023-05-22 21:58:59 +00:00
if (!hasVertexWeights)
continue;
bi.mWeights.resize(numVertices);
for (auto& [vertex, weight] : bi.mWeights)
2022-09-22 18:26:05 +00:00
{
nif->read(vertex);
nif->read(weight);
}
2022-09-22 18:26:05 +00:00
}
}
void NiSkinData::post(Reader& nif)
{
mPartitions.post(nif);
}
2023-09-27 09:17:44 +00:00
void BSSkinBoneData::read(NIFStream* nif)
{
mBones.resize(nif->get<uint32_t>());
for (BoneInfo& bone : mBones)
{
nif->read(bone.mBoundSphere);
nif->read(bone.mTransform);
}
}
void NiSkinPartition::read(NIFStream* nif)
2022-09-22 18:26:05 +00:00
{
2023-09-05 20:19:35 +00:00
mPartitions.resize(nif->get<uint32_t>());
2023-07-09 10:14:27 +00:00
if (nif->getBethVersion() == NIFFile::BethVersion::BETHVER_SSE)
{
nif->read(mDataSize);
nif->read(mVertexSize);
mVertexDesc.read(nif);
mVertexData.resize(mDataSize / mVertexSize);
for (auto& vertexData : mVertexData)
vertexData.read(nif, mVertexDesc.mFlags);
}
for (auto& partition : mPartitions)
partition.read(nif);
2022-09-22 18:26:05 +00:00
}
void NiSkinPartition::Partition::read(NIFStream* nif)
{
2023-09-05 20:19:35 +00:00
uint16_t numVertices, numTriangles, numBones, numStrips, bonesPerVertex;
nif->read(numVertices);
nif->read(numTriangles);
nif->read(numBones);
nif->read(numStrips);
nif->read(bonesPerVertex);
nif->readVector(mBones, numBones);
bool hasPresenceFlags = nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0);
if (!hasPresenceFlags || nif->get<bool>())
2023-09-05 20:19:35 +00:00
nif->readVector(mVertexMap, numVertices);
if (!hasPresenceFlags || nif->get<bool>())
2023-11-12 07:37:03 +00:00
nif->readVector(mWeights, static_cast<size_t>(numVertices) * bonesPerVertex);
2020-11-07 01:04:46 +00:00
std::vector<unsigned short> stripLengths;
2023-07-09 23:07:51 +00:00
nif->readVector(stripLengths, numStrips);
if (!hasPresenceFlags || nif->get<bool>())
{
2020-11-07 01:04:46 +00:00
if (numStrips)
2022-09-22 18:26:05 +00:00
{
2023-09-05 20:19:35 +00:00
mStrips.resize(numStrips);
for (size_t i = 0; i < numStrips; i++)
2023-09-05 20:19:35 +00:00
nif->readVector(mStrips[i], stripLengths[i]);
2022-09-22 18:26:05 +00:00
}
2023-07-09 23:07:51 +00:00
else
2023-09-05 20:19:35 +00:00
nif->readVector(mTriangles, numTriangles * 3);
2022-09-22 18:26:05 +00:00
}
2023-09-05 20:19:35 +00:00
if (nif->get<uint8_t>() != 0)
2023-11-12 07:37:03 +00:00
nif->readVector(mBoneIndices, static_cast<size_t>(numVertices) * bonesPerVertex);
2020-11-07 01:04:46 +00:00
if (nif->getBethVersion() > NIFFile::BethVersion::BETHVER_FO3)
2022-09-22 18:26:05 +00:00
{
2023-09-05 20:19:35 +00:00
nif->read(mLODLevel);
nif->read(mGlobalVB);
2023-09-05 22:12:29 +00:00
if (nif->getBethVersion() == NIFFile::BethVersion::BETHVER_SSE)
{
mVertexDesc.read(nif);
nif->readVector(mTrueTriangles, numTriangles * 3);
}
}
2023-07-09 10:14:27 +00:00
2023-09-05 22:12:29 +00:00
// Not technically a part of the loading process
if (mTrueTriangles.empty() && !mVertexMap.empty())
2023-09-05 20:19:35 +00:00
{
if (!mStrips.empty())
{
mTrueStrips = mStrips;
for (auto& strip : mTrueStrips)
for (auto& index : strip)
index = mVertexMap[index];
}
else if (!mTriangles.empty())
{
mTrueTriangles = mTriangles;
for (unsigned short& index : mTrueTriangles)
index = mVertexMap[index];
}
2023-07-09 10:14:27 +00:00
}
}
2020-11-07 01:04:46 +00:00
void NiMorphData::read(NIFStream* nif)
{
uint32_t numMorphs, numVerts;
nif->read(numMorphs);
nif->read(numVerts);
nif->read(mRelativeTargets);
2020-11-07 01:04:46 +00:00
mMorphs.resize(numMorphs);
for (MorphData& morph : mMorphs)
2022-09-22 18:26:05 +00:00
{
morph.mKeyFrames = std::make_shared<FloatKeyMap>();
morph.mKeyFrames->read(nif, /*morph*/ true);
nif->readVector(morph.mVertices, numVerts);
2022-09-22 18:26:05 +00:00
}
2020-11-07 01:04:46 +00:00
}
void NiKeyframeData::read(NIFStream* nif)
{
mRotations = std::make_shared<QuaternionKeyMap>();
mRotations->read(nif);
if (mRotations->mInterpolationType == InterpolationType_XYZ)
{
if (nif->getVersion() <= NIFStream::generateVersion(10, 1, 0, 0))
2023-09-04 21:11:41 +00:00
mAxisOrder = static_cast<AxisOrder>(nif->get<uint32_t>());
2018-11-04 19:57:10 +00:00
mXRotations = std::make_shared<FloatKeyMap>();
2020-11-07 01:04:46 +00:00
mYRotations = std::make_shared<FloatKeyMap>();
2018-11-04 19:57:10 +00:00
mZRotations = std::make_shared<FloatKeyMap>();
mXRotations->read(nif);
mYRotations->read(nif);
mZRotations->read(nif);
2020-11-07 01:04:46 +00:00
}
mTranslations = std::make_shared<Vector3KeyMap>();
mTranslations->read(nif);
mScales = std::make_shared<FloatKeyMap>();
mScales->read(nif);
}
2022-09-22 18:26:05 +00:00
2020-11-07 01:04:46 +00:00
void NiPalette::read(NIFStream* nif)
{
bool useAlpha = nif->get<uint8_t>() != 0;
uint32_t alphaMask = useAlpha ? 0 : 0xFF000000;
uint32_t numEntries;
nif->read(numEntries);
2020-11-07 01:04:46 +00:00
// Fill the entire palette with black even if there isn't enough entries.
mColors.resize(numEntries > 256 ? numEntries : 256);
for (uint32_t i = 0; i < numEntries; i++)
{
nif->read(mColors[i]);
mColors[i] |= alphaMask;
}
2020-11-07 01:04:46 +00:00
}
void NiStringPalette::read(NIFStream* nif)
{
mPalette = nif->getStringPalette();
if (nif->get<uint32_t>() != mPalette.size())
Log(Debug::Warning) << "NIFFile Warning: Failed size check in NiStringPalette. File: "
<< nif->getFile().getFilename();
}
void NiBoolData::read(NIFStream* nif)
{
2023-09-13 21:15:02 +00:00
mKeyList = std::make_shared<BoolKeyMap>();
mKeyList->read(nif);
}
2020-11-07 00:40:21 +00:00
2023-09-20 01:57:10 +00:00
void NiAdditionalGeometryData::read(NIFStream* nif)
{
nif->read(mNumVertices);
mBlockInfos.resize(nif->get<uint32_t>());
for (DataStream& info : mBlockInfos)
info.read(nif);
mBlocks.resize(nif->get<uint32_t>());
for (DataBlock& block : mBlocks)
block.read(nif, recType == RC_BSPackedAdditionalGeometryData);
}
void NiAdditionalGeometryData::DataStream::read(NIFStream* nif)
{
nif->read(mType);
nif->read(mUnitSize);
nif->read(mTotalSize);
nif->read(mStride);
nif->read(mBlockIndex);
nif->read(mBlockOffset);
nif->read(mFlags);
}
void NiAdditionalGeometryData::DataBlock::read(NIFStream* nif, bool bsPacked)
{
nif->read(mValid);
if (!mValid)
return;
nif->read(mBlockSize);
nif->readVector(mBlockOffsets, nif->get<uint32_t>());
nif->readVector(mDataSizes, nif->get<uint32_t>());
nif->readVector(mData, mDataSizes.size() * mBlockSize);
if (bsPacked)
{
nif->read(mShaderIndex);
nif->read(mTotalSize);
}
}
2023-05-22 19:28:05 +00:00
void BSMultiBound::read(NIFStream* nif)
{
mData.read(nif);
}
void BSMultiBound::post(Reader& nif)
{
mData.post(nif);
}
2023-09-20 00:42:59 +00:00
void BSMultiBoundAABB::read(NIFStream* nif)
{
nif->read(mPosition);
nif->read(mExtents);
}
2023-05-22 19:28:05 +00:00
void BSMultiBoundOBB::read(NIFStream* nif)
{
nif->read(mCenter);
nif->read(mSize);
nif->read(mRotation);
2023-05-22 19:28:05 +00:00
}
void BSMultiBoundSphere::read(NIFStream* nif)
{
nif->read(mSphere);
2023-05-22 19:28:05 +00:00
}
} // Namespace