diff --git a/examples/PhysicsDemo/main.cpp b/examples/PhysicsDemo/main.cpp index 0a7bf7885..b7a320b55 100644 --- a/examples/PhysicsDemo/main.cpp +++ b/examples/PhysicsDemo/main.cpp @@ -104,14 +104,11 @@ int main() std::shared_ptr colliderMat = Nz::Graphics::Instance()->GetDefaultMaterials().basicMaterial->Instantiate(); colliderMat->SetValueProperty("BaseColor", Nz::Color::Green()); - for (std::string_view passName : { "DepthPass", "ForwardPass" }) + colliderMat->UpdatePassesStates([](Nz::RenderStates& states) { - colliderMat->UpdatePassStates(passName, [](Nz::RenderStates& states) - { - states.primitiveMode = Nz::PrimitiveMode::LineList; - return true; - }); - } + states.primitiveMode = Nz::PrimitiveMode::LineList; + return true; + }); std::shared_ptr colliderModel; { diff --git a/examples/Showcase/main.cpp b/examples/Showcase/main.cpp index 8bf4ff3db..e4512e4b7 100644 --- a/examples/Showcase/main.cpp +++ b/examples/Showcase/main.cpp @@ -18,10 +18,6 @@ NAZARA_REQUEST_DEDICATED_GPU() int main() { - std::filesystem::path resourceDir = "assets/examples"; - if (!std::filesystem::is_directory(resourceDir) && std::filesystem::is_directory("../.." / resourceDir)) - resourceDir = "../.." / resourceDir; - Nz::Application app; Nz::PluginLoader loader; @@ -43,10 +39,16 @@ int main() Nz::Window& mainWindow = windowing.CreateWindow(Nz::VideoMode(1280, 720), windowTitle); auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow); - //physSytem.GetPhysWorld().SetGravity({ 0.f, -9.81f, 0.f }); + auto& fs = app.AddComponent(); + { + std::filesystem::path resourceDir = "assets/examples"; + if (!std::filesystem::is_directory(resourceDir) && std::filesystem::is_directory("../.." / resourceDir)) + resourceDir = "../.." / resourceDir; - Nz::TextureParams texParams; - texParams.renderDevice = device; + fs.Mount("assets", resourceDir); + } + + physSytem.GetPhysWorld().SetGravity({ 0.f, -9.81f, 0.f }); entt::handle playerEntity = world.CreateEntity(); entt::handle playerRotation = world.CreateEntity(); @@ -55,9 +57,28 @@ int main() auto& playerNode = playerEntity.emplace(); playerNode.SetPosition(0.f, 1.8f, 1.f); - auto& playerBody = playerEntity.emplace(&physSytem.GetPhysWorld()); + auto playerCollider = std::make_shared(Nz::Vector3f(0.2f, 1.8f, 0.2f)); + + auto& playerBody = playerEntity.emplace(physSytem.CreateRigidBody(playerCollider)); playerBody.SetMass(42.f); - playerBody.SetGeom(std::make_shared(Nz::Vector3f::Unit())); + + std::shared_ptr colliderMesh = Nz::Mesh::Build(playerCollider->GenerateMesh()); + std::shared_ptr colliderGraphicalMesh = Nz::GraphicalMesh::BuildFromMesh(*colliderMesh); + + std::shared_ptr colliderMat = Nz::Graphics::Instance()->GetDefaultMaterials().basicMaterial->Instantiate(); + colliderMat->SetValueProperty("BaseColor", Nz::Color::Green()); + colliderMat->UpdatePassesStates([](Nz::RenderStates& states) + { + states.primitiveMode = Nz::PrimitiveMode::LineList; + return true; + }); + + auto colliderModel = std::make_shared(colliderGraphicalMesh); + for (std::size_t i = 0; i < colliderModel->GetSubMeshCount(); ++i) + colliderModel->SetMaterial(i, colliderMat); + + auto& playerGfx = playerEntity.emplace(); + playerGfx.AttachRenderable(std::move(colliderModel)); auto& playerRotNode = playerRotation.emplace(); playerRotNode.SetParent(playerNode); @@ -76,10 +97,10 @@ int main() Nz::MeshParams meshParams; meshParams.animated = true; meshParams.center = true; - meshParams.vertexScale = Nz::Vector3f(0.1f, 0.1f, 0.1f); + meshParams.vertexScale = Nz::Vector3f(0.1f); meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent_Skinning); - std::shared_ptr bobMesh = Nz::Mesh::LoadFromFile(resourceDir / "character/Gangnam Style.fbx", meshParams); + std::shared_ptr bobMesh = fs.Load("assets/character/Gangnam Style.fbx", meshParams); if (!bobMesh) { NazaraError("Failed to load bob mesh"); @@ -92,7 +113,7 @@ int main() animParam.jointScale = meshParams.vertexScale; animParam.jointOffset = meshParams.vertexOffset; - std::shared_ptr bobAnim = Nz::Animation::LoadFromFile(resourceDir / "character/Gangnam Style.fbx", animParam); + std::shared_ptr bobAnim = fs.Load("assets/character/Gangnam Style.fbx", animParam); if (!bobAnim) { NazaraError("Failed to load bob anim"); @@ -103,12 +124,9 @@ int main() std::cout << "joint count: " << skeleton->GetJointCount() << std::endl; - const Nz::Boxf& bobAABB = bobMesh->GetAABB(); std::shared_ptr bobGfxMesh = Nz::GraphicalMesh::BuildFromMesh(*bobMesh); - - //std::shared_ptr material = Nz::Graphics::Instance()->GetDefaultMaterials().basicTransparent; - std::shared_ptr bobModel = std::make_shared(std::move(bobGfxMesh)); + std::vector> materials(bobMesh->GetMaterialCount()); std::bitset<5> alphaMaterials("01010"); @@ -140,15 +158,17 @@ int main() { for (std::size_t x = 0; x < 10; ++x) { - entt::entity bobEntity = registry.create(); + entt::handle bobEntity = world.CreateEntity(); - auto& bobNode = registry.emplace(bobEntity); + auto& bobNode = bobEntity.emplace(); bobNode.SetPosition(Nz::Vector3f(x - 5.f, 0.f, -float(y))); //bobNode.SetRotation(Nz::EulerAnglesf(-90.f, -90.f, 0.f)); //bobNode.SetScale(1.f / 40.f * 0.5f); - auto& bobGfx = registry.emplace(bobEntity); + auto& bobGfx = bobEntity.emplace(); bobGfx.AttachRenderable(bobModel); + + auto& sharedSkeleton = bobEntity.emplace(skeleton); } }*/ @@ -201,16 +221,15 @@ int main() sphereMesh->CreateStatic(); sphereMesh->BuildSubMesh(Nz::Primitive::UVSphere(1.f, 50, 50)); sphereMesh->SetMaterialCount(1); - sphereMesh->GenerateNormalsAndTangents(); std::shared_ptr gfxMesh = Nz::GraphicalMesh::BuildFromMesh(*sphereMesh); // Textures - Nz::TextureParams srgbTexParams = texParams; + Nz::TextureParams srgbTexParams; srgbTexParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB; std::shared_ptr sphereMat = Nz::Graphics::Instance()->GetDefaultMaterials().phongMaterial->Instantiate(); - sphereMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_basecolor.png", srgbTexParams)); + sphereMat->SetTextureProperty("BaseColorMap", fs.Load("assets/Rusty/rustediron2_basecolor.png", srgbTexParams)); std::shared_ptr sphereModel = std::make_shared(std::move(gfxMesh)); for (std::size_t i = 0; i < sphereModel->GetSubMeshCount(); ++i) @@ -221,9 +240,6 @@ int main() sphereNode.SetInheritScale(false); sphereNode.SetParentJoint(bobEntity, "RightHand"); - auto& sphereBody = sphereEntity.emplace(&physSytem.GetPhysWorld()); - sphereBody.SetGeom(std::make_shared(0.1f)); - auto& sphereGfx = sphereEntity.emplace(); sphereGfx.AttachRenderable(sphereModel); } @@ -255,14 +271,11 @@ int main() } std::shared_ptr textMat = Nz::Graphics::Instance()->GetDefaultMaterials().phongMaterial->Instantiate(); - for (const char* pass : { "DepthPass", "ShadowPass", "ForwardPass" }) + textMat->UpdatePassesStates([](Nz::RenderStates& renderStates) { - textMat->UpdatePassStates(pass, [](Nz::RenderStates& renderStates) - { - renderStates.faceCulling = Nz::FaceCulling::None; - return true; - }); - } + renderStates.faceCulling = Nz::FaceCulling::None; + return true; + }); textMat->UpdatePassStates("ForwardPass", [](Nz::RenderStates& renderStates) { @@ -293,20 +306,14 @@ int main() entityNode.SetRotation(Nz::EulerAnglesf(-45.f, 0.f, 0.f)); } - entt::handle planeEntity = world.CreateEntity(); - Nz::Boxf floorBox; + entt::handle floorEntity = world.CreateEntity(); { Nz::MeshParams meshPrimitiveParams; meshPrimitiveParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV); Nz::Vector2f planeSize(25.f, 25.f); - Nz::Mesh planeMesh; - planeMesh.CreateStatic(); - planeMesh.BuildSubMesh(Nz::Primitive::Plane(planeSize, Nz::Vector2ui(0u), Nz::Matrix4f::Identity(), Nz::Rectf(0.f, 0.f, 10.f, 10.f)), meshPrimitiveParams); - planeMesh.SetMaterialCount(1); - - std::shared_ptr planeMeshGfx = Nz::GraphicalMesh::BuildFromMesh(planeMesh); + std::shared_ptr planeMeshGfx = Nz::GraphicalMesh::Build(Nz::Primitive::Plane(planeSize, Nz::Vector2ui(0u), Nz::Matrix4f::Identity(), Nz::Rectf(0.f, 0.f, 10.f, 10.f)), meshPrimitiveParams); Nz::TextureSamplerInfo planeSampler; planeSampler.anisotropyLevel = 16; @@ -314,33 +321,26 @@ int main() planeSampler.wrapModeV = Nz::SamplerWrap::Repeat; std::shared_ptr planeMat = Nz::Graphics::Instance()->GetDefaultMaterials().phongMaterial->Instantiate(); - planeMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "dev_grey.png", texParams), planeSampler); - - floorBox = planeMesh.GetAABB(); + planeMat->SetTextureProperty("BaseColorMap", fs.Load("assets/dev_grey.png"), planeSampler); std::shared_ptr planeModel = std::make_shared(std::move(planeMeshGfx)); planeModel->SetMaterial(0, planeMat); - auto& planeGfx = planeEntity.emplace(); + auto& planeGfx = floorEntity.emplace(); planeGfx.AttachRenderable(planeModel); - planeEntity.emplace(); + floorEntity.emplace(); - auto& planeBody = planeEntity.emplace(&physSytem.GetPhysWorld()); + auto& planeBody = floorEntity.emplace(&physSytem.GetPhysWorld()); planeBody.SetGeom(std::make_shared(Nz::Vector3f(planeSize.x, 0.5f, planeSize.y), Nz::Vector3f(0.f, -0.25f, 0.f))); - Nz::Mesh boxMesh; - boxMesh.CreateStatic(); - boxMesh.BuildSubMesh(Nz::Primitive::Box(Nz::Vector3f(0.5f, 0.5f, 0.5f)), meshPrimitiveParams); - boxMesh.SetMaterialCount(1); - - std::shared_ptr boxMeshGfx = Nz::GraphicalMesh::BuildFromMesh(boxMesh); + std::shared_ptr boxMeshGfx = Nz::GraphicalMesh::Build(Nz::Primitive::Box(Nz::Vector3f(0.5f, 0.5f, 0.5f)), meshPrimitiveParams); std::shared_ptr boxModel = std::make_shared(std::move(boxMeshGfx)); boxModel->SetMaterial(0, planeMat); entt::handle boxEntity = world.CreateEntity(); - boxEntity.emplace().SetPosition(Nz::Vector3f(0.f, 0.25f, -0.5f)); + boxEntity.emplace(); boxEntity.emplace().AttachRenderable(boxModel); } @@ -383,24 +383,25 @@ int main() { float updateTime = deltaTime->AsSeconds(); - /*auto& playerBody = registry.get(playerEntity); + auto& playerBody = playerEntity.get(); + //playerBody.SetAngularDamping(std::numeric_limits::max()); float mass = playerBody.GetMass(); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::RShift)) playerBody.AddForce(Nz::Vector3f(0.f, mass * 50.f, 0.f)); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up)) playerBody.AddForce(Nz::Vector3f::Forward() * 25.f * mass, Nz::CoordSys::Local); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Down) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Down)) playerBody.AddForce(Nz::Vector3f::Backward() * 25.f * mass, Nz::CoordSys::Local); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Left) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Left)) playerBody.AddForce(Nz::Vector3f::Left() * 25.f * mass, Nz::CoordSys::Local); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Right) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D)) - playerBody.AddForce(Nz::Vector3f::Right() * 25.f * mass, Nz::CoordSys::Local);*/ + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Right)) + playerBody.AddForce(Nz::Vector3f::Right() * 25.f * mass, Nz::CoordSys::Local); float cameraSpeed = 2.f; @@ -408,16 +409,16 @@ int main() if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)) cameraNode.Move(Nz::Vector3f::Up() * cameraSpeed * updateTime, Nz::CoordSys::Global); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)) cameraNode.Move(Nz::Vector3f::Forward() * cameraSpeed * updateTime, Nz::CoordSys::Local); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Down) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S)) cameraNode.Move(Nz::Vector3f::Backward() * cameraSpeed * updateTime, Nz::CoordSys::Local); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Left) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q)) cameraNode.Move(Nz::Vector3f::Left() * cameraSpeed * updateTime, Nz::CoordSys::Local); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Right) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D)) + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D)) cameraNode.Move(Nz::Vector3f::Right() * cameraSpeed * updateTime, Nz::CoordSys::Local); if (!paused) diff --git a/include/Nazara/Core/Time.inl b/include/Nazara/Core/Time.inl index df0dacd59..2f35a1fe2 100644 --- a/include/Nazara/Core/Time.inl +++ b/include/Nazara/Core/Time.inl @@ -6,8 +6,8 @@ namespace Nz { - constexpr Time::Time(Int64 microseconds) : - m_nanoseconds(microseconds) + constexpr Time::Time(Int64 nanoseconds) : + m_nanoseconds(nanoseconds) { } diff --git a/include/Nazara/Utility/Animation.hpp b/include/Nazara/Utility/Animation.hpp index e87d45353..85d3dcc3c 100644 --- a/include/Nazara/Utility/Animation.hpp +++ b/include/Nazara/Utility/Animation.hpp @@ -57,6 +57,8 @@ namespace Nz class NAZARA_UTILITY_API Animation : public Resource { public: + using Params = AnimationParams; + Animation(); Animation(const Animation&) = delete; Animation(Animation&&) noexcept;