Graphics/ModelInstance: Add UpdateWorldMatrix method

This commit is contained in:
Jérôme Leclercq 2021-06-05 19:05:06 +02:00
parent bd1f5214b8
commit 743f8eeb24
4 changed files with 57 additions and 25 deletions

View File

@ -220,34 +220,16 @@ int main()
std::vector<std::uint8_t> instanceDataBuffer(instanceUboOffsets.totalSize); std::vector<std::uint8_t> instanceDataBuffer(instanceUboOffsets.totalSize);
Nz::ModelInstance modelInstance1(spaceshipMat->GetSettings()); Nz::ModelInstance modelInstance1(spaceshipMat->GetSettings());
{ spaceshipMat->UpdateShaderBinding(modelInstance1.GetShaderBinding());
spaceshipMat->UpdateShaderBinding(modelInstance1.GetShaderBinding()); modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Right()));
Nz::AccessByOffset<Nz::Matrix4f&>(instanceDataBuffer.data(), instanceUboOffsets.worldMatrixOffset) = Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Right());
std::shared_ptr<Nz::AbstractBuffer>& instanceDataUBO = modelInstance1.GetInstanceBuffer();
instanceDataUBO->Fill(instanceDataBuffer.data(), 0, instanceDataBuffer.size());
}
Nz::ModelInstance modelInstance2(spaceshipMat->GetSettings()); Nz::ModelInstance modelInstance2(spaceshipMat->GetSettings());
{ spaceshipMat->UpdateShaderBinding(modelInstance2.GetShaderBinding());
spaceshipMat->UpdateShaderBinding(modelInstance2.GetShaderBinding()); modelInstance2.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Right() * 3.f));
Nz::AccessByOffset<Nz::Matrix4f&>(instanceDataBuffer.data(), instanceUboOffsets.worldMatrixOffset) = Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Right() * 3.f);
std::shared_ptr<Nz::AbstractBuffer>& instanceDataUBO = modelInstance2.GetInstanceBuffer();
instanceDataUBO->Fill(instanceDataBuffer.data(), 0, instanceDataBuffer.size());
}
Nz::ModelInstance planeInstance(planeMat->GetSettings()); Nz::ModelInstance planeInstance(planeMat->GetSettings());
{ planeMat->UpdateShaderBinding(planeInstance.GetShaderBinding());
planeMat->UpdateShaderBinding(planeInstance.GetShaderBinding()); planeInstance.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Up() * 2.f));
Nz::AccessByOffset<Nz::Matrix4f&>(instanceDataBuffer.data(), instanceUboOffsets.worldMatrixOffset) = Nz::Matrix4f::Translate(Nz::Vector3f::Up() * 2.f);
std::shared_ptr<Nz::AbstractBuffer>& instanceDataUBO = planeInstance.GetInstanceBuffer();
instanceDataUBO->Fill(instanceDataBuffer.data(), 0, instanceDataBuffer.size());
}
std::shared_ptr<Nz::AbstractBuffer> viewerDataUBO = Nz::Graphics::Instance()->GetViewerDataUBO(); std::shared_ptr<Nz::AbstractBuffer> viewerDataUBO = Nz::Graphics::Instance()->GetViewerDataUBO();
@ -1045,6 +1027,8 @@ int main()
forwardEnabled = !forwardEnabled; forwardEnabled = !forwardEnabled;
else if (event.key.virtualKey == Nz::Keyboard::VKey::A) else if (event.key.virtualKey == Nz::Keyboard::VKey::A)
lightAnimation = !lightAnimation; lightAnimation = !lightAnimation;
else if (event.key.virtualKey == Nz::Keyboard::VKey::E)
modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Transform(viewerPos, camQuat));
break; break;
} }

View File

@ -8,6 +8,7 @@
#define NAZARA_MODELINSTANCE_HPP #define NAZARA_MODELINSTANCE_HPP
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Graphics/Config.hpp> #include <Nazara/Graphics/Config.hpp>
#include <Nazara/Renderer/ShaderBinding.hpp> #include <Nazara/Renderer/ShaderBinding.hpp>
#include <memory> #include <memory>
@ -15,7 +16,9 @@
namespace Nz namespace Nz
{ {
class AbstractBuffer; class AbstractBuffer;
class CommandBufferBuilder;
class MaterialSettings; class MaterialSettings;
class UploadPool;
class NAZARA_GRAPHICS_API ModelInstance class NAZARA_GRAPHICS_API ModelInstance
{ {
@ -29,12 +32,19 @@ namespace Nz
inline const std::shared_ptr<AbstractBuffer>& GetInstanceBuffer() const; inline const std::shared_ptr<AbstractBuffer>& GetInstanceBuffer() const;
inline ShaderBinding& GetShaderBinding(); inline ShaderBinding& GetShaderBinding();
void UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder);
inline void UpdateWorldMatrix(const Matrix4f& worldMatrix);
inline void UpdateWorldMatrix(const Matrix4f& worldMatrix, const Matrix4f& invWorldMatrix);
ModelInstance& operator=(const ModelInstance&) = delete; ModelInstance& operator=(const ModelInstance&) = delete;
ModelInstance& operator=(ModelInstance&&) noexcept = default; ModelInstance& operator=(ModelInstance&&) noexcept = default;
private: private:
std::shared_ptr<AbstractBuffer> m_instanceDataBuffer; std::shared_ptr<AbstractBuffer> m_instanceDataBuffer;
Matrix4f m_invWorldMatrix;
Matrix4f m_worldMatrix;
ShaderBindingPtr m_shaderBinding; ShaderBindingPtr m_shaderBinding;
bool m_dataInvalided;
}; };
} }

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/ModelInstance.hpp> #include <Nazara/Graphics/ModelInstance.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
namespace Nz namespace Nz
@ -21,6 +22,22 @@ namespace Nz
{ {
return *m_shaderBinding; return *m_shaderBinding;
} }
inline void ModelInstance::UpdateWorldMatrix(const Matrix4f& worldMatrix)
{
m_worldMatrix = worldMatrix;
if (!m_worldMatrix.GetInverseAffine(&m_invWorldMatrix))
NazaraError("failed to inverse world matrix");
m_dataInvalided = true;
}
inline void ModelInstance::UpdateWorldMatrix(const Matrix4f& worldMatrix, const Matrix4f& invWorldMatrix)
{
m_worldMatrix = worldMatrix;
m_invWorldMatrix = invWorldMatrix;
m_dataInvalided = true;
}
} }
#include <Nazara/Graphics/DebugOff.hpp> #include <Nazara/Graphics/DebugOff.hpp>

View File

@ -7,11 +7,16 @@
#include <Nazara/Graphics/Graphics.hpp> #include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/MaterialSettings.hpp> #include <Nazara/Graphics/MaterialSettings.hpp>
#include <Nazara/Graphics/PredefinedShaderStructs.hpp> #include <Nazara/Graphics/PredefinedShaderStructs.hpp>
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <Nazara/Renderer/UploadPool.hpp>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
namespace Nz namespace Nz
{ {
ModelInstance::ModelInstance(const std::shared_ptr<const MaterialSettings>& settings) ModelInstance::ModelInstance(const std::shared_ptr<const MaterialSettings>& settings) :
m_invWorldMatrix(Nz::Matrix4f::Identity()),
m_worldMatrix(Nz::Matrix4f::Identity()),
m_dataInvalided(true)
{ {
Nz::PredefinedInstanceData instanceUboOffsets = Nz::PredefinedInstanceData::GetOffsets(); Nz::PredefinedInstanceData instanceUboOffsets = Nz::PredefinedInstanceData::GetOffsets();
@ -48,4 +53,20 @@ namespace Nz
if (!bindings.empty()) if (!bindings.empty())
m_shaderBinding->Update(bindings.data(), bindings.size()); m_shaderBinding->Update(bindings.data(), bindings.size());
} }
void ModelInstance::UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder)
{
if (m_dataInvalided)
{
Nz::PredefinedInstanceData instanceUboOffsets = Nz::PredefinedInstanceData::GetOffsets();
auto& allocation = uploadPool.Allocate(m_instanceDataBuffer->GetSize());
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, instanceUboOffsets.worldMatrixOffset) = m_worldMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, instanceUboOffsets.invWorldMatrixOffset) = m_invWorldMatrix;
builder.CopyBuffer(allocation, m_instanceDataBuffer.get());
m_dataInvalided = false;
}
}
} }