Update examples and tests code

This commit is contained in:
SirLynix 2023-01-21 19:10:07 +01:00 committed by Jérôme Leclercq
parent 2b7ff9274c
commit 4668a1d158
11 changed files with 418 additions and 533 deletions

View File

@ -82,13 +82,11 @@ int main()
else
rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL;
Nz::Modules<Nz::Graphics> nazara(rendererConfig);
Nz::Application<Nz::Graphics> app(rendererConfig);
nzsl::ShaderWriter::States states;
states.shaderModuleResolver = Nz::Graphics::Instance()->GetShaderModuleResolver();
Nz::RenderWindow window;
Nz::MeshParams meshParams;
meshParams.center = true;
meshParams.vertexRotation = Nz::EulerAnglesf(0.f, -90.f, 0.f);
@ -98,12 +96,11 @@ int main()
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
const Nz::RenderDeviceInfo& deviceInfo = device->GetDeviceInfo();
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
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::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080), windowTitle);
Nz::WindowSwapchain windowSwapchain(device, window);
std::shared_ptr<Nz::Mesh> spaceship = Nz::Mesh::LoadFromFile(resourceDir / "Spaceship/spaceship.obj", meshParams);
if (!spaceship)
@ -1061,8 +1058,6 @@ int main()
Nz::EulerAnglesf camAngles(-30.f, 0.f, 0.f);
Nz::Quaternionf camQuat(camAngles);
window.EnableEventPolling(true);
Nz::MillisecondClock updateClock;
Nz::MillisecondClock fpsClock;
unsigned int fps = 0;
@ -1093,76 +1088,69 @@ int main()
return Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, elapsedTime * rotationSpeed, 0.f)) * direction;
};
while (window.IsOpen())
window.GetEventHandler().OnEvent.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent& event)
{
Nz::Time now = Nz::GetElapsedNanoseconds();
if (lightAnimation)
elapsedTime += now - time;
time = now;
Nz::WindowEvent event;
while (window.PollEvent(&event))
switch (event.type)
{
switch (event.type)
case Nz::WindowEventType::MouseMoved: // La souris a bougé
{
case Nz::WindowEventType::Quit:
window.Close();
break;
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.3f; // Sensibilité de la souris
case Nz::WindowEventType::MouseMoved: // La souris a bougé
{
// 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();
// 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);
// 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);
camQuat = camAngles;
break;
}
case Nz::WindowEventType::KeyPressed:
{
if (event.key.scancode == Nz::Keyboard::Scancode::Space)
{
float elapsedSeconds = elapsedTime.AsSeconds();
float rotationSpeed = ComputeLightAnimationSpeed(viewerPos);
auto& spotLight = spotLights.emplace_back();
spotLight.color = Nz::Color(0.4f, 0.4f, 1.f);
spotLight.radius = 5.f;
spotLight.position = AnimateLightPosition(viewerPos, rotationSpeed, -elapsedSeconds);
spotLight.direction = AnimateLightDirection(camQuat * Nz::Vector3f::Forward(), rotationSpeed, -elapsedSeconds);
lightUpdate = true;
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::F)
forwardEnabled = !forwardEnabled;
else if (event.key.virtualKey == Nz::Keyboard::VKey::A)
lightAnimation = !lightAnimation;
else if (event.key.virtualKey == Nz::Keyboard::VKey::B)
bloomEnabled = !bloomEnabled;
else if (event.key.virtualKey == Nz::Keyboard::VKey::E)
modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Transform(viewerPos, camQuat));
break;
}
case Nz::WindowEventType::Resized:
{
Nz::Vector2ui newSize = window.GetSize();
viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newSize.x) / newSize.y, 0.1f, 1000.f));
break;
}
default:
break;
camQuat = camAngles;
break;
}
case Nz::WindowEventType::KeyPressed:
{
if (event.key.scancode == Nz::Keyboard::Scancode::Space)
{
float elapsedSeconds = elapsedTime.AsSeconds();
float rotationSpeed = ComputeLightAnimationSpeed(viewerPos);
auto& spotLight = spotLights.emplace_back();
spotLight.color = Nz::Color(0.4f, 0.4f, 1.f);
spotLight.radius = 5.f;
spotLight.position = AnimateLightPosition(viewerPos, rotationSpeed, -elapsedSeconds);
spotLight.direction = AnimateLightDirection(camQuat * Nz::Vector3f::Forward(), rotationSpeed, -elapsedSeconds);
lightUpdate = true;
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::F)
forwardEnabled = !forwardEnabled;
else if (event.key.virtualKey == Nz::Keyboard::VKey::A)
lightAnimation = !lightAnimation;
else if (event.key.virtualKey == Nz::Keyboard::VKey::B)
bloomEnabled = !bloomEnabled;
else if (event.key.virtualKey == Nz::Keyboard::VKey::E)
modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Transform(viewerPos, camQuat));
break;
}
case Nz::WindowEventType::Resized:
{
Nz::Vector2ui newSize = window.GetSize();
viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newSize.x) / newSize.y, 0.1f, 1000.f));
break;
}
default:
break;
}
});
app.AddUpdater([&](Nz::Time deltaTime)
{
if (lightAnimation)
elapsedTime += deltaTime;
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
{
@ -1194,11 +1182,11 @@ int main()
viewerInstance.UpdateViewMatrix(Nz::Matrix4f::TransformInverse(viewerPos, camQuat));
}
Nz::RenderFrame frame = window.AcquireFrame();
Nz::RenderFrame frame = windowSwapchain.AcquireFrame();
if (!frame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
return;
}
currentFrame = &frame;
@ -1535,7 +1523,7 @@ int main()
bakedGraph.Execute(frame);
const Nz::RenderTarget* windowRT = window.GetRenderTarget();
const Nz::RenderTarget* windowRT = &windowSwapchain.GetSwapchain();
frame.Execute([&](Nz::CommandBufferBuilder& builder)
{
Nz::Recti windowRenderRect(0, 0, window.GetSize().x, window.GetSize().y);
@ -1572,7 +1560,7 @@ int main()
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS");
fps = 0;
}
}
});
return EXIT_SUCCESS;
return app.Run();
}

View File

@ -21,9 +21,15 @@ int main()
else
rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL;
Nz::Modules<Nz::Graphics> nazara(rendererConfig);
Nz::Application<Nz::Graphics> app(rendererConfig);
Nz::RenderWindow window;
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
std::string windowTitle = "Physically Based Rendering Test";
Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080), windowTitle);
Nz::WindowSwapchain windowSwapchain(device, window);
Nz::MeshParams meshParams;
meshParams.center = true;
@ -31,15 +37,6 @@ int main()
meshParams.vertexScale = Nz::Vector3f(0.002f);
meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent);
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
std::string windowTitle = "Physically Based Rendering Test";
if (!window.Create(device, Nz::VideoMode(1920, 1080, 32), windowTitle))
{
std::cout << "Failed to create Window" << std::endl;
return __LINE__;
}
std::shared_ptr<Nz::Mesh> sphereMesh = std::make_shared<Nz::Mesh>();
sphereMesh->CreateStatic();
sphereMesh->BuildSubMesh(Nz::Primitive::UVSphere(1.f, 50, 50));
@ -74,7 +71,7 @@ int main()
Nz::Vector2ui windowSize = window.GetSize();
Nz::Camera camera(window.GetRenderTarget());
Nz::Camera camera(&windowSwapchain.GetSwapchain());
//camera.UpdateClearColor(Nz::Color::Gray);
Nz::ViewerInstance& viewerInstance = camera.GetViewerInstance();
@ -102,65 +99,58 @@ int main()
Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
Nz::Quaternionf camQuat(camAngles);
window.EnableEventPolling(true);
Nz::MillisecondClock updateClock;
Nz::MillisecondClock fpsClock;
unsigned int fps = 0;
Nz::Mouse::SetRelativeMouseMode(true);
while (window.IsOpen())
window.GetEventHandler().OnEvent.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent& event)
{
Nz::WindowEvent event;
while (window.PollEvent(&event))
switch (event.type)
{
switch (event.type)
case Nz::WindowEventType::KeyPressed:
if (event.key.virtualKey == Nz::Keyboard::VKey::N)
{
if (materialInstance->GetTextureProperty(normalMapProperty))
materialInstance->SetTextureProperty(normalMapProperty, {});
else
materialInstance->SetTextureProperty(normalMapProperty, normalMap);
}
break;
case Nz::WindowEventType::MouseMoved: // La souris a bougé
{
case Nz::WindowEventType::Quit:
window.Close();
break;
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.3f; // Sensibilité de la souris
case Nz::WindowEventType::KeyPressed:
if (event.key.virtualKey == Nz::Keyboard::VKey::N)
{
if (materialInstance->GetTextureProperty(normalMapProperty))
materialInstance->SetTextureProperty(normalMapProperty, {});
else
materialInstance->SetTextureProperty(normalMapProperty, normalMap);
}
break;
// 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();
case Nz::WindowEventType::MouseMoved: // La souris a bougé
{
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.3f; // Sensibilité de la souris
// 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);
// 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);
camQuat = camAngles;
//light->UpdateRotation(camQuat);
break;
}
case Nz::WindowEventType::Resized:
{
Nz::Vector2ui newWindowSize = window.GetSize();
viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f));
viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize));
break;
}
default:
break;
camQuat = camAngles;
//light->UpdateRotation(camQuat);
break;
}
}
case Nz::WindowEventType::Resized:
{
Nz::Vector2ui newWindowSize = window.GetSize();
viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f));
viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize));
break;
}
default:
break;
}
});
app.AddUpdater([&](Nz::Time elapsedTime)
{
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
{
float cameraSpeed = 2.f * deltaTime->AsSeconds();
@ -191,11 +181,11 @@ int main()
//light->UpdatePosition(viewerPos);
}
Nz::RenderFrame frame = window.AcquireFrame();
Nz::RenderFrame frame = windowSwapchain.AcquireFrame();
if (!frame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
return;
}
viewerInstance.UpdateViewMatrix(Nz::Matrix4f::TransformInverse(viewerPos, camAngles));
@ -213,7 +203,7 @@ int main()
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS");
fps = 0;
}
}
});
return EXIT_SUCCESS;
return app.Run();
}

View File

@ -1,4 +1,5 @@
#include <Nazara/Core.hpp>
#include <Nazara/Core/AppEntitySystemComponent.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Graphics.hpp>
@ -36,27 +37,28 @@ int main()
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Nz::Modules<Nz::Graphics, Nz::Physics2D> nazara(rendererConfig);
Nz::Application<Nz::Graphics, Nz::Physics2D> app(rendererConfig);
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();
Nz::Physics2DSystem& physSytem = ecs.AddSystem<Nz::Physics2DSystem>();
Nz::RenderSystem& renderSystem = ecs.AddSystem<Nz::RenderSystem>();
std::string windowTitle = "Physics 2D";
Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080, 32), windowTitle);
Nz::WindowSwapchain& windowSwapchain = renderSystem.CreateSwapchain(window);
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
entt::registry registry;
Nz::SystemGraph systemGraph(registry);
Nz::Physics2DSystem& physSytem = systemGraph.AddSystem<Nz::Physics2DSystem>();
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
std::string windowTitle = "Graphics Test";
Nz::RenderWindow& window = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080), windowTitle);
Nz::Vector2f windowSize = Nz::Vector2f(window.GetSize());
physSytem.GetPhysWorld().SetGravity({ 0.f, -98.1f });
entt::entity viewer = registry.create();
entt::handle viewer = ecs.CreateEntity();
{
registry.emplace<Nz::NodeComponent>(viewer);
auto& cameraComponent = registry.emplace<Nz::CameraComponent>(viewer, window.GetRenderTarget(), Nz::ProjectionType::Orthographic);
viewer.emplace<Nz::NodeComponent>();
auto& cameraComponent = viewer.emplace<Nz::CameraComponent>(&windowSwapchain.GetSwapchain(), Nz::ProjectionType::Orthographic);
cameraComponent.UpdateRenderMask(1);
cameraComponent.UpdateClearColor(Nz::Color(0.5f, 0.5f, 0.5f));
}
@ -75,23 +77,23 @@ int main()
{
for (std::size_t x = 0; x < 30; ++x)
{
entt::entity spriteEntity = registry.create();
entt::handle spriteEntity = ecs.CreateEntity();
{
std::shared_ptr<Nz::Sprite> sprite = std::make_shared<Nz::Sprite>(spriteMaterial);
sprite->SetSize({ 32.f, 32.f });
sprite->SetOrigin({ 0.5f, 0.5f });
registry.emplace<Nz::NodeComponent>(spriteEntity).SetPosition(windowSize.x * 0.5f + x * 48.f - 15.f * 48.f, windowSize.y / 2 + y * 48.f);
spriteEntity.emplace<Nz::NodeComponent>().SetPosition(windowSize.x * 0.5f + x * 48.f - 15.f * 48.f, windowSize.y / 2 + y * 48.f);
registry.emplace<Nz::GraphicsComponent>(spriteEntity).AttachRenderable(sprite, 1);
auto& rigidBody = registry.emplace<Nz::RigidBody2DComponent>(spriteEntity, physSytem.CreateRigidBody(50.f, std::make_shared<Nz::BoxCollider2D>(Nz::Vector2f(32.f, 32.f))));
spriteEntity.emplace<Nz::GraphicsComponent>().AttachRenderable(sprite, 1);
auto& rigidBody = spriteEntity.emplace<Nz::RigidBody2DComponent>(physSytem.CreateRigidBody(50.f, std::make_shared<Nz::BoxCollider2D>(Nz::Vector2f(32.f, 32.f))));
rigidBody.SetFriction(0.9f);
//rigidBody.SetElasticity(0.99f);
}
}
}
entt::entity groundEntity = registry.create();
entt::handle groundEntity = ecs.CreateEntity();
{
std::shared_ptr<Nz::Tilemap> tilemap = std::make_shared<Nz::Tilemap>(Nz::Vector2ui(40, 20), Nz::Vector2f(64.f, 64.f), 18);
tilemap->SetOrigin({ 0.5f, 0.5f });
@ -111,17 +113,15 @@ int main()
}
}
registry.emplace<Nz::NodeComponent>(groundEntity).SetPosition(windowSize.x * 0.5f, -windowSize.y * 0.2f);
registry.emplace<Nz::GraphicsComponent>(groundEntity).AttachRenderable(tilemap, 1);
auto& rigidBody = registry.emplace<Nz::RigidBody2DComponent>(groundEntity, physSytem.CreateRigidBody(0.f, std::make_shared<Nz::BoxCollider2D>(tilemap->GetSize())));
groundEntity.emplace<Nz::NodeComponent>().SetPosition(windowSize.x * 0.5f, -windowSize.y * 0.2f);
groundEntity.emplace<Nz::GraphicsComponent>().AttachRenderable(tilemap, 1);
auto& rigidBody = groundEntity.emplace<Nz::RigidBody2DComponent>(physSytem.CreateRigidBody(0.f, std::make_shared<Nz::BoxCollider2D>(tilemap->GetSize())));
rigidBody.SetFriction(0.9f);
}
Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
Nz::Quaternionf camQuat(camAngles);
window.EnableEventPolling(true);
Nz::MillisecondClock secondClock;
unsigned int fps = 0;
@ -132,39 +132,16 @@ int main()
Nz::PidController<Nz::Vector3f> headingController(0.5f, 0.f, 0.05f);
Nz::PidController<Nz::Vector3f> upController(1.f, 0.f, 0.1f);
bool showColliders = false;
while (window.IsOpen())
app.AddUpdater([&](Nz::Time elapsedTime)
{
Nz::WindowEvent event;
while (window.PollEvent(&event))
{
switch (event.type)
{
case Nz::WindowEventType::Quit:
window.Close();
break;
case Nz::WindowEventType::KeyPressed:
break;
case Nz::WindowEventType::MouseMoved:
break;
default:
break;
}
}
systemGraph.Update();
fps++;
if (secondClock.RestartIfOver(Nz::Time::Second()))
{
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities");
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(ecs.GetRegistry().alive()) + " entities");
fps = 0;
}
}
});
return EXIT_SUCCESS;
return app.Run();
}

View File

@ -1,10 +1,11 @@
#include <Nazara/Core.hpp>
#include <Nazara/Core/AppEntitySystemComponent.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
#include <Nazara/Graphics/Components.hpp>
#include <Nazara/Graphics/Systems.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
#include <Nazara/Math/PidController.hpp>
#include <Nazara/Physics3D.hpp>
#include <Nazara/Physics3D/Components.hpp>
@ -36,7 +37,17 @@ int main()
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Nz::Modules<Nz::Graphics, Nz::Physics3D> nazara(rendererConfig);
Nz::Application<Nz::Graphics, Nz::Physics3D> app(rendererConfig);
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();
Nz::Physics3DSystem& physSytem = ecs.AddSystem<Nz::Physics3DSystem>();
Nz::RenderSystem& renderSystem = ecs.AddSystem<Nz::RenderSystem>();
std::string windowTitle = "Physics 3D";
Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1920, 1080, 32), windowTitle);
Nz::WindowSwapchain& windowSwapchain = renderSystem.CreateSwapchain(window);
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
@ -94,21 +105,12 @@ int main()
Nz::VertexMapper vertexMapper(*spaceshipMesh->GetSubMesh(0));
Nz::SparsePtr<Nz::Vector3f> vertices = vertexMapper.GetComponentPtr<Nz::Vector3f>(Nz::VertexComponent::Position);
entt::registry 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();
entt::handle viewer = ecs.CreateEntity();
{
registry.emplace<Nz::NodeComponent>(viewer);
auto& cameraComponent = registry.emplace<Nz::CameraComponent>(viewer, window.GetRenderTarget());
viewer.emplace<Nz::NodeComponent>();
auto& cameraComponent = viewer.emplace<Nz::CameraComponent>(&windowSwapchain.GetSwapchain());
cameraComponent.UpdateRenderMask(1);
cameraComponent.UpdateClearColor(Nz::Color(0.5f, 0.5f, 0.5f));
}
@ -136,40 +138,40 @@ int main()
colliderModel->SetMaterial(i, colliderMat);
}
entt::entity textEntity = registry.create();
entt::handle textEntity = ecs.CreateEntity();
{
auto& entityGfx = registry.emplace<Nz::GraphicsComponent>(textEntity);
auto& entityGfx = textEntity.emplace<Nz::GraphicsComponent>();
entityGfx.AttachRenderable(sprite, 1);
auto& entityNode = registry.emplace<Nz::NodeComponent>(textEntity);
auto& entityNode = textEntity.emplace<Nz::NodeComponent>();
entityNode.SetPosition(0.f, 5.f, 0.f);
}
entt::entity playerEntity = registry.create();
entt::handle playerEntity = ecs.CreateEntity();
entt::entity headingEntity = registry.create();
entt::handle headingEntity = ecs.CreateEntity();
{
auto& entityLight = registry.emplace<Nz::LightComponent>(playerEntity);
auto& entityLight = playerEntity.emplace<Nz::LightComponent>();
auto& spotLight = entityLight.AddLight<Nz::SpotLight>(1);
spotLight.EnableShadowCasting(true);
spotLight.UpdateShadowMapSize(1024);
auto& entityGfx = registry.emplace<Nz::GraphicsComponent>(playerEntity);
auto& entityGfx = playerEntity.emplace<Nz::GraphicsComponent>();
entityGfx.AttachRenderable(model, 1);
auto& entityNode = registry.emplace<Nz::NodeComponent>(playerEntity);
auto& entityNode = playerEntity.emplace<Nz::NodeComponent>();
entityNode.SetPosition(Nz::Vector3f(12.5f, 0.f, 25.f));
auto& entityPhys = registry.emplace<Nz::RigidBody3DComponent>(playerEntity, physSytem.CreateRigidBody(shipCollider));
auto& entityPhys = playerEntity.emplace<Nz::RigidBody3DComponent>(physSytem.CreateRigidBody(shipCollider));
entityPhys.SetMass(50.f);
entityPhys.SetAngularDamping(Nz::Vector3f::Zero());
auto& headingNode = registry.emplace<Nz::NodeComponent>(headingEntity);
auto& headingNode = headingEntity.emplace<Nz::NodeComponent>();
headingNode.SetInheritRotation(false);
headingNode.SetParent(entityNode);
}
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);
viewer.get<Nz::NodeComponent>().SetParent(headingEntity);
viewer.get<Nz::NodeComponent>().SetPosition(Nz::Vector3f::Backward() * 2.5f + Nz::Vector3f::Up() * 1.f);
for (std::size_t x = 0; x < 3; ++x)
{
@ -177,15 +179,15 @@ int main()
{
for (std::size_t z = 0; z < 3; ++z)
{
entt::entity entity = registry.create();
auto& entityGfx = registry.emplace<Nz::GraphicsComponent>(entity);
entt::handle entity = ecs.CreateEntity();
auto& entityGfx = entity.emplace<Nz::GraphicsComponent>();
entityGfx.AttachRenderable(model, 1);
auto& entityNode = registry.emplace<Nz::NodeComponent>(entity);
auto& entityNode = entity.emplace<Nz::NodeComponent>();
entityNode.SetPosition(Nz::Vector3f(x * 2.f, y * 1.5f, z * 2.f));
entityNode.SetRotation(Nz::EulerAnglesf(0.f, Nz::TurnAnglef(0.5f), 0.f));
auto& entityPhys = registry.emplace<Nz::RigidBody3DComponent>(entity, physSytem.CreateRigidBody(shipCollider));
auto& entityPhys = entity.emplace<Nz::RigidBody3DComponent>(physSytem.CreateRigidBody(shipCollider));
entityPhys.SetMass(1.f);
entityPhys.SetAngularDamping(Nz::Vector3f::Zero());
entityPhys.SetLinearDamping(0.f);
@ -196,8 +198,6 @@ int main()
Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
Nz::Quaternionf camQuat(camAngles);
window.EnableEventPolling(true);
Nz::MillisecondClock updateClock;
Nz::MillisecondClock fpsClock;
unsigned int fps = 0;
@ -208,82 +208,69 @@ int main()
Nz::PidController<Nz::Vector3f> upController(1.f, 0.f, 0.1f);
bool showColliders = false;
while (window.IsOpen())
Nz::WindowEventHandler& eventHandler = window.GetEventHandler();
eventHandler.OnKeyPressed.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& event)
{
Nz::WindowEvent event;
while (window.PollEvent(&event))
if (event.virtualKey == Nz::Keyboard::VKey::A)
{
switch (event.type)
bool alphaTestEnabled = std::get<bool>(*material->GetValueProperty("AlphaTest"));
material->SetValueProperty("AlphaTest", !alphaTestEnabled);
}
else if (event.virtualKey == Nz::Keyboard::VKey::B)
{
showColliders = !showColliders;
if (showColliders)
{
case Nz::WindowEventType::Quit:
window.Close();
break;
case Nz::WindowEventType::KeyPressed:
if (event.key.virtualKey == Nz::Keyboard::VKey::A)
{
bool alphaTestEnabled = std::get<bool>(*material->GetValueProperty("AlphaTest"));
material->SetValueProperty("AlphaTest", !alphaTestEnabled);
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::B)
{
showColliders = !showColliders;
if (showColliders)
{
auto view = registry.view<Nz::GraphicsComponent, Nz::RigidBody3DComponent>();
for (auto [entity, gfxComponent, _] : view.each())
gfxComponent.AttachRenderable(colliderModel, 1);
}
else
{
auto view = registry.view<Nz::GraphicsComponent, Nz::RigidBody3DComponent>();
for (auto [entity, gfxComponent, _] : view.each())
gfxComponent.DetachRenderable(colliderModel);
}
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::Space)
{
entt::entity entity = registry.create();
auto& entityGfx = registry.emplace<Nz::GraphicsComponent>(entity);
entityGfx.AttachRenderable(model, 1);
if (showColliders)
entityGfx.AttachRenderable(colliderModel, 1);
registry.emplace<Nz::NodeComponent>(entity);
auto& entityPhys = registry.emplace<Nz::RigidBody3DComponent>(entity, physSytem.CreateRigidBody(shipCollider));
entityPhys.SetMass(1.f);
entityPhys.SetAngularDamping(Nz::Vector3f::Zero());
entityPhys.SetLinearDamping(0.f);
}
break;
case Nz::WindowEventType::MouseMoved:
{
float sensitivity = 0.3f;
camAngles.yaw = camAngles.yaw - event.mouseMove.deltaX * sensitivity;
camAngles.pitch = camAngles.pitch - event.mouseMove.deltaY * sensitivity;
camAngles.Normalize();
camQuat = camAngles;
registry.get<Nz::NodeComponent>(headingEntity).SetRotation(camQuat);
break;
}
default:
break;
auto view = ecs.GetRegistry().view<Nz::GraphicsComponent, Nz::RigidBody3DComponent>();
for (auto [entity, gfxComponent, _] : view.each())
gfxComponent.AttachRenderable(colliderModel, 1);
}
else
{
auto view = ecs.GetRegistry().view<Nz::GraphicsComponent, Nz::RigidBody3DComponent>();
for (auto [entity, gfxComponent, _] : view.each())
gfxComponent.DetachRenderable(colliderModel);
}
}
else if (event.virtualKey == Nz::Keyboard::VKey::Space)
{
entt::handle entity = ecs.CreateEntity();
auto& entityGfx = entity.emplace<Nz::GraphicsComponent>();
entityGfx.AttachRenderable(model, 1);
if (showColliders)
entityGfx.AttachRenderable(colliderModel, 1);
entity.emplace<Nz::NodeComponent>();
auto& entityPhys = entity.emplace<Nz::RigidBody3DComponent>(physSytem.CreateRigidBody(shipCollider));
entityPhys.SetMass(1.f);
entityPhys.SetAngularDamping(Nz::Vector3f::Zero());
entityPhys.SetLinearDamping(0.f);
}
});
eventHandler.OnMouseMoved.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
{
float sensitivity = 0.3f;
camAngles.yaw = camAngles.yaw - event.deltaX * sensitivity;
camAngles.pitch = camAngles.pitch - event.deltaY * sensitivity;
camAngles.Normalize();
camQuat = camAngles;
headingEntity.get<Nz::NodeComponent>().SetRotation(camQuat);
});
app.AddUpdater([&](Nz::Time elapsedTime)
{
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
{
float elapsedTime = deltaTime->AsSeconds();
auto spaceshipView = registry.view<Nz::NodeComponent, Nz::RigidBody3DComponent>();
auto spaceshipView = ecs.GetRegistry().view<Nz::NodeComponent, Nz::RigidBody3DComponent>();
for (auto&& [entity, node, _] : spaceshipView.each())
{
if (entity == playerEntity)
@ -291,17 +278,17 @@ int main()
Nz::Vector3f spaceshipPos = node.GetPosition(Nz::CoordSys::Global);
if (spaceshipPos.GetSquaredLength() > Nz::IntegralPow(20.f, 2))
registry.destroy(entity);
ecs.GetRegistry().destroy(entity);
}
Nz::RigidBody3DComponent& playerShipBody = registry.get<Nz::RigidBody3DComponent>(playerEntity);
Nz::RigidBody3DComponent& playerShipBody = playerEntity.get<Nz::RigidBody3DComponent>();
Nz::Quaternionf currentRotation = playerShipBody.GetRotation();
Nz::Vector3f desiredHeading = registry.get<Nz::NodeComponent>(headingEntity).GetForward();
Nz::Vector3f desiredHeading = headingEntity.get<Nz::NodeComponent>().GetForward();
Nz::Vector3f currentHeading = currentRotation * Nz::Vector3f::Forward();
Nz::Vector3f headingError = currentHeading.CrossProduct(desiredHeading);
Nz::Vector3f desiredUp = registry.get<Nz::NodeComponent>(headingEntity).GetUp();
Nz::Vector3f desiredUp = headingEntity.get<Nz::NodeComponent>().GetUp();
Nz::Vector3f currentUp = currentRotation * Nz::Vector3f::Up();
Nz::Vector3f upError = currentUp.CrossProduct(desiredUp);
@ -329,16 +316,14 @@ int main()
playerShipBody.AddForce(Nz::Vector3f::Down() * 3.f * mass, Nz::CoordSys::Local);
}
systemGraph.Update();
fps++;
if (fpsClock.RestartIfOver(Nz::Time::Second()))
{
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities");
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(ecs.GetRegistry().alive()) + " entities");
fps = 0;
}
}
});
return EXIT_SUCCESS;
return app.Run();
}

View File

@ -1,4 +1,5 @@
#include <Nazara/Core.hpp>
#include <Nazara/Core/AppEntitySystemComponent.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Graphics.hpp>
@ -37,47 +38,49 @@ int main()
else
rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL;
Nz::Modules<Nz::Graphics> nazara(rendererConfig);
Nz::Application<Nz::Graphics> app(rendererConfig);
Nz::PluginLoader loader;
Nz::Plugin<Nz::AssimpPlugin> assimp = loader.Load<Nz::AssimpPlugin>();
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
entt::registry registry;
Nz::SystemGraph systemGraph(registry);
systemGraph.AddSystem<Nz::SkeletonSystem>();
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();
ecs.AddSystem<Nz::SkeletonSystem>();
Nz::Physics3DSystem& physSytem = systemGraph.AddSystem<Nz::Physics3DSystem>();
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
Nz::Physics3DSystem& physSytem = ecs.AddSystem<Nz::Physics3DSystem>();
Nz::RenderSystem& renderSystem = ecs.AddSystem<Nz::RenderSystem>();
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
std::string windowTitle = "Skinning test";
Nz::RenderWindow& window = renderSystem.CreateWindow(device, Nz::VideoMode(1280, 720, 32), windowTitle);
Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), windowTitle);
auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow);
physSytem.GetPhysWorld().SetGravity({ 0.f, -9.81f, 0.f });
Nz::TextureParams texParams;
texParams.renderDevice = device;
entt::entity playerEntity = registry.create();
entt::entity playerRotation = registry.create();
entt::entity playerCamera = registry.create();
entt::handle playerEntity = ecs.CreateEntity();
entt::handle playerRotation = ecs.CreateEntity();
entt::handle playerCamera = ecs.CreateEntity();
{
auto& playerNode = registry.emplace<Nz::NodeComponent>(playerEntity);
auto& playerNode = playerEntity.emplace<Nz::NodeComponent>();
playerNode.SetPosition(0.f, 1.8f, 1.f);
auto& playerBody = registry.emplace<Nz::RigidBody3DComponent>(playerEntity, &physSytem.GetPhysWorld());
auto& playerBody = playerEntity.emplace<Nz::RigidBody3DComponent>(&physSytem.GetPhysWorld());
playerBody.SetMass(42.f);
playerBody.SetGeom(std::make_shared<Nz::BoxCollider3D>(Nz::Vector3f::Unit()));
auto& playerRotNode = registry.emplace<Nz::NodeComponent>(playerRotation);
auto& playerRotNode = playerRotation.emplace<Nz::NodeComponent>();
playerRotNode.SetParent(playerNode);
auto& cameraNode = registry.emplace<Nz::NodeComponent>(playerCamera);
auto& cameraNode = playerCamera.emplace<Nz::NodeComponent>();
cameraNode.SetPosition(Nz::Vector3f::Up() * 2.f + Nz::Vector3f::Backward());
//cameraNode.SetParent(playerRotNode);
auto& cameraComponent = registry.emplace<Nz::CameraComponent>(playerCamera, window.GetRenderTarget());
auto& cameraComponent = playerCamera.emplace<Nz::CameraComponent>(&windowSwapchain.GetSwapchain());
cameraComponent.UpdateZNear(0.2f);
cameraComponent.UpdateZFar(10000.f);
cameraComponent.UpdateRenderMask(1);
@ -163,14 +166,14 @@ int main()
}
}*/
entt::handle bobEntity = entt::handle(registry, registry.create());
entt::entity lightEntity1 = registry.create();
entt::handle bobEntity = ecs.CreateEntity();
entt::handle lightEntity1 = ecs.CreateEntity();
{
auto& lightNode = registry.emplace<Nz::NodeComponent>(lightEntity1);
auto& lightNode = lightEntity1.emplace<Nz::NodeComponent>();
lightNode.SetPosition(Nz::Vector3f::Up() * 3.f + Nz::Vector3f::Backward() * 1.f);
lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 0.f, 0.f));
auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity1);
auto& cameraLight = lightEntity1.emplace<Nz::LightComponent>();
auto& spotLight = cameraLight.AddLight<Nz::SpotLight>(0xFFFFFFFF);
spotLight.UpdateColor(Nz::Color::Red());
@ -179,20 +182,21 @@ int main()
spotLight.EnableShadowCasting(true);
}
entt::entity lightEntity2 = registry.create();
entt::handle lightEntity2 = ecs.CreateEntity();
{
auto& lightNode = registry.emplace<Nz::NodeComponent>(lightEntity2);
auto& lightNode = lightEntity2.emplace<Nz::NodeComponent>();
lightNode.SetPosition(Nz::Vector3f::Up() * 3.5f + Nz::Vector3f::Right() * 1.5f);
lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 90.f, 0.f));
auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity2);
auto& cameraLight = lightEntity2.emplace<Nz::LightComponent>();
auto& spotLight = cameraLight.AddLight<Nz::SpotLight>(0xFFFFFFFF);
spotLight.UpdateColor(Nz::Color::Green());
spotLight.EnableShadowCasting(true);
spotLight.UpdateShadowMapSize(1024);
}
entt::entity lightEntity3 = registry.create();
entt::handle lightEntity3 = ecs.CreateEntity();
{
auto& bobNode = bobEntity.emplace<Nz::NodeComponent>();
@ -205,8 +209,7 @@ int main()
auto& sharedSkeleton = bobEntity.emplace<Nz::SharedSkeletonComponent>(skeleton);
entt::entity sphereEntity = registry.create();
entt::handle sphereEntity = ecs.CreateEntity();
{
std::shared_ptr<Nz::Mesh> sphereMesh = std::make_shared<Nz::Mesh>();
sphereMesh->CreateStatic();
@ -227,35 +230,35 @@ int main()
for (std::size_t i = 0; i < sphereModel->GetSubMeshCount(); ++i)
sphereModel->SetMaterial(i, sphereMat);
auto& sphereNode = registry.emplace<Nz::NodeComponent>(sphereEntity);
auto& sphereNode = sphereEntity.emplace<Nz::NodeComponent>();
sphereNode.SetScale(0.1f);
sphereNode.SetInheritScale(false);
sphereNode.SetParentJoint(bobEntity, "RightHand");
auto& sphereBody = registry.emplace<Nz::RigidBody3DComponent>(sphereEntity, &physSytem.GetPhysWorld());
auto& sphereBody = sphereEntity.emplace<Nz::RigidBody3DComponent>(&physSytem.GetPhysWorld());
sphereBody.SetGeom(std::make_shared<Nz::SphereCollider3D>(0.1f));
auto& sphereGfx = registry.emplace<Nz::GraphicsComponent>(sphereEntity);
auto& sphereGfx = sphereEntity.emplace<Nz::GraphicsComponent>();
sphereGfx.AttachRenderable(sphereModel, 0xFFFFFFFF);
}
entt::entity smallBobEntity = registry.create();
auto& smallBobNode = registry.emplace<Nz::NodeComponent>(smallBobEntity);
entt::handle smallBobEntity = ecs.CreateEntity();
auto& smallBobNode = smallBobEntity.emplace<Nz::NodeComponent>();
smallBobNode.SetParentJoint(bobEntity, "LeftHand");
auto& smallBobGfx = registry.emplace<Nz::GraphicsComponent>(smallBobEntity);
auto& smallBobGfx = smallBobEntity.emplace<Nz::GraphicsComponent>();
smallBobGfx.AttachRenderable(bobModel, 0xFFFFFFFF);
registry.emplace<Nz::SharedSkeletonComponent>(smallBobEntity, skeleton);
smallBobEntity.emplace<Nz::SharedSkeletonComponent>(skeleton);
{
auto& lightNode = registry.emplace<Nz::NodeComponent>(lightEntity3);
auto& lightNode = lightEntity3.emplace<Nz::NodeComponent>();
//lightNode.SetPosition(Nz::Vector3f::Up() * 4.f);
lightNode.SetPosition(Nz::Vector3f::Down() * 7.5f + Nz::Vector3f::Backward() * 2.5f);
//lightNode.SetRotation(Nz::EulerAnglesf(-45.f, 180.f, 0.f));
lightNode.SetParentJoint(bobEntity, "Spine2");
auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity3);
auto& cameraLight = lightEntity3.emplace<Nz::LightComponent>();
auto& pointLight = cameraLight.AddLight<Nz::PointLight>(0xFFFFFFFF);
pointLight.UpdateColor(Nz::Color::Blue());
@ -294,17 +297,17 @@ int main()
sprite->UpdateRenderLayer(1);
sprite->Update(Nz::SimpleTextDrawer::Draw("Shadow-mapping !", 72), 0.002f);
entt::entity textEntity = registry.create();
entt::handle textEntity = ecs.CreateEntity();
{
auto& entityGfx = registry.emplace<Nz::GraphicsComponent>(textEntity);
auto& entityGfx = textEntity.emplace<Nz::GraphicsComponent>();
entityGfx.AttachRenderable(sprite, 1);
auto& entityNode = registry.emplace<Nz::NodeComponent>(textEntity);
auto& entityNode = textEntity.emplace<Nz::NodeComponent>();
entityNode.SetPosition(Nz::Vector3f::Up() * 0.5f + Nz::Vector3f::Backward() * 0.66f + Nz::Vector3f::Left() * 0.5f);
entityNode.SetRotation(Nz::EulerAnglesf(-45.f, 0.f, 0.f));
}
entt::entity planeEntity = registry.create();
entt::handle planeEntity = ecs.CreateEntity();
Nz::Boxf floorBox;
{
Nz::MeshParams meshPrimitiveParams;
@ -332,12 +335,12 @@ int main()
std::shared_ptr<Nz::Model> planeModel = std::make_shared<Nz::Model>(std::move(planeMeshGfx), planeMesh.GetAABB());
planeModel->SetMaterial(0, planeMat);
auto& planeGfx = registry.emplace<Nz::GraphicsComponent>(planeEntity);
auto& planeGfx = planeEntity.emplace<Nz::GraphicsComponent>();
planeGfx.AttachRenderable(planeModel, 0xFFFFFFFF);
registry.emplace<Nz::NodeComponent>(planeEntity);
planeEntity.emplace<Nz::NodeComponent>();
auto& planeBody = registry.emplace<Nz::RigidBody3DComponent>(planeEntity, &physSytem.GetPhysWorld());
auto& planeBody = planeEntity.emplace<Nz::RigidBody3DComponent>(&physSytem.GetPhysWorld());
planeBody.SetGeom(std::make_shared<Nz::BoxCollider3D>(Nz::Vector3f(planeSize.x, 0.5f, planeSize.y), Nz::Vector3f(0.f, -0.25f, 0.f)));
Nz::Mesh boxMesh;
@ -350,13 +353,11 @@ int main()
std::shared_ptr<Nz::Model> boxModel = std::make_shared<Nz::Model>(std::move(boxMeshGfx), boxMesh.GetAABB());
boxModel->SetMaterial(0, planeMat);
entt::entity boxEntity = registry.create();
registry.emplace<Nz::NodeComponent>(boxEntity).SetPosition(Nz::Vector3f(0.f, 0.25f, -0.5f));
registry.emplace<Nz::GraphicsComponent>(boxEntity).AttachRenderable(boxModel, 0xFFFFFFFF);
entt::handle boxEntity = ecs.CreateEntity();
boxEntity.emplace<Nz::NodeComponent>().SetPosition(Nz::Vector3f(0.f, 0.25f, -0.5f));
boxEntity.emplace<Nz::GraphicsComponent>().AttachRenderable(boxModel, 0xFFFFFFFF);
}
window.EnableEventPolling(true);
Nz::MillisecondClock fpsClock, updateClock;
float incr = 0.f;
unsigned int currentFrame = 0;
@ -365,49 +366,33 @@ int main()
Nz::UInt64 fps = 0;
bool paused = false;
while (window.IsOpen())
Nz::WindowEventHandler& eventHandler = mainWindow.GetEventHandler();
eventHandler.OnKeyPressed.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& event)
{
Nz::WindowEvent event;
while (window.PollEvent(&event))
{
switch (event.type)
{
case Nz::WindowEventType::Quit:
window.Close();
break;
if (event.virtualKey == Nz::Keyboard::VKey::P)
paused = !paused;
});
case Nz::WindowEventType::KeyPressed:
{
if (event.type == Nz::WindowEventType::KeyPressed && event.key.virtualKey == Nz::Keyboard::VKey::P)
paused = !paused;
eventHandler.OnMouseMoved.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
{
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.3f; // Sensibilité de la souris
break;
}
// On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris
camAngles.yaw = camAngles.yaw - event.deltaX * sensitivity;
camAngles.yaw.Normalize();
case Nz::WindowEventType::MouseMoved:
{
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.3f; // Sensibilité de la souris
// 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.deltaY * sensitivity, -89.f, 89.f);
// 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);*/
auto& playerRotNode = registry.get<Nz::NodeComponent>(playerCamera);
playerRotNode.SetRotation(camAngles);
break;
}
default:
break;
}
}
/*auto& playerRotNode = registry.get<Nz::NodeComponent>(playerRotation);
playerRotNode.SetRotation(camAngles);*/
auto& playerRotNode = playerCamera.get<Nz::NodeComponent>();
playerRotNode.SetRotation(camAngles);
});
app.AddUpdater([&](Nz::Time elapsedTime)
{
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
{
float updateTime = deltaTime->AsSeconds();
@ -433,7 +418,7 @@ int main()
float cameraSpeed = 2.f;
auto& cameraNode = registry.get<Nz::NodeComponent>(playerCamera);
auto& cameraNode = playerCamera.get<Nz::NodeComponent>();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space))
cameraNode.Move(Nz::Vector3f::Up() * cameraSpeed * updateTime, Nz::CoordSys::Global);
@ -517,9 +502,9 @@ int main()
playerShipBody.AddForce(Nz::Vector3f::Down() * 3.f * mass, Nz::CoordSys::Local);*/
}
Nz::DebugDrawer& debugDrawer = renderSystem.GetFramePipeline().GetDebugDrawer();
auto& lightNode = registry.get<Nz::NodeComponent>(lightEntity3);
auto& lightNode = lightEntity3.get<Nz::NodeComponent>();
//debugDrawer.DrawLine(lightNode.GetPosition(Nz::CoordSys::Global), lightNode.GetForward() * 10.f, Nz::Color::Blue());
Nz::Vector3f pos = lightNode.GetPosition(Nz::CoordSys::Global);
debugDrawer.DrawBox(Nz::Boxf(pos.x - 0.05f, pos.y - 0.05f, pos.z - 0.05f, 0.1f, 0.1f, 0.1f), Nz::Color::Blue());
@ -528,16 +513,16 @@ int main()
if (floorBox.Intersect(test, &intersection))
debugDrawer.DrawBox(intersection, Nz::Color::Green());*/
systemGraph.Update();
fps++;
if (fpsClock.RestartIfOver(Nz::Time::Second()))
{
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities");
mainWindow.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(ecs.GetRegistry().alive()) + " entities");
fps = 0;
}
}
});
return app.Run();
return EXIT_SUCCESS;
}

View File

@ -11,8 +11,8 @@
int main(int argc, char* argv[])
{
// This "example" has only one purpose: Giving an empty project for you to test whatever you want
// If you wish to have multiple test projects, you only have to copy/paste this directory and change the name in the build.lua
Nz::Modules<Nz::Audio, Nz::Core, Nz::Graphics, Nz::Network, Nz::Physics2D, Nz::Physics3D, Nz::Renderer, Nz::Utility> nazara;
// If you wish to have multiple test projects, you only have to copy/paste this directory and change the name in the xmake.lua
Nz::Application<Nz::Audio, Nz::Core, Nz::Graphics, Nz::Network, Nz::Physics2D, Nz::Physics3D, Nz::Renderer, Nz::Utility> app;
return EXIT_SUCCESS;
}

View File

@ -10,17 +10,14 @@
#include <Nazara/Renderer.hpp>
#include <Nazara/Utility.hpp>
#include <Nazara/Utility/Components.hpp>
#include <entt/entt.hpp>
#include <chrono>
#include <iostream>
#include <thread>
int main()
{
Nz::Application<Nz::Graphics> app;
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), "Hello world");
Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), "Tut01 - Hello world");
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();

View File

@ -1,30 +1,34 @@
// Sources pour https://github.com/NazaraEngine/NazaraEngine/wiki/(FR)-Tutoriel:-%5B02%5D-Gestion-des-événements
#include <Nazara/Core/Application.hpp>
#include <Nazara/Core/AppEntitySystemComponent.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Graphics/Components.hpp>
#include <Nazara/Graphics/Systems.hpp>
#include <Nazara/Platform/AppWindowingComponent.hpp>
#include <Nazara/Renderer.hpp>
#include <Nazara/Utility.hpp>
#include <Nazara/Utility/Components.hpp>
#include <entt/entt.hpp>
#include <chrono>
#include <iostream>
#include <thread>
int main()
{
Nz::Modules<Nz::Graphics> nazara;
Nz::Application<Nz::Graphics> app;
entt::registry 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");
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), "Tut02 - Hello world");
entt::entity cameraEntity = registry.create();
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();
Nz::RenderSystem& renderSystem = ecs.AddSystem<Nz::RenderSystem>();
auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow);
entt::handle cameraEntity = ecs.CreateEntity();
{
registry.emplace<Nz::NodeComponent>(cameraEntity);
auto& cameraComponent = registry.emplace<Nz::CameraComponent>(cameraEntity, mainWindow.GetRenderTarget(), Nz::ProjectionType::Orthographic);
cameraEntity.emplace<Nz::NodeComponent>();
auto& cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(&windowSwapchain.GetSwapchain(), Nz::ProjectionType::Orthographic);
cameraComponent.UpdateClearColor(Nz::Color(0.46f, 0.48f, 0.84f, 1.f));
}
@ -36,10 +40,10 @@ int main()
std::shared_ptr<Nz::TextSprite> textSprite = std::make_shared<Nz::TextSprite>();
textSprite->Update(textDrawer);
entt::entity textEntity = registry.create();
entt::handle textEntity = ecs.CreateEntity();
{
auto& nodeComponent = registry.emplace<Nz::NodeComponent>(textEntity);
auto& gfxComponent = registry.emplace<Nz::GraphicsComponent>(textEntity);
auto& nodeComponent = textEntity.emplace<Nz::NodeComponent>();
auto& gfxComponent = textEntity.emplace<Nz::GraphicsComponent>();
gfxComponent.AttachRenderable(textSprite, 0xFFFFFFFF);
Nz::Boxf textBox = textSprite->GetAABB();
@ -47,8 +51,8 @@ int main()
nodeComponent.SetPosition(windowSize.x / 2 - textBox.width / 2, windowSize.y / 2 - textBox.height / 2);
}
Nz::EventHandler& eventHandler = mainWindow.GetEventHandler();
eventHandler.OnKeyPressed.Connect([&](const Nz::EventHandler*, const Nz::WindowEvent::KeyEvent& e)
Nz::WindowEventHandler& eventHandler = mainWindow.GetEventHandler();
eventHandler.OnKeyPressed.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& e)
{
textDrawer.SetText("You pressed " + Nz::Keyboard::GetKeyName(e.virtualKey));
textSprite->Update(textDrawer);
@ -56,7 +60,7 @@ int main()
Nz::Boxf textBox = textSprite->GetAABB();
Nz::Vector2ui windowSize = mainWindow.GetSize();
auto& nodeComponent = registry.get<Nz::NodeComponent>(textEntity);
auto& nodeComponent = textEntity.get<Nz::NodeComponent>();
nodeComponent.SetPosition(windowSize.x / 2 - textBox.width / 2, windowSize.y / 2 - textBox.height / 2);
// Profitons-en aussi pour nous donner un moyen de quitter le programme
@ -64,11 +68,5 @@ int main()
mainWindow.Close(); // Cette ligne casse la boucle de la fenêtre
});
while (mainWindow.IsOpen())
{
mainWindow.ProcessEvents();
systemGraph.Update();
}
return EXIT_SUCCESS;
return app.Run();
}

View File

@ -1,4 +1,5 @@
#include <Nazara/Core.hpp>
#include <Nazara/Core/AppEntitySystemComponent.hpp>
#include <Nazara/Core/Systems.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Graphics.hpp>
@ -37,18 +38,17 @@ int main()
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Nz::Modules<Nz::Graphics, Nz::Physics3D, Nz::Widgets> nazara(rendererConfig);
Nz::Application<Nz::Graphics, Nz::Widgets> app(rendererConfig);
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
auto& windowing = app.AddComponent<Nz::AppWindowingComponent>();
Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1920, 1080), "Widget demo");
std::string windowTitle = "Widget Test";
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();
entt::registry registry;
Nz::SystemGraph systemGraph(registry);
Nz::RenderSystem& renderSystem = systemGraph.AddSystem<Nz::RenderSystem>();
Nz::RenderWindow& mainWindow = renderSystem.CreateWindow(device, Nz::VideoMode(1920, 1080), windowTitle);
Nz::RenderSystem& renderSystem = ecs.AddSystem<Nz::RenderSystem>();
auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow);
Nz::Canvas canvas2D(registry, mainWindow.GetEventHandler(), mainWindow.GetCursorController().CreateHandle(), 0xFFFFFFFF);
Nz::Canvas canvas2D(ecs.GetRegistry(), mainWindow.GetEventHandler(), mainWindow.GetCursorController().CreateHandle(), 0xFFFFFFFF);
canvas2D.Resize(Nz::Vector2f(mainWindow.GetSize()));
Nz::LabelWidget* labelWidget = canvas2D.Add<Nz::LabelWidget>();
@ -72,7 +72,7 @@ int main()
samplerInfo.anisotropyLevel = 8;
Nz::TextureParams texParams;
texParams.renderDevice = device;
texParams.renderDevice = Nz::Graphics::Instance()->GetRenderDevice();
texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB;
std::shared_ptr<Nz::MaterialInstance> materialInstance = material->Instantiate();
@ -118,44 +118,13 @@ int main()
textAreaWidget2->SetBackgroundColor(Nz::Color::White());
textAreaWidget2->SetTextColor(Nz::Color::Black());*/
entt::entity viewer2D = registry.create();
entt::handle viewer2D = ecs.CreateEntity();
{
registry.emplace<Nz::NodeComponent>(viewer2D);
auto& cameraComponent = registry.emplace<Nz::CameraComponent>(viewer2D, mainWindow.GetRenderTarget(), Nz::ProjectionType::Orthographic);
cameraComponent.UpdateClearColor(Nz::Color(0.678f, 0.847f, 0.9f, 1.f));
viewer2D.emplace<Nz::NodeComponent>();
auto& cameraComponent = viewer2D.emplace<Nz::CameraComponent>(&windowSwapchain.GetSwapchain(), Nz::ProjectionType::Orthographic);
cameraComponent.UpdateClearColor(Nz::Color(0.46f, 0.48f, 0.84f, 1.f));
}
mainWindow.EnableEventPolling(true);
Nz::MillisecondClock fpsClock;
unsigned int fps = 0;
while (mainWindow.IsOpen())
{
Nz::WindowEvent event;
while (mainWindow.PollEvent(&event))
{
switch (event.type)
{
case Nz::WindowEventType::Quit:
mainWindow.Close();
break;
default:
break;
}
}
systemGraph.Update();
fps++;
if (fpsClock.RestartIfOver(Nz::Time::Second()))
{
mainWindow.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities");
fps = 0;
}
}
return EXIT_SUCCESS;
return app.Run();
}

View File

@ -6,10 +6,7 @@
#include <NZSL/LangWriter.hpp>
#include <NZSL/Parser.hpp>
#include <Nazara/Utility.hpp>
#include <array>
#include <chrono>
#include <iostream>
#include <thread>
NAZARA_REQUEST_DEDICATED_GPU()
@ -123,13 +120,13 @@ int main()
});
std::string windowTitle = "Compute test";
Nz::RenderWindow window;
if (!window.Create(device, Nz::VideoMode(1280, 720, 32), windowTitle))
Nz::Window window;
if (!window.Create(Nz::VideoMode(1280, 720), windowTitle))
{
std::cout << "Failed to create Window" << std::endl;
std::abort();
}
Nz::WindowSwapchain windowSwapchain(device, window);
Nz::Vector2ui windowSize = window.GetSize();
constexpr float textureSize = 512.f;
@ -146,7 +143,7 @@ int main()
{
window.ProcessEvents();
Nz::RenderFrame frame = window.AcquireFrame();
Nz::RenderFrame frame = windowSwapchain.AcquireFrame();
if (!frame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
@ -168,7 +165,7 @@ int main()
}
}
const Nz::RenderTarget* windowRT = window.GetRenderTarget();
const Nz::RenderTarget* windowRT = &windowSwapchain.GetSwapchain();
frame.Execute([&](Nz::CommandBufferBuilder& builder)
{
builder.BeginDebugRegion("Compute part", Nz::Color::Blue());

View File

@ -25,8 +25,6 @@ int main()
Nz::Modules<Nz::Graphics> nazara(rendererConfig);
Nz::RenderWindow window;
Nz::MeshParams meshParams;
meshParams.center = true;
meshParams.vertexRotation = Nz::EulerAnglesf(0.f, -90.f, 0.f);
@ -36,11 +34,13 @@ int main()
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))
Nz::Window window;
if (!window.Create(Nz::VideoMode(1280, 720), windowTitle))
{
std::cout << "Failed to create Window" << std::endl;
return __LINE__;
std::abort();
}
Nz::WindowSwapchain windowSwapchain(device, window);
std::shared_ptr<Nz::Mesh> spaceshipMesh = Nz::Mesh::LoadFromFile(resourceDir / "Spaceship/spaceship.obj", meshParams);
if (!spaceshipMesh)
@ -80,7 +80,7 @@ int main()
Nz::Vector2ui windowSize = window.GetSize();
Nz::Camera camera(window.GetRenderTarget());
Nz::Camera camera(&windowSwapchain.GetSwapchain());
camera.UpdateClearColor(Nz::Color::Gray());
Nz::ViewerInstance& viewerInstance = camera.GetViewerInstance();
@ -114,72 +114,71 @@ int main()
Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
Nz::Quaternionf camQuat(camAngles);
window.EnableEventPolling(true);
Nz::MillisecondClock updateClock;
Nz::MillisecondClock fpsClock;
unsigned int fps = 0;
Nz::Mouse::SetRelativeMouseMode(true);
window.GetEventHandler().OnEvent.Connect([&](const Nz::WindowEventHandler*, const Nz::WindowEvent& event)
{
switch (event.type)
{
case Nz::WindowEventType::Quit:
window.Close();
break;
case Nz::WindowEventType::KeyPressed:
if (event.key.virtualKey == Nz::Keyboard::VKey::A)
{
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, materialInstance);
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::B)
{
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, materialInstance2);
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::Space)
{
modelInstance->UpdateWorldMatrix(Nz::Matrix4f::Translate(viewerPos));
}
break;
case Nz::WindowEventType::MouseMoved: // La souris a bougé
{
// 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);
camQuat = camAngles;
light->UpdateRotation(camQuat);
break;
}
case Nz::WindowEventType::Resized:
{
Nz::Vector2ui newWindowSize = window.GetSize();
viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f));
viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize));
break;
}
default:
break;
}
});
while (window.IsOpen())
{
Nz::WindowEvent event;
while (window.PollEvent(&event))
{
switch (event.type)
{
case Nz::WindowEventType::Quit:
window.Close();
break;
case Nz::WindowEventType::KeyPressed:
if (event.key.virtualKey == Nz::Keyboard::VKey::A)
{
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, materialInstance);
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::B)
{
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, materialInstance2);
}
else if (event.key.virtualKey == Nz::Keyboard::VKey::Space)
{
modelInstance->UpdateWorldMatrix(Nz::Matrix4f::Translate(viewerPos));
}
break;
case Nz::WindowEventType::MouseMoved: // La souris a bougé
{
// 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);
camQuat = camAngles;
light->UpdateRotation(camQuat);
break;
}
case Nz::WindowEventType::Resized:
{
Nz::Vector2ui newWindowSize = window.GetSize();
viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(newWindowSize.x) / newWindowSize.y, 0.1f, 1000.f));
viewerInstance.UpdateTargetSize(Nz::Vector2f(newWindowSize));
break;
}
default:
break;
}
}
Nz::Window::ProcessEvents();
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
{
@ -211,7 +210,7 @@ int main()
light->UpdatePosition(viewerPos);
}
Nz::RenderFrame frame = window.AcquireFrame();
Nz::RenderFrame frame = windowSwapchain.AcquireFrame();
if (!frame)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));