Upgrade Utility

This commit is contained in:
Jérôme Leclercq
2021-05-24 19:10:53 +02:00
parent b936946154
commit cce32a64d4
120 changed files with 2328 additions and 2971 deletions

View File

@@ -36,6 +36,7 @@ SOFTWARE.
#include <Nazara/Utility/Skeleton.hpp>
#include <Nazara/Utility/StaticMesh.hpp>
#include <Nazara/Utility/VertexMapper.hpp>
#include <Nazara/Utility/Utility.hpp>
#include <assimp/cfileio.h>
#include <assimp/cimport.h>
#include <assimp/config.h>
@@ -74,9 +75,13 @@ void ProcessJoints(aiNode* node, Skeleton* skeleton, const std::set<std::string>
ProcessJoints(node->mChildren[i], skeleton, joints);
}
bool IsSupported(const std::string& extension)
bool IsSupported(const std::string_view& extension)
{
std::string dotExt = '.' + extension;
std::string dotExt;
dotExt.reserve(extension.size() + 1);
dotExt += '.';
dotExt += extension;
return (aiIsExtensionSupported(dotExt.data()) == AI_TRUE);
}
@@ -89,7 +94,7 @@ Ternary CheckAnimation(Stream& /*stream*/, const AnimationParams& parameters)
return Ternary::Unknown;
}
AnimationRef LoadAnimation(Stream& stream, const AnimationParams& parameters)
std::shared_ptr<Animation> LoadAnimation(Stream& stream, const AnimationParams& parameters)
{
std::string streamPath = stream.GetPath().generic_u8string();
@@ -141,7 +146,7 @@ AnimationRef LoadAnimation(Stream& stream, const AnimationParams& parameters)
maxFrameCount = std::max({ maxFrameCount, nodeAnim->mNumPositionKeys, nodeAnim->mNumRotationKeys, nodeAnim->mNumScalingKeys });
}
AnimationRef anim = Animation::New();
std::shared_ptr<Animation> anim = std::make_shared<Animation>();
anim->CreateSkeletal(maxFrameCount, animation->mNumChannels);
@@ -184,7 +189,7 @@ Ternary CheckMesh(Stream& /*stream*/, const MeshParams& parameters)
return Ternary::Unknown;
}
MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
std::shared_ptr<Mesh> LoadMesh(Stream& stream, const MeshParams& parameters)
{
std::string streamPath = stream.GetPath().generic_u8string();
@@ -221,16 +226,16 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
int excludedComponents = 0;
if (!parameters.vertexDeclaration->HasComponent(VertexComponent_Color))
if (!parameters.vertexDeclaration->HasComponent(VertexComponent::Color))
excludedComponents |= aiComponent_COLORS;
if (!parameters.vertexDeclaration->HasComponent(VertexComponent_Normal))
if (!parameters.vertexDeclaration->HasComponent(VertexComponent::Normal))
excludedComponents |= aiComponent_NORMALS;
if (!parameters.vertexDeclaration->HasComponent(VertexComponent_Tangent))
if (!parameters.vertexDeclaration->HasComponent(VertexComponent::Tangent))
excludedComponents |= aiComponent_TANGENTS_AND_BITANGENTS;
if (!parameters.vertexDeclaration->HasComponent(VertexComponent_TexCoord))
if (!parameters.vertexDeclaration->HasComponent(VertexComponent::TexCoord))
excludedComponents |= aiComponent_TEXCOORDS;
aiPropertyStore* properties = aiCreatePropertyStore();
@@ -271,7 +276,7 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
}
}
MeshRef mesh = Mesh::New();
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
if (animatedMesh)
{
mesh->CreateSkeletal(UInt32(joints.size()));
@@ -302,9 +307,9 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
// Index buffer
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
IndexBufferRef indexBuffer = IndexBuffer::New(largeIndices, indexCount, parameters.storage, parameters.indexBufferFlags);
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(largeIndices, indexCount, parameters.storage, parameters.indexBufferFlags);
IndexMapper indexMapper(indexBuffer, BufferAccess_DiscardAndWrite);
IndexMapper indexMapper(*indexBuffer, BufferAccess::DiscardAndWrite);
IndexIterator index = indexMapper.begin();
for (unsigned int faceIdx = 0; faceIdx < iMesh->mNumFaces; ++faceIdx)
@@ -324,8 +329,8 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
if (normalTangentMatrix.HasScale())
normalTangentMatrix.ApplyScale(1.f / normalTangentMatrix.GetScale());
VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent_Skinning), vertexCount, parameters.storage, parameters.vertexBufferFlags);
BufferMapper<VertexBuffer> vertexMapper(vertexBuffer, BufferAccess_ReadWrite);
std::shared_ptr<VertexBuffer> vertexBuffer = std::make_shared<VertexBuffer>(VertexDeclaration::Get(VertexLayout::XYZ_Normal_UV_Tangent_Skinning), vertexCount, parameters.storage, parameters.vertexBufferFlags);
BufferMapper<VertexBuffer> vertexMapper(*vertexBuffer, BufferAccess::ReadWrite);
SkeletalMeshVertex* vertices = static_cast<SkeletalMeshVertex*>(vertexMapper.GetPointer());
for (std::size_t vertexIdx = 0; vertexIdx < vertexCount; ++vertexIdx)
@@ -357,7 +362,7 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
}
// Submesh
SkeletalMeshRef subMesh = SkeletalMesh::New(vertexBuffer, indexBuffer);
std::shared_ptr<SkeletalMesh> subMesh = std::make_shared<SkeletalMesh>(vertexBuffer, indexBuffer);
subMesh->SetMaterialIndex(iMesh->mMaterialIndex);
auto matIt = materials.find(iMesh->mMaterialIndex);
@@ -385,20 +390,20 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
if (wrapKey)
{
SamplerWrap wrap = SamplerWrap_Clamp;
SamplerWrap wrap = SamplerWrap::Clamp;
switch (mapMode[0])
{
case aiTextureMapMode_Clamp:
case aiTextureMapMode_Decal:
wrap = SamplerWrap_Clamp;
wrap = SamplerWrap::Clamp;
break;
case aiTextureMapMode_Mirror:
wrap = SamplerWrap_MirroredRepeat;
wrap = SamplerWrap::MirroredRepeat;
break;
case aiTextureMapMode_Wrap:
wrap = SamplerWrap_Repeat;
wrap = SamplerWrap::Repeat;
break;
default:
@@ -460,9 +465,9 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
// Index buffer
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
IndexBufferRef indexBuffer = IndexBuffer::New(largeIndices, indexCount, parameters.storage, parameters.indexBufferFlags);
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(largeIndices, indexCount, parameters.storage, parameters.indexBufferFlags);
IndexMapper indexMapper(indexBuffer, BufferAccess_DiscardAndWrite);
IndexMapper indexMapper(*indexBuffer, BufferAccess::DiscardAndWrite);
IndexIterator index = indexMapper.begin();
for (unsigned int faceIdx = 0; faceIdx < iMesh->mNumFaces; ++faceIdx)
@@ -484,18 +489,18 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
if (normalTangentMatrix.HasScale())
normalTangentMatrix.ApplyScale(1.f / normalTangentMatrix.GetScale());
VertexBufferRef vertexBuffer = VertexBuffer::New(parameters.vertexDeclaration, vertexCount, parameters.storage, parameters.vertexBufferFlags);
std::shared_ptr<VertexBuffer> vertexBuffer = std::make_shared<VertexBuffer>(parameters.vertexDeclaration, vertexCount, parameters.storage, parameters.vertexBufferFlags);
VertexMapper vertexMapper(vertexBuffer, BufferAccess_DiscardAndWrite);
VertexMapper vertexMapper(*vertexBuffer, BufferAccess::DiscardAndWrite);
auto posPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position);
auto posPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent::Position);
for (unsigned int vertexIdx = 0; vertexIdx < vertexCount; ++vertexIdx)
{
aiVector3D position = iMesh->mVertices[vertexIdx];
*posPtr++ = parameters.matrix * Vector3f(position.x, position.y, position.z);
}
if (auto normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal))
if (auto normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent::Normal))
{
for (unsigned int vertexIdx = 0; vertexIdx < vertexCount; ++vertexIdx)
{
@@ -505,7 +510,7 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
}
bool generateTangents = false;
if (auto tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent))
if (auto tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent::Tangent))
{
if (iMesh->HasTangentsAndBitangents())
{
@@ -519,7 +524,7 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
generateTangents = true;
}
if (auto uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord))
if (auto uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent::TexCoord))
{
if (iMesh->HasTextureCoords(0))
{
@@ -539,7 +544,7 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
vertexMapper.Unmap();
// Submesh
StaticMeshRef subMesh = StaticMesh::New(vertexBuffer, indexBuffer);
std::shared_ptr<StaticMesh> subMesh = std::make_shared<StaticMesh>(vertexBuffer, indexBuffer);
subMesh->GenerateAABB();
subMesh->SetMaterialIndex(iMesh->mMaterialIndex);
@@ -571,20 +576,20 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
if (wrapKey)
{
SamplerWrap wrap = SamplerWrap_Clamp;
SamplerWrap wrap = SamplerWrap::Clamp;
switch (mapMode[0])
{
case aiTextureMapMode_Clamp:
case aiTextureMapMode_Decal:
wrap = SamplerWrap_Clamp;
wrap = SamplerWrap::Clamp;
break;
case aiTextureMapMode_Mirror:
wrap = SamplerWrap_MirroredRepeat;
wrap = SamplerWrap::MirroredRepeat;
break;
case aiTextureMapMode_Wrap:
wrap = SamplerWrap_Repeat;
wrap = SamplerWrap::Repeat;
break;
default:
@@ -636,18 +641,51 @@ MeshRef LoadMesh(Stream& stream, const MeshParams& parameters)
return mesh;
}
namespace
{
const Nz::AnimationLoader::Entry* animationLoaderEntry = nullptr;
const Nz::MeshLoader::Entry* meshLoaderEntry = nullptr;
}
extern "C"
{
NAZARA_EXPORT int PluginLoad()
{
Nz::AnimationLoader::RegisterLoader(IsSupported, CheckAnimation, LoadAnimation);
Nz::MeshLoader::RegisterLoader(IsSupported, CheckMesh, LoadMesh);
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::AnimationLoader& animationLoader = utility->GetAnimationLoader();
animationLoaderEntry = animationLoader.RegisterLoader({
IsSupported,
nullptr,
nullptr,
CheckAnimation,
LoadAnimation
});
Nz::MeshLoader& meshLoader = utility->GetMeshLoader();
meshLoaderEntry = meshLoader.RegisterLoader({
IsSupported,
nullptr,
nullptr,
CheckMesh,
LoadMesh
});
return 1;
}
NAZARA_EXPORT void PluginUnload()
{
Nz::AnimationLoader::RegisterLoader(IsSupported, CheckAnimation, LoadAnimation);
Nz::MeshLoader::UnregisterLoader(IsSupported, CheckMesh, LoadMesh);
Nz::Utility* utility = Nz::Utility::Instance();
NazaraAssert(utility, "utility module is not instancied");
Nz::AnimationLoader& animationLoader = utility->GetAnimationLoader();
animationLoader.UnregisterLoader(animationLoaderEntry);
animationLoaderEntry = nullptr;
Nz::MeshLoader& meshLoader = utility->GetMeshLoader();
meshLoader.UnregisterLoader(meshLoaderEntry);
meshLoaderEntry = nullptr;
}
}