diff --git a/include/Nazara/Graphics/ParticleMapper.inl b/include/Nazara/Graphics/ParticleMapper.inl index 289fdf045..b60eab4bf 100644 --- a/include/Nazara/Graphics/ParticleMapper.inl +++ b/include/Nazara/Graphics/ParticleMapper.inl @@ -1,9 +1,10 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Engine - Graphics module" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include +#include namespace Nz { @@ -26,7 +27,7 @@ namespace Nz std::size_t offset; m_declaration->GetComponent(component, &enabled, &type, &offset); - if (enabled) + if (enabled && GetComponentTypeOf() == type) { ///TODO: Check the ratio between the type of the attribute and the template type ? return SparsePtr(m_ptr + offset, m_declaration->GetStride()); @@ -57,7 +58,7 @@ namespace Nz std::size_t offset; m_declaration->GetComponent(component, &enabled, &type, &offset); - if (enabled) + if (enabled && GetComponentTypeOf() == type) { ///TODO: Check the ratio between the type of the attribute and the template type ? return SparsePtr(m_ptr + offset, m_declaration->GetStride()); diff --git a/include/Nazara/Utility/Algorithm.hpp b/include/Nazara/Utility/Algorithm.hpp index da7ab56eb..d3cc0475d 100644 --- a/include/Nazara/Utility/Algorithm.hpp +++ b/include/Nazara/Utility/Algorithm.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 @@ -8,18 +8,24 @@ #define NAZARA_ALGORITHM_UTILITY_HPP #include +#include #include #include #include +#include #include #include +#include #include -#include -#include namespace Nz { class Joint; + class VertexStruct_XYZ_Normal_UV_Tangent; + class VertexStruct_XYZ_Normal_UV_Tangent_Skinning; + + using MeshVertex = VertexStruct_XYZ_Normal_UV_Tangent; + using SkeletalMeshVertex = VertexStruct_XYZ_Normal_UV_Tangent_Skinning; struct SkinningData { @@ -59,6 +65,10 @@ namespace Nz NAZARA_UTILITY_API void SkinPositionNormalTangent(const SkinningData& data, unsigned int startVertex, unsigned int vertexCount); NAZARA_UTILITY_API void TransformVertices(VertexPointers vertexPointers, unsigned int vertexCount, const Matrix4f& matrix); + + template constexpr ComponentType ComponentTypeId(); + template constexpr const ComponentType GetComponentTypeOf(); } +#include #endif // NAZARA_ALGORITHM_UTILITY_HPP diff --git a/include/Nazara/Utility/Algorithm.inl b/include/Nazara/Utility/Algorithm.inl new file mode 100644 index 000000000..61bdb9e77 --- /dev/null +++ b/include/Nazara/Utility/Algorithm.inl @@ -0,0 +1,45 @@ +#pragma once + +#include +#include + +namespace Nz +{ + namespace Detail + { + template + struct IsSuitableForComponent + { + constexpr static bool value = false; + }; + } + + template constexpr ComponentType ComponentTypeId() + { + static_assert(Detail::IsSuitableForComponent::value, "This type cannot be used as a component."); + return ComponentType{}; + } + + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Color; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double1; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double2; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double3; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double4; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float1; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float2; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float3; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float4; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int1; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int2; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int3; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int4; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Quaternion; } + + template + constexpr const ComponentType GetComponentTypeOf() + { + return ComponentTypeId>(); + } +} + +#include \ No newline at end of file diff --git a/include/Nazara/Utility/Mesh.hpp b/include/Nazara/Utility/Mesh.hpp index 90d90f4ce..eb93ac783 100644 --- a/include/Nazara/Utility/Mesh.hpp +++ b/include/Nazara/Utility/Mesh.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 @@ -36,6 +36,13 @@ namespace Nz bool center = false; ///< If true, will center the mesh vertices around the origin bool optimizeIndexBuffers = true; ///< Optimize the index buffers after loading, improve cache locality (and thus rendering speed) but increase loading time. + /* The declaration must have a Vector3f position component enabled + * If the declaration has a Vector2f UV component enabled, UV are generated + * If the declaration has a Vector3f Normals component enabled, Normals are generated. + * If the declaration has a Vector3f Tangents component enabled, Tangents are generated. + */ + VertexDeclaration* vertexDeclaration = VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent); + bool IsValid() const; }; diff --git a/include/Nazara/Utility/VertexDeclaration.hpp b/include/Nazara/Utility/VertexDeclaration.hpp index 594f1b5f0..1490857cf 100644 --- a/include/Nazara/Utility/VertexDeclaration.hpp +++ b/include/Nazara/Utility/VertexDeclaration.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 @@ -38,6 +38,8 @@ namespace Nz void EnableComponent(VertexComponent component, ComponentType type, std::size_t offset); void GetComponent(VertexComponent component, bool* enabled, ComponentType* type, std::size_t* offset) const; + bool HasComponent(VertexComponent component) const; + template bool HasComponentOfType(VertexComponent component) const; std::size_t GetStride() const; void SetStride(std::size_t stride); diff --git a/include/Nazara/Utility/VertexDeclaration.inl b/include/Nazara/Utility/VertexDeclaration.inl index 26cd39736..8f8e74179 100644 --- a/include/Nazara/Utility/VertexDeclaration.inl +++ b/include/Nazara/Utility/VertexDeclaration.inl @@ -1,9 +1,10 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 namespace Nz { @@ -15,6 +16,17 @@ namespace Nz return object.release(); } + + template + bool VertexDeclaration::HasComponentOfType(VertexComponent component) const + { + bool enabled; + Nz::ComponentType type; + + GetComponent(component, &enabled, &type, nullptr); + + return enabled && GetComponentTypeOf() == type; + } } #include diff --git a/include/Nazara/Utility/VertexMapper.hpp b/include/Nazara/Utility/VertexMapper.hpp index 486d000fd..cfa27efe7 100644 --- a/include/Nazara/Utility/VertexMapper.hpp +++ b/include/Nazara/Utility/VertexMapper.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 @@ -27,6 +27,7 @@ namespace Nz ~VertexMapper(); template SparsePtr GetComponentPtr(VertexComponent component); + template bool HasComponentOfType(VertexComponent component) const; void Unmap(); diff --git a/include/Nazara/Utility/VertexMapper.inl b/include/Nazara/Utility/VertexMapper.inl index 6892f9091..3c4a95959 100644 --- a/include/Nazara/Utility/VertexMapper.inl +++ b/include/Nazara/Utility/VertexMapper.inl @@ -1,9 +1,10 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 namespace Nz { @@ -19,7 +20,7 @@ namespace Nz std::size_t offset; declaration->GetComponent(component, &enabled, &type, &offset); - if (enabled) + if (enabled && GetComponentTypeOf() == type) { ///TODO: Vérifier le rapport entre le type de l'attribut et le type template ? return SparsePtr(static_cast(m_mapper.GetPointer()) + offset, declaration->GetStride()); @@ -30,6 +31,12 @@ namespace Nz return SparsePtr(); } } + + template + bool VertexMapper::HasComponentOfType(VertexComponent component) const + { + return m_mapper.GetBuffer()->GetVertexDeclaration()->HasComponentOfType(component); + } } #include diff --git a/src/Nazara/Graphics/ParticleDeclaration.cpp b/src/Nazara/Graphics/ParticleDeclaration.cpp index e618abb13..a9a8e983d 100644 --- a/src/Nazara/Graphics/ParticleDeclaration.cpp +++ b/src/Nazara/Graphics/ParticleDeclaration.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Engine - Graphics module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -286,7 +286,7 @@ namespace Nz // ParticleLayout_Billboard : ParticleStruct_Billboard declaration = &s_declarations[ParticleLayout_Billboard]; declaration->EnableComponent(ParticleComponent_Color, ComponentType_Color, NazaraOffsetOf(ParticleStruct_Billboard, color)); - declaration->EnableComponent(ParticleComponent_Life, ComponentType_Int1, NazaraOffsetOf(ParticleStruct_Billboard, life)); + declaration->EnableComponent(ParticleComponent_Life, ComponentType_Float1, NazaraOffsetOf(ParticleStruct_Billboard, life)); declaration->EnableComponent(ParticleComponent_Normal, ComponentType_Float3, NazaraOffsetOf(ParticleStruct_Billboard, normal)); declaration->EnableComponent(ParticleComponent_Position, ComponentType_Float3, NazaraOffsetOf(ParticleStruct_Billboard, position)); declaration->EnableComponent(ParticleComponent_Rotation, ComponentType_Float1, NazaraOffsetOf(ParticleStruct_Billboard, rotation)); @@ -297,7 +297,7 @@ namespace Nz // ParticleLayout_Model : ParticleStruct_Model declaration = &s_declarations[ParticleLayout_Model]; - declaration->EnableComponent(ParticleComponent_Life, ComponentType_Int1, NazaraOffsetOf(ParticleStruct_Model, life)); + declaration->EnableComponent(ParticleComponent_Life, ComponentType_Float1, NazaraOffsetOf(ParticleStruct_Model, life)); declaration->EnableComponent(ParticleComponent_Position, ComponentType_Float3, NazaraOffsetOf(ParticleStruct_Model, position)); declaration->EnableComponent(ParticleComponent_Rotation, ComponentType_Quaternion, NazaraOffsetOf(ParticleStruct_Model, rotation)); declaration->EnableComponent(ParticleComponent_Velocity, ComponentType_Float3, NazaraOffsetOf(ParticleStruct_Model, velocity)); @@ -307,7 +307,7 @@ namespace Nz // ParticleLayout_Sprite : ParticleStruct_Sprite declaration = &s_declarations[ParticleLayout_Sprite]; declaration->EnableComponent(ParticleComponent_Color, ComponentType_Color, NazaraOffsetOf(ParticleStruct_Sprite, color)); - declaration->EnableComponent(ParticleComponent_Life, ComponentType_Int1, NazaraOffsetOf(ParticleStruct_Sprite, life)); + declaration->EnableComponent(ParticleComponent_Life, ComponentType_Float1, NazaraOffsetOf(ParticleStruct_Sprite, life)); declaration->EnableComponent(ParticleComponent_Position, ComponentType_Float3, NazaraOffsetOf(ParticleStruct_Sprite, position)); declaration->EnableComponent(ParticleComponent_Rotation, ComponentType_Float1, NazaraOffsetOf(ParticleStruct_Sprite, rotation)); declaration->EnableComponent(ParticleComponent_Velocity, ComponentType_Float3, NazaraOffsetOf(ParticleStruct_Sprite, velocity)); diff --git a/src/Nazara/Graphics/SkinningManager.cpp b/src/Nazara/Graphics/SkinningManager.cpp index 5f36ebe81..6460a1f1c 100644 --- a/src/Nazara/Graphics/SkinningManager.cpp +++ b/src/Nazara/Graphics/SkinningManager.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Engine - Graphics module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include diff --git a/src/Nazara/Utility/AlgorithmUtility.cpp b/src/Nazara/Utility/AlgorithmUtility.cpp index a672abfa0..111c45f1e 100644 --- a/src/Nazara/Utility/AlgorithmUtility.cpp +++ b/src/Nazara/Utility/AlgorithmUtility.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 @@ -31,6 +31,8 @@ #include #include #include +#include +#include namespace Nz { diff --git a/src/Nazara/Utility/Mesh.cpp b/src/Nazara/Utility/Mesh.cpp index 6d5a98b20..8feef9f8d 100644 --- a/src/Nazara/Utility/Mesh.cpp +++ b/src/Nazara/Utility/Mesh.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 @@ -41,6 +41,12 @@ namespace Nz return false; } + if (vertexDeclaration == nullptr) + { + NazaraError("The vertex declaration can't be null"); + return false; + } + return true; } @@ -101,6 +107,7 @@ namespace Nz NazaraAssert(m_impl, "Mesh should be created first"); NazaraAssert(m_impl->animationType == AnimationType_Static, "Submesh building only works for static meshes"); NazaraAssert(params.IsValid(), "Invalid parameters"); + NazaraAssert(params.vertexDeclaration->HasComponentOfType(VertexComponent_Position), "The vertex declaration doesn't have a Vector3 position component"); Boxf aabb; IndexBufferRef indexBuffer; @@ -109,7 +116,7 @@ namespace Nz Matrix4f matrix(primitive.matrix); matrix *= params.matrix; - VertexDeclaration* declaration = VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent); + VertexDeclaration* declaration = params.vertexDeclaration; switch (primitive.type) { diff --git a/src/Nazara/Utility/VertexDeclaration.cpp b/src/Nazara/Utility/VertexDeclaration.cpp index 43a214f30..7edbd3124 100644 --- a/src/Nazara/Utility/VertexDeclaration.cpp +++ b/src/Nazara/Utility/VertexDeclaration.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 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 @@ -120,6 +120,15 @@ namespace Nz *offset = vertexComponent.offset; } + bool VertexDeclaration::HasComponent(VertexComponent component) const + { + bool enabled; + + GetComponent(component, &enabled, nullptr, nullptr); + + return enabled; + } + std::size_t VertexDeclaration::GetStride() const { return m_stride;