Commit current work

This commit is contained in:
SirLynix 2022-05-13 17:48:35 +02:00 committed by Jérôme Leclercq
parent a7ab2fbaf4
commit b8ea79c40e
12 changed files with 619 additions and 641 deletions

View File

@ -31,7 +31,7 @@ int main()
//rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL;
Nz::Modules<Nz::Graphics> nazara(rendererConfig);
Nz::PluginManager::Mount(Nz::Plugin::Assimp);
Nz::CallOnExit unmountAssimp([]
{
@ -91,22 +91,25 @@ int main()
Nz::MeshParams meshParams;
meshParams.animated = true;
meshParams.center = true;
meshParams.texCoordScale = Nz::Vector2f(1.f, -1.f);
meshParams.texCoordOffset = Nz::Vector2f(0.f, 1.f);
//meshParams.matrix = Nz::Matrix4f::Scale(Nz::Vector3f(10.f));
meshParams.vertexOffset = Nz::Vector3f(3.f, 0.f, 0.f);
meshParams.vertexRotation = Nz::EulerAnglesf(0.f, 0.f, 0.f);
meshParams.vertexScale = Nz::Vector3f(0.1f, 0.1f, 0.1f);
meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent_Skinning);
std::shared_ptr<Nz::Mesh> bobMesh = Nz::Mesh::LoadFromFile(resourceDir / "hellknight/hellknight.md5mesh", meshParams);
std::shared_ptr<Nz::Mesh> bobMesh = Nz::Mesh::LoadFromFile(resourceDir / "character/Gangnam Style.fbx", meshParams);
if (!bobMesh)
{
NazaraError("Failed to load bob mesh");
return __LINE__;
}
Nz::AnimationParams matParams;
matParams.skeleton = bobMesh->GetSkeleton();
Nz::AnimationParams animParam;
animParam.skeleton = bobMesh->GetSkeleton();
animParam.jointRotation = meshParams.vertexRotation;
animParam.jointScale = meshParams.vertexScale;
animParam.jointOffset = meshParams.vertexOffset;
std::shared_ptr<Nz::Animation> bobAnim = Nz::Animation::LoadFromFile(resourceDir / "hellknight/idle.md5anim", matParams);
std::shared_ptr<Nz::Animation> bobAnim = Nz::Animation::LoadFromFile(resourceDir / "character/Wave Hip Hop Dance.fbx", animParam);
if (!bobAnim)
{
NazaraError("Failed to load bob anim");
@ -143,6 +146,7 @@ int main()
std::shared_ptr<Nz::GraphicalMesh> bobGfxMesh = std::make_shared<Nz::GraphicalMesh>(*bobMesh);
std::shared_ptr<Nz::Model> bobModel = std::make_shared<Nz::Model>(std::move(bobGfxMesh), bobAABB);
std::vector<std::shared_ptr<Nz::Material>> materials(bobMesh->GetMaterialCount());
for (std::size_t i = 0; i < bobMesh->GetMaterialCount(); ++i)
{
std::string matPath;
@ -155,12 +159,15 @@ int main()
bobMatPass->EnableDepthBuffer(true);
{
std::filesystem::path path(matPath);
//path.replace_extension(".bmp");
Nz::BasicMaterial basicMat(*bobMatPass);
if (matPath.find("gob") != matPath.npos)
{
bobMatPass->EnableFlag(Nz::MaterialPassFlag::SortByDistance);
basicMat.SetAlphaMap(Nz::Texture::LoadFromFile(matPath, texParams));
basicMat.SetAlphaMap(Nz::Texture::LoadFromFile(path, texParams));
bobMatPass->EnableDepthWrite(false);
bobMatPass->EnableBlending(true);
@ -168,14 +175,17 @@ int main()
bobMatPass->SetBlendFunc(Nz::BlendFunc::SrcAlpha, Nz::BlendFunc::InvSrcAlpha, Nz::BlendFunc::One, Nz::BlendFunc::Zero);
}
else
basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(matPath, texParams));
basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(path, texParams));
}
bobMat->AddPass("ForwardPass", bobMatPass);
bobModel->SetMaterial(i, bobMat);
materials[i] = bobMat;
}
for (std::size_t i = 0; i < bobMesh->GetSubMeshCount(); ++i)
bobModel->SetMaterial(i, materials[bobMesh->GetSubMesh(i)->GetMaterialIndex()]);
/*for (std::size_t y = 0; y < 50; ++y)
{
for (std::size_t x = 0; x < 50; ++x)
@ -195,9 +205,9 @@ int main()
entt::entity bobEntity = registry.create();
{
auto& bobNode = registry.emplace<Nz::NodeComponent>(bobEntity);
bobNode.SetRotation(Nz::EulerAnglesf(90.f, -90.f, 0.f));
bobNode.SetScale(1.f / 40.f * 0.5f);
bobNode.SetPosition(bobNode.GetScale() * Nz::Vector3f(0.f, bobAABB.height / 2.f, 0.f));
//bobNode.SetRotation(Nz::EulerAnglesf(-90.f, -90.f, 0.f));
//bobNode.SetScale(1.f / 40.f * 0.5f);
//bobNode.SetPosition(bobNode.GetScale() * Nz::Vector3f(0.f, -bobAABB.height / 2.f + bobAABB.y, 0.f));
auto& bobGfx = registry.emplace<Nz::GraphicsComponent>(bobEntity);
bobGfx.AttachRenderable(bobModel, 0xFFFFFFFF);

View File

@ -2,5 +2,6 @@ target("Showcase")
set_group("Examples")
set_kind("binary")
add_deps("NazaraAudio", "NazaraGraphics", "NazaraPhysics2D", "NazaraPhysics3D", "NazaraWidgets")
add_deps("PluginAssimp", { links = {} })
add_packages("entt")
add_files("main.cpp")

View File

@ -132,7 +132,7 @@ namespace Nz
static Matrix4 Transform(const Vector3<T>& translation, const Quaternion<T>& rotation);
static Matrix4 Transform(const Vector3<T>& translation, const Quaternion<T>& rotation, const Vector3<T>& scale);
static Matrix4 TransformInverse(const Vector3<T>& translation, const Quaternion<T>& rotation);
static Matrix4 TransformInverse(const Vector3<T>& translation, const Quaternion<T>& rotation, const Vector3<T>&scale);
static Matrix4 TransformInverse(const Vector3<T>& translation, const Quaternion<T>& rotation, const Vector3<T>& scale);
static Matrix4 Zero();
T m11, m12, m13, m14,

View File

@ -1031,6 +1031,16 @@ namespace Nz
return MakeTransform(-(invRot * translation), invRot);
}
template<typename T>
Matrix4<T>& Matrix4<T>::MakeTransformInverse(const Vector3<T>& translation, const Quaternion<T>& rotation, const Vector3<T>& scale)
{
// A view matrix must apply an inverse transformation of the 'world' matrix
Quaternion<T> invRot = rotation.GetConjugate(); // Inverse of the rotation
Vector3<T> invScale = T(1.0) / scale;
return MakeTransform(-(invScale * (invRot * translation)), invRot, invScale);
}
/*!
* \brief Makes the matrix an inverse transform matrix (aka view matrix)
* \return A reference to this matrix
@ -1696,6 +1706,15 @@ namespace Nz
return mat;
}
template<typename T>
Matrix4<T> Matrix4<T>::TransformInverse(const Vector3<T>& translation, const Quaternion<T>& rotation, const Vector3<T>& scale)
{
Matrix4 mat;
mat.MakeTransformInverse(translation, rotation, scale);
return mat;
}
/*!
* \brief Shorthand for the 'view' matrix
* \return A Matrix4 which is the 'view matrix'

View File

@ -13,6 +13,8 @@
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Core/ResourceParameters.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utils/MovablePtr.hpp>
@ -32,6 +34,13 @@ namespace Nz
// Reference skeleton
const Skeleton* skeleton = nullptr;
// Transform joints by these transformations
Vector3f jointOffset = Vector3f::Zero();
Quaternionf jointRotation = Quaternionf::Identity();
Vector3f jointScale = Vector3f::Unit();
bool IsValid() const;
};

View File

@ -38,8 +38,12 @@ namespace Nz
// Buffer usage flags used to build the vertex buffers
BufferUsageFlags vertexBufferFlags = BufferUsage::DirectMapping | BufferUsage::Read | BufferUsage::Write;
// A matrix which will transform every vertex position
Matrix4f matrix = Matrix4f::Identity();
// Transform vertices and joints by these transformations
Vector3f vertexOffset = Vector3f::Zero();
Quaternionf vertexRotation = Quaternionf::Identity();
Vector3f vertexScale = Vector3f::Unit();
// Offset to apply on the texture coordinates (not scaled)
Vector2f texCoordOffset = {0.f, 0.f};

File diff suppressed because it is too large Load Diff

View File

@ -1131,32 +1131,4 @@ namespace Nz
skinningInfos.outputUv[i] = skinningInfos.inputUv[i];
}
}
/*********************************Transform*********************************/
void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix)
{
if (vertexPointers.positionPtr)
{
for (UInt64 i = 0; i < vertexCount; ++i)
*vertexPointers.positionPtr++ = matrix.Transform(*vertexPointers.positionPtr);
}
if (vertexPointers.normalPtr || vertexPointers.tangentPtr)
{
Vector3f scale = matrix.GetScale();
if (vertexPointers.normalPtr)
{
for (UInt64 i = 0; i < vertexCount; ++i)
*vertexPointers.normalPtr++ = matrix.Transform(*vertexPointers.normalPtr, 0.f) / scale;
}
if (vertexPointers.tangentPtr)
{
for (UInt64 i = 0; i < vertexCount; ++i)
*vertexPointers.tangentPtr++ = matrix.Transform(*vertexPointers.tangentPtr, 0.f) / scale;
}
}
}
}

View File

@ -185,7 +185,14 @@ namespace Nz
constexpr float ScaleAdjust = 1.f / 27.8f; // Make a 50 Quake 2 units character a 1.8 unit long
scale *= ScaleAdjust;
scale *= parameters.vertexScale;
translate *= ScaleAdjust;
translate += parameters.vertexOffset;
// Align the model to our coordinates system
Quaternionf rotation = EulerAnglesf(-90.f, 90.f, 0.f);
rotation *= parameters.vertexRotation;
VertexMapper vertexMapper(*vertexBuffer);
@ -208,22 +215,14 @@ namespace Nz
}
}
// Align the model to our coordinates system
Quaternionf rotationQuat = EulerAnglesf(-90.f, 90.f, 0.f);
Nz::Matrix4f matrix = Matrix4f::Transform(translate, rotationQuat, scale);
matrix *= parameters.matrix;
// Vertex normals
if (auto normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent::Normal))
{
Nz::Matrix4f normalMatrix = Matrix4f::Rotate(rotationQuat);
normalMatrix *= parameters.matrix;
for (UInt32 v = 0; v < header.num_vertices; ++v)
{
const MD2_Vertex& vert = vertices[v];
*normalPtr++ = normalMatrix.Transform(md2Normals[vert.n], 0.f);
*normalPtr++ = TransformNormalTRS(rotation, scale, md2Normals[vert.n]);
}
}
@ -234,7 +233,7 @@ namespace Nz
{
const MD2_Vertex& vert = vertices[v];
*posPtr++ = matrix * Vector3f(vert.x, vert.y, vert.z);
*posPtr++ = TransformPositionTRS(translate, rotation, scale, Vector3f(vert.x, vert.y, vert.z));
}
}

View File

@ -66,7 +66,6 @@ namespace Nz
// Le hellknight de Doom 3 fait ~120 unités, et il est dit qu'il fait trois mètres
// Nous réduisons donc la taille générale des fichiers MD5 de 1/40
Matrix4f matrix = Matrix4f::Transform(Nz::Vector3f::Zero(), rotationQuat, Vector3f(1.f / 40.f));
matrix *= parameters.matrix;
rotationQuat = Quaternionf::Identity();
@ -216,7 +215,7 @@ namespace Nz
// Material
ParameterList matData;
matData.SetParameter(MaterialData::FilePath, (baseDir / md5Mesh.shader).generic_u8string());
matData.SetParameter(MaterialData::DiffuseTexturePath, (baseDir / md5Mesh.shader).generic_u8string());
mesh->SetMaterialData(i, std::move(matData));
@ -342,7 +341,7 @@ namespace Nz
// Material
ParameterList matData;
matData.SetParameter(MaterialData::FilePath, (baseDir / md5Mesh.shader).generic_u8string());
matData.SetParameter(MaterialData::DiffuseTexturePath, (baseDir / md5Mesh.shader).generic_u8string());
mesh->SetMaterialData(i, std::move(matData));
}

View File

@ -271,11 +271,6 @@ namespace Nz
// Remplissage des vertices
// Make sure the normal matrix won't rescale our normals
Nz::Matrix4f normalMatrix = parameters.matrix;
if (normalMatrix.HasScale())
normalMatrix.ApplyScale(1.f / normalMatrix.GetScale());
bool hasNormals = true;
bool hasTexCoords = true;
@ -299,13 +294,13 @@ namespace Nz
if (posPtr)
{
const Vector4f& vec = positions[vertexIndices.position - 1];
posPtr[index] = Vector3f(parameters.matrix * vec);
posPtr[index] = TransformPositionTRS(parameters.vertexOffset, parameters.vertexRotation, parameters.vertexScale, Vector3f(vec));
}
if (hasNormals)
{
if (vertexIndices.normal > 0)
normalPtr[index] = normalMatrix.Transform(normals[vertexIndices.normal - 1], 0.f);
normalPtr[index] = TransformNormalTRS(parameters.vertexRotation, parameters.vertexScale, normals[vertexIndices.normal - 1]);
else
hasNormals = false;
}

View File

@ -25,12 +25,6 @@ namespace Nz
{
bool MeshParams::IsValid() const
{
if (matrix == Matrix4f::Zero())
{
NazaraError("Invalid matrix");
return false;
}
if (!vertexDeclaration)
{
NazaraError("The vertex declaration can't be null");
@ -87,8 +81,7 @@ namespace Nz
std::shared_ptr<IndexBuffer> indexBuffer;
std::shared_ptr<VertexBuffer> vertexBuffer;
Matrix4f matrix(primitive.matrix);
matrix *= params.matrix;
Matrix4f matrix = Matrix4f::ConcatenateTransform(primitive.matrix, Matrix4f::Transform(params.vertexOffset, params.vertexRotation, params.vertexScale));
const std::shared_ptr<VertexDeclaration>& declaration = params.vertexDeclaration;