1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-06 00:55:50 +00:00
OpenMW/components/nif/nifkey.hpp

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

156 lines
5.3 KiB
C++
Raw Normal View History

/// File to handle keys used by nif file records
#ifndef OPENMW_COMPONENTS_NIF_NIFKEY_HPP
#define OPENMW_COMPONENTS_NIF_NIFKEY_HPP
2015-02-17 16:08:55 +00:00
#include <map>
#include "exception.hpp"
#include "niffile.hpp"
2022-07-21 11:51:34 +00:00
#include "nifstream.hpp"
namespace Nif
{
2019-12-31 10:38:57 +00:00
enum InterpolationType
{
InterpolationType_Unknown = 0,
InterpolationType_Linear = 1,
InterpolationType_Quadratic = 2,
InterpolationType_TBC = 3,
InterpolationType_XYZ = 4,
InterpolationType_Constant = 5
};
2022-09-22 18:26:05 +00:00
template <typename T>
struct KeyT
{
T mValue;
T mInTan; // Only for Quadratic interpolation, and never for QuaternionKeyList
T mOutTan; // Only for Quadratic interpolation, and never for QuaternionKeyList
2022-09-22 18:26:05 +00:00
// FIXME: Implement TBC interpolation
2022-09-22 18:26:05 +00:00
/*
float mTension; // Only for TBC interpolation
float mBias; // Only for TBC interpolation
float mContinuity; // Only for TBC interpolation
2022-09-22 18:26:05 +00:00
*/
};
using FloatKey = KeyT<float>;
using Vector3Key = KeyT<osg::Vec3f>;
using Vector4Key = KeyT<osg::Vec4f>;
using QuaternionKey = KeyT<osg::Quat>;
2022-09-22 18:26:05 +00:00
template <typename T, T (NIFStream::*getValue)()>
struct KeyMapT
2022-09-22 18:26:05 +00:00
{
using MapType = std::map<float, KeyT<T>>;
2022-09-22 18:26:05 +00:00
using ValueType = T;
using KeyType = KeyT<T>;
unsigned int mInterpolationType = InterpolationType_Unknown;
MapType mKeys;
2022-09-16 02:55:37 +00:00
// Read in a KeyGroup (see http://niftools.sourceforge.net/doc/nif/NiKeyframeData.html)
void read(NIFStream* nif, bool morph = false)
{
assert(nif);
2022-09-22 18:26:05 +00:00
2022-09-16 02:55:37 +00:00
if (morph && nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 106))
nif->getString(); // Frame name
if (morph && nif->getVersion() > NIFStream::generateVersion(10, 1, 0, 0))
2022-09-22 18:26:05 +00:00
{
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 104)
2022-09-16 02:55:37 +00:00
&& nif->getVersion() <= NIFStream::generateVersion(20, 1, 0, 2) && nif->getBethVersion() < 10)
nif->getFloat(); // Legacy weight
return;
2022-09-22 18:26:05 +00:00
}
size_t count = nif->getUInt();
if (count != 0 || morph)
2021-03-18 07:47:06 +00:00
mInterpolationType = nif->getUInt();
KeyType key = {};
2022-09-22 18:26:05 +00:00
if (mInterpolationType == InterpolationType_Linear || mInterpolationType == InterpolationType_Constant)
{
for (size_t i = 0; i < count; i++)
2022-09-22 18:26:05 +00:00
{
float time = nif->getFloat();
readValue(*nif, key);
mKeys[time] = key;
2022-09-22 18:26:05 +00:00
}
}
2019-12-31 10:38:57 +00:00
else if (mInterpolationType == InterpolationType_Quadratic)
{
for (size_t i = 0; i < count; i++)
2022-09-22 18:26:05 +00:00
{
float time = nif->getFloat();
readQuadratic(*nif, key);
mKeys[time] = key;
2022-09-22 18:26:05 +00:00
}
}
2019-12-31 10:38:57 +00:00
else if (mInterpolationType == InterpolationType_TBC)
{
for (size_t i = 0; i < count; i++)
2022-09-22 18:26:05 +00:00
{
float time = nif->getFloat();
readTBC(*nif, key);
mKeys[time] = key;
2022-09-22 18:26:05 +00:00
}
}
else if (mInterpolationType == InterpolationType_XYZ)
2022-09-22 18:26:05 +00:00
{
// XYZ keys aren't actually read here.
// data.cpp sees that the last type read was InterpolationType_XYZ and:
// Eats a floating point number, then
// Re-runs the read function 3 more times.
// When it does that it's reading in a bunch of InterpolationType_Linear keys, not
// InterpolationType_XYZ.
2022-09-22 18:26:05 +00:00
}
2021-12-26 14:56:19 +00:00
else if (count != 0)
2022-09-22 18:26:05 +00:00
{
throw Nif::Exception("Unhandled interpolation type: " + std::to_string(mInterpolationType),
nif->getFile().getFilename());
}
}
private:
static void readValue(NIFStream& nif, KeyT<T>& key) { key.mValue = (nif.*getValue)(); }
template <typename U>
static void readQuadratic(NIFStream& nif, KeyT<U>& key)
{
readValue(nif, key);
key.mInTan = (nif.*getValue)();
key.mOutTan = (nif.*getValue)();
}
static void readQuadratic(NIFStream& nif, KeyT<osg::Quat>& key) { readValue(nif, key); }
static void readTBC(NIFStream& nif, KeyT<T>& key)
{
readValue(nif, key);
2015-02-17 16:08:55 +00:00
/*key.mTension = */ nif.getFloat();
/*key.mBias = */ nif.getFloat();
/*key.mContinuity = */ nif.getFloat();
}
};
using FloatKeyMap = KeyMapT<float, &NIFStream::getFloat>;
using Vector3KeyMap = KeyMapT<osg::Vec3f, &NIFStream::getVector3>;
using Vector4KeyMap = KeyMapT<osg::Vec4f, &NIFStream::getVector4>;
using QuaternionKeyMap = KeyMapT<osg::Quat, &NIFStream::getQuaternion>;
using ByteKeyMap = KeyMapT<char, &NIFStream::getChar>;
2022-09-22 18:26:05 +00:00
using FloatKeyMapPtr = std::shared_ptr<FloatKeyMap>;
using Vector3KeyMapPtr = std::shared_ptr<Vector3KeyMap>;
using Vector4KeyMapPtr = std::shared_ptr<Vector4KeyMap>;
using QuaternionKeyMapPtr = std::shared_ptr<QuaternionKeyMap>;
using ByteKeyMapPtr = std::shared_ptr<ByteKeyMap>;
} // Namespace
#endif //#ifndef OPENMW_COMPONENTS_NIF_NIFKEY_HPP