moving camera

This commit is contained in:
2024-06-12 17:18:10 +02:00
parent 75cd769681
commit 5ec6ff42ab

View File

@@ -7,24 +7,33 @@
#include <Nazara/Physics3D.hpp> #include <Nazara/Physics3D.hpp>
#include <random> #include <random>
static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw) {
return {
-std::sin(yaw),
0,
-std::cos(yaw),
};
}
static void CreateLight(Nz::EnttWorld &world) static void CreateLight(Nz::EnttWorld &world)
{ {
entt::handle lightEntity = world.CreateEntity(); entt::handle lightEntity = world.CreateEntity();
{ {
auto &lightNode = lightEntity.emplace<Nz::NodeComponent>(); auto &lightNode = lightEntity.emplace<Nz::NodeComponent>();
lightNode.SetPosition({0, 1, 0}); lightNode.SetPosition({0, 5, 0});
auto &entityLight = lightEntity.emplace<Nz::LightComponent>(); auto &entityLight = lightEntity.emplace<Nz::LightComponent>();
auto &spotLight = entityLight.AddLight<Nz::PointLight>(1); auto &spotLight = entityLight.AddLight<Nz::PointLight>(1);
spotLight.EnableShadowCasting(true); spotLight.EnableShadowCasting(true);
spotLight.UpdateShadowMapSize(1024); spotLight.UpdateShadowMapSize(1024);
spotLight.UpdateRadius(10.0f);
spotLight.UpdateDiffuseFactor(0.5f);
} }
} }
static void CreateBoxes(Nz::EnttWorld& world) static void CreateBoxes(Nz::EnttWorld &world)
{ {
constexpr float BoxDims = 16.f; constexpr float BoxDims = 1.f;
std::mt19937 rd(42); std::mt19937 rd(42);
std::uniform_real_distribution<float> colorDis(0.f, 360.f); std::uniform_real_distribution<float> colorDis(0.f, 360.f);
@@ -55,7 +64,7 @@ static void CreateBoxes(Nz::EnttWorld& world)
boxEntity.emplace<Nz::GraphicsComponent>(std::move(sphereModel)); boxEntity.emplace<Nz::GraphicsComponent>(std::move(sphereModel));
auto &ballNode = boxEntity.emplace<Nz::NodeComponent>(); auto &ballNode = boxEntity.emplace<Nz::NodeComponent>();
ballNode.SetPosition({xRandom(rd), yRandom(rd), zRandom(rd)}); ballNode.SetPosition({xRandom(rd), yRandom(rd) + 20.0f, zRandom(rd)});
ballNode.SetScale({width, height, depth}); ballNode.SetScale({width, height, depth});
std::shared_ptr<Nz::BoxCollider3D> boxCollider = std::make_shared<Nz::BoxCollider3D>(Nz::Vector3f(width, height, depth)); std::shared_ptr<Nz::BoxCollider3D> boxCollider = std::make_shared<Nz::BoxCollider3D>(Nz::Vector3f(width, height, depth));
@@ -115,7 +124,6 @@ static void CreateModel(Nz::EnttWorld &world)
for (std::size_t i = 0; i < deambModel->GetSubMeshCount(); ++i) for (std::size_t i = 0; i < deambModel->GetSubMeshCount(); ++i)
deambModel->SetMaterial(i, material); deambModel->SetMaterial(i, material);
Nz::VertexMapper vertexMapper(*deambuMesh->GetSubMesh(0)); Nz::VertexMapper vertexMapper(*deambuMesh->GetSubMesh(0));
Nz::SparsePtr<Nz::Vector3f> vertices = vertexMapper.GetComponentPtr<Nz::Vector3f>(Nz::VertexComponent::Position); Nz::SparsePtr<Nz::Vector3f> vertices = vertexMapper.GetComponentPtr<Nz::Vector3f>(Nz::VertexComponent::Position);
@@ -129,7 +137,7 @@ static void CreateModel(Nz::EnttWorld &world)
static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window) static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Application<Nz::Graphics, Nz::Physics3D> &app)
{ {
Nz::RenderSystem &renderSystem = world.AddSystem<Nz::RenderSystem>(); Nz::RenderSystem &renderSystem = world.AddSystem<Nz::RenderSystem>();
@@ -143,10 +151,18 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window)
entt::handle cameraEntity = world.CreateEntity(); entt::handle cameraEntity = world.CreateEntity();
auto &cameraNode = cameraEntity.emplace<Nz::NodeComponent>(); auto &cameraNode = cameraEntity.emplace<Nz::NodeComponent>();
cameraNode.SetPosition({0, 5, 0}); cameraNode.SetPosition({0, 1, 0});
auto &cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(std::make_shared<Nz::RenderWindow>(windowSwapchain), Nz::ProjectionType::Perspective); auto &cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(std::make_shared<Nz::RenderWindow>(windowSwapchain), Nz::ProjectionType::Perspective);
cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f)); cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f));
cameraComponent.UpdateZNear(0.1f);
cameraComponent.UpdateZFar(100.0f);
entt::handle playerEntity = world.CreateEntity();
auto &playerNode = playerEntity.emplace<Nz::NodeComponent>();
playerNode.SetPosition({0, 5, 0});
cameraNode.SetParent(playerEntity);
window.GetEventHandler().OnMouseMoved.Connect([&](const Nz::WindowEventHandler * /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent &event) window.GetEventHandler().OnMouseMoved.Connect([&](const Nz::WindowEventHandler * /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent &event)
{ {
@@ -157,8 +173,49 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window)
camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f); camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f);
cameraNode.SetRotation(camAngles); }); camAngles.roll = 0.0f;
cameraNode.SetRotation(camAngles);
});
std::shared_ptr<Nz::BoxCollider3D> boxCollider = std::make_shared<Nz::BoxCollider3D>(Nz::Vector3f(1, 2, 1));
static const int playerMass = 100000;
Nz::RigidBody3D::DynamicSettings settings;
settings.geom = boxCollider;
settings.mass = playerMass;
auto &playerBody = playerEntity.emplace<Nz::RigidBody3DComponent>(settings);
app.AddUpdaterFunc([&](){
static Nz::MillisecondClock updateClock;
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
{
camAngles.roll = 0;
cameraNode.SetRotation(camAngles);
Nz::Vector3f front = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians());
Nz::Vector3f left = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians() + 3.1415926535/2.0);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z))
playerBody.AddForce(front * 10.f * playerMass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S))
playerBody.AddForce(-front * 10.f * playerMass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q))
playerBody.AddForce(left * 10.f * playerMass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D))
playerBody.AddForce(-left * 10.f * playerMass, Nz::CoordSys::Local);
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space))
playerBody.AddForce(Nz::Vector3f::Up() * 15.f * playerMass, Nz::CoordSys::Local);
}
});
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@@ -174,12 +231,12 @@ int main(int argc, char **argv)
auto &world = ecs.AddWorld<Nz::EnttWorld>(); auto &world = ecs.AddWorld<Nz::EnttWorld>();
auto& physSystem = world.AddSystem<Nz::Physics3DSystem>(); auto &physSystem = world.AddSystem<Nz::Physics3DSystem>();
physSystem.GetPhysWorld().SetMaxStepCount(1); physSystem.GetPhysWorld().SetMaxStepCount(1);
physSystem.GetPhysWorld().SetStepSize(Nz::Time::TickDuration(50)); physSystem.GetPhysWorld().SetStepSize(Nz::Time::TickDuration(50));
physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f); physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f);
CreateCamera(world, window); CreateCamera(world, window, app);
CreateBoxes(world); CreateBoxes(world);
CreateModel(world); CreateModel(world);
CreateLight(world); CreateLight(world);
@@ -189,15 +246,15 @@ int main(int argc, char **argv)
Nz::MillisecondClock fpsClock; Nz::MillisecondClock fpsClock;
unsigned int fps = 0; unsigned int fps = 0;
app.AddUpdaterFunc([&](){ app.AddUpdaterFunc([&]()
{
fps++; fps++;
if (fpsClock.RestartIfOver(Nz::Time::Second())) if (fpsClock.RestartIfOver(Nz::Time::Second()))
{ {
window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(world.GetAliveEntityCount()) + " entities"); window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(world.GetAliveEntityCount()) + " entities");
fps = 0; fps = 0;
} } });
});
return app.Run(); return app.Run();
} }