#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include NAZARA_REQUEST_DEDICATED_GPU() int main() { std::filesystem::path resourceDir = "resources"; if (!std::filesystem::is_directory(resourceDir) && std::filesystem::is_directory(".." / resourceDir)) resourceDir = ".." / resourceDir; Nz::Renderer::Config rendererConfig; std::cout << "Run using Vulkan? (y/n)" << std::endl; if (std::getchar() != 'n') rendererConfig.preferredAPI = Nz::RenderAPI::Vulkan; else rendererConfig.preferredAPI = Nz::RenderAPI::OpenGL; std::cin.ignore(std::numeric_limits::max(), '\n'); Nz::Modules nazara(rendererConfig); Nz::RenderWindow window; std::shared_ptr 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.storage = Nz::DataStorage::Software; meshParams.matrix = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, 90.f, 0.f)) * Nz::Matrix4f::Scale(Nz::Vector3f(0.002f)); meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_UV); std::shared_ptr spaceshipMesh = Nz::Mesh::LoadFromFile(resourceDir / "Spaceship/spaceship.obj", meshParams); if (!spaceshipMesh) { NazaraError("Failed to load model"); return __LINE__; } const Nz::Boxf& spaceshipAABB = spaceshipMesh->GetAABB(); std::shared_ptr gfxMesh = std::make_shared(*spaceshipMesh); // Texture std::shared_ptr material = std::make_shared(); std::shared_ptr depthPass = std::make_shared(Nz::DepthMaterial::GetSettings()); depthPass->EnableDepthBuffer(true); depthPass->EnableDepthClamp(true); depthPass->EnableFaceCulling(true); std::shared_ptr materialPass = std::make_shared(Nz::BasicMaterial::GetSettings()); materialPass->EnableDepthBuffer(true); materialPass->EnableDepthClamp(true); materialPass->EnableFaceCulling(true); //material->AddPass("DepthPass", depthPass); material->AddPass("ForwardPass", materialPass); Nz::TextureSamplerInfo samplerInfo; samplerInfo.anisotropyLevel = 8; Nz::TextureParams texParams; texParams.renderDevice = device; texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB; Nz::BasicMaterial basicMat(*materialPass); basicMat.EnableAlphaTest(false); basicMat.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); basicMat.SetDiffuseSampler(samplerInfo); Nz::DepthMaterial basicMatDepth(*depthPass); basicMatDepth.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); std::shared_ptr model = std::make_shared(std::move(gfxMesh), spaceshipAABB); for (std::size_t i = 0; i < model->GetSubMeshCount(); ++i) model->SetMaterial(i, material); std::shared_ptr spriteMaterial = std::make_shared(); /*std::shared_ptr spriteDepthPass = std::make_shared(Nz::DepthMaterial::GetSettings()); spriteDepthPass->EnableDepthBuffer(true); spriteDepthPass->EnableDepthClamp(true); //spriteDepthPass->EnableFaceCulling(true);*/ std::shared_ptr spriteMaterialPass = std::make_shared(Nz::BasicMaterial::GetSettings()); spriteMaterialPass->EnableDepthBuffer(true); spriteMaterialPass->EnableDepthWrite(false); spriteMaterialPass->EnableDepthClamp(true); //spriteMaterialPass->EnableFaceCulling(true); spriteMaterialPass->EnableFlag(Nz::MaterialPassFlag::Transparent); spriteMaterialPass->EnableBlending(true); spriteMaterialPass->SetBlendEquation(Nz::BlendEquation::Add, Nz::BlendEquation::Add); spriteMaterialPass->SetBlendFunc(Nz::BlendFunc::SrcAlpha, Nz::BlendFunc::InvSrcAlpha, Nz::BlendFunc::One, Nz::BlendFunc::Zero); //Nz::BasicMaterial basicSpriteMat(*spriteMaterialPass); //basicSpriteMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "dev_grey.png", texParams)); //spriteMaterial->AddPass("DepthPass", spriteDepthPass); spriteMaterial->AddPass("ForwardPass", spriteMaterialPass); std::shared_ptr sprite = std::make_shared(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); //for (std::size_t i = 16; i < 200; i += 3) // sprite->Update(Nz::SimpleTextDrawer::Draw("Voix ambiguë d'un cœur qui, au zéphyr, préfère les jattes de kiwis", i), 0.01f); Nz::Vector2ui windowSize = window.GetSize(); Nz::VertexMapper vertexMapper(*spaceshipMesh->GetSubMesh(0), Nz::BufferAccess::ReadOnly); Nz::SparsePtr vertices = vertexMapper.GetComponentPtr(Nz::VertexComponent::Position); entt::registry registry; Nz::Physics3DSystem physSytem(registry); Nz::RenderSystem renderSystem(registry); Nz::Canvas canvas2D(registry, window.GetEventHandler(), window.GetCursorController().CreateHandle(), 2); canvas2D.Resize(Nz::Vector2f(window.GetSize())); Nz::LabelWidget* labelWidget = canvas2D.Add(); labelWidget->SetPosition(0.f, 300.f, 0.f); labelWidget->EnableBackground(true); labelWidget->UpdateText(Nz::SimpleTextDrawer::Draw("Bonjour Paris !", 72)); Nz::ButtonWidget* buttonWidget = canvas2D.Add(); buttonWidget->SetPosition(200.f, 400.f); buttonWidget->UpdateText(Nz::SimpleTextDrawer::Draw("Press me senpai", 72)); buttonWidget->Resize(buttonWidget->GetPreferredSize()); buttonWidget->OnButtonTrigger.Connect([](const Nz::ButtonWidget*) { std::cout << "Coucou !" << std::endl; }); entt::entity viewer2D = registry.create(); { registry.emplace(viewer2D); auto& cameraComponent = registry.emplace(viewer2D, window.GetRenderTarget(), Nz::ProjectionType::Orthographic); cameraComponent.UpdateRenderOrder(1); cameraComponent.UpdateRenderMask(2); } entt::entity viewer = registry.create(); { registry.emplace(viewer); auto& cameraComponent = registry.emplace(viewer, window.GetRenderTarget()); cameraComponent.UpdateRenderMask(1); } auto shipCollider = std::make_shared(vertices, vertexMapper.GetVertexCount(), 0.01f); std::shared_ptr colliderMat = std::make_shared(); std::shared_ptr colliderMatPass = std::make_shared(Nz::BasicMaterial::GetSettings()); colliderMatPass->EnableDepthBuffer(true); colliderMatPass->SetPrimitiveMode(Nz::PrimitiveMode::LineList); colliderMat->AddPass("ForwardPass", colliderMatPass); Nz::BasicMaterial colliderBasicMat(*colliderMatPass); colliderBasicMat.SetDiffuseColor(Nz::Color::Green); std::shared_ptr colliderModel; { std::shared_ptr colliderMesh = Nz::Mesh::Build(shipCollider->GenerateMesh()); std::shared_ptr colliderGraphicalMesh = std::make_shared(*colliderMesh); colliderModel = std::make_shared(colliderGraphicalMesh, spaceshipAABB); for (std::size_t i = 0; i < colliderModel->GetSubMeshCount(); ++i) colliderModel->SetMaterial(i, colliderMat); } entt::entity textEntity = registry.create(); { auto& entityGfx = registry.emplace(textEntity); entityGfx.AttachRenderable(sprite); auto& entityNode = registry.emplace(textEntity); entityNode.SetPosition(0.f, 5.f, 0.f); } entt::entity playerEntity = registry.create(); entt::entity headingEntity = registry.create(); { auto& entityGfx = registry.emplace(playerEntity); entityGfx.AttachRenderable(model, 1); auto& entityNode = registry.emplace(playerEntity); entityNode.SetPosition(Nz::Vector3f(12.5f, 0.f, 25.f)); auto& entityPhys = registry.emplace(playerEntity, physSytem.CreateRigidBody(shipCollider)); entityPhys.SetMass(50.f); entityPhys.SetAngularDamping(Nz::Vector3f::Zero()); auto& headingNode = registry.emplace(headingEntity); headingNode.SetInheritRotation(false); headingNode.SetParent(registry, playerEntity); } registry.get(viewer).SetParent(registry, headingEntity); registry.get(viewer).SetPosition(Nz::Vector3f::Backward() * 2.5f + Nz::Vector3f::Up() * 1.f); for (std::size_t x = 0; x < 3; ++x) { for (std::size_t y = 0; y < 3; ++y) { for (std::size_t z = 0; z < 3; ++z) { entt::entity entity = registry.create(); auto& entityGfx = registry.emplace(entity); entityGfx.AttachRenderable(model, 1); //entityGfx.AttachRenderable(sprite); auto& entityNode = registry.emplace(entity); entityNode.SetPosition(Nz::Vector3f(x * 2.f, y * 1.5f, z * 2.f)); auto& entityPhys = registry.emplace(entity, physSytem.CreateRigidBody(shipCollider)); entityPhys.SetMass(1.f); entityPhys.SetAngularDamping(Nz::Vector3f::Zero()); entityPhys.SetLinearDamping(0.f); } } } Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); Nz::Quaternionf camQuat(camAngles); window.EnableEventPolling(true); Nz::Clock updateClock; Nz::Clock secondClock; unsigned int fps = 0; //Nz::Mouse::SetRelativeMouseMode(true); float elapsedTime = 0.f; Nz::UInt64 time = Nz::GetElapsedMicroseconds(); Nz::PidController headingController(0.5f, 0.f, 0.05f); Nz::PidController upController(1.f, 0.f, 0.1f); bool showColliders = false; while (window.IsOpen()) { Nz::UInt64 now = Nz::GetElapsedMicroseconds(); elapsedTime = (now - time) / 1'000'000.f; time = now; 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) { basicMat.EnableAlphaTest(!basicMat.IsAlphaTestEnabled()); basicMatDepth.EnableAlphaTest(!basicMatDepth.IsAlphaTestEnabled()); } else if (event.key.virtualKey == Nz::Keyboard::VKey::B) { showColliders = !showColliders; if (showColliders) { auto view = registry.view(); for (auto [entity, gfxComponent] : view.each()) gfxComponent.AttachRenderable(colliderModel); } else { auto view = registry.view(); 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(entity); entityGfx.AttachRenderable(model); if (showColliders) entityGfx.AttachRenderable(colliderModel); registry.emplace(entity); auto& entityPhys = registry.emplace(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(headingEntity).SetRotation(camQuat); break; } default: break; } } if (updateClock.GetMilliseconds() > 1000 / 60) { float updateTime = updateClock.Restart() / 1'000'000.f; physSytem.Update(registry, 1000.f / 60.f); auto spaceshipView = registry.view(); for (auto&& [entity, node, _] : spaceshipView.each()) { if (entity == playerEntity) continue; Nz::Vector3f spaceshipPos = node.GetPosition(Nz::CoordSys::Global); if (spaceshipPos.GetSquaredLength() > Nz::IntegralPow(200.f, 2)) registry.destroy(entity); } Nz::RigidBody3DComponent& playerShipBody = registry.get(playerEntity); Nz::Quaternionf currentRotation = playerShipBody.GetRotation(); Nz::Vector3f desiredHeading = registry.get(headingEntity).GetForward(); Nz::Vector3f currentHeading = currentRotation * Nz::Vector3f::Forward(); Nz::Vector3f headingError = currentHeading.CrossProduct(desiredHeading); Nz::Vector3f desiredUp = registry.get(headingEntity).GetUp(); Nz::Vector3f currentUp = currentRotation * Nz::Vector3f::Up(); Nz::Vector3f upError = currentUp.CrossProduct(desiredUp); playerShipBody.AddTorque(headingController.Update(headingError, elapsedTime) * 10.f); playerShipBody.AddTorque(upController.Update(upError, elapsedTime) * 10.f); float mass = playerShipBody.GetMass(); if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)) playerShipBody.AddForce(Nz::Vector3f::Forward() * 2.5f * mass, Nz::CoordSys::Local); if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Down) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S)) playerShipBody.AddForce(Nz::Vector3f::Backward() * 2.5f * mass, Nz::CoordSys::Local); if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Left) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q)) playerShipBody.AddForce(Nz::Vector3f::Left() * 2.5f * mass, Nz::CoordSys::Local); if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Right) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D)) playerShipBody.AddForce(Nz::Vector3f::Right() * 2.5f * mass, Nz::CoordSys::Local); if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::LShift) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::RShift)) playerShipBody.AddForce(Nz::Vector3f::Up() * 3.f * mass, Nz::CoordSys::Local); if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::LControl) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::RControl)) playerShipBody.AddForce(Nz::Vector3f::Down() * 3.f * mass, Nz::CoordSys::Local); } Nz::RenderFrame frame = window.AcquireFrame(); if (!frame) continue; renderSystem.Render(registry, frame); frame.Present(); fps++; if (secondClock.GetMilliseconds() >= 1000) { window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(registry.alive()) + " entities"); fps = 0; secondClock.Restart(); } } registry.clear(); return EXIT_SUCCESS; }