Big skeletal animation update
Added MeshInfos demo Added MD5Mesh/MD5Anim loader support Added Node class Fixed ResourceParams not being exported Added support for skeletal animation (Animation/Mesh/Joint/SkeletalMesh/Skeleton) Meshes are now only stored with VertexStruct_XYZ_Normal_UV_Tangent type Moved Sequence declaration to Sequence.hpp -Animation: Renamed Create to Create[Keyframe|Skeletal] -AxisAlignedBox: Added Contains method Added GetCorner method Added GetCube method Added Transform method -Cube/Rect: Added GetPosition method Added GetSize method (Almost) Fixed ExtendTo method Fixed GetCenter method -File: Added GetDirectory static function Added GetPath method Renamed GetDirectoryPath method to GetDirectory -Math module: Fixed constructor/methods taking a non-const array GetNormal/Normalize methods now takes an optionnal integer pointer (returning length) Made all classes default constructor trivial Inverse, MakeIdentity, MakeZero, Normalize, Set methods now returns reference to object -Matrix4: Modified methods to avoid copies Removed COW (Too much overhead) Removed Concatenate[Affine] static function -Mesh: Renamed Create to Create[Keyframe|Skeletal|Static] Renamed Skin to Material -MeshParams: No longer takes declaration argument Renamed loadAnimations to animated Storage default to BufferStorage_Hardware if supported and BufferStorage_Software otherwise -OpenGL: Added glGetBooleanv function Added glIsEnabled function -Quaternion: Added ComputeW method Added Conjugate method -Renderer: Added IsEnabled static function Fixed GetLineWidth function not being static Removed SetVertexDeclaration -RenderWindow: Made CopyTo[Image|Texture] method constant -Resource Fixed RemoveResourceListener crash -ResourceLoader: Loaders are now used in a LIFO context -Stream: Renamed GetLine method to ReadLine -String: Fixed Simplified -Utility module Added configuration define for strict resource parsing -VertexBuffer Now takes a VertexDeclaration pointer -VertexDeclaration No longer throw an error when getting a non-existing element Former-commit-id: f7358c1231d6af48b799d2f24f077a001e16785b
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <Nazara/Utility/Animation.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
@@ -13,15 +14,17 @@ struct NzAnimationImpl
|
||||
{
|
||||
std::map<NzString, unsigned int> sequenceMap;
|
||||
std::vector<NzSequence> sequences;
|
||||
std::vector<NzSequenceJoint> sequenceJoints; // Uniquement pour les animations squelettiques
|
||||
nzAnimationType type;
|
||||
unsigned int frameCount;
|
||||
unsigned int jointCount; // Uniquement pour les animations squelettiques
|
||||
};
|
||||
|
||||
bool NzAnimationParams::IsValid() const
|
||||
{
|
||||
if (startFrame > endFrame)
|
||||
{
|
||||
NazaraError("Start frame must be lower than end frame");
|
||||
NazaraError("Start frame index must be smaller than end frame index");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -41,9 +44,34 @@ bool NzAnimation::AddSequence(const NzSequence& sequence)
|
||||
NazaraError("Animation not created");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sequence.frameCount == 0)
|
||||
{
|
||||
NazaraError("Sequence frame count must be over 0");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int index = m_impl->sequences.size();
|
||||
if (m_impl->type == nzAnimationType_Skeletal)
|
||||
{
|
||||
unsigned int endFrame = sequence.firstFrame + sequence.frameCount - 1;
|
||||
if (endFrame >= m_impl->frameCount)
|
||||
{
|
||||
m_impl->frameCount = endFrame+1;
|
||||
m_impl->sequenceJoints.resize(m_impl->frameCount*m_impl->jointCount);
|
||||
}
|
||||
}
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
else
|
||||
{
|
||||
unsigned int endFrame = sequence.firstFrame + sequence.frameCount - 1;
|
||||
if (endFrame >= m_impl->frameCount)
|
||||
{
|
||||
NazaraError("Sequence end frame is over animation end frame");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!sequence.name.IsEmpty())
|
||||
{
|
||||
@@ -56,7 +84,7 @@ bool NzAnimation::AddSequence(const NzSequence& sequence)
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl->sequenceMap[sequence.name] = index;
|
||||
m_impl->sequenceMap[sequence.name] = m_impl->sequences.size();
|
||||
}
|
||||
|
||||
m_impl->sequences.push_back(sequence);
|
||||
@@ -64,17 +92,72 @@ bool NzAnimation::AddSequence(const NzSequence& sequence)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzAnimation::Create(nzAnimationType type, unsigned int frameCount)
|
||||
void NzAnimation::AnimateSkeleton(NzSkeleton* targetSkeleton, unsigned int frameA, unsigned int frameB, float interpolation) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Animation not created");
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_impl->type != nzAnimationType_Skeletal)
|
||||
{
|
||||
NazaraError("Animation is not skeletal");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!targetSkeleton || !targetSkeleton->IsValid())
|
||||
{
|
||||
NazaraError("Target skeleton is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetSkeleton->GetJointCount() != m_impl->jointCount)
|
||||
{
|
||||
NazaraError("Target skeleton joint count must match animation joint count");
|
||||
return;
|
||||
}
|
||||
|
||||
if (frameA >= m_impl->frameCount)
|
||||
{
|
||||
NazaraError("Frame A is out of range (" + NzString::Number(frameA) + " >= " + NzString::Number(m_impl->frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
|
||||
if (frameB >= m_impl->frameCount)
|
||||
{
|
||||
NazaraError("Frame B is out of range (" + NzString::Number(frameB) + " >= " + NzString::Number(m_impl->frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (interpolation < 0.f || interpolation > 1.f)
|
||||
{
|
||||
NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
for (unsigned int i = 0; i < m_impl->jointCount; ++i)
|
||||
{
|
||||
NzJoint* joint = targetSkeleton->GetJoint(i);
|
||||
|
||||
NzSequenceJoint& sequenceJointA = m_impl->sequenceJoints[frameA*m_impl->jointCount + i];
|
||||
NzSequenceJoint& sequenceJointB = m_impl->sequenceJoints[frameB*m_impl->jointCount + i];
|
||||
|
||||
joint->SetRotation(NzQuaternionf::Slerp(sequenceJointA.rotation, sequenceJointB.rotation, interpolation));
|
||||
joint->SetScale(NzVector3f::Lerp(sequenceJointA.scale, sequenceJointB.scale, interpolation));
|
||||
joint->SetTranslation(NzVector3f::Lerp(sequenceJointA.translation, sequenceJointB.translation, interpolation));
|
||||
}
|
||||
}
|
||||
|
||||
bool NzAnimation::CreateKeyframe(unsigned int frameCount)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (type == nzAnimationType_Static)
|
||||
{
|
||||
NazaraError("Invalid type");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (frameCount == 0)
|
||||
{
|
||||
NazaraError("Frame count must be over zero");
|
||||
@@ -84,7 +167,29 @@ bool NzAnimation::Create(nzAnimationType type, unsigned int frameCount)
|
||||
|
||||
m_impl = new NzAnimationImpl;
|
||||
m_impl->frameCount = frameCount;
|
||||
m_impl->type = type;
|
||||
m_impl->type = nzAnimationType_Keyframe;
|
||||
|
||||
NotifyCreated();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzAnimation::CreateSkeletal(unsigned int frameCount, unsigned int jointCount)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (frameCount == 0)
|
||||
{
|
||||
NazaraError("Frame count must be over zero");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl = new NzAnimationImpl;
|
||||
m_impl->frameCount = frameCount;
|
||||
m_impl->jointCount = jointCount;
|
||||
m_impl->sequenceJoints.resize(frameCount*jointCount);
|
||||
m_impl->type = nzAnimationType_Skeletal;
|
||||
|
||||
NotifyCreated();
|
||||
return true;
|
||||
@@ -122,18 +227,19 @@ NzSequence* NzAnimation::GetSequence(const NzString& sequenceName)
|
||||
NazaraError("Animation not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto it = m_impl->sequenceMap.find(sequenceName);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->sequenceMap.end())
|
||||
{
|
||||
NazaraError("Sequence not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->sequences[it->second];
|
||||
#else
|
||||
return &m_impl->sequences[m_impl->sequenceMap[sequenceName]];
|
||||
#endif
|
||||
}
|
||||
|
||||
NzSequence* NzAnimation::GetSequence(unsigned int index)
|
||||
@@ -163,18 +269,19 @@ const NzSequence* NzAnimation::GetSequence(const NzString& sequenceName) const
|
||||
NazaraError("Animation not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto it = m_impl->sequenceMap.find(sequenceName);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->sequenceMap.end())
|
||||
{
|
||||
NazaraError("Sequence not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->sequences[it->second];
|
||||
#else
|
||||
return &m_impl->sequences[m_impl->sequenceMap[sequenceName]];
|
||||
#endif
|
||||
}
|
||||
|
||||
const NzSequence* NzAnimation::GetSequence(unsigned int index) const
|
||||
@@ -217,18 +324,57 @@ int NzAnimation::GetSequenceIndex(const NzString& sequenceName) const
|
||||
NazaraError("Animation not created");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto it = m_impl->sequenceMap.find(sequenceName);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->sequenceMap.end())
|
||||
{
|
||||
NazaraError("Sequence not found");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
return it->second;
|
||||
#else
|
||||
return m_impl->sequenceMap[sequenceName];
|
||||
}
|
||||
|
||||
NzSequenceJoint* NzAnimation::GetSequenceJoints(unsigned int frameIndex)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Animation not created");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (m_impl->type != nzAnimationType_Skeletal)
|
||||
{
|
||||
NazaraError("Animation is not skeletal");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->sequenceJoints[frameIndex*m_impl->jointCount];
|
||||
}
|
||||
|
||||
const NzSequenceJoint* NzAnimation::GetSequenceJoints(unsigned int frameIndex) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Animation not created");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (m_impl->type != nzAnimationType_Skeletal)
|
||||
{
|
||||
NazaraError("Animation is not skeletal");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->sequenceJoints[frameIndex*m_impl->jointCount];
|
||||
}
|
||||
|
||||
nzAnimationType NzAnimation::GetType() const
|
||||
|
||||
@@ -38,6 +38,24 @@ bool NzAxisAlignedBox::Contains(const NzAxisAlignedBox& box)
|
||||
return m_cube.Contains(box.m_cube);
|
||||
}
|
||||
|
||||
bool NzAxisAlignedBox::Contains(const NzVector3f& vector)
|
||||
{
|
||||
switch (m_extend)
|
||||
{
|
||||
case nzExtend_Finite:
|
||||
return m_cube.Contains(vector);
|
||||
|
||||
case nzExtend_Infinite:
|
||||
return true;
|
||||
|
||||
case nzExtend_Null:
|
||||
return false;
|
||||
}
|
||||
|
||||
NazaraError("Extend type not handled (0x" + NzString::Number(m_extend, 16) + ')');
|
||||
return false;
|
||||
}
|
||||
|
||||
void NzAxisAlignedBox::ExtendTo(const NzAxisAlignedBox& box)
|
||||
{
|
||||
switch (m_extend)
|
||||
@@ -81,17 +99,50 @@ void NzAxisAlignedBox::ExtendTo(const NzVector3f& vector)
|
||||
break;
|
||||
|
||||
case nzExtend_Null:
|
||||
// Nous étendons l'AABB en la construisant de l'origine jusqu'au vecteur
|
||||
m_cube.x = 0.f;
|
||||
m_cube.y = 0.f;
|
||||
m_cube.z = 0.f;
|
||||
m_cube.width = std::fabs(vector.x);
|
||||
m_cube.height = std::fabs(vector.y);
|
||||
m_cube.depth = std::fabs(vector.z);
|
||||
m_extend = nzExtend_Finite;
|
||||
m_cube.Set(vector, vector);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
NzVector3f NzAxisAlignedBox::GetCorner(nzCorner corner) const
|
||||
{
|
||||
switch (corner)
|
||||
{
|
||||
case nzCorner_FarLeftBottom:
|
||||
return NzVector3f(m_cube.x, m_cube.y, m_cube.z);
|
||||
|
||||
case nzCorner_FarLeftTop:
|
||||
return NzVector3f(m_cube.x, m_cube.y+m_cube.height, m_cube.z);
|
||||
|
||||
case nzCorner_FarRightBottom:
|
||||
return NzVector3f(m_cube.x+m_cube.width, m_cube.y, m_cube.z);
|
||||
|
||||
case nzCorner_FarRightTop:
|
||||
return NzVector3f(m_cube.x+m_cube.width, m_cube.y+m_cube.height, m_cube.z);
|
||||
|
||||
case nzCorner_NearLeftBottom:
|
||||
return NzVector3f(m_cube.x, m_cube.y, m_cube.z+m_cube.depth);
|
||||
|
||||
case nzCorner_NearLeftTop:
|
||||
return NzVector3f(m_cube.x, m_cube.y+m_cube.height, m_cube.z+m_cube.depth);
|
||||
|
||||
case nzCorner_NearRightBottom:
|
||||
return NzVector3f(m_cube.x+m_cube.width, m_cube.y, m_cube.z+m_cube.depth);
|
||||
|
||||
case nzCorner_NearRightTop:
|
||||
return NzVector3f(m_cube.x+m_cube.width, m_cube.y+m_cube.height, m_cube.z+m_cube.depth);
|
||||
}
|
||||
|
||||
NazaraError("Corner not handled (0x" + NzString::Number(corner, 16) + ')');
|
||||
return NzVector3f();
|
||||
}
|
||||
|
||||
NzCubef NzAxisAlignedBox::GetCube() const
|
||||
{
|
||||
return m_cube;
|
||||
}
|
||||
|
||||
nzExtend NzAxisAlignedBox::GetExtend() const
|
||||
{
|
||||
return m_extend;
|
||||
@@ -99,12 +150,12 @@ nzExtend NzAxisAlignedBox::GetExtend() const
|
||||
|
||||
NzVector3f NzAxisAlignedBox::GetMaximum() const
|
||||
{
|
||||
return NzVector3f(m_cube.x+m_cube.width, m_cube.y+m_cube.height, m_cube.z+m_cube.depth);
|
||||
return m_cube.GetPosition() + m_cube.GetSize();
|
||||
}
|
||||
|
||||
NzVector3f NzAxisAlignedBox::GetMinimum() const
|
||||
{
|
||||
return NzVector3f(m_cube.x, m_cube.y, m_cube.z);
|
||||
return m_cube.GetPosition();
|
||||
}
|
||||
|
||||
bool NzAxisAlignedBox::IsFinite() const
|
||||
@@ -155,6 +206,22 @@ NzString NzAxisAlignedBox::ToString() const
|
||||
return "NzAxisAlignedBox(ERROR)";
|
||||
}
|
||||
|
||||
void NzAxisAlignedBox::Transform(const NzMatrix4f& matrix)
|
||||
{
|
||||
if (m_extend != nzExtend_Finite)
|
||||
return;
|
||||
|
||||
NzVector3f center = matrix.Transform(m_cube.GetCenter(), 0.f); // 0.f pour annuler la translation
|
||||
NzVector3f halfSize = m_cube.GetSize() * 0.5f;
|
||||
|
||||
halfSize.Set(std::fabs(matrix(0,0))*halfSize.x + std::fabs(matrix(1,0))*halfSize.y + std::fabs(matrix(2,0))*halfSize.z,
|
||||
std::fabs(matrix(0,1))*halfSize.x + std::fabs(matrix(1,1))*halfSize.y + std::fabs(matrix(2,1))*halfSize.z,
|
||||
std::fabs(matrix(0,2))*halfSize.x + std::fabs(matrix(1,2))*halfSize.y + std::fabs(matrix(2,2))*halfSize.z);
|
||||
|
||||
|
||||
m_cube.Set(center - halfSize, center + halfSize);
|
||||
}
|
||||
|
||||
NzAxisAlignedBox::operator NzString() const
|
||||
{
|
||||
return ToString();
|
||||
|
||||
@@ -88,26 +88,23 @@ bool NzBuffer::Create(unsigned int length, nzUInt8 typeSize, nzBufferStorage sto
|
||||
{
|
||||
Destroy();
|
||||
|
||||
// On tente d'abord de faire un buffer hardware, si supporté
|
||||
if (s_bufferFunctions[storage])
|
||||
{
|
||||
NzBufferImpl* impl = s_bufferFunctions[storage](this, m_type);
|
||||
if (!impl->Create(length*typeSize, usage))
|
||||
{
|
||||
NazaraError("Failed to create buffer");
|
||||
delete impl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
m_impl = impl;
|
||||
}
|
||||
else
|
||||
// Notre buffer est-il supporté ?
|
||||
if (!s_bufferFunctions[storage])
|
||||
{
|
||||
NazaraError("Buffer storage not supported");
|
||||
return false;
|
||||
}
|
||||
|
||||
NzBufferImpl* impl = s_bufferFunctions[storage](this, m_type);
|
||||
if (!impl->Create(length*typeSize, usage))
|
||||
{
|
||||
NazaraError("Failed to create buffer");
|
||||
delete impl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
m_impl = impl;
|
||||
m_length = length;
|
||||
m_typeSize = typeSize;
|
||||
m_storage = storage;
|
||||
|
||||
@@ -34,6 +34,7 @@ bool NzCursor::Create(const NzImage& cursor, int hotSpotX, int hotSpotY)
|
||||
NazaraError("Failed to create cursor implementation");
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -50,6 +51,7 @@ void NzCursor::Destroy()
|
||||
if (m_impl)
|
||||
{
|
||||
m_impl->Destroy();
|
||||
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ bool NzIcon::Create(const NzImage& icon)
|
||||
NazaraError("Failed to create icon implementation");
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -45,6 +46,7 @@ void NzIcon::Destroy()
|
||||
if (m_impl)
|
||||
{
|
||||
m_impl->Destroy();
|
||||
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
|
||||
52
src/Nazara/Utility/Joint.cpp
Normal file
52
src/Nazara/Utility/Joint.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Joint.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzJoint::NzJoint(NzSkeleton* skeleton) :
|
||||
m_skeleton(skeleton)
|
||||
{
|
||||
}
|
||||
|
||||
NzJoint::NzJoint(const NzJoint& joint) :
|
||||
NzNode(joint),
|
||||
m_inverseBindMatrix(joint.m_inverseBindMatrix),
|
||||
m_name(joint.m_name),
|
||||
m_skeleton(joint.m_skeleton)
|
||||
{
|
||||
}
|
||||
|
||||
NzMatrix4f NzJoint::GetInverseBindMatrix() const
|
||||
{
|
||||
return m_inverseBindMatrix;
|
||||
}
|
||||
|
||||
NzString NzJoint::GetName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
NzSkeleton* NzJoint::GetSkeleton()
|
||||
{
|
||||
return m_skeleton;
|
||||
}
|
||||
|
||||
const NzSkeleton* NzJoint::GetSkeleton() const
|
||||
{
|
||||
return m_skeleton;
|
||||
}
|
||||
|
||||
void NzJoint::SetInverseBindMatrix(const NzMatrix4f& matrix)
|
||||
{
|
||||
m_inverseBindMatrix = matrix;
|
||||
}
|
||||
|
||||
void NzJoint::SetName(const NzString& name)
|
||||
{
|
||||
m_name = name;
|
||||
|
||||
m_skeleton->InvalidateJointMap();
|
||||
}
|
||||
@@ -3,11 +3,456 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/KeyframeMesh.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <vector>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
struct NzKeyframeMeshImpl
|
||||
{
|
||||
nzBufferAccess lockAccess;
|
||||
NzAxisAlignedBox* aabb;
|
||||
NzMeshVertex* lockBuffer;
|
||||
NzVector3f* normals;
|
||||
NzVector3f* positions;
|
||||
NzVector3f* tangents;
|
||||
const NzIndexBuffer* indexBuffer = nullptr;
|
||||
NzVertexBuffer* vertexBuffer;
|
||||
unsigned int frameCount;
|
||||
unsigned int lockLevel = 0;
|
||||
};
|
||||
|
||||
NzKeyframeMesh::NzKeyframeMesh(const NzMesh* parent) :
|
||||
NzSubMesh(parent)
|
||||
{
|
||||
}
|
||||
|
||||
NzKeyframeMesh::~NzKeyframeMesh() = default;
|
||||
NzKeyframeMesh::~NzKeyframeMesh()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzKeyframeMesh::Create(NzVertexBuffer* vertexBuffer, unsigned int frameCount, bool lock)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!vertexBuffer)
|
||||
{
|
||||
NazaraError("Invalid vertex buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (vertexBuffer->GetTypeSize() < sizeof(NzMeshVertex))
|
||||
{
|
||||
NazaraError("Vertex buffer type size must be greater or equal than size of mesh vertex");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (frameCount == 0)
|
||||
{
|
||||
NazaraError("Frame count must be over 0");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
vertexBuffer->AddResourceReference();
|
||||
|
||||
m_impl = new NzKeyframeMeshImpl;
|
||||
m_impl->aabb = new NzAxisAlignedBox[frameCount+1]; // La première case représente l'AABB interpolée
|
||||
m_impl->frameCount = frameCount;
|
||||
m_impl->vertexBuffer = vertexBuffer;
|
||||
|
||||
unsigned int vertexCount = vertexBuffer->GetVertexCount();
|
||||
|
||||
m_impl->positions = new NzVector3f[frameCount*vertexCount];
|
||||
m_impl->normals = new NzVector3f[frameCount*vertexCount];
|
||||
m_impl->tangents = new NzVector3f[frameCount*vertexCount];
|
||||
|
||||
if (lock && !Lock(nzBufferAccess_DiscardAndWrite))
|
||||
{
|
||||
NazaraError("Failed to lock buffer");
|
||||
Destroy();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
NotifyCreated();
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzKeyframeMesh::Destroy()
|
||||
{
|
||||
if (m_impl)
|
||||
{
|
||||
NotifyDestroy();
|
||||
|
||||
if (m_impl->indexBuffer)
|
||||
m_impl->indexBuffer->RemoveResourceReference();
|
||||
|
||||
m_impl->vertexBuffer->RemoveResourceReference();
|
||||
|
||||
delete[] m_impl->aabb;
|
||||
delete[] m_impl->normals;
|
||||
delete[] m_impl->positions;
|
||||
delete[] m_impl->tangents;
|
||||
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void NzKeyframeMesh::Finish()
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
InterpolateImpl(0, 0, 0.f);
|
||||
}
|
||||
|
||||
const NzAxisAlignedBox& NzKeyframeMesh::GetAABB() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return NzAxisAlignedBox::Null;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->aabb[0];
|
||||
}
|
||||
|
||||
nzAnimationType NzKeyframeMesh::GetAnimationType() const
|
||||
{
|
||||
return nzAnimationType_Keyframe;
|
||||
}
|
||||
|
||||
unsigned int NzKeyframeMesh::GetFrameCount() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->frameCount;
|
||||
}
|
||||
|
||||
const NzIndexBuffer* NzKeyframeMesh::GetIndexBuffer() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->indexBuffer;
|
||||
}
|
||||
|
||||
bool NzKeyframeMesh::GetVertex(NzMeshVertex* dest, unsigned int frameIndex, unsigned int vertexIndex, bool queryUV) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (frameIndex >= m_impl->frameCount)
|
||||
{
|
||||
NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')');
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount();
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (vertexIndex >= vertexCount)
|
||||
{
|
||||
NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(vertexCount) + ')');
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int index = frameIndex*vertexCount + vertexIndex;
|
||||
|
||||
dest->normal = m_impl->normals[index];
|
||||
dest->position = m_impl->positions[index];
|
||||
dest->tangent = m_impl->tangents[index];
|
||||
|
||||
if (queryUV)
|
||||
{
|
||||
if (!Lock(nzBufferAccess_ReadOnly))
|
||||
{
|
||||
NazaraError("Failed to query UV");
|
||||
return false;
|
||||
}
|
||||
|
||||
NzMeshVertex& vertex = m_impl->lockBuffer[vertexIndex];
|
||||
dest->uv = vertex.uv;
|
||||
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const NzVertexBuffer* NzKeyframeMesh::GetVertexBuffer() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->vertexBuffer;
|
||||
}
|
||||
|
||||
void NzKeyframeMesh::Interpolate(unsigned int frameA, unsigned int frameB, float interpolation)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_parent->HasAnimation())
|
||||
{
|
||||
NazaraError("Parent mesh has no animation");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int frameCount = m_parent->GetFrameCount();
|
||||
if (frameA >= frameCount)
|
||||
{
|
||||
NazaraError("Frame A is out of range (" + NzString::Number(frameA) + " >= " + NzString::Number(frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
|
||||
if (frameB >= frameCount)
|
||||
{
|
||||
NazaraError("Frame B is out of range (" + NzString::Number(frameB) + " >= " + NzString::Number(frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (interpolation < 0.f || interpolation > 1.f)
|
||||
{
|
||||
NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
InterpolateImpl(frameA, frameB, interpolation);
|
||||
|
||||
m_parent->InvalidateAABB();
|
||||
}
|
||||
|
||||
bool NzKeyframeMesh::IsAnimated() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzKeyframeMesh::IsValid()
|
||||
{
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
|
||||
bool NzKeyframeMesh::Lock(nzBufferAccess access) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (m_impl->lockLevel == 0)
|
||||
{
|
||||
m_impl->lockBuffer = reinterpret_cast<NzMeshVertex*>(m_impl->vertexBuffer->Map(access));
|
||||
if (!m_impl->lockBuffer)
|
||||
{
|
||||
NazaraError("Failed to lock vertex buffer");
|
||||
m_impl->lockLevel = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
m_impl->lockAccess = access;
|
||||
}
|
||||
else
|
||||
{
|
||||
///FIXME: Vérifier cette condition
|
||||
if (m_impl->lockAccess != access &&
|
||||
(m_impl->lockAccess != nzBufferAccess_ReadWrite || access != nzBufferAccess_WriteOnly) &&
|
||||
(m_impl->lockAccess != nzBufferAccess_ReadWrite || access != nzBufferAccess_ReadOnly) &&
|
||||
(m_impl->lockAccess != nzBufferAccess_DiscardAndWrite || access != nzBufferAccess_WriteOnly))
|
||||
{
|
||||
NazaraError("Vertex buffer already locked by an incompatible lock access");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_impl->lockLevel++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzKeyframeMesh::SetAABB(unsigned int frameIndex, const NzAxisAlignedBox& aabb)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return;
|
||||
}
|
||||
|
||||
if (frameIndex >= m_impl->frameCount)
|
||||
{
|
||||
NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl->aabb[frameIndex+1] = aabb;
|
||||
}
|
||||
|
||||
void NzKeyframeMesh::SetIndexBuffer(const NzIndexBuffer* indexBuffer)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (m_impl->indexBuffer)
|
||||
m_impl->indexBuffer->RemoveResourceReference();
|
||||
|
||||
if (indexBuffer)
|
||||
indexBuffer->AddResourceReference();
|
||||
|
||||
m_impl->indexBuffer = indexBuffer;
|
||||
}
|
||||
|
||||
bool NzKeyframeMesh::SetVertex(const NzMeshVertex& source, unsigned int frameIndex, unsigned int vertexIndex, bool setUV)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (frameIndex >= m_impl->frameCount)
|
||||
{
|
||||
NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')');
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount();
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (vertexIndex >= vertexCount)
|
||||
{
|
||||
NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(vertexCount) + ')');
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int index = frameIndex*vertexCount + vertexIndex;
|
||||
|
||||
m_impl->normals[index] = source.normal;
|
||||
m_impl->positions[index] = source.position;
|
||||
m_impl->tangents[index] = source.tangent;
|
||||
|
||||
if (setUV)
|
||||
{
|
||||
if (!Lock(nzBufferAccess_WriteOnly))
|
||||
{
|
||||
NazaraError("Failed to write UV");
|
||||
return false;
|
||||
}
|
||||
|
||||
NzMeshVertex& vertex = m_impl->lockBuffer[vertexIndex];
|
||||
vertex.uv = source.uv;
|
||||
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzKeyframeMesh::Unlock() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_impl->lockLevel == 0)
|
||||
{
|
||||
NazaraWarning("Unlock called on non-locked texture");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (--m_impl->lockLevel == 0)
|
||||
{
|
||||
if (!m_impl->vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap vertex buffer, expect mesh corruption");
|
||||
}
|
||||
}
|
||||
|
||||
void NzKeyframeMesh::InterpolateImpl(unsigned int frameA, unsigned int frameB, float interpolation)
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Keyframe mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Interpolation de l'AABB
|
||||
m_impl->aabb[0] = NzAxisAlignedBox::Lerp(m_impl->aabb[frameA+1], m_impl->aabb[frameB+1], interpolation);
|
||||
|
||||
if (!Lock(nzBufferAccess_WriteOnly))
|
||||
{
|
||||
NazaraError("Failed to lock vertex buffer");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount();
|
||||
|
||||
// Adaptation des indices
|
||||
frameA *= vertexCount;
|
||||
frameB *= vertexCount;
|
||||
|
||||
NzMeshVertex* vertex = m_impl->lockBuffer;
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
vertex->normal = NzVector3f::Lerp(m_impl->positions[frameA+i], m_impl->positions[frameB+i], interpolation);
|
||||
vertex->position = NzVector3f::Lerp(m_impl->positions[frameA+i], m_impl->positions[frameB+i], interpolation);
|
||||
vertex->tangent = NzVector3f::Lerp(m_impl->positions[frameA+i], m_impl->positions[frameB+i], interpolation);
|
||||
|
||||
vertex++;
|
||||
}
|
||||
|
||||
Unlock();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <vector>
|
||||
|
||||
struct md2_header
|
||||
{
|
||||
@@ -51,14 +50,6 @@ struct md2_triangle
|
||||
nzUInt16 texCoords[3];
|
||||
};
|
||||
|
||||
struct md2_frame
|
||||
{
|
||||
NzVector3f scale;
|
||||
NzVector3f translate;
|
||||
char name[16];
|
||||
std::vector<md2_vertex> vertices;
|
||||
};
|
||||
|
||||
extern const nzUInt32 md2Ident;
|
||||
extern const NzVector3f md2Normals[162];
|
||||
|
||||
|
||||
@@ -7,16 +7,18 @@
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Math/Basic.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Utility/KeyframeMesh.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD2/Constants.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD2/Mesh.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
bool NzLoader_MD2_Check(NzInputStream& stream, const NzMeshParams& parameters)
|
||||
bool Check(NzInputStream& stream, const NzMeshParams& parameters)
|
||||
{
|
||||
NazaraUnused(parameters);
|
||||
|
||||
@@ -24,7 +26,7 @@ namespace
|
||||
if (stream.Read(&magic[0], 2*sizeof(nzUInt32)) != 2*sizeof(nzUInt32))
|
||||
return false;
|
||||
|
||||
#if defined(NAZARA_BIG_ENDIAN)
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
NzByteSwap(&magic[0], sizeof(nzUInt32));
|
||||
NzByteSwap(&magic[1], sizeof(nzUInt32));
|
||||
#endif
|
||||
@@ -32,7 +34,7 @@ namespace
|
||||
return magic[0] == md2Ident && magic[1] == 8;
|
||||
}
|
||||
|
||||
bool NzLoader_MD2_Load(NzMesh* mesh, NzInputStream& stream, const NzMeshParams& parameters)
|
||||
bool Load(NzMesh* mesh, NzInputStream& stream, const NzMeshParams& parameters)
|
||||
{
|
||||
md2_header header;
|
||||
if (stream.Read(&header, sizeof(md2_header)) != sizeof(md2_header))
|
||||
@@ -41,7 +43,7 @@ namespace
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(NAZARA_BIG_ENDIAN)
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
NzByteSwap(&header.skinwidth, sizeof(nzUInt32));
|
||||
NzByteSwap(&header.skinheight, sizeof(nzUInt32));
|
||||
NzByteSwap(&header.framesize, sizeof(nzUInt32));
|
||||
@@ -67,16 +69,16 @@ namespace
|
||||
|
||||
/// Création du mesh
|
||||
// Animé ou statique, c'est la question
|
||||
bool animated;
|
||||
unsigned int startFrame = NzClamp(parameters.animation.startFrame, 0U, static_cast<unsigned int>(header.num_frames-1));
|
||||
unsigned int endFrame = NzClamp(parameters.animation.endFrame, 0U, static_cast<unsigned int>(header.num_frames-1));
|
||||
|
||||
if (parameters.loadAnimations && startFrame != endFrame)
|
||||
animated = true;
|
||||
bool animated = (parameters.animated && startFrame != endFrame);
|
||||
if (animated)
|
||||
mesh->CreateKeyframe();
|
||||
else
|
||||
animated = false;
|
||||
mesh->CreateStatic();
|
||||
|
||||
if (!mesh->Create((animated) ? nzAnimationType_Keyframe : nzAnimationType_Static)) // Ne devrait jamais échouer
|
||||
if (!mesh->IsValid()) // Ne devrait jamais échouer
|
||||
{
|
||||
NazaraInternalError("Failed to create mesh");
|
||||
return false;
|
||||
@@ -87,11 +89,12 @@ namespace
|
||||
{
|
||||
stream.SetCursorPos(header.offset_skins);
|
||||
{
|
||||
NzString baseDir = stream.GetDirectory();
|
||||
char skin[68];
|
||||
for (unsigned int i = 0; i < header.num_skins; ++i)
|
||||
{
|
||||
stream.Read(skin, 68*sizeof(char));
|
||||
mesh->AddSkin(skin);
|
||||
mesh->AddMaterial(baseDir + skin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,16 +103,13 @@ namespace
|
||||
if (animated)
|
||||
{
|
||||
NzAnimation* animation = new NzAnimation;
|
||||
if (animation->Create(nzAnimationType_Keyframe, endFrame-startFrame+1))
|
||||
if (animation->CreateKeyframe(endFrame-startFrame+1))
|
||||
{
|
||||
// Décodage des séquences
|
||||
NzString frameName;
|
||||
///TODO: Optimiser le calcul
|
||||
char last[16];
|
||||
|
||||
NzSequence sequence;
|
||||
sequence.framePerSecond = 10; // Par défaut pour les animations MD2
|
||||
|
||||
char name[16], last[16];
|
||||
stream.SetCursorPos(header.offset_frames + startFrame*header.framesize + offsetof(md2_frame, name));
|
||||
stream.SetCursorPos(header.offset_frames + startFrame*header.framesize + 2*sizeof(NzVector3f));
|
||||
stream.Read(last, 16*sizeof(char));
|
||||
|
||||
int pos = std::strlen(last)-1;
|
||||
@@ -122,10 +122,16 @@ namespace
|
||||
}
|
||||
last[pos+1] = '\0';
|
||||
|
||||
unsigned int numFrames = 0;
|
||||
NzSequence sequence;
|
||||
sequence.firstFrame = startFrame;
|
||||
sequence.frameCount = 0;
|
||||
sequence.frameRate = 10; // Par défaut pour les animations MD2
|
||||
sequence.name = last;
|
||||
|
||||
char name[16];
|
||||
for (unsigned int i = startFrame; i <= endFrame; ++i)
|
||||
{
|
||||
stream.SetCursorPos(header.offset_frames + i*header.framesize + offsetof(md2_frame, name));
|
||||
stream.SetCursorPos(header.offset_frames + i*header.framesize + 2*sizeof(NzVector3f));
|
||||
stream.Read(name, 16*sizeof(char));
|
||||
|
||||
pos = std::strlen(name)-1;
|
||||
@@ -140,24 +146,20 @@ namespace
|
||||
|
||||
if (std::strcmp(name, last) != 0) // Si les deux frames n'ont pas le même nom
|
||||
{
|
||||
// Alors on enregistre la séquence
|
||||
sequence.firstFrame = i-numFrames;
|
||||
sequence.lastFrame = i-1;
|
||||
sequence.name = last;
|
||||
animation->AddSequence(sequence);
|
||||
|
||||
std::strcpy(last, name);
|
||||
|
||||
numFrames = 0;
|
||||
// Alors on enregistre la séquence actuelle
|
||||
animation->AddSequence(sequence);
|
||||
|
||||
// Et on initialise la séquence suivante
|
||||
sequence.firstFrame = i;
|
||||
sequence.frameCount = 0;
|
||||
sequence.name = last;
|
||||
}
|
||||
|
||||
numFrames++;
|
||||
sequence.frameCount++;
|
||||
}
|
||||
|
||||
// On ajoute la dernière frame (Qui n'a pas été traitée par la boucle)
|
||||
sequence.firstFrame = endFrame-numFrames;
|
||||
sequence.lastFrame = endFrame;
|
||||
sequence.name = last;
|
||||
animation->AddSequence(sequence);
|
||||
|
||||
mesh->SetAnimation(animation);
|
||||
@@ -169,15 +171,118 @@ namespace
|
||||
|
||||
/// Chargement des submesh
|
||||
// Actuellement le loader ne charge qu'un submesh
|
||||
// TODO: Utiliser les commandes OpenGL pour accélérer le rendu
|
||||
NzMD2Mesh* subMesh = new NzMD2Mesh(mesh);
|
||||
if (!subMesh->Create(header, stream, parameters))
|
||||
// TODO: Utiliser les commandes OpenGL pour créer des indices et accélérer le rendu
|
||||
unsigned int frameCount = endFrame - startFrame + 1;
|
||||
unsigned int vertexCount = header.num_tris * 3;
|
||||
|
||||
std::unique_ptr<NzVertexBuffer> vertexBuffer(new NzVertexBuffer(NzMesh::GetDeclaration(), vertexCount, parameters.storage, nzBufferUsage_Dynamic));
|
||||
std::unique_ptr<NzKeyframeMesh> subMesh(new NzKeyframeMesh(mesh));
|
||||
if (!subMesh->Create(vertexBuffer.get(), frameCount))
|
||||
{
|
||||
NazaraError("Failed to create MD2 mesh");
|
||||
NazaraError("Failed to create SubMesh");
|
||||
return false;
|
||||
}
|
||||
|
||||
mesh->AddSubMesh(subMesh);
|
||||
vertexBuffer->SetPersistent(false);
|
||||
vertexBuffer.release();
|
||||
|
||||
/// Lecture des triangles
|
||||
std::vector<md2_triangle> triangles(header.num_tris);
|
||||
|
||||
stream.SetCursorPos(header.offset_tris);
|
||||
stream.Read(&triangles[0], header.num_tris*sizeof(md2_triangle));
|
||||
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
for (unsigned int i = 0; i < header.num_tris; ++i)
|
||||
{
|
||||
NzByteSwap(&triangles[i].vertices[0], sizeof(nzUInt16));
|
||||
NzByteSwap(&triangles[i].texCoords[0], sizeof(nzUInt16));
|
||||
|
||||
NzByteSwap(&triangles[i].vertices[1], sizeof(nzUInt16));
|
||||
NzByteSwap(&triangles[i].texCoords[1], sizeof(nzUInt16));
|
||||
|
||||
NzByteSwap(&triangles[i].vertices[2], sizeof(nzUInt16));
|
||||
NzByteSwap(&triangles[i].texCoords[2], sizeof(nzUInt16));
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Lecture des coordonnées de texture
|
||||
std::vector<md2_texCoord> texCoords(header.num_st);
|
||||
|
||||
// Lecture des coordonnées de texture
|
||||
stream.SetCursorPos(header.offset_st);
|
||||
stream.Read(&texCoords[0], header.num_st*sizeof(md2_texCoord));
|
||||
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
for (unsigned int i = 0; i < header.num_st; ++i)
|
||||
{
|
||||
NzByteSwap(&texCoords[i].u, sizeof(nzInt16));
|
||||
NzByteSwap(&texCoords[i].v, sizeof(nzInt16));
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Chargement des frames
|
||||
stream.SetCursorPos(header.offset_frames + header.framesize*startFrame);
|
||||
|
||||
// Pour que le modèle soit correctement aligné, on génère un quaternion que nous appliquerons à chacune des vertices
|
||||
NzQuaternionf rotationQuat = NzEulerAnglesf(-90.f, 90.f, 0.f);
|
||||
|
||||
md2_vertex* vertices = new md2_vertex[header.num_vertices];
|
||||
for (unsigned int f = 0; f < frameCount; ++f)
|
||||
{
|
||||
NzVector3f scale, translate;
|
||||
|
||||
stream.Read(scale, sizeof(NzVector3f));
|
||||
stream.Read(translate, sizeof(NzVector3f));
|
||||
stream.Read(nullptr, 16*sizeof(char));
|
||||
stream.Read(vertices, header.num_vertices*sizeof(md2_vertex));
|
||||
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
NzByteSwap(&scale.x, sizeof(float));
|
||||
NzByteSwap(&scale.y, sizeof(float));
|
||||
NzByteSwap(&scale.z, sizeof(float));
|
||||
|
||||
NzByteSwap(&translate.x, sizeof(float));
|
||||
NzByteSwap(&translate.y, sizeof(float));
|
||||
NzByteSwap(&translate.z, sizeof(float));
|
||||
#endif
|
||||
|
||||
NzAxisAlignedBox aabb;
|
||||
for (unsigned int t = 0; t < header.num_tris; ++t)
|
||||
{
|
||||
for (unsigned int v = 0; v < 3; ++v)
|
||||
{
|
||||
const md2_vertex& vert = vertices[triangles[t].vertices[v]];
|
||||
NzVector3f position = rotationQuat * NzVector3f(vert.x * scale.x + translate.x, vert.y * scale.y + translate.y, vert.z * scale.z + translate.z);
|
||||
|
||||
// On fait en sorte d'étendre l'AABB pour qu'il contienne ce sommet
|
||||
aabb.ExtendTo(position);
|
||||
|
||||
// Et on finit par copier les éléments dans le buffer
|
||||
NzMeshVertex vertex;
|
||||
vertex.normal = md2Normals[vert.n];
|
||||
vertex.position = position;
|
||||
|
||||
// On ne définit les coordonnées de texture que pour la première frame
|
||||
bool firstFrame = (f == 0);
|
||||
if (firstFrame)
|
||||
{
|
||||
const md2_texCoord& texC = texCoords[triangles[t].texCoords[v]];
|
||||
vertex.uv.Set(texC.u / static_cast<float>(header.skinwidth), 1.f - texC.v / static_cast<float>(header.skinheight));
|
||||
}
|
||||
|
||||
subMesh->SetVertex(vertex, f, vertexCount - (t*3 + v) - 1, firstFrame);
|
||||
}
|
||||
}
|
||||
|
||||
subMesh->SetAABB(f, aabb);
|
||||
}
|
||||
delete[] vertices;
|
||||
|
||||
subMesh->Unlock();
|
||||
|
||||
subMesh->SetMaterialIndex(0);
|
||||
mesh->AddSubMesh(subMesh.release());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -185,14 +290,10 @@ namespace
|
||||
|
||||
void NzLoaders_MD2_Register()
|
||||
{
|
||||
NzMD2Mesh::Initialize();
|
||||
|
||||
NzMeshLoader::RegisterLoader("md2", NzLoader_MD2_Check, NzLoader_MD2_Load);
|
||||
NzMeshLoader::RegisterLoader("md2", Check, Load);
|
||||
}
|
||||
|
||||
void NzLoaders_MD2_Unregister()
|
||||
{
|
||||
NzMeshLoader::UnregisterLoader("md2", NzLoader_MD2_Check, NzLoader_MD2_Load);
|
||||
|
||||
NzMD2Mesh::Uninitialize();
|
||||
NzMeshLoader::UnregisterLoader("md2", Check, Load);
|
||||
}
|
||||
|
||||
@@ -1,292 +0,0 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Loaders/MD2/Mesh.hpp>
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Math/Matrix4.hpp>
|
||||
#include <Nazara/Utility/IndexBuffer.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/VertexBuffer.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzMD2Mesh::NzMD2Mesh(const NzMesh* parent) :
|
||||
NzKeyframeMesh(parent),
|
||||
m_frames(nullptr),
|
||||
m_indexBuffer(nullptr),
|
||||
m_vertexBuffer(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
NzMD2Mesh::~NzMD2Mesh()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzMD2Mesh::Create(const md2_header& header, NzInputStream& stream, const NzMeshParams& parameters)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
unsigned int startFrame = NzClamp(parameters.animation.startFrame, 0U, static_cast<unsigned int>(header.num_frames-1));
|
||||
unsigned int endFrame = NzClamp(parameters.animation.endFrame, 0U, static_cast<unsigned int>(header.num_frames-1));
|
||||
|
||||
m_frameCount = endFrame - startFrame + 1;
|
||||
m_vertexCount = header.num_tris * 3;
|
||||
|
||||
/// Chargement des vertices
|
||||
std::vector<md2_texCoord> texCoords(header.num_st);
|
||||
std::vector<md2_triangle> triangles(header.num_tris);
|
||||
|
||||
// Lecture des coordonnées de texture
|
||||
stream.SetCursorPos(header.offset_st);
|
||||
stream.Read(&texCoords[0], header.num_st*sizeof(md2_texCoord));
|
||||
|
||||
#if defined(NAZARA_BIG_ENDIAN)
|
||||
for (unsigned int i = 0; i < header.num_st; ++i)
|
||||
{
|
||||
NzByteSwap(&texCoords[i].u, sizeof(nzInt16));
|
||||
NzByteSwap(&texCoords[i].v, sizeof(nzInt16));
|
||||
}
|
||||
#endif
|
||||
|
||||
stream.SetCursorPos(header.offset_tris);
|
||||
stream.Read(&triangles[0], header.num_tris*sizeof(md2_triangle));
|
||||
|
||||
#if defined(NAZARA_BIG_ENDIAN)
|
||||
for (unsigned int i = 0; i < header.num_tris; ++i)
|
||||
{
|
||||
NzByteSwap(&triangles[i].vertices[0], sizeof(nzUInt16));
|
||||
NzByteSwap(&texCoords[i].texCoords[0], sizeof(nzUInt16));
|
||||
|
||||
NzByteSwap(&triangles[i].vertices[1], sizeof(nzUInt16));
|
||||
NzByteSwap(&texCoords[i].texCoords[1], sizeof(nzUInt16));
|
||||
|
||||
NzByteSwap(&triangles[i].vertices[2], sizeof(nzUInt16));
|
||||
NzByteSwap(&texCoords[i].texCoords[2], sizeof(nzUInt16));
|
||||
}
|
||||
#endif
|
||||
|
||||
stream.SetCursorPos(header.offset_frames + header.framesize*startFrame);
|
||||
|
||||
md2_frame frame;
|
||||
frame.vertices.resize(header.num_vertices);
|
||||
|
||||
// Pour que le modèle soit correctement aligné, on génère un quaternion que nous appliquerons à chacune des vertices
|
||||
NzQuaternionf rotationQuat = NzEulerAnglesf(-90.f, 90.f, 0.f);
|
||||
//NzMatrix4f rotationMatrix = NzMatrix4f::Rotate(NzEulerAnglesf(-90.f, -90.f, 0.f));
|
||||
|
||||
unsigned int stride = s_declaration.GetStride(nzElementStream_VertexData);
|
||||
|
||||
m_frames = new Frame[m_frameCount];
|
||||
for (unsigned int i = 0; i < m_frameCount; ++i)
|
||||
{
|
||||
stream.Read(&frame.scale, sizeof(NzVector3f));
|
||||
stream.Read(&frame.translate, sizeof(NzVector3f));
|
||||
stream.Read(&frame.name, 16*sizeof(char));
|
||||
stream.Read(&frame.vertices[0], header.num_vertices*sizeof(md2_vertex));
|
||||
|
||||
#if defined(NAZARA_BIG_ENDIAN)
|
||||
NzByteSwap(&frame.scale.x, sizeof(float));
|
||||
NzByteSwap(&frame.scale.y, sizeof(float));
|
||||
NzByteSwap(&frame.scale.z, sizeof(float));
|
||||
|
||||
NzByteSwap(&frame.translate.x, sizeof(float));
|
||||
NzByteSwap(&frame.translate.y, sizeof(float));
|
||||
NzByteSwap(&frame.translate.z, sizeof(float));
|
||||
#endif
|
||||
|
||||
m_frames[i].normal = new nzUInt8[m_vertexCount]; // Nous stockons l'indice MD2 de la normale plutôt que la normale (gain d'espace)
|
||||
m_frames[i].vertices = new NzVector3f[m_vertexCount];
|
||||
|
||||
NzVector3f max, min;
|
||||
for (unsigned int t = 0; t < header.num_tris; ++t)
|
||||
{
|
||||
for (unsigned int v = 0; v < 3; ++v)
|
||||
{
|
||||
const md2_vertex& vert = frame.vertices[triangles[t].vertices[v]];
|
||||
|
||||
NzVector3f vertex = rotationQuat * NzVector3f(vert.x * frame.scale.x + frame.translate.x, vert.y * frame.scale.y + frame.translate.y, vert.z * frame.scale.z + frame.translate.z);
|
||||
|
||||
// On fait en sorte d'avoir deux vertices de délimitation, définissant un rectangle dans l'espace
|
||||
max.Maximize(vertex);
|
||||
min.Minimize(vertex);
|
||||
|
||||
// Le MD2 ne définit pas ses vertices dans le bon ordre, il nous faut donc les ajouter dans l'ordre inverse
|
||||
unsigned int index = m_vertexCount - (t*3 + v) - 1;
|
||||
m_frames[i].normal[index] = vert.n;
|
||||
m_frames[i].vertices[index] = vertex;
|
||||
}
|
||||
}
|
||||
|
||||
m_frames[i].aabb.SetExtends(min, max);
|
||||
}
|
||||
|
||||
m_indexBuffer = nullptr; // Pas d'indexbuffer pour l'instant
|
||||
m_vertexBuffer = new NzVertexBuffer(m_vertexCount, (3+3+2)*sizeof(float), parameters.storage, nzBufferUsage_Dynamic);
|
||||
|
||||
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(m_vertexBuffer->Map(nzBufferAccess_WriteOnly));
|
||||
if (!ptr)
|
||||
{
|
||||
NazaraError("Failed to map vertex buffer");
|
||||
Destroy();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// On avance jusqu'aux dernières coordonnées de texture et on les définit dans l'ordre inverse
|
||||
ptr += s_declaration.GetElement(nzElementStream_VertexData, nzElementUsage_TexCoord)->offset + stride * (m_vertexCount-1);
|
||||
for (unsigned int t = 0; t < header.num_tris; ++t)
|
||||
{
|
||||
for (unsigned int v = 0; v < 3; ++v)
|
||||
{
|
||||
const md2_texCoord& texC = texCoords[triangles[t].texCoords[v]];
|
||||
|
||||
NzVector2f* coords = reinterpret_cast<NzVector2f*>(ptr);
|
||||
coords->x = texC.u / static_cast<float>(header.skinwidth);
|
||||
coords->y = 1.f - texC.v / static_cast<float>(header.skinheight);
|
||||
|
||||
ptr -= stride;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_vertexBuffer->Unmap())
|
||||
{
|
||||
NazaraError("Failed to unmap buffer");
|
||||
Destroy();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
m_vertexBuffer->AddResourceReference();
|
||||
m_vertexBuffer->SetPersistent(false);
|
||||
|
||||
AnimateImpl(0, 0, 0.f);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzMD2Mesh::Destroy()
|
||||
{
|
||||
if (m_frames)
|
||||
{
|
||||
for (unsigned int i = 0; i < m_frameCount; ++i)
|
||||
{
|
||||
delete[] m_frames[i].normal;
|
||||
delete[] m_frames[i].vertices;
|
||||
}
|
||||
|
||||
delete[] m_frames;
|
||||
m_frames = nullptr;
|
||||
}
|
||||
|
||||
if (m_indexBuffer)
|
||||
{
|
||||
m_indexBuffer->RemoveResourceReference();
|
||||
m_indexBuffer = nullptr;
|
||||
}
|
||||
|
||||
if (m_vertexBuffer)
|
||||
{
|
||||
m_vertexBuffer->RemoveResourceReference();
|
||||
m_vertexBuffer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const NzAxisAlignedBox& NzMD2Mesh::GetAABB() const
|
||||
{
|
||||
return m_aabb;
|
||||
}
|
||||
|
||||
nzAnimationType NzMD2Mesh::GetAnimationType() const
|
||||
{
|
||||
if (m_frameCount > 1)
|
||||
return nzAnimationType_Keyframe;
|
||||
else
|
||||
return nzAnimationType_Static;
|
||||
}
|
||||
|
||||
unsigned int NzMD2Mesh::GetFrameCount() const
|
||||
{
|
||||
return m_frameCount;
|
||||
}
|
||||
|
||||
const NzIndexBuffer* NzMD2Mesh::GetIndexBuffer() const
|
||||
{
|
||||
return nullptr;
|
||||
//return m_indexBuffer;
|
||||
}
|
||||
|
||||
nzPrimitiveType NzMD2Mesh::GetPrimitiveType() const
|
||||
{
|
||||
return nzPrimitiveType_TriangleList;
|
||||
}
|
||||
|
||||
const NzVertexBuffer* NzMD2Mesh::GetVertexBuffer() const
|
||||
{
|
||||
return m_vertexBuffer;
|
||||
}
|
||||
|
||||
const NzVertexDeclaration* NzMD2Mesh::GetVertexDeclaration() const
|
||||
{
|
||||
return &s_declaration;
|
||||
}
|
||||
|
||||
void NzMD2Mesh::Initialize()
|
||||
{
|
||||
NzVertexElement elements[3];
|
||||
elements[0].offset = 0;
|
||||
elements[0].type = nzElementType_Float3;
|
||||
elements[0].usage = nzElementUsage_Position;
|
||||
|
||||
elements[1].offset = 3*sizeof(float);
|
||||
elements[1].type = nzElementType_Float3;
|
||||
elements[1].usage = nzElementUsage_Normal;
|
||||
|
||||
elements[2].offset = 3*sizeof(float) + 3*sizeof(float);
|
||||
elements[2].type = nzElementType_Float2;
|
||||
elements[2].usage = nzElementUsage_TexCoord;
|
||||
|
||||
s_declaration.Create(elements, 3);
|
||||
}
|
||||
|
||||
void NzMD2Mesh::Uninitialize()
|
||||
{
|
||||
s_declaration.Destroy();
|
||||
}
|
||||
|
||||
void NzMD2Mesh::AnimateImpl(unsigned int frameA, unsigned int frameB, float interpolation)
|
||||
{
|
||||
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(m_vertexBuffer->Map(nzBufferAccess_WriteOnly));
|
||||
if (!ptr)
|
||||
{
|
||||
NazaraError("Failed to map vertex buffer");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int stride = s_declaration.GetStride(nzElementStream_VertexData);
|
||||
unsigned int positionOffset = s_declaration.GetElement(nzElementStream_VertexData, nzElementUsage_Position)->offset;
|
||||
unsigned int normalOffset = s_declaration.GetElement(nzElementStream_VertexData, nzElementUsage_Normal)->offset;
|
||||
|
||||
Frame* fA = &m_frames[frameA];
|
||||
Frame* fB = &m_frames[frameB];
|
||||
for (unsigned int i = 0; i < m_vertexCount; ++i)
|
||||
{
|
||||
NzVector3f* position = reinterpret_cast<NzVector3f*>(ptr + positionOffset);
|
||||
NzVector3f* normal = reinterpret_cast<NzVector3f*>(ptr + normalOffset);
|
||||
|
||||
*position = fA->vertices[i] + interpolation * (fB->vertices[i] - fA->vertices[i]);
|
||||
*normal = md2Normals[fA->normal[i]] + interpolation * (md2Normals[fB->normal[i]] - md2Normals[fA->normal[i]]);
|
||||
|
||||
ptr += stride;
|
||||
}
|
||||
|
||||
if (!m_vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap vertex buffer, expect mesh corruption");
|
||||
|
||||
// Interpolation de l'AABB
|
||||
NzVector3f max1 = fA->aabb.GetMaximum();
|
||||
NzVector3f min1 = fA->aabb.GetMinimum();
|
||||
m_aabb.SetExtends(min1 + interpolation * (fB->aabb.GetMinimum() - min1), max1 + interpolation * (fB->aabb.GetMaximum() - max1));
|
||||
}
|
||||
|
||||
NzVertexDeclaration NzMD2Mesh::s_declaration;
|
||||
@@ -1,65 +0,0 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_LOADERS_MD2_MESH_HPP
|
||||
#define NAZARA_LOADERS_MD2_MESH_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Utility/KeyframeMesh.hpp>
|
||||
#include <Nazara/Utility/VertexDeclaration.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD2/Constants.hpp>
|
||||
|
||||
class NzIndexBuffer;
|
||||
class NzInputStream;
|
||||
class NzVertexBuffer;
|
||||
struct NzMeshParams;
|
||||
|
||||
class NAZARA_API NzMD2Mesh : public NzKeyframeMesh
|
||||
{
|
||||
public:
|
||||
NzMD2Mesh(const NzMesh* parent);
|
||||
~NzMD2Mesh();
|
||||
|
||||
bool Create(const md2_header& header, NzInputStream& stream, const NzMeshParams& parameters);
|
||||
void Destroy();
|
||||
|
||||
const NzAxisAlignedBox& GetAABB() const;
|
||||
nzAnimationType GetAnimationType() const;
|
||||
unsigned int GetFrameCount() const;
|
||||
const NzIndexBuffer* GetIndexBuffer() const;
|
||||
nzPrimitiveType GetPrimitiveType() const;
|
||||
const NzVertexBuffer* GetVertexBuffer() const;
|
||||
const NzVertexDeclaration* GetVertexDeclaration() const;
|
||||
|
||||
bool IsAnimated() const;
|
||||
|
||||
static void Initialize();
|
||||
static void Uninitialize();
|
||||
|
||||
private:
|
||||
void AnimateImpl(unsigned int frameA, unsigned int frameB, float interpolation);
|
||||
|
||||
struct Frame
|
||||
{
|
||||
NzAxisAlignedBox aabb;
|
||||
nzUInt8* normal;
|
||||
NzVector3f* tangents;
|
||||
NzVector3f* vertices;
|
||||
char name[16];
|
||||
};
|
||||
|
||||
NzAxisAlignedBox m_aabb;
|
||||
Frame* m_frames;
|
||||
NzIndexBuffer* m_indexBuffer;
|
||||
NzVertexBuffer* m_vertexBuffer;
|
||||
unsigned int m_frameCount;
|
||||
unsigned int m_vertexCount;
|
||||
|
||||
static NzVertexDeclaration s_declaration;
|
||||
};
|
||||
|
||||
#endif // NAZARA_LOADERS_MD2_MESH_HPP
|
||||
15
src/Nazara/Utility/Loaders/MD5Anim.hpp
Normal file
15
src/Nazara/Utility/Loaders/MD5Anim.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_LOADERS_MD5ANIM_HPP
|
||||
#define NAZARA_LOADERS_MD5ANIM_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
|
||||
void NzLoaders_MD5Anim_Register();
|
||||
void NzLoaders_MD5Anim_Unregister();
|
||||
|
||||
#endif // NAZARA_LOADERS_MD5ANIM_HPP
|
||||
32
src/Nazara/Utility/Loaders/MD5Anim/Loader.cpp
Normal file
32
src/Nazara/Utility/Loaders/MD5Anim/Loader.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Loaders/MD5Anim.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD5Anim/Parser.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
bool Check(NzInputStream& stream, const NzAnimationParams& parameters)
|
||||
{
|
||||
NzMD5AnimParser parser(stream, parameters);
|
||||
return parser.Check();
|
||||
}
|
||||
|
||||
bool Load(NzAnimation* animation, NzInputStream& stream, const NzAnimationParams& parameters)
|
||||
{
|
||||
NzMD5AnimParser parser(stream, parameters);
|
||||
return parser.Parse(animation);
|
||||
}
|
||||
}
|
||||
|
||||
void NzLoaders_MD5Anim_Register()
|
||||
{
|
||||
NzAnimationLoader::RegisterLoader("md5anim", Check, Load);
|
||||
}
|
||||
|
||||
void NzLoaders_MD5Anim_Unregister()
|
||||
{
|
||||
NzAnimationLoader::UnregisterLoader("md5anim", Check, Load);
|
||||
}
|
||||
529
src/Nazara/Utility/Loaders/MD5Anim/Parser.cpp
Normal file
529
src/Nazara/Utility/Loaders/MD5Anim/Parser.cpp
Normal file
@@ -0,0 +1,529 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Loaders/MD5Anim/Parser.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Math/Basic.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
#include <Nazara/Utility/StaticMesh.hpp>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzMD5AnimParser::NzMD5AnimParser(NzInputStream& stream, const NzAnimationParams& parameters) :
|
||||
m_stream(stream),
|
||||
m_parameters(parameters),
|
||||
m_keepLastLine(false),
|
||||
m_frameIndex(0),
|
||||
m_frameRate(0),
|
||||
m_lineCount(0),
|
||||
m_streamFlags(stream.GetStreamOptions())
|
||||
{
|
||||
if ((m_streamFlags & nzStreamOption_Text) == 0)
|
||||
m_stream.SetStreamOptions(m_streamFlags | nzStreamOption_Text);
|
||||
}
|
||||
|
||||
NzMD5AnimParser::~NzMD5AnimParser()
|
||||
{
|
||||
if ((m_streamFlags & nzStreamOption_Text) == 0)
|
||||
m_stream.SetStreamOptions(m_streamFlags);
|
||||
}
|
||||
|
||||
bool NzMD5AnimParser::Check()
|
||||
{
|
||||
if (!Advance(false))
|
||||
return false;
|
||||
|
||||
unsigned int version;
|
||||
if (std::sscanf(&m_currentLine[0], " MD5Version %u", &version) != 1)
|
||||
return false;
|
||||
|
||||
return version == 10;
|
||||
}
|
||||
|
||||
bool NzMD5AnimParser::Parse(NzAnimation* animation)
|
||||
{
|
||||
while (Advance(false))
|
||||
{
|
||||
switch (m_currentLine[0])
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
case 'M': // MD5Version
|
||||
if (m_currentLine.GetWord(0) != "MD5Version")
|
||||
UnrecognizedLine();
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 'b': // baseframe/bounds
|
||||
if (m_currentLine.StartsWith("baseframe {"))
|
||||
{
|
||||
if (!ParseBaseframe())
|
||||
{
|
||||
Error("Failed to parse baseframe");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (m_currentLine.StartsWith("bounds {"))
|
||||
{
|
||||
if (!ParseBounds())
|
||||
{
|
||||
Error("Failed to parse bounds");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
else
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
case 'c': // commandline
|
||||
if (m_currentLine.GetWord(0) != "commandline")
|
||||
UnrecognizedLine();
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 'f':
|
||||
{
|
||||
unsigned int index;
|
||||
if (std::sscanf(&m_currentLine[0], "frame %u {", &index) == 1)
|
||||
{
|
||||
if (m_frameIndex != index)
|
||||
{
|
||||
Error("Unexpected frame index (expected " + NzString::Number(m_frameIndex) + ", got " + NzString::Number(index) + ')');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ParseFrame())
|
||||
{
|
||||
Error("Failed to parse frame");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_frameIndex++;
|
||||
}
|
||||
else if (std::sscanf(&m_currentLine[0], "frameRate %u", &m_frameRate) != 1)
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'h': // hierarchy
|
||||
if (m_currentLine.StartsWith("hierarchy {"))
|
||||
{
|
||||
if (!ParseHierarchy())
|
||||
{
|
||||
Error("Failed to parse hierarchy");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
else
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
|
||||
case 'n': // num[Frames/Joints]
|
||||
{
|
||||
unsigned int count;
|
||||
if (std::sscanf(&m_currentLine[0], "numAnimatedComponents %u", &count) == 1)
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!m_animatedComponents.empty())
|
||||
Warning("Animated components count is already defined");
|
||||
#endif
|
||||
|
||||
m_animatedComponents.resize(count);
|
||||
}
|
||||
else if (std::sscanf(&m_currentLine[0], "numFrames %u", &count) == 1)
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!m_frames.empty())
|
||||
Warning("Frame count is already defined");
|
||||
#endif
|
||||
|
||||
m_frames.resize(count);
|
||||
}
|
||||
else if (std::sscanf(&m_currentLine[0], "numJoints %u", &count) == 1)
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!m_joints.empty())
|
||||
Warning("Joint count is already defined");
|
||||
#endif
|
||||
|
||||
m_joints.resize(count);
|
||||
}
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
else
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int frameCount = m_frames.size();
|
||||
if (frameCount == 0)
|
||||
{
|
||||
NazaraError("Frame count is invalid or missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int jointCount = m_joints.size();
|
||||
if (jointCount == 0)
|
||||
{
|
||||
NazaraError("Joint count is invalid or missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_frameIndex != frameCount)
|
||||
{
|
||||
NazaraError("Missing frame infos: [" + NzString::Number(m_frameIndex) + ',' + NzString::Number(frameCount) + ']');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_frameRate == 0)
|
||||
{
|
||||
NazaraWarning("Framerate is either invalid or missing, assuming a default value of 24");
|
||||
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().SubstrFrom(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.rotation = m_frames[j].joints[i].orient;
|
||||
else
|
||||
sequenceJoint.rotation = rotationQuat * m_frames[j].joints[i].orient;
|
||||
|
||||
sequenceJoint.scale = NzVector3f(1.f, 1.f, 1.f);
|
||||
|
||||
if (parent >= 0)
|
||||
sequenceJoint.translation = m_frames[j].joints[i].pos;
|
||||
else
|
||||
sequenceJoint.translation = rotationQuat * m_frames[j].joints[i].pos;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMD5AnimParser::Advance(bool required)
|
||||
{
|
||||
if (!m_keepLastLine)
|
||||
{
|
||||
do
|
||||
{
|
||||
if (m_stream.EndOfStream())
|
||||
{
|
||||
if (required)
|
||||
Error("Incomplete MD5 file");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
m_lineCount++;
|
||||
|
||||
m_currentLine = m_stream.ReadLine();
|
||||
m_currentLine = m_currentLine.SubstrTo("//"); // On ignore les commentaires
|
||||
m_currentLine.Simplify(); // Pour un traitement plus simple
|
||||
}
|
||||
while (m_currentLine.IsEmpty());
|
||||
}
|
||||
else
|
||||
m_keepLastLine = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzMD5AnimParser::Error(const NzString& message)
|
||||
{
|
||||
NazaraError(message + " at line #" + NzString::Number(m_lineCount));
|
||||
}
|
||||
|
||||
bool NzMD5AnimParser::ParseBaseframe()
|
||||
{
|
||||
unsigned int jointCount = m_joints.size();
|
||||
if (jointCount == 0)
|
||||
{
|
||||
Error("Joint count is invalid or missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < jointCount; ++i)
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
if (std::sscanf(&m_currentLine[0], "( %f %f %f ) ( %f %f %f )", &m_joints[i].bindPos.x, &m_joints[i].bindPos.y, &m_joints[i].bindPos.z,
|
||||
&m_joints[i].bindOrient.x, &m_joints[i].bindOrient.y, &m_joints[i].bindOrient.z) != 6)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
if (m_currentLine != '}')
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
Warning("Bounds braces closing not found");
|
||||
#endif
|
||||
|
||||
// On tente de survivre à l'erreur
|
||||
m_keepLastLine = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMD5AnimParser::ParseBounds()
|
||||
{
|
||||
unsigned int frameCount = m_frames.size();
|
||||
if (frameCount == 0)
|
||||
{
|
||||
Error("Frame count is invalid or missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < frameCount; ++i)
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
NzVector3f min, max;
|
||||
if (std::sscanf(&m_currentLine[0], "( %f %f %f ) ( %f %f %f )", &min.x, &min.y, &min.z, &max.x, &max.y, &max.z) != 6)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_frames[i].aabb.SetExtends(min, max);
|
||||
}
|
||||
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
if (m_currentLine != '}')
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
Warning("Bounds braces closing not found");
|
||||
#endif
|
||||
|
||||
// On tente de survivre à l'erreur
|
||||
m_keepLastLine = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMD5AnimParser::ParseFrame()
|
||||
{
|
||||
unsigned int animatedComponentsCount = m_animatedComponents.size();
|
||||
if (animatedComponentsCount == 0)
|
||||
{
|
||||
Error("Animated components count is missing or invalid");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int jointCount = m_joints.size();
|
||||
if (jointCount == 0)
|
||||
{
|
||||
Error("Joint count is invalid or missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
NzString line;
|
||||
|
||||
unsigned int count = 0;
|
||||
do
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
unsigned int index = 0;
|
||||
unsigned int size = m_currentLine.GetSize();
|
||||
do
|
||||
{
|
||||
float f;
|
||||
int read;
|
||||
if (std::sscanf(&m_currentLine[index], "%f%n", &f, &read) != 1)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
index += read;
|
||||
|
||||
m_animatedComponents[count] = f;
|
||||
|
||||
count++;
|
||||
}
|
||||
while (index < size);
|
||||
}
|
||||
while (count < animatedComponentsCount);
|
||||
|
||||
m_frames[m_frameIndex].joints.resize(jointCount);
|
||||
|
||||
for (unsigned int i = 0; i < jointCount; ++i)
|
||||
{
|
||||
NzQuaternionf jointOrient = m_joints[i].bindOrient;
|
||||
NzVector3f jointPos = m_joints[i].bindPos;
|
||||
unsigned int j = 0;
|
||||
|
||||
if (m_joints[i].flags & 1) // Px
|
||||
jointPos.x = m_animatedComponents[m_joints[i].index + j++];
|
||||
|
||||
if (m_joints[i].flags & 2) // Py
|
||||
jointPos.y = m_animatedComponents[m_joints[i].index + j++];
|
||||
|
||||
if (m_joints[i].flags & 4) // Pz
|
||||
jointPos.z = m_animatedComponents[m_joints[i].index + j++];
|
||||
|
||||
if (m_joints[i].flags & 8) // Qx
|
||||
jointOrient.x = m_animatedComponents[m_joints[i].index + j++];
|
||||
|
||||
if (m_joints[i].flags & 16) // Qy
|
||||
jointOrient.y = m_animatedComponents[m_joints[i].index + j++];
|
||||
|
||||
if (m_joints[i].flags & 32) // Qz
|
||||
jointOrient.z = m_animatedComponents[m_joints[i].index + j++];
|
||||
|
||||
jointOrient.ComputeW();
|
||||
|
||||
m_frames[m_frameIndex].joints[i].orient = jointOrient;
|
||||
m_frames[m_frameIndex].joints[i].pos = jointPos;
|
||||
}
|
||||
|
||||
if (!Advance(false))
|
||||
return true;
|
||||
|
||||
if (m_currentLine != '}')
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
Warning("Hierarchy braces closing not found");
|
||||
#endif
|
||||
|
||||
// On tente de survivre à l'erreur
|
||||
m_keepLastLine = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMD5AnimParser::ParseHierarchy()
|
||||
{
|
||||
unsigned int jointCount = m_joints.size();
|
||||
if (jointCount == 0)
|
||||
{
|
||||
Error("Joint count is invalid or missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < jointCount; ++i)
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
unsigned int pos = m_currentLine.Find(' ');
|
||||
if (pos == NzString::npos)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pos >= 64)
|
||||
{
|
||||
NazaraError("Joint name is too long (>= 64 characters)");
|
||||
return false;
|
||||
}
|
||||
|
||||
char name[64];
|
||||
if (std::sscanf(&m_currentLine[0], "%s %d %u %u", &name[0], &m_joints[i].parent, &m_joints[i].flags, &m_joints[i].index) != 4)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_joints[i].name = name;
|
||||
m_joints[i].name.Trim('"');
|
||||
|
||||
int parent = m_joints[i].parent;
|
||||
if (parent >= 0)
|
||||
{
|
||||
if (static_cast<unsigned int>(parent) >= jointCount)
|
||||
{
|
||||
Error("Joint's parent is out of bounds (" + NzString::Number(parent) + " >= " + NzString::Number(jointCount) + ')');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
if (m_currentLine != '}')
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
Warning("Hierarchy braces closing not found");
|
||||
#endif
|
||||
|
||||
// On tente de survivre à l'erreur
|
||||
m_keepLastLine = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzMD5AnimParser::Warning(const NzString& message)
|
||||
{
|
||||
NazaraWarning(message + " at line #" + NzString::Number(m_lineCount));
|
||||
}
|
||||
|
||||
void NzMD5AnimParser::UnrecognizedLine(bool error)
|
||||
{
|
||||
NzString message = "Unrecognized \"" + m_currentLine + '"';
|
||||
|
||||
if (error)
|
||||
Error(message);
|
||||
else
|
||||
Warning(message);
|
||||
}
|
||||
72
src/Nazara/Utility/Loaders/MD5Anim/Parser.hpp
Normal file
72
src/Nazara/Utility/Loaders/MD5Anim/Parser.hpp
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_LOADERS_MD5ANIM_PARSER_HPP
|
||||
#define NAZARA_LOADERS_MD5ANIM_PARSER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Utility/Animation.hpp>
|
||||
#include <Nazara/Utility/AxisAlignedBox.hpp>
|
||||
#include <vector>
|
||||
|
||||
class NzMD5AnimParser
|
||||
{
|
||||
public:
|
||||
NzMD5AnimParser(NzInputStream& stream, const NzAnimationParams& parameters);
|
||||
~NzMD5AnimParser();
|
||||
|
||||
bool Check();
|
||||
bool Parse(NzAnimation* animation);
|
||||
|
||||
private:
|
||||
struct Frame
|
||||
{
|
||||
struct Joint
|
||||
{
|
||||
NzQuaternionf orient;
|
||||
NzVector3f pos;
|
||||
};
|
||||
|
||||
std::vector<Joint> joints;
|
||||
NzAxisAlignedBox aabb;
|
||||
};
|
||||
|
||||
struct Joint
|
||||
{
|
||||
NzQuaternionf bindOrient;
|
||||
NzString name;
|
||||
NzVector3f bindPos;
|
||||
int parent;
|
||||
unsigned int flags;
|
||||
unsigned int index;
|
||||
};
|
||||
|
||||
bool Advance(bool required = true);
|
||||
void Error(const NzString& message);
|
||||
bool ParseBaseframe();
|
||||
bool ParseBounds();
|
||||
bool ParseFrame();
|
||||
bool ParseHierarchy();
|
||||
void Warning(const NzString& message);
|
||||
void UnrecognizedLine(bool error = false);
|
||||
|
||||
std::vector<float> m_animatedComponents;
|
||||
std::vector<Frame> m_frames;
|
||||
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;
|
||||
unsigned int m_lineCount;
|
||||
unsigned int m_streamFlags;
|
||||
};
|
||||
|
||||
#endif // NAZARA_LOADERS_MD5ANIM_PARSER_HPP
|
||||
15
src/Nazara/Utility/Loaders/MD5Mesh.hpp
Normal file
15
src/Nazara/Utility/Loaders/MD5Mesh.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_LOADERS_MD5MESH_HPP
|
||||
#define NAZARA_LOADERS_MD5MESH_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
|
||||
void NzLoaders_MD5Mesh_Register();
|
||||
void NzLoaders_MD5Mesh_Unregister();
|
||||
|
||||
#endif // NAZARA_LOADERS_MD5MESH_HPP
|
||||
32
src/Nazara/Utility/Loaders/MD5Mesh/Loader.cpp
Normal file
32
src/Nazara/Utility/Loaders/MD5Mesh/Loader.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Loaders/MD5Mesh.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD5Mesh/Parser.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
bool Check(NzInputStream& stream, const NzMeshParams& parameters)
|
||||
{
|
||||
NzMD5MeshParser parser(stream, parameters);
|
||||
return parser.Check();
|
||||
}
|
||||
|
||||
bool Load(NzMesh* mesh, NzInputStream& stream, const NzMeshParams& parameters)
|
||||
{
|
||||
NzMD5MeshParser parser(stream, parameters);
|
||||
return parser.Parse(mesh);
|
||||
}
|
||||
}
|
||||
|
||||
void NzLoaders_MD5Mesh_Register()
|
||||
{
|
||||
NzMeshLoader::RegisterLoader("md5mesh", Check, Load);
|
||||
}
|
||||
|
||||
void NzLoaders_MD5Mesh_Unregister()
|
||||
{
|
||||
NzMeshLoader::UnregisterLoader("md5mesh", Check, Load);
|
||||
}
|
||||
756
src/Nazara/Utility/Loaders/MD5Mesh/Parser.cpp
Normal file
756
src/Nazara/Utility/Loaders/MD5Mesh/Parser.cpp
Normal file
@@ -0,0 +1,756 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Loaders/MD5Mesh/Parser.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Math/Basic.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/StaticMesh.hpp>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzMD5MeshParser::NzMD5MeshParser(NzInputStream& stream, const NzMeshParams& parameters) :
|
||||
m_stream(stream),
|
||||
m_parameters(parameters),
|
||||
m_keepLastLine(false),
|
||||
m_lineCount(0),
|
||||
m_meshIndex(0),
|
||||
m_streamFlags(stream.GetStreamOptions())
|
||||
{
|
||||
if ((m_streamFlags & nzStreamOption_Text) == 0)
|
||||
m_stream.SetStreamOptions(m_streamFlags | nzStreamOption_Text);
|
||||
}
|
||||
|
||||
NzMD5MeshParser::~NzMD5MeshParser()
|
||||
{
|
||||
if ((m_streamFlags & nzStreamOption_Text) == 0)
|
||||
m_stream.SetStreamOptions(m_streamFlags);
|
||||
}
|
||||
|
||||
bool NzMD5MeshParser::Check()
|
||||
{
|
||||
if (!Advance(false))
|
||||
return false;
|
||||
|
||||
unsigned int version;
|
||||
if (std::sscanf(&m_currentLine[0], "MD5Version %u", &version) != 1)
|
||||
return false;
|
||||
|
||||
return version == 10;
|
||||
}
|
||||
|
||||
bool NzMD5MeshParser::Parse(NzMesh* mesh)
|
||||
{
|
||||
while (Advance(false))
|
||||
{
|
||||
switch (m_currentLine[0])
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
case 'M': // MD5Version
|
||||
if (m_currentLine.GetWord(0) != "MD5Version")
|
||||
UnrecognizedLine();
|
||||
break;
|
||||
|
||||
case 'c': // commandline
|
||||
if (m_currentLine.GetWord(0) != "commandline")
|
||||
UnrecognizedLine();
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 'j': // joints
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!m_currentLine.StartsWith("joints {"))
|
||||
{
|
||||
UnrecognizedLine();
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ParseJoints())
|
||||
{
|
||||
Error("Failed to parse joints");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'm': // mesh
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (m_currentLine != "mesh {")
|
||||
{
|
||||
UnrecognizedLine();
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (m_meshIndex >= m_meshes.size())
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
Warning("More meshes than registred");
|
||||
#endif
|
||||
|
||||
m_meshes.push_back(Mesh());
|
||||
}
|
||||
|
||||
if (!ParseMesh())
|
||||
{
|
||||
NazaraError("Failed to parse mesh");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_meshIndex++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'n': // num[Frames/Joints]
|
||||
{
|
||||
unsigned int count;
|
||||
if (std::sscanf(&m_currentLine[0], "numJoints %u", &count) == 1)
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!m_joints.empty())
|
||||
Warning("Joint count is already defined");
|
||||
#endif
|
||||
|
||||
m_joints.resize(count);
|
||||
}
|
||||
else if (std::sscanf(&m_currentLine[0], "numMeshes %u", &count) == 1)
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!m_meshes.empty())
|
||||
Warning("Mesh count is already defined");
|
||||
#endif
|
||||
|
||||
m_meshes.resize(count);
|
||||
}
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
else
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 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, 180.f, 0.f);
|
||||
NzString baseDir = m_stream.GetDirectory();
|
||||
|
||||
if (m_parameters.animated)
|
||||
{
|
||||
if (!mesh->CreateSkeletal(m_joints.size())) // Ne devrait jamais échouer
|
||||
{
|
||||
NazaraInternalError("Failed to create mesh");
|
||||
return false;
|
||||
}
|
||||
|
||||
NzSkeleton* skeleton = mesh->GetSkeleton();
|
||||
for (unsigned int i = 0; i < m_joints.size(); ++i)
|
||||
{
|
||||
NzJoint* joint = skeleton->GetJoint(i);
|
||||
|
||||
int parent = m_joints[i].parent;
|
||||
if (parent >= 0)
|
||||
joint->SetParent(skeleton->GetJoint(parent));
|
||||
|
||||
joint->SetName(m_joints[i].name);
|
||||
|
||||
NzMatrix4f bindMatrix;
|
||||
bindMatrix.MakeRotation((parent >= 0) ? m_joints[i].bindOrient : rotationQuat * m_joints[i].bindOrient);
|
||||
bindMatrix.SetTranslation(m_joints[i].bindPos); // Plus rapide que de multiplier par une matrice de translation
|
||||
|
||||
joint->SetInverseBindMatrix(bindMatrix.InverseAffine());
|
||||
}
|
||||
|
||||
for (const Mesh& md5Mesh : m_meshes)
|
||||
{
|
||||
void* ptr;
|
||||
unsigned int indexCount = md5Mesh.triangles.size()*3;
|
||||
unsigned int vertexCount = md5Mesh.vertices.size();
|
||||
unsigned int weightCount = md5Mesh.weights.size();
|
||||
|
||||
// Index buffer
|
||||
nzUInt8 indexSize;
|
||||
if (vertexCount > std::numeric_limits<nzUInt16>::max())
|
||||
indexSize = 4;
|
||||
else if (vertexCount > std::numeric_limits<nzUInt8>::max())
|
||||
indexSize = 2;
|
||||
else
|
||||
indexSize = 1;
|
||||
|
||||
std::unique_ptr<NzIndexBuffer> indexBuffer(new NzIndexBuffer(indexCount, indexSize, m_parameters.storage));
|
||||
if (!indexBuffer->GetBuffer()->IsValid())
|
||||
{
|
||||
NazaraError("Failed to create index buffer");
|
||||
continue;
|
||||
}
|
||||
|
||||
ptr = indexBuffer->Map(nzBufferAccess_WriteOnly);
|
||||
if (!ptr)
|
||||
{
|
||||
NazaraError("Failed to map index buffer");
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (indexSize)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
nzUInt8* index = reinterpret_cast<nzUInt8*>(ptr);
|
||||
|
||||
for (const Mesh::Triangle& triangle : md5Mesh.triangles)
|
||||
{
|
||||
// On les respécifie dans le bon ordre
|
||||
*index++ = triangle.x;
|
||||
*index++ = triangle.z;
|
||||
*index++ = triangle.y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
nzUInt16* index = reinterpret_cast<nzUInt16*>(ptr);
|
||||
|
||||
for (const Mesh::Triangle& triangle : md5Mesh.triangles)
|
||||
{
|
||||
// On les respécifie dans le bon ordre
|
||||
*index++ = triangle.x;
|
||||
*index++ = triangle.z;
|
||||
*index++ = triangle.y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
nzUInt32* index = reinterpret_cast<nzUInt32*>(ptr);
|
||||
|
||||
for (const Mesh::Triangle& triangle : md5Mesh.triangles)
|
||||
{
|
||||
// On les respécifie dans le bon ordre
|
||||
*index++ = triangle.x;
|
||||
*index++ = triangle.z;
|
||||
*index++ = triangle.y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!indexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap index buffer");
|
||||
|
||||
std::unique_ptr<NzVertexBuffer> vertexBuffer(new NzVertexBuffer(NzMesh::GetDeclaration(), vertexCount, m_parameters.storage, nzBufferUsage_Dynamic));
|
||||
|
||||
std::unique_ptr<NzSkeletalMesh> subMesh(new NzSkeletalMesh(mesh));
|
||||
if (!subMesh->Create(vertexBuffer.get(), weightCount))
|
||||
{
|
||||
NazaraError("Failed to create skeletal mesh");
|
||||
continue;
|
||||
}
|
||||
|
||||
subMesh->SetIndexBuffer(indexBuffer.get());
|
||||
indexBuffer->SetPersistent(false);
|
||||
indexBuffer.release();
|
||||
|
||||
vertexBuffer->SetPersistent(false);
|
||||
vertexBuffer.release();
|
||||
|
||||
NzWeight* weights = subMesh->GetWeight();
|
||||
for (unsigned int i = 0; i < weightCount; ++i)
|
||||
{
|
||||
weights->jointIndex = md5Mesh.weights[i].joint;
|
||||
weights->weight = md5Mesh.weights[i].bias;
|
||||
weights++;
|
||||
}
|
||||
|
||||
NzMeshVertex* bindPosVertex = reinterpret_cast<NzMeshVertex*>(subMesh->GetBindPoseBuffer());
|
||||
NzVertexWeight* vertexWeight = subMesh->GetVertexWeight();
|
||||
for (const Mesh::Vertex& vertex : md5Mesh.vertices)
|
||||
{
|
||||
// Skinning MD5 (Formule d'Id Tech)
|
||||
NzVector3f finalPos(NzVector3f::Zero());
|
||||
|
||||
vertexWeight->weights.resize(vertex.weightCount);
|
||||
for (unsigned int j = 0; j < vertex.weightCount; ++j)
|
||||
{
|
||||
const Mesh::Weight& weight = md5Mesh.weights[vertex.startWeight + j];
|
||||
const Joint& joint = m_joints[weight.joint];
|
||||
|
||||
finalPos += (joint.bindPos + joint.bindOrient*weight.pos) * weight.bias;
|
||||
vertexWeight->weights[j] = vertex.startWeight + j;
|
||||
}
|
||||
|
||||
bindPosVertex->position = finalPos;
|
||||
bindPosVertex->uv.Set(vertex.uv.x, 1.f-vertex.uv.y);
|
||||
bindPosVertex++;
|
||||
vertexWeight++;
|
||||
}
|
||||
|
||||
// Material
|
||||
if (!md5Mesh.shader.IsEmpty())
|
||||
{
|
||||
unsigned int skinIndex;
|
||||
if (mesh->AddMaterial(baseDir + md5Mesh.shader, &skinIndex))
|
||||
subMesh->SetMaterialIndex(skinIndex);
|
||||
else
|
||||
NazaraWarning("Failed to add mesh shader");
|
||||
}
|
||||
|
||||
if (!mesh->AddSubMesh(subMesh.get()))
|
||||
{
|
||||
NazaraError("Failed to add submesh");
|
||||
continue;
|
||||
}
|
||||
|
||||
subMesh.release();
|
||||
|
||||
// Animation
|
||||
// Il est peut-être éventuellement possible que la probabilité que l'animation ait le même nom soit non-nulle.
|
||||
NzString animationPath = m_stream.GetPath();
|
||||
if (!animationPath.IsEmpty())
|
||||
{
|
||||
animationPath.Replace(".md5mesh", ".md5anim", -8, NzString::CaseInsensitive);
|
||||
if (NzFile::Exists(animationPath))
|
||||
{
|
||||
std::unique_ptr<NzAnimation> animation(new NzAnimation);
|
||||
if (animation->LoadFromFile(animationPath) && mesh->SetAnimation(animation.get()))
|
||||
animation.release();
|
||||
else
|
||||
NazaraWarning("Failed to load mesh's animation");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!mesh->CreateStatic()) // Ne devrait jamais échouer
|
||||
{
|
||||
NazaraInternalError("Failed to create mesh");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const Mesh& md5Mesh : m_meshes)
|
||||
{
|
||||
void* ptr;
|
||||
unsigned int indexCount = md5Mesh.triangles.size()*3;
|
||||
unsigned int vertexCount = md5Mesh.vertices.size();
|
||||
|
||||
// Index buffer
|
||||
nzUInt8 indexSize;
|
||||
if (vertexCount > std::numeric_limits<nzUInt16>::max())
|
||||
indexSize = 4;
|
||||
else if (vertexCount > std::numeric_limits<nzUInt8>::max())
|
||||
indexSize = 2;
|
||||
else
|
||||
indexSize = 1;
|
||||
|
||||
std::unique_ptr<NzIndexBuffer> indexBuffer(new NzIndexBuffer(indexCount, indexSize, m_parameters.storage));
|
||||
if (!indexBuffer->GetBuffer()->IsValid())
|
||||
{
|
||||
NazaraError("Failed to create index buffer");
|
||||
continue;
|
||||
}
|
||||
|
||||
ptr = indexBuffer->Map(nzBufferAccess_WriteOnly);
|
||||
if (!ptr)
|
||||
{
|
||||
NazaraError("Failed to map index buffer");
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (indexSize)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
nzUInt8* index = reinterpret_cast<nzUInt8*>(ptr);
|
||||
|
||||
for (const Mesh::Triangle& triangle : md5Mesh.triangles)
|
||||
{
|
||||
// On les respécifie dans le bon ordre
|
||||
*index++ = triangle.x;
|
||||
*index++ = triangle.z;
|
||||
*index++ = triangle.y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
nzUInt16* index = reinterpret_cast<nzUInt16*>(ptr);
|
||||
|
||||
for (const Mesh::Triangle& triangle : md5Mesh.triangles)
|
||||
{
|
||||
// On les respécifie dans le bon ordre
|
||||
*index++ = triangle.x;
|
||||
*index++ = triangle.z;
|
||||
*index++ = triangle.y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
nzUInt32* index = reinterpret_cast<nzUInt32*>(ptr);
|
||||
|
||||
for (const Mesh::Triangle& triangle : md5Mesh.triangles)
|
||||
{
|
||||
// On les respécifie dans le bon ordre
|
||||
*index++ = triangle.x;
|
||||
*index++ = triangle.z;
|
||||
*index++ = triangle.y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!indexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap index buffer");
|
||||
|
||||
// Vertex buffer
|
||||
std::unique_ptr<NzVertexBuffer> vertexBuffer(new NzVertexBuffer(NzMesh::GetDeclaration(), vertexCount, m_parameters.storage, nzBufferUsage_Dynamic));
|
||||
if (!vertexBuffer->GetBuffer()->IsValid())
|
||||
{
|
||||
NazaraError("Failed to create vertex buffer");
|
||||
continue;
|
||||
}
|
||||
|
||||
ptr = vertexBuffer->Map(nzBufferAccess_WriteOnly);
|
||||
if (!ptr)
|
||||
{
|
||||
NazaraError("Failed to map vertex buffer");
|
||||
continue;
|
||||
}
|
||||
|
||||
NzAxisAlignedBox aabb;
|
||||
NzMeshVertex* vertex = reinterpret_cast<NzMeshVertex*>(ptr);
|
||||
for (const Mesh::Vertex& md5Vertex : md5Mesh.vertices)
|
||||
{
|
||||
// Skinning MD5 (Formule d'Id Tech)
|
||||
NzVector3f finalPos(NzVector3f::Zero());
|
||||
for (unsigned int j = 0; j < md5Vertex.weightCount; ++j)
|
||||
{
|
||||
const Mesh::Weight& weight = md5Mesh.weights[md5Vertex.startWeight + j];
|
||||
const Joint& joint = m_joints[weight.joint];
|
||||
|
||||
finalPos += (joint.bindPos + joint.bindOrient*weight.pos) * weight.bias;
|
||||
}
|
||||
|
||||
// On retourne le modèle dans le bon sens
|
||||
finalPos = rotationQuat * finalPos;
|
||||
aabb.ExtendTo(finalPos);
|
||||
|
||||
vertex->position = finalPos;
|
||||
vertex->uv.Set(md5Vertex.uv.x, 1.f - md5Vertex.uv.y);
|
||||
vertex++;
|
||||
}
|
||||
|
||||
if (!vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap vertex buffer");
|
||||
|
||||
// Submesh
|
||||
std::unique_ptr<NzStaticMesh> subMesh(new NzStaticMesh(mesh));
|
||||
if (!subMesh->Create(vertexBuffer.get()))
|
||||
{
|
||||
NazaraError("Failed to create static submesh");
|
||||
continue;
|
||||
}
|
||||
|
||||
vertexBuffer->SetPersistent(false);
|
||||
vertexBuffer.release();
|
||||
|
||||
subMesh->SetAABB(aabb);
|
||||
subMesh->SetIndexBuffer(indexBuffer.get());
|
||||
|
||||
indexBuffer->SetPersistent(false);
|
||||
indexBuffer.release();
|
||||
|
||||
// Material
|
||||
if (!md5Mesh.shader.IsEmpty())
|
||||
{
|
||||
unsigned int skinIndex;
|
||||
if (mesh->AddMaterial(baseDir + md5Mesh.shader, &skinIndex))
|
||||
subMesh->SetMaterialIndex(skinIndex);
|
||||
else
|
||||
NazaraWarning("Failed to add mesh shader");
|
||||
}
|
||||
|
||||
if (!mesh->AddSubMesh(subMesh.get()))
|
||||
{
|
||||
NazaraError("Failed to add submesh");
|
||||
continue;
|
||||
}
|
||||
|
||||
subMesh.release();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMD5MeshParser::Advance(bool required)
|
||||
{
|
||||
if (!m_keepLastLine)
|
||||
{
|
||||
do
|
||||
{
|
||||
if (m_stream.EndOfStream())
|
||||
{
|
||||
if (required)
|
||||
Error("Incomplete MD5 file");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
m_lineCount++;
|
||||
|
||||
m_currentLine = m_stream.ReadLine();
|
||||
m_currentLine = m_currentLine.SubstrTo("//"); // On ignore les commentaires
|
||||
m_currentLine.Simplify(); // Pour un traitement plus simple
|
||||
}
|
||||
while (m_currentLine.IsEmpty());
|
||||
}
|
||||
else
|
||||
m_keepLastLine = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzMD5MeshParser::Error(const NzString& message)
|
||||
{
|
||||
NazaraError(message + " at line #" + NzString::Number(m_lineCount));
|
||||
}
|
||||
|
||||
bool NzMD5MeshParser::ParseJoints()
|
||||
{
|
||||
unsigned int jointCount = m_joints.size();
|
||||
if (jointCount == 0)
|
||||
{
|
||||
Error("Joint count is invalid or missing");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < jointCount; ++i)
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
unsigned int pos = m_currentLine.Find(' ');
|
||||
if (pos == NzString::npos)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pos >= 64)
|
||||
{
|
||||
NazaraError("Joint name is too long (>= 64 characters)");
|
||||
return false;
|
||||
}
|
||||
|
||||
char name[64];
|
||||
if (std::sscanf(&m_currentLine[0], "%s %d ( %f %f %f ) ( %f %f %f )", &name[0], &m_joints[i].parent,
|
||||
&m_joints[i].bindPos.x, &m_joints[i].bindPos.y, &m_joints[i].bindPos.z,
|
||||
&m_joints[i].bindOrient.x, &m_joints[i].bindOrient.y, &m_joints[i].bindOrient.z) != 8)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_joints[i].name = name;
|
||||
m_joints[i].name.Trim('"');
|
||||
|
||||
int parent = m_joints[i].parent;
|
||||
if (parent >= 0)
|
||||
{
|
||||
if (static_cast<unsigned int>(parent) >= jointCount)
|
||||
{
|
||||
Error("Joint's parent is out of bounds (" + NzString::Number(parent) + " >= " + NzString::Number(jointCount) + ')');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_joints[i].bindOrient.ComputeW(); // On calcule la composante W
|
||||
}
|
||||
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
if (m_currentLine != '}')
|
||||
{
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
Warning("Hierarchy braces closing not found");
|
||||
#endif
|
||||
|
||||
// On tente de survivre à l'erreur
|
||||
m_keepLastLine = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMD5MeshParser::ParseMesh()
|
||||
{
|
||||
bool finished = false;
|
||||
while (!finished && Advance(false))
|
||||
{
|
||||
switch (m_currentLine[0])
|
||||
{
|
||||
case '}':
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case 's': // shader
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!m_currentLine.StartsWith("shader "))
|
||||
{
|
||||
UnrecognizedLine();
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_meshes[m_meshIndex].shader = m_currentLine.Substr(7);
|
||||
m_meshes[m_meshIndex].shader.Trim('"');
|
||||
break;
|
||||
|
||||
case 'n': // num[tris/verts]
|
||||
{
|
||||
unsigned int count;
|
||||
if (std::sscanf(&m_currentLine[0], "numtris %u", &count) == 1)
|
||||
{
|
||||
m_meshes[m_meshIndex].triangles.resize(count);
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
Mesh::Triangle& triangle = m_meshes[m_meshIndex].triangles[i];
|
||||
unsigned int index;
|
||||
if (std::sscanf(&m_currentLine[0], "tri %u %u %u %u", &index, &triangle.x, &triangle.y, &triangle.z) != 4)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index != i)
|
||||
{
|
||||
Error("Unexpected triangle index (expected " + NzString::Number(i) + ", got " + NzString::Number(index) + ')');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (std::sscanf(&m_currentLine[0], "numverts %u", &count) == 1)
|
||||
{
|
||||
m_meshes[m_meshIndex].vertices.resize(count);
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
Mesh::Vertex& vertex = m_meshes[m_meshIndex].vertices[i];
|
||||
unsigned int index;
|
||||
if (std::sscanf(&m_currentLine[0], "vert %u ( %f %f ) %u %u", &index, &vertex.uv.x, &vertex.uv.y, &vertex.startWeight, &vertex.weightCount) != 5)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index != i)
|
||||
{
|
||||
Error("Unexpected vertex index (expected " + NzString::Number(i) + ", got " + NzString::Number(index) + ')');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (std::sscanf(&m_currentLine[0], "numweights %u", &count) == 1)
|
||||
{
|
||||
m_meshes[m_meshIndex].weights.resize(count);
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
{
|
||||
if (!Advance())
|
||||
return false;
|
||||
|
||||
Mesh::Weight& weight = m_meshes[m_meshIndex].weights[i];
|
||||
unsigned int index;
|
||||
if (std::sscanf(&m_currentLine[0], "weight %u %u %f ( %f %f %f )", &index, &weight.joint, &weight.bias,
|
||||
&weight.pos.x, &weight.pos.y, &weight.pos.z) != 6)
|
||||
{
|
||||
UnrecognizedLine(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index != i)
|
||||
{
|
||||
Error("Unexpected weight index (expected " + NzString::Number(i) + ", got " + NzString::Number(index) + ')');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
else
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
UnrecognizedLine();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_meshes[m_meshIndex].triangles.size() == 0)
|
||||
{
|
||||
NazaraError("Mesh has no triangles");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_meshes[m_meshIndex].vertices.size() == 0)
|
||||
{
|
||||
NazaraError("Mesh has no vertices");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_meshes[m_meshIndex].weights.size() == 0)
|
||||
{
|
||||
NazaraError("Mesh has no weights");
|
||||
return false;
|
||||
}
|
||||
|
||||
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
|
||||
if (!finished)
|
||||
Warning("Mesh braces closing not found");
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzMD5MeshParser::Warning(const NzString& message)
|
||||
{
|
||||
NazaraWarning(message + " at line #" + NzString::Number(m_lineCount));
|
||||
}
|
||||
|
||||
void NzMD5MeshParser::UnrecognizedLine(bool error)
|
||||
{
|
||||
NzString message = "Unrecognized \"" + m_currentLine + '"';
|
||||
|
||||
if (error)
|
||||
Error(message);
|
||||
else
|
||||
Warning(message);
|
||||
}
|
||||
78
src/Nazara/Utility/Loaders/MD5Mesh/Parser.hpp
Normal file
78
src/Nazara/Utility/Loaders/MD5Mesh/Parser.hpp
Normal file
@@ -0,0 +1,78 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_LOADERS_MD5MESH_PARSER_HPP
|
||||
#define NAZARA_LOADERS_MD5MESH_PARSER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <vector>
|
||||
|
||||
class NzMD5MeshParser
|
||||
{
|
||||
public:
|
||||
NzMD5MeshParser(NzInputStream& stream, const NzMeshParams& parameters);
|
||||
~NzMD5MeshParser();
|
||||
|
||||
bool Check();
|
||||
bool Parse(NzMesh* mesh);
|
||||
|
||||
private:
|
||||
struct Joint
|
||||
{
|
||||
NzQuaternionf bindOrient;
|
||||
NzString name;
|
||||
NzVector3f bindPos;
|
||||
int parent;
|
||||
};
|
||||
|
||||
struct Mesh
|
||||
{
|
||||
typedef NzVector3ui Triangle;
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
NzVector2f uv;
|
||||
unsigned int startWeight;
|
||||
unsigned int weightCount;
|
||||
};
|
||||
|
||||
struct Weight
|
||||
{
|
||||
NzVector3f pos;
|
||||
float bias;
|
||||
unsigned int joint;
|
||||
};
|
||||
|
||||
std::vector<Triangle> triangles;
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<Weight> weights;
|
||||
NzString shader;
|
||||
};
|
||||
|
||||
bool Advance(bool required = true);
|
||||
void Error(const NzString& message);
|
||||
bool ParseJoints();
|
||||
bool ParseMesh();
|
||||
void Warning(const NzString& message);
|
||||
void UnrecognizedLine(bool error = false);
|
||||
|
||||
std::vector<Joint> m_joints;
|
||||
std::vector<Mesh> m_meshes;
|
||||
NzInputStream& m_stream;
|
||||
NzString m_currentLine;
|
||||
const NzMeshParams& m_parameters;
|
||||
bool m_keepLastLine;
|
||||
unsigned int m_lineCount;
|
||||
unsigned int m_meshIndex;
|
||||
unsigned int m_streamFlags;
|
||||
};
|
||||
|
||||
#endif // NAZARA_LOADERS_MD5MESH_PARSER_HPP
|
||||
@@ -37,6 +37,8 @@ namespace
|
||||
nzUInt8 padding[54];
|
||||
};
|
||||
|
||||
//static_assert(sizeof(pcx_header) == 1024, "PCX header must be 1024 bytes sized");
|
||||
|
||||
bool Check(NzInputStream& stream, const NzImageParams& parameters)
|
||||
{
|
||||
NazaraUnused(parameters);
|
||||
|
||||
@@ -6,12 +6,21 @@
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Utility/Buffer.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/KeyframeMesh.hpp>
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/SubMesh.hpp>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzMeshParams::NzMeshParams()
|
||||
{
|
||||
if (!NzBuffer::IsSupported(storage))
|
||||
storage = nzBufferStorage_Software;
|
||||
}
|
||||
|
||||
bool NzMeshParams::IsValid() const
|
||||
{
|
||||
if (!animation.IsValid())
|
||||
@@ -31,12 +40,14 @@ bool NzMeshParams::IsValid() const
|
||||
|
||||
struct NzMeshImpl
|
||||
{
|
||||
std::deque<NzString> skins;
|
||||
std::map<NzString, unsigned int> subMeshMap;
|
||||
std::vector<NzString> materials;
|
||||
std::vector<NzSubMesh*> subMeshes;
|
||||
nzAnimationType animationType;
|
||||
NzAxisAlignedBox aabb;
|
||||
NzSkeleton skeleton; // Uniquement pour les animations squelettiques
|
||||
const NzAnimation* animation = nullptr;
|
||||
unsigned int jointCount; // Uniquement pour les animations squelettiques
|
||||
};
|
||||
|
||||
NzMesh::~NzMesh()
|
||||
@@ -44,7 +55,7 @@ NzMesh::~NzMesh()
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzMesh::AddSkin(const NzString& skin, bool setDefault)
|
||||
bool NzMesh::AddMaterial(const NzString& matPath, unsigned int* matIndex)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
@@ -53,17 +64,32 @@ bool NzMesh::AddSkin(const NzString& skin, bool setDefault)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (skin.IsEmpty())
|
||||
if (matPath.IsEmpty())
|
||||
{
|
||||
NazaraError("Skin is empty");
|
||||
NazaraError("Material path is empty");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (setDefault)
|
||||
m_impl->skins.push_front(skin);
|
||||
else
|
||||
m_impl->skins.push_back(skin);
|
||||
NzString path = NzFile::NormalizeSeparators(matPath);
|
||||
|
||||
for (unsigned int i = 0; i < m_impl->materials.size(); ++i)
|
||||
{
|
||||
if (m_impl->materials[i] == path) // Ce skin est-il déjà présent ?
|
||||
{
|
||||
if (matIndex)
|
||||
*matIndex = i;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Sinon on l'ajoute
|
||||
|
||||
if (matIndex)
|
||||
*matIndex = m_impl->materials.size();
|
||||
|
||||
m_impl->materials.push_back(matPath);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -82,6 +108,12 @@ bool NzMesh::AddSubMesh(NzSubMesh* subMesh)
|
||||
NazaraError("Invalid submesh");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (subMesh->GetAnimationType() != m_impl->animationType)
|
||||
{
|
||||
NazaraError("Submesh's animation type must match mesh animation type");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
subMesh->AddResourceListener(this, m_impl->subMeshes.size());
|
||||
@@ -119,6 +151,12 @@ bool NzMesh::AddSubMesh(const NzString& identifier, NzSubMesh* subMesh)
|
||||
NazaraError("Invalid submesh");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_impl->animationType != subMesh->GetAnimationType())
|
||||
{
|
||||
NazaraError("Submesh's animation type must match mesh animation type");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
int index = m_impl->subMeshes.size();
|
||||
@@ -159,7 +197,9 @@ void NzMesh::Animate(unsigned int frameA, unsigned int frameB, float interpolati
|
||||
NazaraError("Frame B is out of range (" + NzString::Number(frameB) + " >= " + NzString::Number(frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (interpolation < 0.f || interpolation > 1.f)
|
||||
{
|
||||
NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')');
|
||||
@@ -167,18 +207,70 @@ void NzMesh::Animate(unsigned int frameA, unsigned int frameB, float interpolati
|
||||
}
|
||||
#endif
|
||||
|
||||
for (NzSubMesh* subMesh : m_impl->subMeshes)
|
||||
subMesh->AnimateImpl(frameA, frameB, interpolation);
|
||||
switch (m_impl->animationType)
|
||||
{
|
||||
case nzAnimationType_Keyframe:
|
||||
for (NzSubMesh* subMesh : m_impl->subMeshes)
|
||||
{
|
||||
NzKeyframeMesh* keyframeMesh = static_cast<NzKeyframeMesh*>(subMesh);
|
||||
keyframeMesh->Interpolate(frameA, frameB, interpolation);
|
||||
}
|
||||
break;
|
||||
|
||||
case nzAnimationType_Skeletal:
|
||||
m_impl->animation->AnimateSkeleton(&m_impl->skeleton, frameA, frameB, interpolation);
|
||||
for (NzSubMesh* subMesh : m_impl->subMeshes)
|
||||
{
|
||||
NzSkeletalMesh* skeletalMesh = static_cast<NzSkeletalMesh*>(subMesh);
|
||||
skeletalMesh->Skin(&m_impl->skeleton);
|
||||
}
|
||||
break;
|
||||
|
||||
case nzAnimationType_Static:
|
||||
// Le safe mode est censé nous protéger de cet appel
|
||||
NazaraInternalError("Static mesh has no animation, please enable safe mode");
|
||||
break;
|
||||
}
|
||||
|
||||
m_impl->aabb.SetNull(); // On invalide l'AABB
|
||||
}
|
||||
|
||||
bool NzMesh::Create(nzAnimationType type)
|
||||
bool NzMesh::CreateKeyframe()
|
||||
{
|
||||
Destroy();
|
||||
|
||||
m_impl = new NzMeshImpl;
|
||||
m_impl->animationType = type;
|
||||
m_impl->animationType = nzAnimationType_Keyframe;
|
||||
|
||||
NotifyCreated();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMesh::CreateSkeletal(unsigned int jointCount)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
m_impl = new NzMeshImpl;
|
||||
m_impl->animationType = nzAnimationType_Skeletal;
|
||||
m_impl->jointCount = jointCount;
|
||||
if (!m_impl->skeleton.Create(jointCount))
|
||||
{
|
||||
NazaraError("Failed to create skeleton");
|
||||
|
||||
delete m_impl;
|
||||
return false;
|
||||
}
|
||||
|
||||
NotifyCreated();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzMesh::CreateStatic()
|
||||
{
|
||||
Destroy();
|
||||
|
||||
m_impl = new NzMeshImpl;
|
||||
m_impl->animationType = nzAnimationType_Static;
|
||||
|
||||
NotifyCreated();
|
||||
return true;
|
||||
@@ -265,7 +357,26 @@ unsigned int NzMesh::GetFrameCount() const
|
||||
return m_impl->animation->GetFrameCount();
|
||||
}
|
||||
|
||||
NzString NzMesh::GetSkin(unsigned int index) const
|
||||
unsigned int NzMesh::GetJointCount() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Mesh not created");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (m_impl->animationType != nzAnimationType_Skeletal)
|
||||
{
|
||||
NazaraError("Mesh's animation type is not skeletal");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->jointCount;
|
||||
}
|
||||
|
||||
NzString NzMesh::GetMaterial(unsigned int index) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
@@ -274,17 +385,17 @@ NzString NzMesh::GetSkin(unsigned int index) const
|
||||
return NzString();
|
||||
}
|
||||
|
||||
if (index >= m_impl->skins.size())
|
||||
if (index >= m_impl->materials.size())
|
||||
{
|
||||
NazaraError("Skin index out of range (" + NzString::Number(index) + " >= " + NzString::Number(m_impl->skins.size()) + ')');
|
||||
NazaraError("Material index out of range (" + NzString::Number(index) + " >= " + NzString::Number(m_impl->materials.size()) + ')');
|
||||
return NzString();
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->skins[index];
|
||||
return m_impl->materials[index];
|
||||
}
|
||||
|
||||
unsigned int NzMesh::GetSkinCount() const
|
||||
unsigned int NzMesh::GetMaterialCount() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
@@ -294,7 +405,45 @@ unsigned int NzMesh::GetSkinCount() const
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->skins.size();
|
||||
return m_impl->materials.size();
|
||||
}
|
||||
|
||||
NzSkeleton* NzMesh::GetSkeleton()
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Animation not created");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (m_impl->animationType != nzAnimationType_Skeletal)
|
||||
{
|
||||
NazaraError("Mesh's animation type is not skeletal");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->skeleton;
|
||||
}
|
||||
|
||||
const NzSkeleton* NzMesh::GetSkeleton() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Animation not created");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (m_impl->animationType != nzAnimationType_Skeletal)
|
||||
{
|
||||
NazaraError("Mesh's animation type is not skeletal");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->skeleton;
|
||||
}
|
||||
|
||||
NzSubMesh* NzMesh::GetSubMesh(const NzString& identifier)
|
||||
@@ -305,18 +454,19 @@ NzSubMesh* NzMesh::GetSubMesh(const NzString& identifier)
|
||||
NazaraError("Mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto it = m_impl->subMeshMap.find(identifier);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->subMeshMap.end())
|
||||
{
|
||||
NazaraError("SubMesh not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->subMeshes[it->second];
|
||||
#else
|
||||
return m_impl->subMeshes[m_impl->subMeshMap[identifier]];
|
||||
#endif
|
||||
}
|
||||
|
||||
NzSubMesh* NzMesh::GetSubMesh(unsigned int index)
|
||||
@@ -346,18 +496,19 @@ const NzSubMesh* NzMesh::GetSubMesh(const NzString& identifier) const
|
||||
NazaraError("Mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto it = m_impl->subMeshMap.find(identifier);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->subMeshMap.end())
|
||||
{
|
||||
NazaraError("SubMesh not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->subMeshes[it->second];
|
||||
#else
|
||||
return m_impl->subMeshes[m_impl->subMeshMap[identifier]];
|
||||
#endif
|
||||
}
|
||||
|
||||
const NzSubMesh* NzMesh::GetSubMesh(unsigned int index) const
|
||||
@@ -400,18 +551,19 @@ int NzMesh::GetSubMeshIndex(const NzString& identifier) const
|
||||
NazaraError("Mesh not created");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto it = m_impl->subMeshMap.find(identifier);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->subMeshMap.end())
|
||||
{
|
||||
NazaraError("SubMesh not found");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
return it->second;
|
||||
#else
|
||||
return m_impl->subMeshMap[identifier];
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int NzMesh::GetVertexCount() const
|
||||
@@ -457,7 +609,7 @@ bool NzMesh::HasAnimation() const
|
||||
return m_impl->animation != nullptr;
|
||||
}
|
||||
|
||||
bool NzMesh::HasSkin(unsigned int index) const
|
||||
bool NzMesh::HasMaterial(unsigned int index) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
@@ -467,7 +619,7 @@ bool NzMesh::HasSkin(unsigned int index) const
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->skins.size() > index;
|
||||
return index < m_impl->materials.size();
|
||||
}
|
||||
|
||||
bool NzMesh::HasSubMesh(const NzString& identifier) const
|
||||
@@ -529,7 +681,7 @@ bool NzMesh::LoadFromStream(NzInputStream& stream, const NzMeshParams& params)
|
||||
return NzMeshLoader::LoadFromStream(this, stream, params);
|
||||
}
|
||||
|
||||
void NzMesh::RemoveSkin(unsigned int index)
|
||||
void NzMesh::RemoveMaterial(unsigned int index)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
@@ -538,18 +690,18 @@ void NzMesh::RemoveSkin(unsigned int index)
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_impl->skins.size() <= index)
|
||||
if (index >= m_impl->materials.size())
|
||||
{
|
||||
NazaraError("Skin index out of range (" + NzString::Number(index) + " >= " + NzString::Number(m_impl->skins.size()) + ')');
|
||||
NazaraError("Material index out of range (" + NzString::Number(index) + " >= " + NzString::Number(m_impl->materials.size()) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// On accède à l'itérateur correspondant à l'entrée #index
|
||||
auto it = m_impl->skins.begin();
|
||||
auto it = m_impl->materials.begin();
|
||||
std::advance(it, index);
|
||||
|
||||
m_impl->skins.erase(it);
|
||||
m_impl->materials.erase(it);
|
||||
}
|
||||
|
||||
void NzMesh::RemoveSubMesh(const NzString& identifier)
|
||||
@@ -639,6 +791,8 @@ bool NzMesh::SetAnimation(const NzAnimation* animation)
|
||||
if (animation->GetType() != m_impl->animationType)
|
||||
{
|
||||
NazaraError("Animation's type must match mesh animation type");
|
||||
m_impl->animation = nullptr;
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
@@ -651,6 +805,59 @@ bool NzMesh::SetAnimation(const NzAnimation* animation)
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzMesh::Skin(const NzSkeleton* skeleton)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Mesh not created");
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_impl->animationType != nzAnimationType_Skeletal)
|
||||
{
|
||||
NazaraError("Mesh's animation type is not skeletal");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
for (NzSubMesh* subMesh : m_impl->subMeshes)
|
||||
{
|
||||
NzSkeletalMesh* skeletalMesh = static_cast<NzSkeletalMesh*>(subMesh);
|
||||
skeletalMesh->Skin(skeleton);
|
||||
}
|
||||
}
|
||||
|
||||
const NzVertexDeclaration* NzMesh::GetDeclaration()
|
||||
{
|
||||
static NzVertexDeclaration declaration;
|
||||
|
||||
if (!declaration.IsValid())
|
||||
{
|
||||
// Déclaration correspondant à NzVertexStruct_XYZ_Normal_UV_Tangent
|
||||
NzVertexElement elements[4];
|
||||
elements[0].offset = 0;
|
||||
elements[0].type = nzElementType_Float3;
|
||||
elements[0].usage = nzElementUsage_Position;
|
||||
|
||||
elements[1].offset = 3*sizeof(float);
|
||||
elements[1].type = nzElementType_Float3;
|
||||
elements[1].usage = nzElementUsage_Normal;
|
||||
|
||||
elements[2].offset = 3*sizeof(float) + 3*sizeof(float);
|
||||
elements[2].type = nzElementType_Float2;
|
||||
elements[2].usage = nzElementUsage_TexCoord;
|
||||
|
||||
elements[3].offset = 3*sizeof(float) + 3*sizeof(float) + 2*sizeof(float);
|
||||
elements[3].type = nzElementType_Float3;
|
||||
elements[3].usage = nzElementUsage_Tangent;
|
||||
|
||||
declaration.Create(elements, 4);
|
||||
}
|
||||
|
||||
return &declaration;
|
||||
}
|
||||
|
||||
void NzMesh::OnResourceCreated(const NzResource* resource, int index)
|
||||
{
|
||||
NazaraUnused(index);
|
||||
|
||||
429
src/Nazara/Utility/Node.cpp
Normal file
429
src/Nazara/Utility/Node.cpp
Normal file
@@ -0,0 +1,429 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Node.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzNode::NzNode() :
|
||||
m_rotation(NzQuaternionf::Identity()),
|
||||
m_scale(NzVector3f(1.f, 1.f, 1.f)),
|
||||
m_translation(NzVector3f::Zero()),
|
||||
m_parent(nullptr),
|
||||
m_derivedUpdated(false),
|
||||
m_inheritRotation(true),
|
||||
m_inheritScale(true),
|
||||
m_inheritTranslation(true),
|
||||
m_matrixUpdated(false)
|
||||
{
|
||||
}
|
||||
|
||||
NzNode::NzNode(const NzNode& node) :
|
||||
m_rotation(node.m_rotation),
|
||||
m_scale(node.m_scale),
|
||||
m_translation(node.m_translation),
|
||||
m_parent(node.m_parent),
|
||||
m_derivedUpdated(false),
|
||||
m_inheritRotation(node.m_inheritRotation),
|
||||
m_inheritScale(node.m_inheritScale),
|
||||
m_inheritTranslation(node.m_inheritTranslation),
|
||||
m_matrixUpdated(false)
|
||||
{
|
||||
if (m_parent)
|
||||
m_parent->AddChild(this);
|
||||
}
|
||||
|
||||
NzNode::~NzNode()
|
||||
{
|
||||
for (NzNode* child : m_childs)
|
||||
child->m_parent = nullptr;
|
||||
|
||||
if (m_parent)
|
||||
m_parent->RemoveChild(this);
|
||||
}
|
||||
|
||||
NzQuaternionf NzNode::GetDerivedRotation() const
|
||||
{
|
||||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
return m_derivedRotation;
|
||||
}
|
||||
|
||||
NzVector3f NzNode::GetDerivedScale() const
|
||||
{
|
||||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
return m_derivedScale;
|
||||
}
|
||||
|
||||
NzVector3f NzNode::GetDerivedTranslation() const
|
||||
{
|
||||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
return m_derivedTranslation;
|
||||
}
|
||||
|
||||
bool NzNode::GetInheritRotation() const
|
||||
{
|
||||
return m_inheritRotation;
|
||||
}
|
||||
|
||||
bool NzNode::GetInheritScale() const
|
||||
{
|
||||
return m_inheritScale;
|
||||
}
|
||||
|
||||
bool NzNode::GetInheritTranslation() const
|
||||
{
|
||||
return m_inheritTranslation;
|
||||
}
|
||||
|
||||
const NzNode* NzNode::GetParent() const
|
||||
{
|
||||
return m_parent;
|
||||
}
|
||||
|
||||
NzQuaternionf NzNode::GetRotation() const
|
||||
{
|
||||
return m_rotation;
|
||||
}
|
||||
|
||||
NzVector3f NzNode::GetScale() const
|
||||
{
|
||||
return m_scale;
|
||||
}
|
||||
|
||||
NzVector3f NzNode::GetTranslation() const
|
||||
{
|
||||
return m_translation;
|
||||
}
|
||||
|
||||
NzMatrix4f NzNode::GetTransformMatrix() const
|
||||
{
|
||||
if (!m_matrixUpdated)
|
||||
UpdateMatrix();
|
||||
|
||||
return m_transformMatrix;
|
||||
}
|
||||
|
||||
NzNode& NzNode::Interpolate(const NzNode& nodeA, const NzNode& nodeB, float interpolation)
|
||||
{
|
||||
m_rotation = NzQuaternionf::Slerp(nodeA.m_rotation, nodeB.m_rotation, interpolation);
|
||||
m_scale = NzVector3f::Lerp(nodeA.m_scale, nodeB.m_scale, interpolation);
|
||||
m_translation = NzVector3f::Lerp(nodeA.m_translation, nodeB.m_translation, interpolation);
|
||||
|
||||
Invalidate();
|
||||
return *this;
|
||||
}
|
||||
|
||||
NzNode& NzNode::Rotate(const NzQuaternionf& rotation, nzCoordSys coordSys)
|
||||
{
|
||||
// Évitons toute mauvaise surprise ..
|
||||
NzQuaternionf q(rotation);
|
||||
q.Normalize();
|
||||
|
||||
switch (coordSys)
|
||||
{
|
||||
case nzCoordSys_Global:
|
||||
{
|
||||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
m_rotation *= m_derivedRotation.GetInverse() * q * m_derivedRotation;
|
||||
break;
|
||||
}
|
||||
|
||||
case nzCoordSys_Local:
|
||||
m_rotation *= q;
|
||||
break;
|
||||
}
|
||||
|
||||
m_rotation.Normalize();
|
||||
|
||||
Invalidate();
|
||||
return *this;
|
||||
}
|
||||
|
||||
NzNode& NzNode::Scale(const NzVector3f& scale)
|
||||
{
|
||||
m_scale *= scale;
|
||||
|
||||
Invalidate();
|
||||
return *this;
|
||||
}
|
||||
|
||||
NzNode& NzNode::Scale(float scale)
|
||||
{
|
||||
m_scale *= scale;
|
||||
|
||||
Invalidate();
|
||||
return *this;
|
||||
}
|
||||
|
||||
NzNode& NzNode::Scale(float scaleX, float scaleY, float scaleZ)
|
||||
{
|
||||
m_scale.x *= scaleX;
|
||||
m_scale.y *= scaleY;
|
||||
m_scale.z *= scaleZ;
|
||||
|
||||
Invalidate();
|
||||
return *this;
|
||||
}
|
||||
|
||||
NzNode& NzNode::Translate(const NzVector3f& movement, nzCoordSys coordSys)
|
||||
{
|
||||
switch (coordSys)
|
||||
{
|
||||
case nzCoordSys_Global:
|
||||
{
|
||||
if (m_parent)
|
||||
m_translation += (m_parent->GetDerivedRotation().GetInverse() * movement) / m_parent->GetDerivedScale(); // Compensation
|
||||
else
|
||||
m_translation += movement; // Rien n'affecte le node
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case nzCoordSys_Local:
|
||||
m_translation += m_scale * (m_rotation * movement);
|
||||
break;
|
||||
}
|
||||
|
||||
Invalidate();
|
||||
return *this;
|
||||
}
|
||||
|
||||
NzNode& NzNode::Translate(float moveX, float moveY, float moveZ, nzCoordSys coordSys)
|
||||
{
|
||||
return Translate(NzVector3f(moveX, moveY, moveZ), coordSys);
|
||||
}
|
||||
|
||||
void NzNode::SetInheritRotation(bool inheritRotation)
|
||||
{
|
||||
///DOC: Un appel redondant est sans effet
|
||||
if (m_inheritRotation != inheritRotation)
|
||||
{
|
||||
m_inheritRotation = inheritRotation;
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
void NzNode::SetInheritScale(bool inheritScale)
|
||||
{
|
||||
///DOC: Un appel redondant est sans effet
|
||||
if (m_inheritScale != inheritScale)
|
||||
{
|
||||
m_inheritScale = inheritScale;
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
void NzNode::SetInheritTranslation(bool inheritTranslation)
|
||||
{
|
||||
if (m_inheritTranslation != inheritTranslation)
|
||||
{
|
||||
m_inheritTranslation = inheritTranslation;
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
void NzNode::SetParent(const NzNode* node)
|
||||
{
|
||||
if (m_parent == node)
|
||||
return;
|
||||
|
||||
if (m_parent)
|
||||
m_parent->RemoveChild(this);
|
||||
|
||||
m_parent = node;
|
||||
if (m_parent)
|
||||
m_parent->AddChild(this);
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
void NzNode::SetParent(const NzNode& node)
|
||||
{
|
||||
SetParent(&node);
|
||||
}
|
||||
|
||||
void NzNode::SetRotation(const NzQuaternionf& rotation, nzCoordSys coordSys)
|
||||
{
|
||||
// Évitons toute mauvaise surprise ..
|
||||
NzQuaternionf q(rotation);
|
||||
q.Normalize();
|
||||
|
||||
switch (coordSys)
|
||||
{
|
||||
case nzCoordSys_Global:
|
||||
{
|
||||
if (m_parent)
|
||||
{
|
||||
m_rotation = q * m_parent->GetDerivedRotation().GetInverse(); ///FIXME: Vérifier si le résultat est correct
|
||||
m_rotation.Normalize();
|
||||
}
|
||||
else
|
||||
m_rotation = q;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case nzCoordSys_Local:
|
||||
m_rotation = q;
|
||||
break;
|
||||
}
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
void NzNode::SetScale(const NzVector3f& scale, nzCoordSys coordSys)
|
||||
{
|
||||
switch (coordSys)
|
||||
{
|
||||
case nzCoordSys_Global:
|
||||
{
|
||||
if (m_parent)
|
||||
m_scale = scale / m_parent->GetDerivedScale();
|
||||
else
|
||||
m_scale = scale;
|
||||
break;
|
||||
}
|
||||
|
||||
case nzCoordSys_Local:
|
||||
m_scale = scale;
|
||||
break;
|
||||
}
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
void NzNode::SetScale(float scale, nzCoordSys coordSys)
|
||||
{
|
||||
SetScale(NzVector3f(scale), coordSys);
|
||||
}
|
||||
|
||||
void NzNode::SetScale(float scaleX, float scaleY, float scaleZ, nzCoordSys coordSys)
|
||||
{
|
||||
SetScale(NzVector3f(scaleX, scaleY, scaleZ), coordSys);
|
||||
}
|
||||
|
||||
void NzNode::SetTranslation(const NzVector3f& translation, nzCoordSys coordSys)
|
||||
{
|
||||
switch (coordSys)
|
||||
{
|
||||
case nzCoordSys_Global:
|
||||
{
|
||||
if (m_parent)
|
||||
m_translation = translation - m_parent->GetDerivedTranslation();
|
||||
else
|
||||
m_translation = translation;
|
||||
break;
|
||||
}
|
||||
|
||||
case nzCoordSys_Local:
|
||||
m_translation = translation;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void NzNode::SetTranslation(float translationX, float translationY, float translationZ, nzCoordSys coordSys)
|
||||
{
|
||||
SetTranslation(NzVector3f(translationX, translationY, translationZ), coordSys);
|
||||
}
|
||||
|
||||
NzNode& NzNode::operator=(const NzNode& node)
|
||||
{
|
||||
SetParent(node.m_parent);
|
||||
|
||||
m_inheritRotation = node.m_inheritRotation;
|
||||
m_inheritScale = node.m_inheritScale;
|
||||
m_inheritTranslation = node.m_inheritTranslation;
|
||||
m_rotation = node.m_rotation;
|
||||
m_scale = node.m_scale;
|
||||
m_translation = node.m_translation;
|
||||
|
||||
Invalidate();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void NzNode::AddChild(NzNode* node) const
|
||||
{
|
||||
auto pair = m_childs.insert(node);
|
||||
|
||||
if (pair.second)
|
||||
node->Invalidate();
|
||||
#ifdef NAZARA_DEBUG
|
||||
else
|
||||
NazaraWarning("Child already in set");
|
||||
#endif
|
||||
}
|
||||
|
||||
void NzNode::Invalidate()
|
||||
{
|
||||
m_derivedUpdated = false;
|
||||
m_matrixUpdated = false;
|
||||
|
||||
for (NzNode* node : m_childs)
|
||||
node->Invalidate();
|
||||
}
|
||||
|
||||
void NzNode::RemoveChild(NzNode* node) const
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (m_childs.erase(node) == 0)
|
||||
NazaraWarning("Child not found in set");
|
||||
#else
|
||||
m_childs.erase(node);
|
||||
#endif
|
||||
}
|
||||
|
||||
void NzNode::UpdateDerived() const
|
||||
{
|
||||
if (m_parent)
|
||||
{
|
||||
if (!m_parent->m_derivedUpdated)
|
||||
m_parent->UpdateDerived();
|
||||
|
||||
if (m_inheritRotation)
|
||||
{
|
||||
m_derivedRotation = m_parent->m_derivedRotation * m_rotation;
|
||||
m_derivedRotation.Normalize();
|
||||
}
|
||||
else
|
||||
m_derivedRotation = m_rotation;
|
||||
|
||||
m_derivedScale = m_scale;
|
||||
if (m_inheritScale)
|
||||
m_derivedScale *= m_parent->m_derivedScale;
|
||||
|
||||
if (m_inheritTranslation)
|
||||
m_derivedTranslation = m_parent->m_derivedRotation*(m_parent->m_derivedScale * m_translation) + m_parent->m_derivedTranslation;
|
||||
else
|
||||
m_derivedTranslation = m_translation;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_derivedRotation = m_rotation;
|
||||
m_derivedScale = m_scale;
|
||||
m_derivedTranslation = m_translation;
|
||||
}
|
||||
|
||||
m_derivedUpdated = true;
|
||||
}
|
||||
|
||||
void NzNode::UpdateMatrix() const
|
||||
{
|
||||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
m_transformMatrix.MakeTransform(m_derivedTranslation, m_derivedScale, m_derivedRotation);
|
||||
|
||||
m_matrixUpdated = true;
|
||||
}
|
||||
@@ -1104,6 +1104,9 @@ namespace
|
||||
|
||||
bool NzPixelFormat::Initialize()
|
||||
{
|
||||
// Réinitialisation
|
||||
std::memset(s_convertFunctions, 0, (nzPixelFormat_Max+1)*(nzPixelFormat_Max+1)*sizeof(NzPixelFormat::ConvertFunction));
|
||||
|
||||
/**********************************BGR8***********************************/
|
||||
RegisterConverter<nzPixelFormat_BGR8, nzPixelFormat_BGRA8>();
|
||||
RegisterConverter<nzPixelFormat_BGR8, nzPixelFormat_L8>();
|
||||
@@ -1320,5 +1323,5 @@ void NzPixelFormat::Uninitialize()
|
||||
s_flipFunctions[i].clear();
|
||||
}
|
||||
|
||||
NzPixelFormat::ConvertFunction NzPixelFormat::s_convertFunctions[nzPixelFormat_Max+1][nzPixelFormat_Max+1] = {{nullptr}}; ///FIXME: Fonctionne correctement ?
|
||||
NzPixelFormat::ConvertFunction NzPixelFormat::s_convertFunctions[nzPixelFormat_Max+1][nzPixelFormat_Max+1];
|
||||
std::map<nzPixelFormat, NzPixelFormat::FlipFunction> NzPixelFormat::s_flipFunctions[nzPixelFlipping_Max+1];
|
||||
|
||||
311
src/Nazara/Utility/SkeletalMesh.cpp
Normal file
311
src/Nazara/Utility/SkeletalMesh.cpp
Normal file
@@ -0,0 +1,311 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
#include <Nazara/Core/Clock.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/VertexStruct.hpp>
|
||||
#include <vector>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
struct NzSkeletalMeshImpl
|
||||
{
|
||||
std::vector<NzVertexWeight> vertexWeights;
|
||||
std::vector<NzWeight> weights;
|
||||
NzAxisAlignedBox aabb;
|
||||
nzUInt8* bindPoseBuffer;
|
||||
const NzIndexBuffer* indexBuffer = nullptr;
|
||||
NzVertexBuffer* vertexBuffer;
|
||||
};
|
||||
|
||||
NzSkeletalMesh::NzSkeletalMesh(const NzMesh* parent) :
|
||||
NzSubMesh(parent)
|
||||
{
|
||||
}
|
||||
|
||||
NzSkeletalMesh::~NzSkeletalMesh()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzSkeletalMesh::Create(NzVertexBuffer* vertexBuffer, unsigned int weightCount)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!vertexBuffer)
|
||||
{
|
||||
NazaraError("Invalid vertex buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (weightCount == 0)
|
||||
{
|
||||
NazaraError("Weight count must be over 0");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
vertexBuffer->AddResourceReference();
|
||||
|
||||
unsigned int vertexCount = vertexBuffer->GetVertexCount();
|
||||
|
||||
m_impl = new NzSkeletalMeshImpl;
|
||||
m_impl->bindPoseBuffer = new nzUInt8[vertexCount*vertexBuffer->GetTypeSize()];
|
||||
m_impl->vertexBuffer = vertexBuffer;
|
||||
m_impl->vertexWeights.resize(vertexCount);
|
||||
m_impl->weights.resize(weightCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzSkeletalMesh::Destroy()
|
||||
{
|
||||
if (m_impl)
|
||||
{
|
||||
if (m_impl->indexBuffer)
|
||||
m_impl->indexBuffer->RemoveResourceReference();
|
||||
|
||||
if (m_impl->vertexBuffer)
|
||||
m_impl->vertexBuffer->RemoveResourceReference();
|
||||
|
||||
delete[] m_impl->bindPoseBuffer;
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const NzAxisAlignedBox& NzSkeletalMesh::GetAABB() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return NzAxisAlignedBox::Null;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_parent->GetSkeleton()->GetAABB();
|
||||
}
|
||||
|
||||
nzAnimationType NzSkeletalMesh::GetAnimationType() const
|
||||
{
|
||||
return nzAnimationType_Skeletal;
|
||||
}
|
||||
|
||||
void* NzSkeletalMesh::GetBindPoseBuffer()
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->bindPoseBuffer;
|
||||
}
|
||||
|
||||
const void* NzSkeletalMesh::GetBindPoseBuffer() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->bindPoseBuffer;
|
||||
}
|
||||
|
||||
const NzIndexBuffer* NzSkeletalMesh::GetIndexBuffer() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->indexBuffer;
|
||||
}
|
||||
|
||||
const NzVertexBuffer* NzSkeletalMesh::GetVertexBuffer() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->vertexBuffer;
|
||||
}
|
||||
|
||||
NzVertexWeight* NzSkeletalMesh::GetVertexWeight(unsigned int vertexIndex)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->vertexWeights[vertexIndex];
|
||||
}
|
||||
|
||||
const NzVertexWeight* NzSkeletalMesh::GetVertexWeight(unsigned int vertexIndex) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->vertexWeights[vertexIndex];
|
||||
}
|
||||
|
||||
NzWeight* NzSkeletalMesh::GetWeight(unsigned int weightIndex)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->weights[weightIndex];
|
||||
}
|
||||
|
||||
const NzWeight* NzSkeletalMesh::GetWeight(unsigned int weightIndex) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->weights[weightIndex];
|
||||
}
|
||||
|
||||
unsigned int NzSkeletalMesh::GetWeightCount() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->weights.size();
|
||||
}
|
||||
|
||||
bool NzSkeletalMesh::IsAnimated() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzSkeletalMesh::IsValid() const
|
||||
{
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
|
||||
void NzSkeletalMesh::Skin()
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
Skin(m_parent->GetSkeleton());
|
||||
}
|
||||
|
||||
void NzSkeletalMesh::Skin(const NzSkeleton* skeleton)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
void* outputBuffer = m_impl->vertexBuffer->Map(nzBufferAccess_DiscardAndWrite);
|
||||
if (!outputBuffer)
|
||||
{
|
||||
NazaraError("Failed to map vertex buffer");
|
||||
return;
|
||||
}
|
||||
|
||||
NzVertexStruct_XYZ_Normal_UV_Tangent* inputVertex = reinterpret_cast<NzVertexStruct_XYZ_Normal_UV_Tangent*>(m_impl->bindPoseBuffer);
|
||||
NzVertexStruct_XYZ_Normal_UV_Tangent* outputVertex = reinterpret_cast<NzVertexStruct_XYZ_Normal_UV_Tangent*>(outputBuffer);
|
||||
|
||||
NzClock c;
|
||||
|
||||
const NzJoint* joints = skeleton->GetJoints();
|
||||
unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount();
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
NzVector3f finalPosition(NzVector3f::Zero());
|
||||
NzVector3f finalNormal(NzVector3f::Zero());
|
||||
NzVector3f finalTangent(NzVector3f::Zero());
|
||||
|
||||
unsigned int weightCount = m_impl->vertexWeights[i].weights.size();
|
||||
for (unsigned int j = 0; j < weightCount; ++j)
|
||||
{
|
||||
const NzWeight& weight = m_impl->weights[m_impl->vertexWeights[i].weights[j]];
|
||||
|
||||
NzMatrix4f mat(joints[weight.jointIndex].GetInverseBindMatrix());
|
||||
mat.ConcatenateAffine(joints[weight.jointIndex].GetTransformMatrix());
|
||||
mat *= weight.weight;
|
||||
|
||||
finalPosition += mat.Transform(inputVertex->position);
|
||||
finalNormal += mat.Transform(inputVertex->normal, 0.f);
|
||||
finalTangent += mat.Transform(inputVertex->tangent, 0.f);
|
||||
}
|
||||
|
||||
finalNormal.Normalize();
|
||||
finalTangent.Normalize();
|
||||
|
||||
outputVertex->normal = finalNormal;
|
||||
outputVertex->position = finalPosition;
|
||||
outputVertex->tangent = finalTangent;
|
||||
outputVertex->uv = inputVertex->uv;
|
||||
|
||||
inputVertex++;
|
||||
outputVertex++;
|
||||
}
|
||||
|
||||
nzUInt64 t = c.GetMicroseconds();
|
||||
|
||||
NazaraError("Skinning took a total of " + NzString::Number(t) + "us\n\n");
|
||||
|
||||
if (!m_impl->vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap vertex buffer");
|
||||
}
|
||||
|
||||
void NzSkeletalMesh::SetIndexBuffer(const NzIndexBuffer* indexBuffer)
|
||||
{
|
||||
if (m_impl->indexBuffer)
|
||||
m_impl->indexBuffer->RemoveResourceReference();
|
||||
|
||||
if (indexBuffer)
|
||||
indexBuffer->AddResourceReference();
|
||||
|
||||
m_impl->indexBuffer = indexBuffer;
|
||||
}
|
||||
384
src/Nazara/Utility/Skeleton.cpp
Normal file
384
src/Nazara/Utility/Skeleton.cpp
Normal file
@@ -0,0 +1,384 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/AxisAlignedBox.hpp>
|
||||
#include <map>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
struct NzSkeletonImpl
|
||||
{
|
||||
std::map<NzString, unsigned int> jointMap;
|
||||
std::vector<NzJoint> joints;
|
||||
NzAxisAlignedBox aabb;
|
||||
bool jointMapUpdated = false;
|
||||
};
|
||||
|
||||
NzSkeleton::NzSkeleton(const NzSkeleton& skeleton)
|
||||
{
|
||||
if (skeleton.m_impl)
|
||||
{
|
||||
m_impl = new NzSkeletonImpl;
|
||||
m_impl->jointMap = skeleton.m_impl->jointMap;
|
||||
m_impl->jointMapUpdated = skeleton.m_impl->jointMapUpdated;
|
||||
m_impl->joints = skeleton.m_impl->joints;
|
||||
}
|
||||
else
|
||||
m_impl = nullptr;
|
||||
}
|
||||
|
||||
NzSkeleton::~NzSkeleton()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzSkeleton::Create(unsigned int jointCount)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (jointCount == 0)
|
||||
{
|
||||
NazaraError("Joint count must be over 0");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl = new NzSkeletonImpl;
|
||||
m_impl->joints.resize(jointCount, NzJoint(this));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzSkeleton::Destroy()
|
||||
{
|
||||
if (m_impl)
|
||||
{
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const NzAxisAlignedBox& NzSkeleton::GetAABB() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return NzAxisAlignedBox::Null;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (m_impl->aabb.IsNull())
|
||||
{
|
||||
for (unsigned int i = 0; i < m_impl->joints.size(); ++i)
|
||||
m_impl->aabb.ExtendTo(m_impl->joints[i].GetDerivedTranslation());
|
||||
}
|
||||
|
||||
return m_impl->aabb;
|
||||
}
|
||||
|
||||
NzJoint* NzSkeleton::GetJoint(const NzString& jointName)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!m_impl->jointMapUpdated)
|
||||
UpdateJointMap();
|
||||
|
||||
auto it = m_impl->jointMap.find(jointName);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->jointMap.end())
|
||||
{
|
||||
NazaraError("Joint not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Invalidation de l'AABB
|
||||
m_impl->aabb.SetNull();
|
||||
|
||||
return &m_impl->joints[it->second];
|
||||
}
|
||||
|
||||
NzJoint* NzSkeleton::GetJoint(unsigned int index)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (index >= m_impl->joints.size())
|
||||
{
|
||||
NazaraError("Joint index out of range (" + NzString::Number(index) + " >= " + NzString::Number(m_impl->joints.size()) + ')');
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Invalidation de l'AABB
|
||||
m_impl->aabb.SetNull();
|
||||
|
||||
return &m_impl->joints[index];
|
||||
}
|
||||
|
||||
const NzJoint* NzSkeleton::GetJoint(const NzString& jointName) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!m_impl->jointMapUpdated)
|
||||
UpdateJointMap();
|
||||
|
||||
auto it = m_impl->jointMap.find(jointName);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->jointMap.end())
|
||||
{
|
||||
NazaraError("Joint not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->joints[it->second];
|
||||
}
|
||||
|
||||
const NzJoint* NzSkeleton::GetJoint(unsigned int index) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (index >= m_impl->joints.size())
|
||||
{
|
||||
NazaraError("Joint index out of range (" + NzString::Number(index) + " >= " + NzString::Number(m_impl->joints.size()) + ')');
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->joints[index];
|
||||
}
|
||||
|
||||
NzJoint* NzSkeleton::GetJoints()
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->joints[0];
|
||||
}
|
||||
|
||||
const NzJoint* NzSkeleton::GetJoints() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &m_impl->joints[0];
|
||||
}
|
||||
|
||||
unsigned int NzSkeleton::GetJointCount() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->joints.size();
|
||||
}
|
||||
|
||||
int NzSkeleton::GetJointIndex(const NzString& jointName) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!m_impl->jointMapUpdated)
|
||||
UpdateJointMap();
|
||||
|
||||
auto it = m_impl->jointMap.find(jointName);
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (it == m_impl->jointMap.end())
|
||||
{
|
||||
NazaraError("Joint not found");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void NzSkeleton::Interpolate(const NzSkeleton& skeletonA, const NzSkeleton& skeletonB, float interpolation)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!skeletonA.IsValid())
|
||||
{
|
||||
NazaraError("Skeleton A is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!skeletonB.IsValid())
|
||||
{
|
||||
NazaraError("Skeleton B is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
if (skeletonA.GetJointCount() != skeletonB.GetJointCount() || m_impl->joints.size() != skeletonA.GetJointCount())
|
||||
{
|
||||
NazaraError("Skeletons must have the same joint count");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
NzJoint* jointsA = &skeletonA.m_impl->joints[0];
|
||||
NzJoint* jointsB = &skeletonB.m_impl->joints[0];
|
||||
for (unsigned int i = 0; i < m_impl->joints.size(); ++i)
|
||||
m_impl->joints[i].Interpolate(jointsA[i], jointsB[i], interpolation);
|
||||
}
|
||||
|
||||
void NzSkeleton::Interpolate(const NzSkeleton& skeletonA, const NzSkeleton& skeletonB, float interpolation, unsigned int* indices, unsigned int indiceCount)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeleton not created");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!skeletonA.IsValid())
|
||||
{
|
||||
NazaraError("Skeleton A is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!skeletonB.IsValid())
|
||||
{
|
||||
NazaraError("Skeleton B is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
if (skeletonA.GetJointCount() != skeletonB.GetJointCount() || m_impl->joints.size() != skeletonA.GetJointCount())
|
||||
{
|
||||
NazaraError("Skeletons must have the same joint count");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
NzJoint* jointsA = &skeletonA.m_impl->joints[0];
|
||||
NzJoint* jointsB = &skeletonB.m_impl->joints[0];
|
||||
for (unsigned int i = 0; i < indiceCount; ++i)
|
||||
{
|
||||
unsigned int index = indices[i];
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (index >= m_impl->joints.size())
|
||||
{
|
||||
NazaraError("Index #" + NzString::Number(i) + " out of range (" + NzString::Number(index) + " >= " + NzString::Number(m_impl->joints.size()) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl->joints[index].Interpolate(jointsA[index], jointsB[index], interpolation);
|
||||
}
|
||||
}
|
||||
|
||||
bool NzSkeleton::IsValid() const
|
||||
{
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
|
||||
NzSkeleton& NzSkeleton::operator=(const NzSkeleton& skeleton)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
if (skeleton.m_impl)
|
||||
{
|
||||
m_impl = new NzSkeletonImpl;
|
||||
m_impl->jointMap = skeleton.m_impl->jointMap;
|
||||
m_impl->jointMapUpdated = skeleton.m_impl->jointMapUpdated;
|
||||
m_impl->joints = skeleton.m_impl->joints;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void NzSkeleton::InvalidateJointMap()
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Invalid skeleton");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl->jointMapUpdated = false;
|
||||
}
|
||||
|
||||
void NzSkeleton::UpdateJointMap() const
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Invalid skeleton");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl->jointMap.clear();
|
||||
for (unsigned int i = 0; i < m_impl->joints.size(); ++i)
|
||||
{
|
||||
NzString name = m_impl->joints[i].GetName();
|
||||
if (!name.IsEmpty())
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
auto it = m_impl->jointMap.find(name);
|
||||
if (it != m_impl->jointMap.end())
|
||||
{
|
||||
NazaraWarning("Joint name \"" + name + "\" is already present in joint map for joint #" + NzString::Number(it->second));
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl->jointMap[name] = i;
|
||||
}
|
||||
}
|
||||
|
||||
m_impl->jointMapUpdated = true;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <Nazara/Utility/StaticMesh.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
@@ -12,36 +13,16 @@ NzSubMesh(parent)
|
||||
{
|
||||
}
|
||||
|
||||
NzStaticMesh::NzStaticMesh(const NzMesh* parent, const NzVertexDeclaration* vertexDeclaration, NzVertexBuffer* vertexBuffer, NzIndexBuffer* indexBuffer) :
|
||||
NzSubMesh(parent)
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!Create(vertexDeclaration, vertexBuffer, indexBuffer))
|
||||
{
|
||||
NazaraError("Failed to create mesh");
|
||||
throw std::runtime_error("Constructor failed");
|
||||
}
|
||||
#else
|
||||
Create(vertexDeclaration, vertexBuffer, indexBuffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
NzStaticMesh::~NzStaticMesh()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzStaticMesh::Create(const NzVertexDeclaration* vertexDeclaration, NzVertexBuffer* vertexBuffer, NzIndexBuffer* indexBuffer)
|
||||
bool NzStaticMesh::Create(NzVertexBuffer* vertexBuffer)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!vertexDeclaration)
|
||||
{
|
||||
NazaraError("Invalid vertex declaration");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!vertexBuffer)
|
||||
{
|
||||
NazaraError("Invalid vertex buffer");
|
||||
@@ -49,17 +30,9 @@ bool NzStaticMesh::Create(const NzVertexDeclaration* vertexDeclaration, NzVertex
|
||||
}
|
||||
#endif
|
||||
|
||||
if (indexBuffer)
|
||||
indexBuffer->AddResourceListener(this);
|
||||
|
||||
m_indexBuffer = indexBuffer;
|
||||
|
||||
m_vertexBuffer = vertexBuffer;
|
||||
m_vertexBuffer->AddResourceListener(this);
|
||||
|
||||
m_vertexDeclaration = vertexDeclaration;
|
||||
m_vertexDeclaration->AddResourceListener(this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -78,12 +51,6 @@ void NzStaticMesh::Destroy()
|
||||
m_vertexBuffer->RemoveResourceListener(this);
|
||||
m_vertexBuffer = nullptr;
|
||||
}
|
||||
|
||||
if (m_vertexDeclaration)
|
||||
{
|
||||
m_vertexDeclaration->RemoveResourceListener(this);
|
||||
m_vertexDeclaration = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool NzStaticMesh::GenerateAABB()
|
||||
@@ -91,31 +58,24 @@ bool NzStaticMesh::GenerateAABB()
|
||||
if (!m_aabb.IsNull())
|
||||
return true;
|
||||
|
||||
const NzVertexElement* position = m_vertexDeclaration->GetElement(nzElementStream_VertexData, nzElementUsage_Position);
|
||||
if (position && position->type == nzElementType_Float3) // Si nous avons des positions du type Vec3
|
||||
// On lock le buffer pour itérer sur toutes les positions et composer notre AABB
|
||||
NzMeshVertex* vertex = reinterpret_cast<NzMeshVertex*>(m_vertexBuffer->Map(nzBufferAccess_ReadOnly));
|
||||
if (!vertex)
|
||||
{
|
||||
// On lock le buffer pour itérer sur toutes les positions et composer notre AABB
|
||||
nzUInt8* buffer = reinterpret_cast<nzUInt8*>(m_vertexBuffer->Map(nzBufferAccess_ReadOnly));
|
||||
if (!buffer)
|
||||
{
|
||||
NazaraWarning("Failed to lock vertex buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer += position->offset;
|
||||
unsigned int stride = m_vertexDeclaration->GetStride(nzElementStream_VertexData);
|
||||
unsigned int vertexCount = m_vertexBuffer->GetVertexCount();
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
m_aabb.ExtendTo(*reinterpret_cast<NzVector3f*>(buffer));
|
||||
|
||||
buffer += stride;
|
||||
}
|
||||
|
||||
if (!m_vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap vertex buffer");
|
||||
NazaraWarning("Failed to lock vertex buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int vertexCount = m_vertexBuffer->GetVertexCount();
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
m_aabb.ExtendTo(vertex->position);
|
||||
vertex++;
|
||||
}
|
||||
|
||||
if (!m_vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap vertex buffer");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -129,31 +89,16 @@ nzAnimationType NzStaticMesh::GetAnimationType() const
|
||||
return nzAnimationType_Static;
|
||||
}
|
||||
|
||||
unsigned int NzStaticMesh::GetFrameCount() const
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
const NzIndexBuffer* NzStaticMesh::GetIndexBuffer() const
|
||||
{
|
||||
return m_indexBuffer;
|
||||
}
|
||||
|
||||
nzPrimitiveType NzStaticMesh::GetPrimitiveType() const
|
||||
{
|
||||
return m_primitiveType;
|
||||
}
|
||||
|
||||
const NzVertexBuffer* NzStaticMesh::GetVertexBuffer() const
|
||||
{
|
||||
return m_vertexBuffer;
|
||||
}
|
||||
|
||||
const NzVertexDeclaration* NzStaticMesh::GetVertexDeclaration() const
|
||||
{
|
||||
return m_vertexDeclaration;
|
||||
}
|
||||
|
||||
bool NzStaticMesh::IsAnimated() const
|
||||
{
|
||||
return false;
|
||||
@@ -161,7 +106,7 @@ bool NzStaticMesh::IsAnimated() const
|
||||
|
||||
bool NzStaticMesh::IsValid() const
|
||||
{
|
||||
return m_vertexBuffer != nullptr && m_vertexDeclaration != nullptr;
|
||||
return m_vertexBuffer != nullptr;
|
||||
}
|
||||
|
||||
void NzStaticMesh::SetAABB(const NzAxisAlignedBox& aabb)
|
||||
@@ -169,19 +114,15 @@ void NzStaticMesh::SetAABB(const NzAxisAlignedBox& aabb)
|
||||
m_aabb = aabb;
|
||||
}
|
||||
|
||||
void NzStaticMesh::SetPrimitiveType(nzPrimitiveType primitiveType)
|
||||
void NzStaticMesh::SetIndexBuffer(const NzIndexBuffer* indexBuffer)
|
||||
{
|
||||
m_primitiveType = primitiveType;
|
||||
}
|
||||
if (m_indexBuffer)
|
||||
m_indexBuffer->RemoveResourceReference();
|
||||
|
||||
void NzStaticMesh::AnimateImpl(unsigned int frameA, unsigned int frameB, float interpolation)
|
||||
{
|
||||
NazaraUnused(frameA);
|
||||
NazaraUnused(frameB);
|
||||
NazaraUnused(interpolation);
|
||||
if (indexBuffer)
|
||||
indexBuffer->AddResourceReference();
|
||||
|
||||
// Le safe mode est censé nous protéger de cet appel
|
||||
NazaraError("Static mesh have no animation, please enable safe mode");
|
||||
m_indexBuffer = indexBuffer;
|
||||
}
|
||||
|
||||
void NzStaticMesh::OnResourceReleased(const NzResource* resource, int index)
|
||||
@@ -192,8 +133,8 @@ void NzStaticMesh::OnResourceReleased(const NzResource* resource, int index)
|
||||
m_indexBuffer = nullptr;
|
||||
else if (resource == m_vertexBuffer)
|
||||
m_vertexBuffer = nullptr;
|
||||
else if (resource == m_vertexDeclaration)
|
||||
m_vertexDeclaration = nullptr;
|
||||
else
|
||||
NazaraInternalError("Not listening to " + NzString::Pointer(resource));
|
||||
|
||||
resource->RemoveResourceListener(this);
|
||||
}
|
||||
|
||||
@@ -12,61 +12,39 @@
|
||||
|
||||
NzSubMesh::NzSubMesh(const NzMesh* parent) :
|
||||
NzResource(false), // Un SubMesh n'est pas persistant par défaut
|
||||
m_parent(parent)
|
||||
m_parent(parent),
|
||||
m_matIndex(0)
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!m_parent)
|
||||
{
|
||||
NazaraError("Parent mesh must be valid");
|
||||
throw std::invalid_argument("Parent mesh must be valid");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
NzSubMesh::~NzSubMesh() = default;
|
||||
|
||||
void NzSubMesh::Animate(unsigned int frameA, unsigned int frameB, float interpolation)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_parent->HasAnimation())
|
||||
{
|
||||
NazaraError("Parent mesh has no animation");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int frameCount = m_parent->GetFrameCount();
|
||||
if (frameA >= frameCount)
|
||||
{
|
||||
NazaraError("Frame A is out of range (" + NzString::Number(frameA) + " >= " + NzString::Number(frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
|
||||
if (frameB >= frameCount)
|
||||
{
|
||||
NazaraError("Frame B is out of range (" + NzString::Number(frameB) + " >= " + NzString::Number(frameCount) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (interpolation < 0.f || interpolation > 1.f)
|
||||
{
|
||||
NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')');
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
AnimateImpl(frameA, frameB, interpolation);
|
||||
|
||||
m_parent->InvalidateAABB();
|
||||
}
|
||||
|
||||
const NzMesh* NzSubMesh::GetParent() const
|
||||
{
|
||||
return m_parent;
|
||||
}
|
||||
|
||||
nzPrimitiveType NzSubMesh::GetPrimitiveType() const
|
||||
{
|
||||
return m_primitiveType;
|
||||
}
|
||||
|
||||
unsigned int NzSubMesh::GetSkinIndex() const
|
||||
{
|
||||
return m_matIndex;
|
||||
}
|
||||
|
||||
unsigned int NzSubMesh::GetVertexCount() const
|
||||
{
|
||||
return GetVertexBuffer()->GetVertexCount();
|
||||
}
|
||||
|
||||
void NzSubMesh::SetPrimitiveType(nzPrimitiveType primitiveType)
|
||||
{
|
||||
m_primitiveType = primitiveType;
|
||||
}
|
||||
|
||||
void NzSubMesh::SetMaterialIndex(unsigned int matIndex)
|
||||
{
|
||||
m_matIndex = matIndex;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <Nazara/Utility/Buffer.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD2.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD5Anim.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD5Mesh.hpp>
|
||||
#include <Nazara/Utility/Loaders/PCX.hpp>
|
||||
#include <Nazara/Utility/Loaders/STB.hpp>
|
||||
#include <Nazara/Utility/PixelFormat.hpp>
|
||||
@@ -48,17 +50,22 @@ bool NzUtility::Initialize()
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Loaders spécialisés
|
||||
// Mesh
|
||||
NzLoaders_MD2_Register(); // Loader de fichiers .MD2 (v8)
|
||||
// On enregistre les loaders pour les extensions
|
||||
// Il s'agit ici d'une liste LIFO, le dernier loader enregistré possède la priorité
|
||||
|
||||
// Image
|
||||
NzLoaders_PCX_Register(); // Loader de fichiers .PCX (1, 4, 8, 24)
|
||||
|
||||
/// Loaders génériques (En dernier pour donner la priorité aux loaders spécialisés)
|
||||
/// Loaders génériques
|
||||
// Image
|
||||
NzLoaders_STB_Register(); // Loader générique (STB)
|
||||
|
||||
/// Loaders spécialisés
|
||||
// Mesh
|
||||
NzLoaders_MD2_Register(); // Loader de fichiers .md2 (v8)
|
||||
NzLoaders_MD5Anim_Register(); // Loader de fichiers .md5anim (v10)
|
||||
NzLoaders_MD5Mesh_Register(); // Loader de fichiers .md5mesh (v10)
|
||||
|
||||
// Image
|
||||
NzLoaders_PCX_Register(); // Loader de fichiers .pcx (1, 4, 8, 24 bits)
|
||||
|
||||
NazaraNotice("Initialized: Utility module");
|
||||
|
||||
return true;
|
||||
@@ -76,6 +83,7 @@ void NzUtility::Uninitialize()
|
||||
|
||||
// Libération du module
|
||||
NzLoaders_MD2_Unregister();
|
||||
NzLoaders_MD5Mesh_Unregister();
|
||||
NzLoaders_PCX_Unregister();
|
||||
NzLoaders_STB_Unregister();
|
||||
|
||||
|
||||
@@ -9,35 +9,54 @@
|
||||
|
||||
///FIXME: Gérer efficacement les erreurs de création du buffer
|
||||
|
||||
NzVertexBuffer::NzVertexBuffer(NzBuffer* buffer, unsigned int startVertex, unsigned int vertexCount) :
|
||||
NzVertexBuffer::NzVertexBuffer(const NzVertexDeclaration* vertexDeclaration, NzBuffer* buffer, unsigned int startVertex, unsigned int vertexCount) :
|
||||
m_buffer(buffer),
|
||||
m_vertexDeclaration(vertexDeclaration),
|
||||
m_ownsBuffer(false),
|
||||
m_startVertex(startVertex),
|
||||
m_vertexCount(vertexCount)
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!m_buffer)
|
||||
if (!m_buffer || !m_buffer->IsValid())
|
||||
{
|
||||
NazaraError("Buffer is null");
|
||||
NazaraError("Buffer is invalid");
|
||||
throw std::invalid_argument("Buffer must be valid");
|
||||
}
|
||||
|
||||
if (!m_vertexDeclaration || !m_vertexDeclaration->IsValid())
|
||||
{
|
||||
NazaraError("Vertex declaration is invalid");
|
||||
throw std::invalid_argument("Invalid vertex declaration");
|
||||
}
|
||||
#endif
|
||||
|
||||
m_buffer->AddResourceReference();
|
||||
m_vertexDeclaration->AddResourceReference();
|
||||
}
|
||||
|
||||
NzVertexBuffer::NzVertexBuffer(unsigned int length, nzUInt8 typeSize, nzBufferStorage storage, nzBufferUsage usage) :
|
||||
NzVertexBuffer::NzVertexBuffer(const NzVertexDeclaration* vertexDeclaration, unsigned int length, nzBufferStorage storage, nzBufferUsage usage) :
|
||||
m_vertexDeclaration(vertexDeclaration),
|
||||
m_ownsBuffer(true),
|
||||
m_startVertex(0),
|
||||
m_vertexCount(length)
|
||||
{
|
||||
m_buffer = new NzBuffer(nzBufferType_Vertex, length, typeSize, storage, usage);
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!m_vertexDeclaration || !m_vertexDeclaration->IsValid())
|
||||
{
|
||||
NazaraError("Vertex declaration is invalid");
|
||||
throw std::invalid_argument("Invalid vertex declaration");
|
||||
}
|
||||
#endif
|
||||
|
||||
m_buffer = new NzBuffer(nzBufferType_Vertex, length, vertexDeclaration->GetStride(nzElementStream_VertexData), storage, usage);
|
||||
m_buffer->AddResourceReference();
|
||||
m_buffer->SetPersistent(false);
|
||||
m_vertexDeclaration->AddResourceReference();
|
||||
}
|
||||
|
||||
NzVertexBuffer::NzVertexBuffer(const NzVertexBuffer& vertexBuffer) :
|
||||
NzResource(true),
|
||||
m_vertexDeclaration(vertexBuffer.m_vertexDeclaration),
|
||||
m_ownsBuffer(vertexBuffer.m_ownsBuffer),
|
||||
m_startVertex(vertexBuffer.m_startVertex),
|
||||
m_vertexCount(vertexBuffer.m_vertexCount)
|
||||
@@ -56,11 +75,14 @@ m_vertexCount(vertexBuffer.m_vertexCount)
|
||||
m_buffer = vertexBuffer.m_buffer;
|
||||
m_buffer->AddResourceReference();
|
||||
}
|
||||
|
||||
m_vertexDeclaration->AddResourceReference();
|
||||
}
|
||||
|
||||
NzVertexBuffer::~NzVertexBuffer()
|
||||
{
|
||||
m_buffer->RemoveResourceReference();
|
||||
m_vertexDeclaration->RemoveResourceReference();
|
||||
}
|
||||
|
||||
bool NzVertexBuffer::Fill(const void* data, unsigned int offset, unsigned int length)
|
||||
@@ -106,6 +128,11 @@ unsigned int NzVertexBuffer::GetVertexCount() const
|
||||
return m_vertexCount;
|
||||
}
|
||||
|
||||
const NzVertexDeclaration* NzVertexBuffer::GetVertexDeclaration() const
|
||||
{
|
||||
return m_vertexDeclaration;
|
||||
}
|
||||
|
||||
bool NzVertexBuffer::IsHardware() const
|
||||
{
|
||||
return m_buffer->IsHardware();
|
||||
|
||||
@@ -244,37 +244,20 @@ const NzVertexElement* NzVertexDeclaration::GetElement(nzElementStream stream, n
|
||||
#endif
|
||||
|
||||
int elementPos = m_sharedImpl->elementPos[stream][usage];
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (elementPos == -1)
|
||||
{
|
||||
NazaraError("Element not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (usageIndex == 0) // Si l'usage index vaut zéro, alors nous sommes certains d'être sur le bon élément (Majorité des cas)
|
||||
return &m_sharedImpl->elements[elementPos];
|
||||
else
|
||||
{
|
||||
elementPos += usageIndex;
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (static_cast<unsigned int>(elementPos) >= m_sharedImpl->elements.size())
|
||||
{
|
||||
NazaraError("Element not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
NzVertexElement& element = m_sharedImpl->elements[elementPos];
|
||||
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (element.stream != stream || element.usage != usage || element.usageIndex != usageIndex)
|
||||
{
|
||||
NazaraError("Element not found");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return &element;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user