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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user