Meshes no longer retains an animation pointer
Former-commit-id: da4d93b815c46284a8977d7fb142201dc23fff61
This commit is contained in:
parent
c019d02b91
commit
2e26d52248
|
|
@ -13,7 +13,6 @@
|
|||
#include <Nazara/Core/ResourceListener.hpp>
|
||||
#include <Nazara/Core/ResourceLoader.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Utility/Animation.hpp>
|
||||
#include <Nazara/Utility/AxisAlignedBox.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/SubMesh.hpp>
|
||||
|
|
@ -23,13 +22,13 @@ struct NAZARA_API NzMeshParams
|
|||
{
|
||||
NzMeshParams(); // Vérifie que le storage indiqué un peu plus bas est supporté
|
||||
|
||||
NzAnimationParams animation;
|
||||
nzBufferStorage storage = nzBufferStorage_Hardware;
|
||||
bool animated = true;
|
||||
|
||||
bool IsValid() const;
|
||||
};
|
||||
|
||||
class NzAnimation;
|
||||
class NzMesh;
|
||||
|
||||
typedef NzVertexStruct_XYZ_Normal_UV_Tangent NzMeshVertex;
|
||||
|
|
@ -57,6 +56,7 @@ class NAZARA_API NzMesh : public NzResource, NzResourceListener
|
|||
void Destroy();
|
||||
|
||||
const NzAxisAlignedBox& GetAABB() const;
|
||||
NzString GetAnimation() const;
|
||||
nzAnimationType GetAnimationType() const;
|
||||
unsigned int GetJointCount() const;
|
||||
NzString GetMaterial(unsigned int index) const;
|
||||
|
|
@ -86,6 +86,7 @@ class NAZARA_API NzMesh : public NzResource, NzResourceListener
|
|||
void RemoveSubMesh(const NzString& identifier);
|
||||
void RemoveSubMesh(unsigned int index);
|
||||
|
||||
void SetAnimation(const NzString& animationPath);
|
||||
void SetMaterial(unsigned int matIndex, const NzString& materialPath);
|
||||
void SetMaterialCount(unsigned int matCount);
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Math/Basic.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Utility/Animation.hpp>
|
||||
#include <Nazara/Utility/KeyframeMesh.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/Loaders/MD2/Constants.hpp>
|
||||
|
|
@ -18,7 +19,8 @@
|
|||
|
||||
namespace
|
||||
{
|
||||
bool Check(NzInputStream& stream, const NzMeshParams& parameters)
|
||||
/// Loader de mesh
|
||||
bool CheckMesh(NzInputStream& stream, const NzMeshParams& parameters)
|
||||
{
|
||||
NazaraUnused(parameters);
|
||||
|
||||
|
|
@ -34,7 +36,7 @@ namespace
|
|||
return magic[0] == md2Ident && magic[1] == 8;
|
||||
}
|
||||
|
||||
bool Load(NzMesh* mesh, NzInputStream& stream, const NzMeshParams& parameters)
|
||||
bool LoadMesh(NzMesh* mesh, NzInputStream& stream, const NzMeshParams& parameters)
|
||||
{
|
||||
md2_header header;
|
||||
if (stream.Read(&header, sizeof(md2_header)) != sizeof(md2_header))
|
||||
|
|
@ -69,11 +71,8 @@ namespace
|
|||
|
||||
/// Création du mesh
|
||||
// Animé ou statique, c'est la question
|
||||
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));
|
||||
|
||||
bool animated = (parameters.animated && startFrame != endFrame);
|
||||
if (animated)
|
||||
///FIXME: Le loader ne traite pas correctement le cas d'un mesh statique
|
||||
if (parameters.animated)
|
||||
mesh->CreateKeyframe();
|
||||
else
|
||||
mesh->CreateStatic();
|
||||
|
|
@ -84,6 +83,8 @@ namespace
|
|||
return false;
|
||||
}
|
||||
|
||||
mesh->SetAnimation(stream.GetPath()); // Même fichier
|
||||
|
||||
/// Chargement des skins
|
||||
if (header.num_skins > 0)
|
||||
{
|
||||
|
|
@ -103,12 +104,11 @@ namespace
|
|||
/// Chargement des submesh
|
||||
// Actuellement le loader ne charge qu'un submesh
|
||||
// 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))
|
||||
if (!subMesh->Create(vertexBuffer.get(), header.num_frames))
|
||||
{
|
||||
NazaraError("Failed to create SubMesh");
|
||||
return false;
|
||||
|
|
@ -153,13 +153,13 @@ namespace
|
|||
#endif
|
||||
|
||||
/// Chargement des frames
|
||||
stream.SetCursorPos(header.offset_frames + header.framesize*startFrame);
|
||||
stream.SetCursorPos(header.offset_frames);
|
||||
|
||||
// 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)
|
||||
for (unsigned int f = 0; f < header.num_frames; ++f)
|
||||
{
|
||||
NzVector3f scale, translate;
|
||||
|
||||
|
|
@ -217,14 +217,125 @@ namespace
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Loader d'animations
|
||||
bool CheckAnim(NzInputStream& stream, const NzAnimationParams& parameters)
|
||||
{
|
||||
NazaraUnused(parameters);
|
||||
|
||||
nzUInt32 magic[2];
|
||||
if (stream.Read(&magic[0], 2*sizeof(nzUInt32)) != 2*sizeof(nzUInt32))
|
||||
return false;
|
||||
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
NzByteSwap(&magic[0], sizeof(nzUInt32));
|
||||
NzByteSwap(&magic[1], sizeof(nzUInt32));
|
||||
#endif
|
||||
|
||||
return magic[0] == md2Ident && magic[1] == 8;
|
||||
}
|
||||
|
||||
bool LoadAnim(NzAnimation* animation, NzInputStream& stream, const NzAnimationParams& parameters)
|
||||
{
|
||||
md2_header header;
|
||||
if (stream.Read(&header, sizeof(md2_header)) != sizeof(md2_header))
|
||||
{
|
||||
NazaraError("Failed to read header");
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
NzByteSwap(&header.framesize, sizeof(nzUInt32));
|
||||
NzByteSwap(&header.num_frames, sizeof(nzUInt32));
|
||||
NzByteSwap(&header.offset_frames, sizeof(nzUInt32));
|
||||
NzByteSwap(&header.offset_end, sizeof(nzUInt32));
|
||||
#endif
|
||||
|
||||
if (stream.GetSize() < header.offset_end)
|
||||
{
|
||||
NazaraError("Incomplete MD2 file");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int startFrame = std::min(parameters.startFrame, static_cast<unsigned int>(header.num_frames-1));
|
||||
unsigned int endFrame = std::min(parameters.endFrame, static_cast<unsigned int>(header.num_frames-1));
|
||||
|
||||
unsigned int frameCount = endFrame - startFrame + 1;
|
||||
if (!animation->CreateKeyframe(frameCount))
|
||||
{
|
||||
NazaraInternalError("Failed to create animaton");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Décodage des séquences
|
||||
///TODO: Optimiser le calcul
|
||||
char last[16];
|
||||
|
||||
stream.SetCursorPos(header.offset_frames + startFrame*header.framesize + 2*sizeof(NzVector3f));
|
||||
stream.Read(last, 16*sizeof(char));
|
||||
|
||||
int pos = std::strlen(last)-1;
|
||||
for (unsigned int j = 0; j < 2; ++j)
|
||||
{
|
||||
if (!std::isdigit(last[pos]))
|
||||
break;
|
||||
|
||||
pos--;
|
||||
}
|
||||
last[pos+1] = '\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 + 2*sizeof(NzVector3f));
|
||||
stream.Read(name, 16*sizeof(char));
|
||||
|
||||
pos = std::strlen(name)-1;
|
||||
for (unsigned int j = 0; j < 2; ++j)
|
||||
{
|
||||
if (!std::isdigit(name[pos]))
|
||||
break;
|
||||
|
||||
pos--;
|
||||
}
|
||||
name[pos+1] = '\0';
|
||||
|
||||
if (std::strcmp(name, last) != 0) // Si les deux frames n'ont pas le même nom
|
||||
{
|
||||
std::strcpy(last, name);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
sequence.frameCount++;
|
||||
}
|
||||
// On ajoute la dernière frame (Qui n'a pas été traitée par la boucle)
|
||||
animation->AddSequence(sequence);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void NzLoaders_MD2_Register()
|
||||
{
|
||||
NzMeshLoader::RegisterLoader("md2", Check, Load);
|
||||
NzAnimationLoader::RegisterLoader("md2", CheckAnim, LoadAnim);
|
||||
NzMeshLoader::RegisterLoader("md2", CheckMesh, LoadMesh);
|
||||
}
|
||||
|
||||
void NzLoaders_MD2_Unregister()
|
||||
{
|
||||
NzMeshLoader::UnregisterLoader("md2", Check, Load);
|
||||
NzAnimationLoader::UnregisterLoader("md2", CheckAnim, LoadAnim);
|
||||
NzMeshLoader::UnregisterLoader("md2", CheckMesh, LoadMesh);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -314,6 +314,16 @@ bool NzMD5MeshParser::Parse(NzMesh* mesh)
|
|||
}
|
||||
|
||||
subMesh.release();
|
||||
|
||||
// Animation
|
||||
// Il est peut-être éventuellement possible que la probabilité que l'animation ait le même nom soit non-nulle.
|
||||
NzString path = m_stream.GetPath();
|
||||
if (!path.IsEmpty())
|
||||
{
|
||||
path.Replace(".md5mesh", ".md5anim", -8, NzString::CaseInsensitive);
|
||||
if (NzFile::Exists(path))
|
||||
mesh->SetAnimation(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Utility/Animation.hpp>
|
||||
#include <Nazara/Utility/Buffer.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/KeyframeMesh.hpp>
|
||||
|
|
@ -23,12 +24,6 @@ NzMeshParams::NzMeshParams()
|
|||
|
||||
bool NzMeshParams::IsValid() const
|
||||
{
|
||||
if (!animation.IsValid())
|
||||
{
|
||||
NazaraError("Invalid animation parameters");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!NzBuffer::IsSupported(storage))
|
||||
{
|
||||
NazaraError("Storage not supported");
|
||||
|
|
@ -46,6 +41,7 @@ struct NzMeshImpl
|
|||
nzAnimationType animationType;
|
||||
NzAxisAlignedBox aabb;
|
||||
NzSkeleton skeleton; // Uniquement pour les animations squelettiques
|
||||
NzString animationPath;
|
||||
unsigned int jointCount; // Uniquement pour les animations squelettiques
|
||||
};
|
||||
|
||||
|
|
@ -275,6 +271,19 @@ const NzAxisAlignedBox& NzMesh::GetAABB() const
|
|||
return m_impl->aabb;
|
||||
}
|
||||
|
||||
NzString NzMesh::GetAnimation() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Mesh not created");
|
||||
return nzAnimationType_Static;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_impl->animationPath;
|
||||
}
|
||||
|
||||
nzAnimationType NzMesh::GetAnimationType() const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
|
|
@ -645,6 +654,19 @@ void NzMesh::RemoveSubMesh(unsigned int index)
|
|||
m_impl->aabb.SetNull(); // On invalide l'AABB
|
||||
}
|
||||
|
||||
void NzMesh::SetAnimation(const NzString& animationPath)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_impl->animationPath = animationPath;
|
||||
}
|
||||
|
||||
void NzMesh::SetMaterial(unsigned int matIndex, const NzString& materialPath)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
|
|
|
|||
Loading…
Reference in New Issue