Moved MD5[Anim|Mesh]Parser loading code to Loader

Similary to OBJParser


Former-commit-id: 243b05f2fbc3899089ef05a29672979d3bbca695
This commit is contained in:
Lynix
2014-07-17 20:15:29 +02:00
parent 4ccf021376
commit fba2e5ae01
7 changed files with 451 additions and 368 deletions

View File

@@ -15,14 +15,62 @@ namespace
nzTernary Check(NzInputStream& stream, const NzAnimationParams& parameters)
{
NzMD5AnimParser parser(stream, parameters);
NzMD5AnimParser parser(stream);
return parser.Check();
}
bool Load(NzAnimation* animation, NzInputStream& stream, const NzAnimationParams& parameters)
{
NzMD5AnimParser parser(stream, parameters);
return parser.Parse(animation);
NzMD5AnimParser parser(stream);
if (!parser.Parse())
{
NazaraError("MD5Anim parser failed");
return false;
}
const NzMD5AnimParser::Frame* frames = parser.GetFrames();
unsigned int frameCount = parser.GetFrameCount();
unsigned int frameRate = parser.GetFrameRate();
const NzMD5AnimParser::Joint* joints = parser.GetJoints();
unsigned int jointCount = parser.GetJointCount();
// À ce stade, nous sommes censés avoir assez d'informations pour créer l'animation
animation->CreateSkeletal(frameCount, jointCount);
NzSequence sequence;
sequence.firstFrame = 0;
sequence.frameCount = frameCount;
sequence.frameRate = frameRate;
sequence.name = stream.GetPath().SubStringFrom(NAZARA_DIRECTORY_SEPARATOR, -1, true);
animation->AddSequence(sequence);
NzSequenceJoint* sequenceJoints = animation->GetSequenceJoints();
// Pour que le squelette soit correctement aligné, il faut appliquer un quaternion "de correction" aux joints à la base du squelette
NzQuaternionf rotationQuat = NzEulerAnglesf(-90.f, 90.f, 0.f);
for (unsigned int i = 0; i < jointCount; ++i)
{
int parent = joints[i].parent;
for (unsigned int j = 0; j < frameCount; ++j)
{
NzSequenceJoint& sequenceJoint = sequenceJoints[j*jointCount + i];
if (parent >= 0)
{
sequenceJoint.position = frames[j].joints[i].pos;
sequenceJoint.rotation = frames[j].joints[i].orient;
}
else
{
sequenceJoint.position = rotationQuat * frames[j].joints[i].pos;
sequenceJoint.rotation = rotationQuat * frames[j].joints[i].orient;
}
sequenceJoint.scale.Set(1.f);
}
}
}
}

View File

@@ -13,9 +13,8 @@
#include <limits>
#include <Nazara/Utility/Debug.hpp>
NzMD5AnimParser::NzMD5AnimParser(NzInputStream& stream, const NzAnimationParams& parameters) :
NzMD5AnimParser::NzMD5AnimParser(NzInputStream& stream) :
m_stream(stream),
m_parameters(parameters),
m_keepLastLine(false),
m_frameIndex(0),
m_frameRate(0),
@@ -47,7 +46,37 @@ nzTernary NzMD5AnimParser::Check()
return nzTernary_False;
}
bool NzMD5AnimParser::Parse(NzAnimation* animation)
unsigned int NzMD5AnimParser::GetAnimatedComponentCount() const
{
return m_animatedComponents.size();
}
const NzMD5AnimParser::Frame* NzMD5AnimParser::GetFrames() const
{
return m_frames.data();
}
unsigned int NzMD5AnimParser::GetFrameCount() const
{
return m_frames.size();
}
unsigned int NzMD5AnimParser::GetFrameRate() const
{
return m_frameRate;
}
const NzMD5AnimParser::Joint* NzMD5AnimParser::GetJoints() const
{
return m_joints.data();
}
unsigned int NzMD5AnimParser::GetJointCount() const
{
return m_joints.size();
}
bool NzMD5AnimParser::Parse()
{
while (Advance(false))
{
@@ -204,47 +233,6 @@ bool NzMD5AnimParser::Parse(NzAnimation* animation)
m_frameRate = 24;
}
// À ce stade, nous sommes censés avoir assez d'informations pour créer l'animation
if (!animation->CreateSkeletal(frameCount, jointCount))
{
NazaraError("Failed to create animation");
return false;
}
NzSequence sequence;
sequence.firstFrame = 0;
sequence.frameCount = m_frames.size();
sequence.frameRate = m_frameRate;
sequence.name = m_stream.GetPath().SubStringFrom(NAZARA_DIRECTORY_SEPARATOR, -1, true);
if (!animation->AddSequence(sequence))
NazaraWarning("Failed to add sequence");
NzSequenceJoint* sequenceJoints = animation->GetSequenceJoints();
// Pour que le squelette soit correctement aligné, il faut appliquer un quaternion "de correction" aux joints à la base du squelette
NzQuaternionf rotationQuat = NzEulerAnglesf(-90.f, 90.f, 0.f);
for (unsigned int i = 0; i < jointCount; ++i)
{
int parent = m_joints[i].parent;
for (unsigned int j = 0; j < frameCount; ++j)
{
NzSequenceJoint& sequenceJoint = sequenceJoints[j*jointCount + i];
if (parent >= 0)
{
sequenceJoint.position = m_frames[j].joints[i].pos;
sequenceJoint.rotation = m_frames[j].joints[i].orient;
}
else
{
sequenceJoint.position = rotationQuat * m_frames[j].joints[i].pos;
sequenceJoint.rotation = rotationQuat * m_frames[j].joints[i].orient;
}
sequenceJoint.scale.Set(1.f);
}
}
return true;
}
@@ -340,7 +328,7 @@ bool NzMD5AnimParser::ParseBounds()
return false;
}
m_frames[i].aabb.Set(min, max);
m_frames[i].bounds.Set(min, max);
}
if (!Advance())

View File

@@ -18,23 +18,16 @@
class NzMD5AnimParser
{
public:
NzMD5AnimParser(NzInputStream& stream, const NzAnimationParams& parameters);
~NzMD5AnimParser();
struct FrameJoint
{
NzQuaternionf orient;
NzVector3f pos;
};
nzTernary Check();
bool Parse(NzAnimation* animation);
private:
struct Frame
{
struct Joint
{
NzQuaternionf orient;
NzVector3f pos;
};
std::vector<Joint> joints;
NzBoxf aabb;
std::vector<FrameJoint> joints;
NzBoxf bounds;
};
struct Joint
@@ -47,6 +40,21 @@ class NzMD5AnimParser
unsigned int index;
};
NzMD5AnimParser(NzInputStream& stream);
~NzMD5AnimParser();
nzTernary Check();
unsigned int GetAnimatedComponentCount() const;
const Frame* GetFrames() const;
unsigned int GetFrameCount() const;
unsigned int GetFrameRate() const;
const Joint* GetJoints() const;
unsigned int GetJointCount() const;
bool Parse();
private:
bool Advance(bool required = true);
void Error(const NzString& message);
bool ParseBaseframe();
@@ -61,7 +69,6 @@ class NzMD5AnimParser
std::vector<Joint> m_joints;
NzInputStream& m_stream;
NzString m_currentLine;
const NzAnimationParams& m_parameters;
bool m_keepLastLine;
unsigned int m_frameIndex;
unsigned int m_frameRate;