Push current work

This commit is contained in:
SirLynix 2022-05-04 12:50:05 +02:00 committed by Jérôme Leclercq
parent 85cd064171
commit e5e3026005
9 changed files with 653 additions and 580 deletions

View File

@ -33,6 +33,10 @@ int main()
Nz::Modules<Nz::Graphics> nazara(rendererConfig); Nz::Modules<Nz::Graphics> nazara(rendererConfig);
Nz::PluginManager::Mount(Nz::Plugin::Assimp); Nz::PluginManager::Mount(Nz::Plugin::Assimp);
Nz::CallOnExit unmountAssimp([]
{
Nz::PluginManager::Unmount(Nz::Plugin::Assimp);
});
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice(); std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
@ -55,19 +59,27 @@ int main()
texParams.renderDevice = device; texParams.renderDevice = device;
entt::entity playerEntity = registry.create(); entt::entity playerEntity = registry.create();
entt::entity playerRotation = registry.create();
entt::entity playerCamera = registry.create();
{ {
auto& playerNode = registry.emplace<Nz::NodeComponent>(playerEntity); auto& playerNode = registry.emplace<Nz::NodeComponent>(playerEntity);
playerNode.SetPosition(0.f, 1.8f, 1.f); playerNode.SetPosition(0.f, 1.8f, 1.f);
playerNode.SetRotation(Nz::EulerAnglesf(-30.f, 0.f, 0.f));
auto& playerBody = registry.emplace<Nz::RigidBody3DComponent>(playerEntity, &physSytem.GetPhysWorld()); auto& playerBody = registry.emplace<Nz::RigidBody3DComponent>(playerEntity, &physSytem.GetPhysWorld());
playerBody.SetMass(0.f); playerBody.SetMass(42.f);
playerBody.SetGeom(std::make_shared<Nz::CapsuleCollider3D>(1.8f, 0.5f)); playerBody.SetGeom(std::make_shared<Nz::BoxCollider3D>(Nz::Vector3f::Unit()));
auto& playerComponent = registry.emplace<Nz::CameraComponent>(playerEntity, window.GetRenderTarget()); auto& playerRotNode = registry.emplace<Nz::NodeComponent>(playerRotation);
playerComponent.UpdateZNear(0.2f); playerRotNode.SetParent(playerNode);
playerComponent.UpdateRenderMask(1);
playerComponent.UpdateClearColor(Nz::Color(127, 127, 127)); auto& cameraNode = registry.emplace<Nz::NodeComponent>(playerCamera);
cameraNode.SetParent(playerRotNode);
auto& cameraComponent = registry.emplace<Nz::CameraComponent>(playerCamera, window.GetRenderTarget());
cameraComponent.UpdateZNear(0.2f);
cameraComponent.UpdateZFar(10000.f);
cameraComponent.UpdateRenderMask(1);
cameraComponent.UpdateClearColor(Nz::Color(0.5f, 0.5f, 0.5f));
} }
Nz::FieldOffsets skeletalOffsets(Nz::StructLayout::Std140); Nz::FieldOffsets skeletalOffsets(Nz::StructLayout::Std140);
@ -76,16 +88,11 @@ int main()
std::vector<Nz::UInt8> skeletalBufferMem(skeletalOffsets.GetAlignedSize()); std::vector<Nz::UInt8> skeletalBufferMem(skeletalOffsets.GetAlignedSize());
Nz::Matrix4f* matrices = Nz::AccessByOffset<Nz::Matrix4f*>(skeletalBufferMem.data(), arrayOffset); Nz::Matrix4f* matrices = Nz::AccessByOffset<Nz::Matrix4f*>(skeletalBufferMem.data(), arrayOffset);
std::shared_ptr<Nz::Animation> bobAnim = Nz::Animation::LoadFromFile(resourceDir / "hellknight/idle2.md5anim");
if (!bobAnim)
{
NazaraError("Failed to load bob anim");
return __LINE__;
}
Nz::MeshParams meshParams; Nz::MeshParams meshParams;
meshParams.animated = true; meshParams.animated = true;
//meshParams.center = 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.matrix = Nz::Matrix4f::Scale(Nz::Vector3f(10.f));
meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent_Skinning); meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent_Skinning);
@ -96,9 +103,22 @@ int main()
return __LINE__; return __LINE__;
} }
Nz::AnimationParams matParams;
matParams.skeleton = bobMesh->GetSkeleton();
std::shared_ptr<Nz::Animation> bobAnim = Nz::Animation::LoadFromFile(resourceDir / "hellknight/idle.md5anim", matParams);
if (!bobAnim)
{
NazaraError("Failed to load bob anim");
return __LINE__;
}
Nz::Skeleton skeleton = *bobMesh->GetSkeleton(); Nz::Skeleton skeleton = *bobMesh->GetSkeleton();
bobAnim->AnimateSkeleton(&skeleton, 0, 1, 0.5f); std::cout << "joint count: " << skeleton.GetJointCount() << std::endl;
//std::cout << "anim joint count: " << bobAnim->GetJointCount() << std::endl;
//bobAnim->AnimateSkeleton(&skeleton, 0, 1, 0.5f);
/*for (std::size_t i = 0; i < bobMesh->GetSubMeshCount(); ++i) /*for (std::size_t i = 0; i < bobMesh->GetSubMeshCount(); ++i)
{ {
@ -114,42 +134,70 @@ int main()
Nz::SkinPosition(skinningData, 0, mapper.GetVertexCount()); Nz::SkinPosition(skinningData, 0, mapper.GetVertexCount());
}*/ }*/
for (std::size_t i = 0; i < skeleton.GetJointCount(); ++i) /*for (std::size_t i = 0; i < skeleton.GetJointCount(); ++i)
matrices[i] = skeleton.GetJoint(i)->GetSkinningMatrix(); matrices[i] = skeleton.GetJoint(i)->GetSkinningMatrix();*/
std::shared_ptr<Nz::RenderBuffer> renderBuffer = device->InstantiateBuffer(Nz::BufferType::Uniform, skeletalBufferMem.size(), Nz::BufferUsage::Write, skeletalBufferMem.data()); std::shared_ptr<Nz::RenderBuffer> renderBuffer = device->InstantiateBuffer(Nz::BufferType::Uniform, skeletalBufferMem.size(), Nz::BufferUsage::Write, skeletalBufferMem.data());
entt::entity bobEntity = registry.create(); const Nz::Boxf& bobAABB = bobMesh->GetAABB();
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);
for (std::size_t i = 0; i < bobMesh->GetMaterialCount(); ++i)
{ {
const Nz::Boxf& bobAABB = bobMesh->GetAABB(); std::string matPath;
std::shared_ptr<Nz::GraphicalMesh> bobGfxMesh = std::make_shared<Nz::GraphicalMesh>(*bobMesh); bobMesh->GetMaterialData(i).GetStringParameter(Nz::MaterialData::DiffuseTexturePath, &matPath);
std::shared_ptr<Nz::Model> bobModel = std::make_shared<Nz::Model>(std::move(bobGfxMesh), bobAABB); std::shared_ptr<Nz::Material> bobMat = std::make_shared<Nz::Material>();
for (std::size_t i = 0; i < bobMesh->GetMaterialCount(); ++i)
std::shared_ptr<Nz::MaterialPass> bobMatPass = std::make_shared<Nz::MaterialPass>(Nz::BasicMaterial::GetSettings());
bobMatPass->SetSharedUniformBuffer(0, renderBuffer);
bobMatPass->EnableDepthBuffer(true);
{ {
std::string matPath; Nz::BasicMaterial basicMat(*bobMatPass);
bobMesh->GetMaterialData(i).GetStringParameter(Nz::MaterialData::FilePath, &matPath); if (matPath.find("gob") != matPath.npos)
std::shared_ptr<Nz::Material> bobMat = std::make_shared<Nz::Material>();
std::shared_ptr<Nz::MaterialPass> bobMatPass = std::make_shared<Nz::MaterialPass>(Nz::BasicMaterial::GetSettings());
bobMatPass->SetSharedUniformBuffer(0, renderBuffer);
bobMatPass->EnableDepthBuffer(true);
{ {
Nz::BasicMaterial basicMat(*bobMatPass); bobMatPass->EnableFlag(Nz::MaterialPassFlag::SortByDistance);
basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(matPath + ".tga", texParams));
}
if (i == 0 || i == 3)
bobMat->AddPass("ForwardPass", bobMatPass);
bobModel->SetMaterial(i, bobMat); basicMat.SetAlphaMap(Nz::Texture::LoadFromFile(matPath, texParams));
bobMatPass->EnableDepthWrite(false);
bobMatPass->EnableBlending(true);
bobMatPass->SetBlendEquation(Nz::BlendEquation::Add, Nz::BlendEquation::Add);
bobMatPass->SetBlendFunc(Nz::BlendFunc::SrcAlpha, Nz::BlendFunc::InvSrcAlpha, Nz::BlendFunc::One, Nz::BlendFunc::Zero);
}
else
basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(matPath, texParams));
} }
bobMat->AddPass("ForwardPass", bobMatPass);
bobModel->SetMaterial(i, bobMat);
}
/*for (std::size_t y = 0; y < 50; ++y)
{
for (std::size_t x = 0; x < 50; ++x)
{
entt::entity bobEntity = registry.create();
auto& bobNode = registry.emplace<Nz::NodeComponent>(bobEntity);
bobNode.SetPosition(Nz::Vector3f(x - 25.f, bobAABB.height / 2.f, -float(y)));
bobNode.SetRotation(Nz::EulerAnglesf(-90.f, -90.f, 0.f));
bobNode.SetScale(1.f / 40.f * 0.5f);
auto& bobGfx = registry.emplace<Nz::GraphicsComponent>(bobEntity);
bobGfx.AttachRenderable(bobModel, 0xFFFFFFFF);
}
}*/
entt::entity bobEntity = registry.create();
{
auto& bobNode = registry.emplace<Nz::NodeComponent>(bobEntity); auto& bobNode = registry.emplace<Nz::NodeComponent>(bobEntity);
bobNode.SetPosition(Nz::Vector3f(0.f, bobAABB.height / 2.f, 0.f)); bobNode.SetRotation(Nz::EulerAnglesf(90.f, -90.f, 0.f));
bobNode.SetRotation(Nz::EulerAnglesf(-90.f, -90.f, 0.f));
bobNode.SetScale(1.f / 40.f * 0.5f); bobNode.SetScale(1.f / 40.f * 0.5f);
bobNode.SetPosition(bobNode.GetScale() * Nz::Vector3f(0.f, bobAABB.height / 2.f, 0.f));
auto& bobGfx = registry.emplace<Nz::GraphicsComponent>(bobEntity); auto& bobGfx = registry.emplace<Nz::GraphicsComponent>(bobEntity);
bobGfx.AttachRenderable(bobModel, 0xFFFFFFFF); bobGfx.AttachRenderable(bobModel, 0xFFFFFFFF);
@ -197,10 +245,13 @@ int main()
planeGfx.AttachRenderable(planeModel, 0xFFFFFFFF); planeGfx.AttachRenderable(planeModel, 0xFFFFFFFF);
} }
window.EnableEventPolling(true);
Nz::Clock fpsClock, updateClock; Nz::Clock fpsClock, updateClock;
float incr = 0.f; float incr = 0.f;
unsigned int currentFrame = 0; unsigned int currentFrame = 0;
unsigned int nextFrame = 1; unsigned int nextFrame = 1;
Nz::EulerAnglesf camAngles = Nz::EulerAnglesf(-30.f, 0.f, 0.f);
Nz::UInt64 lastTime = Nz::GetElapsedMicroseconds(); Nz::UInt64 lastTime = Nz::GetElapsedMicroseconds();
Nz::UInt64 fps = 0; Nz::UInt64 fps = 0;
while (window.IsOpen()) while (window.IsOpen())
@ -222,7 +273,21 @@ int main()
break; break;
case Nz::WindowEventType::MouseMoved: case Nz::WindowEventType::MouseMoved:
{
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.3f; // Sensibilité de la souris
// On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris
camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity;
camAngles.yaw.Normalize();
// Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles
camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY * sensitivity, -89.f, 89.f);
auto& playerRotNode = registry.get<Nz::NodeComponent>(playerRotation);
playerRotNode.SetRotation(camAngles);
break; break;
}
default: default:
break; break;
@ -237,8 +302,22 @@ int main()
auto& playerBody = registry.get<Nz::RigidBody3DComponent>(playerEntity); auto& playerBody = registry.get<Nz::RigidBody3DComponent>(playerEntity);
float mass = playerBody.GetMass();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)) if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space))
playerBody.AddForce(Nz::Vector3f(0.f, playerBody.GetMass() * 50.f, 0.f)); playerBody.AddForce(Nz::Vector3f(0.f, mass * 50.f, 0.f));
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z))
playerBody.AddForce(Nz::Vector3f::Forward() * 25.f * mass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Down) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S))
playerBody.AddForce(Nz::Vector3f::Backward() * 25.f * mass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Left) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q))
playerBody.AddForce(Nz::Vector3f::Left() * 25.f * mass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Right) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D))
playerBody.AddForce(Nz::Vector3f::Right() * 25.f * mass, Nz::CoordSys::Local);
incr += 1.f / 60.f * 30.f; incr += 1.f / 60.f * 30.f;
if (incr >= 1.f) if (incr >= 1.f)
@ -251,6 +330,8 @@ int main()
nextFrame = 0; nextFrame = 0;
} }
std::cout << currentFrame << std::endl;
bobAnim->AnimateSkeleton(&skeleton, currentFrame, nextFrame, incr); bobAnim->AnimateSkeleton(&skeleton, currentFrame, nextFrame, incr);
for (std::size_t i = 0; i < skeleton.GetJointCount(); ++i) for (std::size_t i = 0; i < skeleton.GetJointCount(); ++i)
matrices[i] = skeleton.GetJoint(i)->GetSkinningMatrix(); matrices[i] = skeleton.GetJoint(i)->GetSkinningMatrix();

View File

@ -256,7 +256,7 @@ namespace Nz
Vector3<T>& OrientedBox<T>::operator()(unsigned int i) Vector3<T>& OrientedBox<T>::operator()(unsigned int i)
{ {
#if NAZARA_MATH_SAFE #if NAZARA_MATH_SAFE
if (i > BoxCornerCount) if (i >= BoxCornerCount)
{ {
std::ostringstream ss; std::ostringstream ss;
ss << "Index out of range: (" << i << " >= " << BoxCornerCount << ")"; ss << "Index out of range: (" << i << " >= " << BoxCornerCount << ")";
@ -281,7 +281,7 @@ namespace Nz
Vector3<T> OrientedBox<T>::operator()(unsigned int i) const Vector3<T> OrientedBox<T>::operator()(unsigned int i) const
{ {
#if NAZARA_MATH_SAFE #if NAZARA_MATH_SAFE
if (i > BoxCornerCount) if (i >= BoxCornerCount)
{ {
std::ostringstream ss; std::ostringstream ss;
ss << "Index out of range: (" << i << " >= " << BoxCornerCount << ")"; ss << "Index out of range: (" << i << " >= " << BoxCornerCount << ")";

View File

@ -21,12 +21,16 @@
namespace Nz namespace Nz
{ {
class Skeleton;
struct NAZARA_UTILITY_API AnimationParams : ResourceParameters struct NAZARA_UTILITY_API AnimationParams : ResourceParameters
{ {
// La frame de fin à charger // La frame de fin à charger
std::size_t endFrame = 0xFFFFFFFF; std::size_t endFrame = 0xFFFFFFFF;
// La frame de début à charger // La frame de début à charger
std::size_t startFrame = 0; std::size_t startFrame = 0;
// Reference skeleton
const Skeleton* skeleton = nullptr;
bool IsValid() const; bool IsValid() const;
}; };
@ -34,7 +38,6 @@ namespace Nz
class Animation; class Animation;
struct Sequence; struct Sequence;
struct SequenceJoint; struct SequenceJoint;
class Skeleton;
using AnimationLibrary = ObjectLibrary<Animation>; using AnimationLibrary = ObjectLibrary<Animation>;
using AnimationLoader = ResourceLoader<Animation, AnimationParams>; using AnimationLoader = ResourceLoader<Animation, AnimationParams>;

View File

@ -23,9 +23,9 @@ namespace Nz
struct SequenceJoint struct SequenceJoint
{ {
Quaternionf rotation; Quaternionf rotation = Quaternionf::Identity();
Vector3f position; Vector3f position = Vector3f::Zero();
Vector3f scale; Vector3f scale = Vector3f::Unit();
}; };
} }

View File

@ -57,6 +57,8 @@ namespace Nz
// Signals: // Signals:
NazaraSignal(OnSkeletonJointsInvalidated, const Skeleton* /*skeleton*/); NazaraSignal(OnSkeletonJointsInvalidated, const Skeleton* /*skeleton*/);
static constexpr std::size_t InvalidJointIndex = std::numeric_limits<std::size_t>::max();
private: private:
void InvalidateJoints(); void InvalidateJoints();
void InvalidateJointMap(); void InvalidateJointMap();

File diff suppressed because it is too large Load Diff

View File

@ -211,7 +211,16 @@ namespace Nz
{ {
NAZARA_USE_ANONYMOUS_NAMESPACE NAZARA_USE_ANONYMOUS_NAMESPACE
Unmount(s_pluginFiles[UnderlyingCast(plugin)]); std::filesystem::path pluginName = s_pluginFiles[UnderlyingCast(plugin)];
#ifdef NAZARA_DEBUG
std::filesystem::path debugPath = pluginName;
debugPath += "-d";
Unmount(debugPath);
#endif
Unmount(pluginName);
} }
/*! /*!
@ -233,7 +242,14 @@ namespace Nz
return; return;
} }
std::filesystem::path canonicalPath = std::filesystem::canonical(pluginPath); std::filesystem::path path = pluginPath;
if (path.extension() != NAZARA_DYNLIB_EXTENSION)
path += NAZARA_DYNLIB_EXTENSION;
if (!std::filesystem::exists(path))
return;
std::filesystem::path canonicalPath = std::filesystem::canonical(path);
auto it = s_plugins.find(canonicalPath); auto it = s_plugins.find(canonicalPath);
if (it == s_plugins.end()) if (it == s_plugins.end())
{ {

View File

@ -34,6 +34,12 @@ namespace Nz
return false; return false;
} }
if (!skeleton)
{
NazaraError("You must set a valid skeleton to load an animation");
return false;
}
return true; return true;
} }

View File

@ -140,7 +140,8 @@ namespace Nz
UpdateJointMap(); UpdateJointMap();
auto it = m_impl->jointMap.find(jointName); auto it = m_impl->jointMap.find(jointName);
NazaraAssert(it != m_impl->jointMap.end(), "joint not found"); if (it == m_impl->jointMap.end())
return InvalidJointIndex;
return it->second; return it->second;
} }