diff --git a/include/Nazara/Utility.hpp b/include/Nazara/Utility.hpp index dd4eb5eef..3d6438b76 100644 --- a/include/Nazara/Utility.hpp +++ b/include/Nazara/Utility.hpp @@ -1,4 +1,4 @@ -// This file was automatically generated on 02 Mar 2013 at 01:32:01 +// This file was automatically generated on 05 Mar 2013 at 10:53:41 /* Nazara Engine - Window module @@ -43,7 +43,6 @@ #include #include #include -#include #include #include #include diff --git a/include/Nazara/Utility/Animation.hpp b/include/Nazara/Utility/Animation.hpp index c7981beb3..7e0c15cf3 100644 --- a/include/Nazara/Utility/Animation.hpp +++ b/include/Nazara/Utility/Animation.hpp @@ -42,7 +42,6 @@ class NAZARA_API NzAnimation : public NzResource bool AddSequence(const NzSequence& sequence); void AnimateSkeleton(NzSkeleton* targetSkeleton, unsigned int frameA, unsigned int frameB, float interpolation) const; - bool CreateKeyframe(unsigned int frameCount); bool CreateSkeletal(unsigned int frameCount, unsigned int jointCount); void Destroy(); diff --git a/include/Nazara/Utility/Enums.hpp b/include/Nazara/Utility/Enums.hpp index 62a21182e..6e72aa79b 100644 --- a/include/Nazara/Utility/Enums.hpp +++ b/include/Nazara/Utility/Enums.hpp @@ -9,7 +9,6 @@ enum nzAnimationType { - nzAnimationType_Keyframe, nzAnimationType_Skeletal, nzAnimationType_Static, diff --git a/include/Nazara/Utility/KeyframeMesh.hpp b/include/Nazara/Utility/KeyframeMesh.hpp deleted file mode 100644 index bd0e50ad5..000000000 --- a/include/Nazara/Utility/KeyframeMesh.hpp +++ /dev/null @@ -1,62 +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_KEYFRAMEMESH_HPP -#define NAZARA_KEYFRAMEMESH_HPP - -#include -#include -#include - -struct NzKeyframeMeshImpl; - -class NAZARA_API NzKeyframeMesh final : public NzSubMesh -{ - friend NzMesh; - - public: - NzKeyframeMesh(const NzMesh* parent); - virtual ~NzKeyframeMesh(); - - bool Create(NzVertexBuffer* vertexBuffer, unsigned int frameCount); - void Destroy(); - - void Finish(); - - void GenerateAABBs(); - - const NzCubef& GetAABB() const override; - nzAnimationType GetAnimationType() const override; - unsigned int GetFrameCount() const; - const NzIndexBuffer* GetIndexBuffer() const override; - NzVector3f GetNormal(unsigned int frameIndex, unsigned int vertexIndex) const; - NzVector3f GetPosition(unsigned int frameIndex, unsigned int vertexIndex) const; - NzVector3f GetTangent(unsigned int frameIndex, unsigned int vertexIndex) const; - NzVector2f GetTexCoords(unsigned int vertexIndex) const; - void GetVertex(unsigned int frameIndex, unsigned int vertexIndex, NzMeshVertex* dest) const; - NzVertexBuffer* GetVertexBuffer(); - const NzVertexBuffer* GetVertexBuffer() const; - unsigned int GetVertexCount() const override; - - void Interpolate(const NzAnimation* animation, unsigned int frameA, unsigned int frameB, float interpolation) const; - - bool IsAnimated() const override; - bool IsValid(); - - void SetAABB(unsigned int frameIndex, const NzCubef& aabb); - void SetIndexBuffer(const NzIndexBuffer* indexBuffer); - void SetNormal(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& normal); - void SetPosition(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& position); - void SetTangent(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& tangent); - void SetTexCoords(unsigned int vertexIndex, const NzVector2f& texCoords); - - private: - void InterpolateImpl(unsigned int frameA, unsigned int frameB, float interpolation) const; - - NzKeyframeMeshImpl* m_impl = nullptr; -}; - -#endif // NAZARA_KEYFRAMEMESH_HPP diff --git a/include/Nazara/Utility/Mesh.hpp b/include/Nazara/Utility/Mesh.hpp index 23dc26c2f..535465fa2 100644 --- a/include/Nazara/Utility/Mesh.hpp +++ b/include/Nazara/Utility/Mesh.hpp @@ -51,7 +51,6 @@ class NAZARA_API NzMesh : public NzResource, NzResourceListener bool AddSubMesh(NzSubMesh* subMesh); bool AddSubMesh(const NzString& identifier, NzSubMesh* subMesh); - bool CreateKeyframe(); bool CreateSkeletal(unsigned int jointCount); bool CreateStatic(); void Destroy(); diff --git a/src/Nazara/Utility/Animation.cpp b/src/Nazara/Utility/Animation.cpp index 510badf58..dc8e1adae 100644 --- a/src/Nazara/Utility/Animation.cpp +++ b/src/Nazara/Utility/Animation.cpp @@ -154,26 +154,6 @@ void NzAnimation::AnimateSkeleton(NzSkeleton* targetSkeleton, unsigned int frame } } -bool NzAnimation::CreateKeyframe(unsigned int frameCount) -{ - 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->type = nzAnimationType_Keyframe; - - NotifyCreated(); - return true; -} - bool NzAnimation::CreateSkeletal(unsigned int frameCount, unsigned int jointCount) { Destroy(); diff --git a/src/Nazara/Utility/KeyframeMesh.cpp b/src/Nazara/Utility/KeyframeMesh.cpp deleted file mode 100644 index b00911b11..000000000 --- a/src/Nazara/Utility/KeyframeMesh.cpp +++ /dev/null @@ -1,608 +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 -#include -#include -#include -#include -#include -#include - -struct NzKeyframeMeshImpl -{ - NzCubef* aabb; - NzVector2f* uv; - NzVector3f* normals; - NzVector3f* positions; - NzVector3f* tangents; - const NzIndexBuffer* indexBuffer = nullptr; - NzVertexBuffer* vertexBuffer; - unsigned int frameCount; -}; - -NzKeyframeMesh::NzKeyframeMesh(const NzMesh* parent) : -NzSubMesh(parent) -{ -} - -NzKeyframeMesh::~NzKeyframeMesh() -{ - Destroy(); -} - -bool NzKeyframeMesh::Create(NzVertexBuffer* vertexBuffer, unsigned int frameCount) -{ - 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 NzCubef[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]; - m_impl->uv = new NzVector2f[vertexCount]; - - 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->uv; - - delete m_impl; - m_impl = nullptr; - } -} - -void NzKeyframeMesh::Finish() -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return; - } - #endif - - GenerateAABBs(); - InterpolateImpl(0, 0, 0.f); -} - -void NzKeyframeMesh::GenerateAABBs() -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return; - } - #endif - - unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount(); - for (unsigned int i = 0; i < m_impl->frameCount; ++i) - { - NzCubef& aabb = m_impl->aabb[i+1]; // l'AABB 0 est celle qui est interpolée - - // Génération de l'AABB selon la position - unsigned int index = i*vertexCount; - for (unsigned int j = 0; j < vertexCount; ++j) - aabb.ExtendTo(m_impl->positions[index+j]); - } -} - -const NzCubef& NzKeyframeMesh::GetAABB() const -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - - static NzCubef dummy; - return dummy; - } - #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; -} - -NzVector3f NzKeyframeMesh::GetNormal(unsigned int frameIndex, unsigned int vertexIndex) const -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return NzVector3f(); - } - - if (frameIndex >= m_impl->frameCount) - { - NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); - return NzVector3f(); - } - #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 NzVector3f(); - } - #endif - - unsigned int index = frameIndex*vertexCount + vertexIndex; - - return m_impl->normals[index]; -} - -NzVector3f NzKeyframeMesh::GetPosition(unsigned int frameIndex, unsigned int vertexIndex) const -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return NzVector3f(); - } - - if (frameIndex >= m_impl->frameCount) - { - NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); - return NzVector3f(); - } - #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 NzVector3f(); - } - #endif - - unsigned int index = frameIndex*vertexCount + vertexIndex; - - return m_impl->positions[index]; -} - -NzVector3f NzKeyframeMesh::GetTangent(unsigned int frameIndex, unsigned int vertexIndex) const -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return NzVector3f(); - } - - if (frameIndex >= m_impl->frameCount) - { - NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); - return NzVector3f(); - } - #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 NzVector3f(); - } - #endif - - unsigned int index = frameIndex*vertexCount + vertexIndex; - - return m_impl->tangents[index]; -} - -NzVector2f NzKeyframeMesh::GetTexCoords(unsigned int vertexIndex) const -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return NzVector2f(); - } - - if (vertexIndex >= m_impl->vertexBuffer->GetVertexCount()) - { - NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(m_impl->vertexBuffer->GetVertexCount()) + ')'); - return NzVector2f(); - } - #endif - - return m_impl->uv[vertexIndex]; -} - -void NzKeyframeMesh::GetVertex(unsigned int frameIndex, unsigned int vertexIndex, NzMeshVertex* dest) const -{ - #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 - - 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; - } - #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]; - dest->uv = m_impl->uv[vertexIndex]; -} - -NzVertexBuffer* NzKeyframeMesh::GetVertexBuffer() -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return nullptr; - } - #endif - - return m_impl->vertexBuffer; -} - -const NzVertexBuffer* NzKeyframeMesh::GetVertexBuffer() const -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return nullptr; - } - #endif - - return m_impl->vertexBuffer; -} - -unsigned int NzKeyframeMesh::GetVertexCount() const -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return 0; - } - #endif - - return m_impl->vertexBuffer->GetVertexCount(); -} - -void NzKeyframeMesh::Interpolate(const NzAnimation* animation, unsigned int frameA, unsigned int frameB, float interpolation) const -{ - #if NAZARA_UTILITY_SAFE - if (!animation || !animation->IsValid()) - { - NazaraError("Animation must be valid"); - return; - } - - if (animation->GetType() != nzAnimationType_Keyframe) - { - NazaraError("Animation must be of keyframe type"); - return; - } - - unsigned int frameCount = animation->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; -} - -void NzKeyframeMesh::SetAABB(unsigned int frameIndex, const NzCubef& 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; -} - -void NzKeyframeMesh::SetNormal(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& normal) -{ - #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 - - 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; - } - #endif - - unsigned int index = frameIndex*vertexCount + vertexIndex; - - m_impl->normals[index] = normal; -} - -void NzKeyframeMesh::SetPosition(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& position) -{ - #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 - - 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; - } - #endif - - unsigned int index = frameIndex*vertexCount + vertexIndex; - - m_impl->positions[index] = position; -} - -void NzKeyframeMesh::SetTangent(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& tangent) -{ - #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 - - 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; - } - #endif - - unsigned int index = frameIndex*vertexCount + vertexIndex; - - m_impl->tangents[index] = tangent; -} - -void NzKeyframeMesh::SetTexCoords(unsigned int vertexIndex, const NzVector2f& uv) -{ - #if NAZARA_UTILITY_SAFE - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return; - } - - if (vertexIndex >= m_impl->vertexBuffer->GetVertexCount()) - { - NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(m_impl->vertexBuffer->GetVertexCount()) + ')'); - return; - } - #endif - - m_impl->uv[vertexIndex] = uv; -} - -void NzKeyframeMesh::InterpolateImpl(unsigned int frameA, unsigned int frameB, float interpolation) const -{ - #ifdef NAZARA_DEBUG - if (!m_impl) - { - NazaraError("Keyframe mesh not created"); - return; - } - #endif - - // Interpolation de l'AABB - m_impl->aabb[0] = NzCubef::Lerp(m_impl->aabb[frameA+1], m_impl->aabb[frameB+1], interpolation); - - NzMeshVertex* vertex = reinterpret_cast(m_impl->vertexBuffer->Map(nzBufferAccess_DiscardAndWrite)); - if (!vertex) - { - NazaraError("Failed to map vertex buffer"); - return; - } - - unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount(); - - // Adaptation des indices - frameA *= vertexCount; - frameB *= vertexCount; - - 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->uv = m_impl->uv[i]; - - vertex->normal.Normalize(); - vertex->tangent.Normalize(); - - vertex++; - } - - m_impl->vertexBuffer->Unmap(); -} diff --git a/src/Nazara/Utility/Loaders/MD2/Loader.cpp b/src/Nazara/Utility/Loaders/MD2/Loader.cpp index 27e8daeec..15479543c 100644 --- a/src/Nazara/Utility/Loaders/MD2/Loader.cpp +++ b/src/Nazara/Utility/Loaders/MD2/Loader.cpp @@ -8,9 +8,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -22,7 +20,7 @@ namespace { /// Loader de mesh - bool CheckMesh(NzInputStream& stream, const NzMeshParams& parameters) + bool Check(NzInputStream& stream, const NzMeshParams& parameters) { NazaraUnused(parameters); @@ -38,7 +36,7 @@ namespace return magic[0] == md2Ident && magic[1] == 8; } - bool LoadMesh(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)) @@ -72,20 +70,13 @@ namespace } /// Création du mesh - // Animé ou statique, c'est la question - if (parameters.animated) - mesh->CreateKeyframe(); - else - mesh->CreateStatic(); - - if (!mesh->IsValid()) // Ne devrait jamais échouer + // Le moteur ne supporte plus les animations image-clé, nous ne pouvons charger qu'en statique + if (!mesh->CreateStatic()) // Ne devrait jamais échouer { NazaraInternalError("Failed to create mesh"); return false; } - mesh->SetAnimation(stream.GetPath()); // Même fichier - /// Chargement des skins if (header.num_skins > 0) { @@ -152,257 +143,77 @@ namespace const unsigned int indexFix[3] = {0, 2, 1}; // Pour respécifier les indices dans le bon ordre + std::unique_ptr vertexBuffer(new NzVertexBuffer(NzMesh::GetDeclaration(), header.num_vertices, parameters.storage, nzBufferUsage_Static)); + std::unique_ptr subMesh(new NzStaticMesh(mesh)); + if (!subMesh->Create(vertexBuffer.get())) + { + NazaraError("Failed to create SubMesh"); + return false; + } + + subMesh->SetIndexBuffer(indexBuffer.get()); + + indexBuffer->SetPersistent(false); + indexBuffer.release(); + + /// Chargement des vertices + stream.SetCursorPos(header.offset_frames); + + std::unique_ptr vertices(new md2_vertex[header.num_vertices]); + NzVector3f scale, translate; + stream.Read(scale, sizeof(NzVector3f)); + stream.Read(translate, sizeof(NzVector3f)); + stream.Read(nullptr, 16*sizeof(char)); // On avance en ignorant le nom de la frame (Géré par l'animation) + stream.Read(vertices.get(), 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 + + NzBufferMapper vertexMapper(vertexBuffer.get(), nzBufferAccess_DiscardAndWrite); + NzMeshVertex* vertex = reinterpret_cast(vertexMapper.GetPointer()); + + /// Chargement des coordonnées de texture + for (unsigned int i = 0; i < header.num_tris; ++i) + { + for (unsigned int j = 0; j < 3; ++j) + { + const unsigned int fixedIndex = indexFix[j]; + const md2_texCoord& texC = texCoords[triangles[i].texCoords[fixedIndex]]; + vertex[triangles[i].vertices[fixedIndex]].uv.Set(static_cast(texC.u) / header.skinwidth, 1.f - static_cast(texC.v)/header.skinheight); + } + } + + /// Chargement des positions // 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); - if (parameters.animated) + for (unsigned int v = 0; v < header.num_vertices; ++v) { - std::unique_ptr vertexBuffer(new NzVertexBuffer(NzMesh::GetDeclaration(), header.num_vertices, parameters.storage, nzBufferUsage_Dynamic)); - std::unique_ptr subMesh(new NzKeyframeMesh(mesh)); - if (!subMesh->Create(vertexBuffer.get(), header.num_frames)) - { - NazaraError("Failed to create SubMesh"); - return false; - } + const md2_vertex& vert = vertices[v]; + NzVector3f position = rotationQuat * NzVector3f(vert.x*scale.x + translate.x, vert.y*scale.y + translate.y, vert.z*scale.z + translate.z); - subMesh->SetIndexBuffer(indexBuffer.get()); + vertex->position = position; + vertex->normal = rotationQuat * md2Normals[vert.n]; - indexBuffer->SetPersistent(false); - indexBuffer.release(); - - vertexBuffer->SetPersistent(false); - vertexBuffer.release(); - - /// Chargement des frames - stream.SetCursorPos(header.offset_frames); - - std::unique_ptr vertices(new md2_vertex[header.num_vertices]); - for (unsigned int f = 0; f < header.num_frames; ++f) - { - NzVector3f scale, translate; - - stream.Read(scale, sizeof(NzVector3f)); - stream.Read(translate, sizeof(NzVector3f)); - stream.Read(nullptr, 16*sizeof(char)); // On avance en ignorant le nom de la frame (Géré par l'animation) - stream.Read(vertices.get(), 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 - - for (unsigned int v = 0; v < header.num_vertices; ++v) - { - const md2_vertex& vert = vertices[v]; - NzVector3f position = rotationQuat * NzVector3f(vert.x * scale.x + translate.x, vert.y * scale.y + translate.y, vert.z * scale.z + translate.z); - - subMesh->SetNormal(f, v, rotationQuat * md2Normals[vert.n]); - subMesh->SetPosition(f, v, position); - } - } - - /// Chargement des coordonnées de texture - for (unsigned int i = 0; i < header.num_tris; ++i) - { - for (unsigned int j = 0; j < 3; ++j) - { - const unsigned int fixedIndex = indexFix[j]; - const md2_texCoord& texC = texCoords[triangles[i].texCoords[fixedIndex]]; - subMesh->SetTexCoords(triangles[i].vertices[fixedIndex], NzVector2f(static_cast(texC.u) / header.skinwidth, 1.f - static_cast(texC.v)/header.skinheight)); - } - } - - subMesh->SetMaterialIndex(0); - mesh->AddSubMesh(subMesh.release()); - } - else - { - std::unique_ptr vertexBuffer(new NzVertexBuffer(NzMesh::GetDeclaration(), header.num_vertices, parameters.storage, nzBufferUsage_Static)); - std::unique_ptr subMesh(new NzStaticMesh(mesh)); - if (!subMesh->Create(vertexBuffer.get())) - { - NazaraError("Failed to create SubMesh"); - return false; - } - - subMesh->SetIndexBuffer(indexBuffer.get()); - - indexBuffer->SetPersistent(false); - indexBuffer.release(); - - /// Chargement des vertices - stream.SetCursorPos(header.offset_frames); - - std::unique_ptr vertices(new md2_vertex[header.num_vertices]); - NzVector3f scale, translate; - stream.Read(scale, sizeof(NzVector3f)); - stream.Read(translate, sizeof(NzVector3f)); - stream.Read(nullptr, 16*sizeof(char)); // On avance en ignorant le nom de la frame (Géré par l'animation) - stream.Read(vertices.get(), 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 - - NzBufferMapper vertexMapper(vertexBuffer.get(), nzBufferAccess_DiscardAndWrite); - NzMeshVertex* vertex = reinterpret_cast(vertexMapper.GetPointer()); - - /// Chargement des coordonnées de texture - for (unsigned int i = 0; i < header.num_tris; ++i) - { - for (unsigned int j = 0; j < 3; ++j) - { - const unsigned int fixedIndex = indexFix[j]; - const md2_texCoord& texC = texCoords[triangles[i].texCoords[fixedIndex]]; - vertex[triangles[i].vertices[fixedIndex]].uv.Set(static_cast(texC.u) / header.skinwidth, 1.f - static_cast(texC.v)/header.skinheight); - } - } - - /// Chargement des positions - for (unsigned int v = 0; v < header.num_vertices; ++v) - { - const md2_vertex& vert = vertices[v]; - NzVector3f position = rotationQuat * NzVector3f(vert.x*scale.x + translate.x, vert.y*scale.y + translate.y, vert.z*scale.z + translate.z); - - vertex->position = position; - vertex->normal = rotationQuat * md2Normals[vert.n]; - - vertex++; - } - - vertexMapper.Unmap(); - - vertexBuffer->SetPersistent(false); - vertexBuffer.release(); - - subMesh->SetMaterialIndex(0); - mesh->AddSubMesh(subMesh.release()); - - mesh->GenerateTangents(); + vertex++; } - return true; - } + vertexMapper.Unmap(); - /// Loader d'animations - bool CheckAnim(NzInputStream& stream, const NzAnimationParams& parameters) - { - NazaraUnused(parameters); + vertexBuffer->SetPersistent(false); + vertexBuffer.release(); - nzUInt32 magic[2]; - if (stream.Read(&magic[0], 2*sizeof(nzUInt32)) != 2*sizeof(nzUInt32)) - return false; + subMesh->SetMaterialIndex(0); + mesh->AddSubMesh(subMesh.release()); - #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(header.num_frames-1)); - unsigned int endFrame = std::min(parameters.endFrame, static_cast(header.num_frames-1)); - - unsigned int frameCount = endFrame - startFrame + 1; - if (!animation->CreateKeyframe(frameCount)) - { - NazaraInternalError("Failed to create animaton"); - return false; - } - - // Le MD2 requiert une interpolation de la dernière à la première frame (en cas de loop) - animation->EnableLoopPointInterpolation(true); - - // 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); + mesh->GenerateTangents(); return true; } @@ -410,12 +221,10 @@ namespace void NzLoaders_MD2_Register() { - NzAnimationLoader::RegisterLoader("md2", CheckAnim, LoadAnim); - NzMeshLoader::RegisterLoader("md2", CheckMesh, LoadMesh); + NzMeshLoader::RegisterLoader("md2", Check, Load); } void NzLoaders_MD2_Unregister() { - NzAnimationLoader::UnregisterLoader("md2", CheckAnim, LoadAnim); - NzMeshLoader::UnregisterLoader("md2", CheckMesh, LoadMesh); + NzMeshLoader::UnregisterLoader("md2", Check, Load); } diff --git a/src/Nazara/Utility/Mesh.cpp b/src/Nazara/Utility/Mesh.cpp index 933f49fac..9135e36dd 100644 --- a/src/Nazara/Utility/Mesh.cpp +++ b/src/Nazara/Utility/Mesh.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -131,17 +130,6 @@ bool NzMesh::AddSubMesh(const NzString& identifier, NzSubMesh* subMesh) return true; } -bool NzMesh::CreateKeyframe() -{ - Destroy(); - - m_impl = new NzMeshImpl; - m_impl->animationType = nzAnimationType_Keyframe; - - NotifyCreated(); - return true; -} - bool NzMesh::CreateSkeletal(unsigned int jointCount) { Destroy(); diff --git a/src/Nazara/Utility/VertexMapper.cpp b/src/Nazara/Utility/VertexMapper.cpp index 1ff8c0738..ec50cb656 100644 --- a/src/Nazara/Utility/VertexMapper.cpp +++ b/src/Nazara/Utility/VertexMapper.cpp @@ -6,7 +6,6 @@ #include #include #include // NzMeshVertex -#include #include #include #include @@ -56,70 +55,6 @@ namespace NzSubMesh* m_subMesh; }; - class KeyframeMeshVertexMapper : public SubMeshVertexMapper - { - public: - KeyframeMeshVertexMapper(NzKeyframeMesh* subMesh) : - SubMeshVertexMapper(subMesh), - m_mesh(subMesh) - { - m_vertexPerFrame = m_mesh->GetVertexCount(); - } - - virtual ~KeyframeMeshVertexMapper() noexcept - { - } - - NzVector3f GetNormal(unsigned int i) const - { - return m_mesh->GetNormal(i/m_vertexPerFrame, i%m_vertexPerFrame); - } - - NzVector3f GetPosition(unsigned int i) const - { - return m_mesh->GetPosition(i/m_vertexPerFrame, i%m_vertexPerFrame); - } - - NzVector3f GetTangent(unsigned int i) const - { - return m_mesh->GetTangent(i/m_vertexPerFrame, i%m_vertexPerFrame); - } - - NzVector2f GetTexCoords(unsigned int i) const - { - return m_mesh->GetTexCoords(i%m_vertexPerFrame); - } - - unsigned int GetVertexCount() const - { - return m_vertexPerFrame*m_mesh->GetFrameCount(); - } - - void SetNormal(unsigned int i, const NzVector3f& normal) - { - m_mesh->SetNormal(i/m_vertexPerFrame, i%m_vertexPerFrame, normal); - } - - void SetPosition(unsigned int i, const NzVector3f& position) - { - m_mesh->SetPosition(i/m_vertexPerFrame, i%m_vertexPerFrame, position); - } - - void SetTangent(unsigned int i, const NzVector3f& tangent) - { - m_mesh->SetTangent(i/m_vertexPerFrame, i%m_vertexPerFrame, tangent); - } - - void SetTexCoords(unsigned int i, const NzVector2f& texCoords) - { - m_mesh->SetTexCoords(i%m_vertexPerFrame, texCoords); - } - - private: - NzKeyframeMesh* m_mesh; - unsigned int m_vertexPerFrame; - }; - class SkeletalMeshVertexMapper : public SubMeshVertexMapper { public: @@ -256,10 +191,6 @@ NzVertexMapper::NzVertexMapper(NzSubMesh* subMesh) { switch (subMesh->GetAnimationType()) { - case nzAnimationType_Keyframe: - m_impl = new KeyframeMeshVertexMapper(static_cast(subMesh)); - break; - case nzAnimationType_Skeletal: m_impl = new SkeletalMeshVertexMapper(static_cast(subMesh)); break; @@ -268,6 +199,7 @@ NzVertexMapper::NzVertexMapper(NzSubMesh* subMesh) m_impl = new StaticMeshVertexMapper(static_cast(subMesh)); break; } + #ifdef NAZARA_DEBUG if (!m_impl) NazaraInternalError("No impl"); ///TODO: Internal, Unexpected