commit 75cd7696819ec1d7d1bbcd2d635f15e019e0852f Author: Persson-dev Date: Sun Jun 9 12:16:18 2024 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fce0372 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + +NazaraLog.log + +.vscode/settings.json +.vscode/compile_commands.json \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..0a442d0 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,13 @@ +{ + "configurations": [ + { + "name": "Linux", + "cppStandard": "c++20", + "includePath": [ + "include" + ], + "compileCommands": ".vscode/compile_commands.json" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..44b7456 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "xmake", + "request": "launch", + "name": "Debug", + "target": "Blitz2", + "cwd": "${workspaceFolder}", + } + ] +} \ No newline at end of file diff --git a/assets/models/sol.mtl b/assets/models/sol.mtl new file mode 100644 index 0000000..be2f57b --- /dev/null +++ b/assets/models/sol.mtl @@ -0,0 +1,12 @@ +# Blender 3.4.1 MTL File: 'None' +# www.blender.org + +newmtl Material +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/assets/models/sol.obj b/assets/models/sol.obj new file mode 100644 index 0000000..6eca845 --- /dev/null +++ b/assets/models/sol.obj @@ -0,0 +1,40 @@ +# Blender 3.4.1 +# www.blender.org +mtllib sol.mtl +o Cube +v 20.000000 0.000000 -20.000000 +v 20.000000 -2.000000 -20.000000 +v 20.000000 0.000000 20.000000 +v 20.000000 -2.000000 20.000000 +v -20.000000 0.000000 -20.000000 +v -20.000000 -2.000000 -20.000000 +v -20.000000 0.000000 20.000000 +v -20.000000 -2.000000 20.000000 +vn -0.0000 1.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -1.0000 -0.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vt 0.625000 0.500000 +vt 0.375000 0.500000 +vt 0.625000 0.750000 +vt 0.375000 0.750000 +vt 0.875000 0.500000 +vt 0.625000 0.250000 +vt 0.125000 0.500000 +vt 0.375000 0.250000 +vt 0.875000 0.750000 +vt 0.625000 1.000000 +vt 0.625000 0.000000 +vt 0.375000 1.000000 +vt 0.375000 0.000000 +vt 0.125000 0.750000 +s 0 +usemtl Material +f 1/1/1 5/5/1 7/9/1 3/3/1 +f 4/4/2 3/3/2 7/10/2 8/12/2 +f 8/13/3 7/11/3 5/6/3 6/8/3 +f 6/7/4 2/2/4 4/4/4 8/14/4 +f 2/2/5 1/1/5 3/3/5 4/4/5 +f 6/8/6 5/6/6 1/1/6 2/2/6 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b36e779 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,203 @@ +#include + +#include +#include +#include +#include +#include +#include + +static void CreateLight(Nz::EnttWorld &world) +{ + entt::handle lightEntity = world.CreateEntity(); + { + auto &lightNode = lightEntity.emplace(); + lightNode.SetPosition({0, 1, 0}); + + auto &entityLight = lightEntity.emplace(); + auto &spotLight = entityLight.AddLight(1); + spotLight.EnableShadowCasting(true); + spotLight.UpdateShadowMapSize(1024); + } + +} + +static void CreateBoxes(Nz::EnttWorld& world) +{ + constexpr float BoxDims = 16.f; + + std::mt19937 rd(42); + std::uniform_real_distribution colorDis(0.f, 360.f); + std::uniform_real_distribution radiusDis(0.1f, 0.5f); + + std::uniform_real_distribution lengthDis(0.2f, 1.5f); + std::shared_ptr boxMesh = Nz::GraphicalMesh::Build(Nz::Primitive::Box(Nz::Vector3f(1.f))); + + constexpr std::size_t BoxCount = 100; + for (std::size_t i = 0; i < BoxCount; ++i) + { + float width = lengthDis(rd); + float height = lengthDis(rd); + float depth = lengthDis(rd); + + std::uniform_real_distribution xRandom(-BoxDims * 0.5f + width, BoxDims * 0.5f - width); + std::uniform_real_distribution yRandom(-BoxDims * 0.5f + height, BoxDims * 0.5f - height); + std::uniform_real_distribution zRandom(-BoxDims * 0.5f + depth, BoxDims * 0.5f - depth); + + entt::handle boxEntity = world.CreateEntity(); + + std::shared_ptr boxMaterial = Nz::MaterialInstance::Instantiate(Nz::MaterialType::Phong); + boxMaterial->SetValueProperty("BaseColor", Nz::Color::sRGBToLinear(Nz::Color::FromHSV(colorDis(rd), 1.f, 1.f))); + + std::shared_ptr sphereModel = std::make_shared(boxMesh); + sphereModel->SetMaterial(0, std::move(boxMaterial)); + + boxEntity.emplace(std::move(sphereModel)); + + auto &ballNode = boxEntity.emplace(); + ballNode.SetPosition({xRandom(rd), yRandom(rd), zRandom(rd)}); + ballNode.SetScale({width, height, depth}); + + std::shared_ptr boxCollider = std::make_shared(Nz::Vector3f(width, height, depth)); + + Nz::RigidBody3D::DynamicSettings settings; + settings.geom = boxCollider; + settings.mass = width * height * depth; + + boxEntity.emplace(settings); + } +} + +static void CreateModel(Nz::EnttWorld &world) +{ + std::filesystem::path resourceDir = "assets/models"; + + Nz::MeshParams meshParams; + meshParams.center = true; + meshParams.vertexRotation = Nz::EulerAnglesf(0.f, 0.f, 0.f); + meshParams.vertexScale = Nz::Vector3f(1.0f); + meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV_Tangent); + + std::shared_ptr deambuMesh = Nz::Mesh::LoadFromFile(resourceDir / "sol.obj", meshParams); + if (!deambuMesh) + { + NazaraError("failed to load model"); + return; + } + + std::shared_ptr gfxMesh = Nz::GraphicalMesh::BuildFromMesh(*deambuMesh); + std::shared_ptr deambModel = std::make_shared(std::move(gfxMesh)); + + entt::handle deambEntity = world.CreateEntity(); + { + auto &entityGfx = deambEntity.emplace(); + entityGfx.AttachRenderable(deambModel); + + auto &entityNode = deambEntity.emplace(); + entityNode.SetPosition(Nz::Vector3f(0.f, 0.f, 0.f)); + } + + std::shared_ptr device = Nz::Graphics::Instance()->GetRenderDevice(); + + std::shared_ptr material = Nz::MaterialInstance::Instantiate(Nz::MaterialType::Phong); + for (std::string_view passName : {"DepthPass", "ForwardPass"}) + { + material->UpdatePassStates(passName, [](Nz::RenderStates &states) + { + states.depthClamp = true; + return true; }); + } + + std::mt19937 rd(42); + std::uniform_real_distribution colorDis(0.f, 360.f); + material->SetValueProperty("BaseColor", Nz::Color::sRGBToLinear(Nz::Color::FromHSV(colorDis(rd), 1.f, 1.f))); + + for (std::size_t i = 0; i < deambModel->GetSubMeshCount(); ++i) + deambModel->SetMaterial(i, material); + + + Nz::VertexMapper vertexMapper(*deambuMesh->GetSubMesh(0)); + Nz::SparsePtr vertices = vertexMapper.GetComponentPtr(Nz::VertexComponent::Position); + + auto shipCollider = std::make_shared(vertices, vertexMapper.GetVertexCount(), 0.1f); + + Nz::RigidBody3D::StaticSettings settings; + settings.geom = shipCollider; + + deambEntity.emplace(settings); +} + +static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); + +static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window) +{ + Nz::RenderSystem &renderSystem = world.AddSystem(); + + Nz::SwapchainParameters params; + params.presentMode.clear(); + params.presentMode.push_back(Nz::PresentMode::VerticalSync); + + Nz::WindowSwapchain &windowSwapchain = renderSystem.CreateSwapchain(window, params); + + // Création de la caméra + entt::handle cameraEntity = world.CreateEntity(); + + auto &cameraNode = cameraEntity.emplace(); + cameraNode.SetPosition({0, 5, 0}); + + auto &cameraComponent = cameraEntity.emplace(std::make_shared(windowSwapchain), Nz::ProjectionType::Perspective); + cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f)); + + window.GetEventHandler().OnMouseMoved.Connect([&](const Nz::WindowEventHandler * /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent &event) + { + constexpr float sensitivity = 0.3f; + + camAngles.yaw -= event.deltaX * sensitivity; + camAngles.yaw.Normalize(); + + camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f); + + cameraNode.SetRotation(camAngles); }); + +} + +int main(int argc, char **argv) +{ + Nz::Application app(argc, argv); + + auto &windowing = app.AddComponent(); + + std::string windowTitle = "Blitz 2"; + Nz::Window &window = windowing.CreateWindow(Nz::VideoMode(1920, 1080, 32), windowTitle); + + auto &ecs = app.AddComponent(); + + auto &world = ecs.AddWorld(); + + auto& physSystem = world.AddSystem(); + physSystem.GetPhysWorld().SetMaxStepCount(1); + physSystem.GetPhysWorld().SetStepSize(Nz::Time::TickDuration(50)); + physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f); + + CreateCamera(world, window); + CreateBoxes(world); + CreateModel(world); + CreateLight(world); + + Nz::Mouse::SetRelativeMouseMode(true); + + Nz::MillisecondClock fpsClock; + unsigned int fps = 0; + + app.AddUpdaterFunc([&](){ + fps++; + + if (fpsClock.RestartIfOver(Nz::Time::Second())) + { + window.SetTitle(windowTitle + " - " + Nz::NumberToString(fps) + " FPS" + " - " + Nz::NumberToString(world.GetAliveEntityCount()) + " entities"); + fps = 0; + } + }); + + return app.Run(); +} diff --git a/xmake.lua b/xmake.lua new file mode 100644 index 0000000..84784ee --- /dev/null +++ b/xmake.lua @@ -0,0 +1,82 @@ +add_rules("mode.debug", "mode.release") + +add_repositories("nazara-repo https://github.com/NazaraEngine/xmake-repo.git") +add_requires("nazaraengine", { debug = is_mode("debug") }) + +set_languages("c++20") + +target("Blitz2") + set_kind("binary") + add_files("src/*.cpp") + add_packages("nazaraengine") + set_rundir(".") + +-- +-- If you want to known more usage about xmake, please see https://xmake.io +-- +-- ## FAQ +-- +-- You can enter the project directory firstly before building project. +-- +-- $ cd projectdir +-- +-- 1. How to build project? +-- +-- $ xmake +-- +-- 2. How to configure project? +-- +-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release] +-- +-- 3. Where is the build output directory? +-- +-- The default output directory is `./build` and you can configure the output directory. +-- +-- $ xmake f -o outputdir +-- $ xmake +-- +-- 4. How to run and debug target after building project? +-- +-- $ xmake run [targetname] +-- $ xmake run -d [targetname] +-- +-- 5. How to install target to the system directory or other output directory? +-- +-- $ xmake install +-- $ xmake install -o installdir +-- +-- 6. Add some frequently-used compilation flags in xmake.lua +-- +-- @code +-- -- add debug and release modes +-- add_rules("mode.debug", "mode.release") +-- +-- -- add macro definition +-- add_defines("NDEBUG", "_GNU_SOURCE=1") +-- +-- -- set warning all as error +-- set_warnings("all", "error") +-- +-- -- set language: c99, c++11 +-- set_languages("c99", "c++11") +-- +-- -- set optimization: none, faster, fastest, smallest +-- set_optimize("fastest") +-- +-- -- add include search directories +-- add_includedirs("/usr/include", "/usr/local/include") +-- +-- -- add link libraries and search directories +-- add_links("tbox") +-- add_linkdirs("/usr/local/lib", "/usr/lib") +-- +-- -- add system link libraries +-- add_syslinks("z", "pthread") +-- +-- -- add compilation and link flags +-- add_cxflags("-stdnolib", "-fno-strict-aliasing") +-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true}) +-- +-- @endcode +-- +