diff --git a/examples/DeferredShading/main.cpp b/examples/DeferredShading/main.cpp index d593687e9..d60eb2f39 100644 --- a/examples/DeferredShading/main.cpp +++ b/examples/DeferredShading/main.cpp @@ -646,31 +646,11 @@ int main() builder.SetScissor(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) }); builder.SetViewport(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) }); - for (Nz::ModelInstance& modelInstance : { std::ref(modelInstance1), std::ref(modelInstance2) }) - { - builder.BindShaderBinding(modelInstance.GetShaderBinding()); - - for (std::size_t i = 0; i < spaceshipModel.GetSubMeshCount(); ++i) - { - builder.BindIndexBuffer(spaceshipModel.GetIndexBuffer(i).get()); - builder.BindVertexBuffer(0, spaceshipModel.GetVertexBuffer(i).get()); - builder.BindPipeline(*spaceshipModel.GetRenderPipeline(i)); - - builder.DrawIndexed(static_cast(spaceshipModel.GetIndexCount(i))); - } - } + spaceshipModel.Draw(builder, modelInstance1); + spaceshipModel.Draw(builder, modelInstance2); // Plane - builder.BindShaderBinding(planeInstance.GetShaderBinding()); - - for (std::size_t i = 0; i < planeModel.GetSubMeshCount(); ++i) - { - builder.BindIndexBuffer(planeModel.GetIndexBuffer(i).get()); - builder.BindVertexBuffer(0, planeModel.GetVertexBuffer(i).get()); - builder.BindPipeline(*planeModel.GetRenderPipeline(i)); - - builder.DrawIndexed(static_cast(planeModel.GetIndexCount(i))); - } + planeModel.Draw(builder, planeInstance); }); Nz::FramePass& lightingPass = graph.AddPass("Lighting pass"); diff --git a/include/Nazara/Graphics/Components/GraphicsComponent.hpp b/include/Nazara/Graphics/Components/GraphicsComponent.hpp new file mode 100644 index 000000000..e69de29bb diff --git a/include/Nazara/Graphics/Components/GraphicsComponent.inl b/include/Nazara/Graphics/Components/GraphicsComponent.inl new file mode 100644 index 000000000..e69de29bb diff --git a/include/Nazara/Graphics/InstancedRenderable.hpp b/include/Nazara/Graphics/InstancedRenderable.hpp new file mode 100644 index 000000000..adee6c2fa --- /dev/null +++ b/include/Nazara/Graphics/InstancedRenderable.hpp @@ -0,0 +1,35 @@ +// 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 + +#pragma once + +#ifndef NAZARA_INSTANCEDRENDERABLE_HPP +#define NAZARA_INSTANCEDRENDERABLE_HPP + +#include +#include + +namespace Nz +{ + class CommandBufferBuilder; + class ModelInstance; + + class NAZARA_GRAPHICS_API InstancedRenderable + { + public: + InstancedRenderable() = default; + InstancedRenderable(const InstancedRenderable&) = delete; + InstancedRenderable(InstancedRenderable&&) noexcept = default; + ~InstancedRenderable(); + + virtual void Draw(CommandBufferBuilder& commandBuffer, ModelInstance& instance) const = 0; + + InstancedRenderable& operator=(const InstancedRenderable&) = delete; + InstancedRenderable& operator=(InstancedRenderable&&) noexcept = default; + }; +} + +#include + +#endif diff --git a/include/Nazara/Graphics/InstancedRenderable.inl b/include/Nazara/Graphics/InstancedRenderable.inl new file mode 100644 index 000000000..f5e049f05 --- /dev/null +++ b/include/Nazara/Graphics/InstancedRenderable.inl @@ -0,0 +1,12 @@ +// 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 + +namespace Nz +{ +} + +#include diff --git a/include/Nazara/Graphics/Model.hpp b/include/Nazara/Graphics/Model.hpp index 47f9a1697..a90961b8d 100644 --- a/include/Nazara/Graphics/Model.hpp +++ b/include/Nazara/Graphics/Model.hpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -19,7 +20,7 @@ namespace Nz class GraphicalMesh; class Material; - class NAZARA_GRAPHICS_API Model + class NAZARA_GRAPHICS_API Model : public InstancedRenderable { public: Model(std::shared_ptr graphicalMesh); @@ -27,6 +28,8 @@ namespace Nz Model(Model&&) noexcept = default; ~Model() = default; + void Draw(CommandBufferBuilder& commandBuffer, ModelInstance& instance) const override; + const std::shared_ptr& GetIndexBuffer(std::size_t subMeshIndex) const; std::size_t GetIndexCount(std::size_t subMeshIndex) const; const std::shared_ptr& GetRenderPipeline(std::size_t subMeshIndex) const; diff --git a/include/Nazara/Utility/Components/NodeComponent.hpp b/include/Nazara/Utility/Components/NodeComponent.hpp new file mode 100644 index 000000000..e69de29bb diff --git a/include/Nazara/Utility/Components/NodeComponent.inl b/include/Nazara/Utility/Components/NodeComponent.inl new file mode 100644 index 000000000..e69de29bb diff --git a/src/Nazara/Graphics/Components/GraphicsComponent.cpp b/src/Nazara/Graphics/Components/GraphicsComponent.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/src/Nazara/Graphics/InstancedRenderable.cpp b/src/Nazara/Graphics/InstancedRenderable.cpp new file mode 100644 index 000000000..ef42be4af --- /dev/null +++ b/src/Nazara/Graphics/InstancedRenderable.cpp @@ -0,0 +1,11 @@ +// 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 + +namespace Nz +{ + InstancedRenderable::~InstancedRenderable() = default; +} diff --git a/src/Nazara/Graphics/Model.cpp b/src/Nazara/Graphics/Model.cpp index f921725e7..04ae5a9a0 100644 --- a/src/Nazara/Graphics/Model.cpp +++ b/src/Nazara/Graphics/Model.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include namespace Nz @@ -26,6 +28,25 @@ namespace Nz } } + void Model::Draw(CommandBufferBuilder& commandBuffer, ModelInstance& instance) const + { + commandBuffer.BindShaderBinding(instance.GetShaderBinding()); + + for (std::size_t i = 0; i < m_subMeshes.size(); ++i) + { + const auto& submeshData = m_subMeshes[i]; + const auto& indexBuffer = m_graphicalMesh->GetIndexBuffer(i); + const auto& vertexBuffer = m_graphicalMesh->GetVertexBuffer(i); + const auto& renderPipeline = submeshData.material->GetPipeline()->GetRenderPipeline(submeshData.vertexBufferData); + + commandBuffer.BindIndexBuffer(indexBuffer.get()); + commandBuffer.BindVertexBuffer(0, vertexBuffer.get()); + commandBuffer.BindPipeline(*renderPipeline); + + commandBuffer.DrawIndexed(static_cast(m_graphicalMesh->GetIndexCount(i))); + } + } + const std::shared_ptr& Model::GetIndexBuffer(std::size_t subMeshIndex) const { return m_graphicalMesh->GetIndexBuffer(subMeshIndex); @@ -47,5 +68,4 @@ namespace Nz { return m_graphicalMesh->GetVertexBuffer(subMeshIndex); } - } diff --git a/src/Nazara/Utility/Components/NodeComponent.cpp b/src/Nazara/Utility/Components/NodeComponent.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/xmake.lua b/xmake.lua index 31edb641f..3c68f861d 100644 --- a/xmake.lua +++ b/xmake.lua @@ -17,7 +17,8 @@ local modules = { end }, Graphics = { - Deps = {"NazaraRenderer"} + Deps = {"NazaraRenderer"}, + Packages = {"entt"} }, Network = { Deps = {"NazaraCore"}, @@ -77,7 +78,7 @@ local modules = { }, Utility = { Deps = {"NazaraCore"}, - Packages = {"freetype", "stb"} + Packages = {"entt", "freetype", "stb"} }, VulkanRenderer = { Deps = {"NazaraRenderer"}, @@ -98,7 +99,7 @@ local modules = { add_repositories("local-repo xmake-repo") -add_requires("chipmunk2d", "dr_wav", "freetype", "libflac", "libsdl", "minimp3", "stb") +add_requires("chipmunk2d", "dr_wav", "entt", "freetype", "libflac", "libsdl", "minimp3", "stb") add_requires("libvorbis", { configs = { with_vorbisenc = false } }) add_requires("openal-soft", { configs = { shared = true }}) add_requires("newtondynamics", { debug = is_plat("windows") and is_mode("debug") }) -- Newton doesn't like compiling in Debug on Linux