1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-27 12:11:35 +00:00

Modernize NiMorphData, palette and BSBound records

This commit is contained in:
Alexei Kotov 2023-09-03 16:29:42 +03:00
parent 5b07a78f2c
commit 384a398b62
3 changed files with 37 additions and 23 deletions

View file

@ -485,16 +485,17 @@ namespace Nif
void NiMorphData::read(NIFStream* nif) void NiMorphData::read(NIFStream* nif)
{ {
int morphCount = nif->getInt(); uint32_t numMorphs, numVerts;
int vertCount = nif->getInt(); nif->read(numMorphs);
nif->getChar(); // Relative targets, always 1 nif->read(numVerts);
nif->read(mRelativeTargets);
mMorphs.resize(morphCount); mMorphs.resize(numMorphs);
for (int i = 0; i < morphCount; i++) for (MorphData& morph : mMorphs)
{ {
mMorphs[i].mKeyFrames = std::make_shared<FloatKeyMap>(); morph.mKeyFrames = std::make_shared<FloatKeyMap>();
mMorphs[i].mKeyFrames->read(nif, /*morph*/ true); morph.mKeyFrames->read(nif, /*morph*/ true);
nif->readVector(mMorphs[i].mVertices, vertCount); nif->readVector(morph.mVertices, numVerts);
} }
} }
@ -521,18 +522,28 @@ namespace Nif
void NiPalette::read(NIFStream* nif) void NiPalette::read(NIFStream* nif)
{ {
unsigned int alphaMask = !nif->getChar() ? 0xFF000000 : 0; bool useAlpha = nif->get<uint8_t>() != 0;
uint32_t alphaMask = useAlpha ? 0 : 0xFF000000;
uint32_t numEntries;
nif->read(numEntries);
// Fill the entire palette with black even if there isn't enough entries. // Fill the entire palette with black even if there isn't enough entries.
colors.resize(256); mColors.resize(256);
unsigned int numEntries = nif->getUInt(); if (numEntries > 256)
for (unsigned int i = 0; i < numEntries; i++) mColors.resize(numEntries);
colors[i] = nif->getUInt() | alphaMask;
for (uint32_t i = 0; i < numEntries; i++)
{
nif->read(mColors[i]);
mColors[i] |= alphaMask;
}
} }
void NiStringPalette::read(NIFStream* nif) void NiStringPalette::read(NIFStream* nif)
{ {
palette = nif->getStringPalette(); mPalette = nif->getStringPalette();
if (nif->getUInt() != palette.size()) if (nif->get<uint32_t>() != mPalette.size())
Log(Debug::Warning) << "NIFFile Warning: Failed size check in NiStringPalette. File: " Log(Debug::Warning) << "NIFFile Warning: Failed size check in NiStringPalette. File: "
<< nif->getFile().getFilename(); << nif->getFile().getFilename();
} }
@ -555,15 +566,15 @@ namespace Nif
void BSMultiBoundOBB::read(NIFStream* nif) void BSMultiBoundOBB::read(NIFStream* nif)
{ {
mCenter = nif->getVector3(); nif->read(mCenter);
mSize = nif->getVector3(); nif->read(mSize);
mRotation = nif->getMatrix3(); nif->read(mRotation);
} }
void BSMultiBoundSphere::read(NIFStream* nif) void BSMultiBoundSphere::read(NIFStream* nif)
{ {
mCenter = nif->getVector3(); nif->read(mCenter);
mRadius = nif->getFloat(); nif->read(mRadius);
} }
} // Namespace } // Namespace

View file

@ -253,6 +253,8 @@ namespace Nif
FloatKeyMapPtr mKeyFrames; FloatKeyMapPtr mKeyFrames;
std::vector<osg::Vec3f> mVertices; std::vector<osg::Vec3f> mVertices;
}; };
uint8_t mRelativeTargets;
std::vector<MorphData> mMorphs; std::vector<MorphData> mMorphs;
void read(NIFStream* nif) override; void read(NIFStream* nif) override;
@ -291,14 +293,15 @@ namespace Nif
struct NiPalette : public Record struct NiPalette : public Record
{ {
// 32-bit RGBA colors that correspond to 8-bit indices // 32-bit RGBA colors that correspond to 8-bit indices
std::vector<unsigned int> colors; std::vector<uint32_t> mColors;
void read(NIFStream* nif) override; void read(NIFStream* nif) override;
}; };
struct NiStringPalette : public Record struct NiStringPalette : public Record
{ {
std::string palette; std::string mPalette;
void read(NIFStream* nif) override; void read(NIFStream* nif) override;
}; };

View file

@ -1782,7 +1782,7 @@ namespace NifOsg
pixelformat = pixelData->fmt == Nif::NiPixelData::NIPXFMT_PAL8 ? GL_RGB : GL_RGBA; pixelformat = pixelData->fmt == Nif::NiPixelData::NIPXFMT_PAL8 ? GL_RGB : GL_RGBA;
// We're going to convert the indices that pixel data contains // We're going to convert the indices that pixel data contains
// into real colors using the palette. // into real colors using the palette.
const auto& palette = pixelData->palette->colors; const auto& palette = pixelData->palette->mColors;
const int numChannels = pixelformat == GL_RGBA ? 4 : 3; const int numChannels = pixelformat == GL_RGBA ? 4 : 3;
unsigned char* data = new unsigned char[pixels.size() * numChannels]; unsigned char* data = new unsigned char[pixels.size() * numChannels];
unsigned char* pixel = data; unsigned char* pixel = data;