Add system graph (wip)

This commit is contained in:
SirLynix
2022-07-02 19:45:50 +02:00
parent 4d24be2ae9
commit 1b678defae
18 changed files with 524 additions and 286 deletions

View File

@@ -41,26 +41,65 @@ int main()
return __LINE__;
}
std::shared_ptr<Nz::Mesh> spaceshipMesh = Nz::Mesh::LoadFromFile(resourceDir / "Spaceship/spaceship.obj", meshParams);
if (!spaceshipMesh)
std::shared_ptr<Nz::Mesh> sphereMesh = std::make_shared<Nz::Mesh>();
sphereMesh->CreateStatic();
std::shared_ptr<Nz::SubMesh> sphereSubmesh = sphereMesh->BuildSubMesh(Nz::Primitive::UVSphere(1.f, 50, 50));
sphereMesh->SetMaterialCount(1);
sphereMesh->GenerateNormalsAndTangents();
std::shared_ptr<Nz::Mesh> debugMesh = std::make_shared<Nz::Mesh>();
debugMesh->CreateStatic();
{
NazaraError("Failed to load model");
return __LINE__;
Nz::VertexMapper sphereMapper(*sphereSubmesh);
std::size_t vertexCount = sphereMapper.GetVertexCount();
Nz::SparsePtr<Nz::Vector3f> positionPtr = sphereMapper.GetComponentPtr<Nz::Vector3f>(Nz::VertexComponent::Position);
Nz::SparsePtr<Nz::Vector3f> normalPtr = sphereMapper.GetComponentPtr<Nz::Vector3f>(Nz::VertexComponent::Normal);
Nz::SparsePtr<Nz::Vector3f> tangentPtr = sphereMapper.GetComponentPtr<Nz::Vector3f>(Nz::VertexComponent::Tangent);
std::shared_ptr<Nz::VertexDeclaration> debugDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Color);
std::vector<Nz::VertexStruct_XYZ_Color> debugVertices(vertexCount * 6);
for (std::size_t i = 0; i < vertexCount; ++i)
{
debugVertices[i * 6 + 0].position = positionPtr[i];
debugVertices[i * 6 + 0].color = Nz::Color::Red;
debugVertices[i * 6 + 1].position = positionPtr[i] + normalPtr[i] * 0.05f;
debugVertices[i * 6 + 1].color = Nz::Color::Red;
debugVertices[i * 6 + 2].position = positionPtr[i];
debugVertices[i * 6 + 2].color = Nz::Color::Blue;
debugVertices[i * 6 + 3].position = positionPtr[i] + tangentPtr[i] * 0.05f;
debugVertices[i * 6 + 3].color = Nz::Color::Blue;
Nz::Vector3f bitangent = Nz::Vector3f::CrossProduct(normalPtr[i], tangentPtr[i]);
debugVertices[i * 6 + 4].position = positionPtr[i];
debugVertices[i * 6 + 4].color = Nz::Color::Cyan;
debugVertices[i * 6 + 5].position = positionPtr[i] + bitangent * 0.05f;
debugVertices[i * 6 + 5].color = Nz::Color::Cyan;
}
std::shared_ptr<Nz::VertexBuffer> normalBuffer = std::make_shared<Nz::VertexBuffer>(debugDeclaration, vertexCount * 6, Nz::BufferUsage::Write, Nz::SoftwareBufferFactory, debugVertices.data());
std::shared_ptr<Nz::StaticMesh> staticMesh = std::make_shared<Nz::StaticMesh>(normalBuffer, nullptr);
staticMesh->GenerateAABB();
staticMesh->SetPrimitiveMode(Nz::PrimitiveMode::LineList);
debugMesh->AddSubMesh(std::move(staticMesh));
}
debugMesh->SetMaterialCount(1);
std::shared_ptr<Nz::GraphicalMesh> gfxMesh = Nz::GraphicalMesh::BuildFromMesh(*spaceshipMesh);
// Texture
std::shared_ptr<Nz::Image> diffuseImage = Nz::Image::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png");
if (!diffuseImage || !diffuseImage->Convert(Nz::PixelFormat::RGBA8_SRGB))
{
NazaraError("Failed to load image");
return __LINE__;
}
std::shared_ptr<Nz::GraphicalMesh> gfxMesh = Nz::GraphicalMesh::BuildFromMesh(*sphereMesh);
std::shared_ptr<Nz::GraphicalMesh> gfxDebugMesh = Nz::GraphicalMesh::BuildFromMesh(*debugMesh);
// Textures
Nz::TextureParams texParams;
texParams.renderDevice = device;
texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB;
std::shared_ptr<Nz::Material> material = std::make_shared<Nz::Material>();
@@ -70,18 +109,35 @@ int main()
material->AddPass("ForwardPass", forwardPass);
std::shared_ptr<Nz::Texture> normalMap = Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/normal.png", texParams);
std::shared_ptr<Nz::Texture> normalMap = Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_normal.png", texParams);
texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB;
std::shared_ptr<Nz::Material> debugMaterial = std::make_shared<Nz::Material>();
std::shared_ptr<Nz::MaterialPass> debugMaterialPass = std::make_shared<Nz::MaterialPass>(Nz::BasicMaterial::GetSettings());
debugMaterialPass->EnableDepthBuffer(true);
debugMaterialPass->SetPrimitiveMode(Nz::PrimitiveMode::LineList);
debugMaterial->AddPass("ForwardPass", debugMaterialPass);
Nz::BasicMaterial debugMat(*std::make_shared<Nz::MaterialPass>(Nz::BasicMaterial::GetSettings()));
Nz::PhongLightingMaterial phongMat(*forwardPass);
phongMat.EnableAlphaTest(false);
phongMat.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams));
phongMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams));
phongMat.SetNormalMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/normal.png", texParams));
phongMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_basecolor.png", texParams));
//pbrMat.SetMetallicMap(Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_metallic.png", texParams));
//pbrMat.SetRoughnessMap(Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_roughness.png", texParams));
phongMat.SetNormalMap(normalMap);
Nz::Model model(std::move(gfxMesh), spaceshipMesh->GetAABB());
Nz::Model model(std::move(gfxMesh), sphereMesh->GetAABB());
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, material);
Nz::Model debugModel(std::move(gfxDebugMesh), debugMesh->GetAABB());
for (std::size_t i = 0; i < debugModel.GetSubMeshCount(); ++i)
debugModel.SetMaterial(i, debugMaterial);
Nz::Vector2ui windowSize = window.GetSize();
Nz::Camera camera(window.GetRenderTarget());
@@ -104,11 +160,12 @@ int main()
std::size_t worldInstanceIndex1 = framePipeline.RegisterWorldInstance(modelInstance);
std::size_t worldInstanceIndex2 = framePipeline.RegisterWorldInstance(modelInstance2);
framePipeline.RegisterRenderable(worldInstanceIndex1, &model, 0xFFFFFFFF, scissorBox);
//framePipeline.RegisterRenderable(worldInstanceIndex1, &debugModel, 0xFFFFFFFF, scissorBox);
framePipeline.RegisterRenderable(worldInstanceIndex2, &model, 0xFFFFFFFF, scissorBox);
//framePipeline.RegisterRenderable(worldInstanceIndex2, &debugModel, 0xFFFFFFFF, scissorBox);
std::shared_ptr<Nz::SpotLight> light = std::make_shared<Nz::SpotLight>();
light->UpdateInnerAngle(Nz::DegreeAnglef(15.f));
light->UpdateOuterAngle(Nz::DegreeAnglef(20.f));
std::shared_ptr<Nz::DirectionalLight> light = std::make_shared<Nz::DirectionalLight>();
light->UpdateRotation(Nz::EulerAnglesf(-45.f, 0.f, 0.f));
framePipeline.RegisterLight(light, 0xFFFFFFFF);
@@ -167,7 +224,7 @@ int main()
camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f);
camQuat = camAngles;
light->UpdateRotation(camQuat);
//light->UpdateRotation(camQuat);
break;
}
@@ -212,7 +269,7 @@ int main()
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::LControl) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::RControl))
viewerPos += Nz::Vector3f::Down() * cameraSpeed;
light->UpdatePosition(viewerPos);
//light->UpdatePosition(viewerPos);
}
Nz::RenderFrame frame = window.AcquireFrame();

View File

@@ -1,4 +1,5 @@
#include <Nazara/Core.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
@@ -40,23 +41,20 @@ int main()
Nz::Modules<Nz::Graphics, Nz::Physics2D> nazara(rendererConfig);
Nz::RenderWindow window;
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
std::string windowTitle = "Graphics Test";
if (!window.Create(device, Nz::VideoMode(1920, 1080, 32), windowTitle))
{
std::cout << "Failed to create Window" << std::endl;
return __LINE__;
}
entt::registry registry;
Nz::Physics2DSystem physSytem(registry);
physSytem.GetPhysWorld().SetGravity({ 0.f, -9.81f });
Nz::SystemGraph systemGraph(registry);
Nz::Physics2DSystem& physSytem = systemGraph.AddSystem<Nz::Physics2DSystem>();
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
Nz::RenderSystem renderSystem(registry);
std::string windowTitle = "Graphics Test";
Nz::RenderWindow& window = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080), windowTitle);
Nz::Vector2ui windowSize = window.GetSize();
physSytem.GetPhysWorld().SetGravity({ 0.f, -9.81f });
entt::entity viewer = registry.create();
{
@@ -166,19 +164,10 @@ int main()
{
float updateTime = updateClock.Restart() / 1'000'000.f;
physSytem.Update(registry, 1000.f / 60.f);
physSytem.Update(1000.f / 60.f);
}
Nz::RenderFrame frame = window.AcquireFrame();
if (!frame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
renderSystem.Render(registry, frame);
frame.Present();
systemGraph.Update();
fps++;

View File

@@ -1,4 +1,5 @@
#include <Nazara/Core.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
@@ -39,17 +40,8 @@ int main()
Nz::Modules<Nz::Graphics, Nz::Physics3D> nazara(rendererConfig);
Nz::RenderWindow window;
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
std::string windowTitle = "Graphics Test";
if (!window.Create(device, Nz::VideoMode(1920, 1080, 32), windowTitle))
{
std::cout << "Failed to create Window" << std::endl;
return __LINE__;
}
Nz::MeshParams meshParams;
meshParams.center = true;
meshParams.matrix = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, 90.f, 0.f)) * Nz::Matrix4f::Scale(Nz::Vector3f(0.002f));
@@ -119,15 +111,19 @@ int main()
std::shared_ptr<Nz::TextSprite> sprite = std::make_shared<Nz::TextSprite>(spriteMaterial);
sprite->Update(Nz::SimpleTextDrawer::Draw("Voix ambiguë d'un cœur qui, au zéphyr, préfère les jattes de kiwis", 72), 0.01f);
Nz::Vector2ui windowSize = window.GetSize();
Nz::VertexMapper vertexMapper(*spaceshipMesh->GetSubMesh(0));
Nz::SparsePtr<Nz::Vector3f> vertices = vertexMapper.GetComponentPtr<Nz::Vector3f>(Nz::VertexComponent::Position);
entt::registry registry;
Nz::Physics3DSystem physSytem(registry);
Nz::RenderSystem renderSystem(registry);
Nz::SystemGraph systemGraph(registry);
Nz::Physics3DSystem& physSytem = systemGraph.AddSystem<Nz::Physics3DSystem>();
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
std::string windowTitle = "Graphics Test";
Nz::RenderWindow& window = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080, 32), windowTitle);
Nz::Vector2ui windowSize = window.GetSize();
entt::entity viewer = registry.create();
{
@@ -187,10 +183,10 @@ int main()
auto& headingNode = registry.emplace<Nz::NodeComponent>(headingEntity);
headingNode.SetInheritRotation(false);
headingNode.SetParent(registry, playerEntity);
headingNode.SetParent(entityNode);
}
registry.get<Nz::NodeComponent>(viewer).SetParent(registry, headingEntity);
registry.get<Nz::NodeComponent>(viewer).SetParent(entt::handle(registry, headingEntity));
registry.get<Nz::NodeComponent>(viewer).SetPosition(Nz::Vector3f::Backward() * 2.5f + Nz::Vector3f::Up() * 1.f);
for (std::size_t x = 0; x < 3; ++x)
@@ -310,10 +306,6 @@ int main()
if (updateClock.GetMilliseconds() > 1000 / 60)
{
float updateTime = updateClock.Restart() / 1'000'000.f;
physSytem.Update(registry, 1000.f / 60.f);
auto spaceshipView = registry.view<Nz::NodeComponent, Nz::RigidBody3DComponent>();
for (auto&& [entity, node, _] : spaceshipView.each())
{
@@ -360,16 +352,7 @@ int main()
playerShipBody.AddForce(Nz::Vector3f::Down() * 3.f * mass, Nz::CoordSys::Local);
}
Nz::RenderFrame frame = window.AcquireFrame();
if (!frame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
renderSystem.Render(registry, frame);
frame.Present();
systemGraph.Update();
fps++;

View File

@@ -1,5 +1,6 @@
// Sources pour https://github.com/NazaraEngine/NazaraEngine/wiki/(FR)-Tutoriel:-%5B01%5D-Hello-World
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Graphics/Components.hpp>
#include <Nazara/Graphics/Systems.hpp>
@@ -15,10 +16,10 @@ int main()
{
Nz::Modules<Nz::Graphics> nazara;
Nz::RenderWindow mainWindow(Nz::Graphics::Instance()->GetRenderDevice(), Nz::VideoMode(1280, 720, 32), "Test");
entt::registry registry;
Nz::RenderSystem renderSystem(registry);
Nz::SystemGraph systemGraph(registry);
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
Nz::RenderWindow& mainWindow = renderSystem.CreateWindow(Nz::Graphics::Instance()->GetRenderDevice(), Nz::VideoMode(1280, 720), "Tut01 - Hello world");
entt::entity cameraEntity = registry.create();
{
@@ -62,17 +63,7 @@ int main()
while (mainWindow.IsOpen())
{
mainWindow.ProcessEvents();
Nz::RenderFrame renderFrame = mainWindow.AcquireFrame();
if (!renderFrame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
renderSystem.Render(registry, renderFrame);
renderFrame.Present();
systemGraph.Update();
}
return EXIT_SUCCESS;

View File

@@ -1,5 +1,6 @@
// Sources pour https://github.com/NazaraEngine/NazaraEngine/wiki/(FR)-Tutoriel:-%5B02%5D-Gestion-des-événements
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Graphics/Components.hpp>
#include <Nazara/Graphics/Systems.hpp>
@@ -15,10 +16,10 @@ int main()
{
Nz::Modules<Nz::Graphics> nazara;
Nz::RenderWindow mainWindow(Nz::Graphics::Instance()->GetRenderDevice(), Nz::VideoMode(1280, 720, 32), "Test");
entt::registry registry;
Nz::RenderSystem renderSystem(registry);
Nz::SystemGraph systemGraph(registry);
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
Nz::RenderWindow& mainWindow = renderSystem.CreateWindow(Nz::Graphics::Instance()->GetRenderDevice(), Nz::VideoMode(1280, 720), "Tut02 - Events");
entt::entity cameraEntity = registry.create();
{
@@ -79,17 +80,7 @@ int main()
while (mainWindow.IsOpen())
{
mainWindow.ProcessEvents();
Nz::RenderFrame renderFrame = mainWindow.AcquireFrame();
if (!renderFrame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
renderSystem.Render(registry, renderFrame);
renderFrame.Present();
systemGraph.Update();
}
return EXIT_SUCCESS;

View File

@@ -1,4 +1,5 @@
#include <Nazara/Core.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
@@ -43,14 +44,11 @@ int main()
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
std::string windowTitle = "Widget Test";
if (!window.Create(device, Nz::VideoMode(1920, 1080, 32), windowTitle))
{
std::cout << "Failed to create Window" << std::endl;
return __LINE__;
}
entt::registry registry;
Nz::RenderSystem renderSystem(registry);
Nz::SystemGraph systemGraph(registry);
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
Nz::RenderWindow& mainWindow = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080), windowTitle);
Nz::Canvas canvas2D(registry, window.GetEventHandler(), window.GetCursorController().CreateHandle(), 0xFFFFFFFF);
canvas2D.Resize(Nz::Vector2f(window.GetSize()));
@@ -148,13 +146,7 @@ int main()
}
}
Nz::RenderFrame frame = window.AcquireFrame();
if (!frame)
continue;
renderSystem.Render(registry, frame);
frame.Present();
systemGraph.Update();
fps++;