From 99007874c4bb709d87cc5f0f02756b16e0172dca Mon Sep 17 00:00:00 2001 From: Tomas Maly Date: Sun, 10 Nov 2024 16:35:30 +0100 Subject: [PATCH] dynamic resolution --- externals/cage | 2 +- sources/engine/rawWindow.cpp | 2 +- sources/simple/dynamicResolution.cpp | 160 +++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 sources/simple/dynamicResolution.cpp diff --git a/externals/cage b/externals/cage index 43661fd..69781d0 160000 --- a/externals/cage +++ b/externals/cage @@ -1 +1 @@ -Subproject commit 43661fd1ffe4c76e608d3f4350f3d1dd8397df97 +Subproject commit 69781d056d3a12454f9aa0d55f76b0b0ed580441 diff --git a/sources/engine/rawWindow.cpp b/sources/engine/rawWindow.cpp index f74f6c8..77b038f 100644 --- a/sources/engine/rawWindow.cpp +++ b/sources/engine/rawWindow.cpp @@ -58,7 +58,7 @@ int main(int argc, char *args[]) // fetch assets Holder model = assets->get(HashString("cage/model/square.obj")); Holder texture = assets->get(HashString("cage-tests/logo/logo.png")); - Holder shader = assets->get(HashString("cage/shader/engine/blit.glsl"))->get(0); + Holder shader = assets->get(HashString("cage/shader/engine/blitPixels.glsl"))->get(0); Holder sound = assets->get(HashString("cage-tests/logo/logo.ogg")); // initialize graphics diff --git a/sources/simple/dynamicResolution.cpp b/sources/simple/dynamicResolution.cpp new file mode 100644 index 0000000..89d4b44 --- /dev/null +++ b/sources/simple/dynamicResolution.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace cage; + +constexpr uint32 AssetsName = HashString("cage-tests/camera-effects/effects.pack"); + +struct DataComponent +{ + uint64 destroyTime = 0; +}; + +void addEntities(uint32 cnt) +{ + const uint64 time = engineControlTime(); + for (uint32 i = 0; i < cnt; i++) + { + Entity *e = engineEntities()->createAnonymous(); + e->value().destroyTime = time + randomRange(200, 300) * 1'000; + e->value().object = HashString("scenes/mcguire/crytek/sponza.object"); + e->value().opacity = randomChance() * 0.4 + 0.3; // prevent depth pre-pass optimization + e->value().position += randomDirection3() * randomRange(0.01, 0.1); + } +} + +void update() +{ + const uint64 time = engineControlTime(); + entitiesVisitor( + [time](Entity *e, const DataComponent &d) + { + if (time > d.destroyTime) + e->destroy(); + }, + engineEntities(), true); + + const sint32 cnt = numeric_cast((sin(Degs(time / 200'000)) + 0.8) * 20); + if (cnt > 0) + addEntities(cnt); +} + +void keyPress(input::KeyPress in) +{ + if (in.key == 32) // space + addEntities(100); +} + +void generateGui() +{ + Holder g = newGuiBuilder(engineGuiEntities()); + auto _1 = g->alignment(Vec2(0)); + auto _2 = g->spoiler(false).text("Dynamic Resolution"); + auto _3 = g->verticalTable(3); + { + g->label().text("Enabled: "); + g->checkBox(engineDynamicResolution().enabled).event(inputFilter([](input::GuiValue v) { engineDynamicResolution().enabled = v.entity->value().state == CheckBoxStateEnum::Checked; })); + g->empty(); + } + { + g->label().text("Target FPS: "); + g->horizontalSliderBar(engineDynamicResolution().targetFps, 1, 90).event(inputFilter([](input::GuiValue v) { engineDynamicResolution().targetFps = v.entity->value().value.value; })); + g->label().text("").update([](Entity *e) { e->value().value = Stringizer() + engineDynamicResolution().targetFps; }); + } + { + g->label().text("Minimum Scale: "); + g->horizontalSliderBar(engineDynamicResolution().minimumScale, 0.1, 1.0).event(inputFilter([](input::GuiValue v) { engineDynamicResolution().minimumScale = v.entity->value().value; })); + g->label().text("").update([](Entity *e) { e->value().value = Stringizer() + engineDynamicResolution().minimumScale; }); + } +} + +int main(int argc, char *args[]) +{ + try + { + initializeConsoleLogger(); + engineInitialize(EngineCreateConfig()); + + // events + const auto updateListener = controlThread().update.listen(update); + const auto closeListener = engineWindow()->events.listen(inputFilter([](input::WindowClose) { engineStop(); })); + const auto keyPressListener = engineWindow()->events.listen(inputFilter(keyPress)); + + // window + engineWindow()->title("dynamic resolution"); + engineWindow()->setMaximized(); + generateGui(); + + // entities + EntityManager *ents = engineEntities(); + ents->defineComponent(DataComponent()); + { // camera + Entity *e = ents->create(1); + TransformComponent &t = e->value(); + t.position = Vec3(11.5, 1, -1); + t.orientation = Quat(Degs(10), Degs(110), Degs()); + CameraComponent &c = e->value(); + c.ambientColor = Vec3(1); + c.ambientIntensity = 0.05; + c.near = 0.1; + c.far = 100; + e->value(); + } + { // skybox + Entity *e = ents->createAnonymous(); + e->value(); + e->value().object = HashString("scenes/common/skybox.obj"); + e->value(); + } + { // sun + Entity *e = ents->create(2); + TransformComponent &t = e->value(); + t.position = Vec3(0, 5, 0); + t.orientation = Quat(Degs(-75), Degs(-120), Degs()); + LightComponent &l = e->value(); + l.lightType = LightTypeEnum::Directional; + l.color = Vec3(1); + l.intensity = 3; + ShadowmapComponent &s = e->value(); + s.resolution = 2048; + s.directionalWorldSize = 30; + } + { // floor + Entity *e = ents->createAnonymous(); + e->value().object = HashString("scenes/common/ground.obj"); + e->value().position = Vec3(0, -1.264425, 0); + } + { // sponza + Entity *e = ents->createAnonymous(); + e->value().object = HashString("scenes/mcguire/crytek/sponza.object"); + e->value(); + } + + Holder fpsCamera = newFpsCamera(ents->get(1)); + fpsCamera->mouseButton = MouseButtonsFlags::Left; + fpsCamera->movementSpeed = 0.3; + Holder statistics = newStatisticsGui(); + + engineAssets()->load(AssetsName); + engineRun(); + engineAssets()->unload(AssetsName); + engineFinalize(); + + return 0; + } + catch (...) + { + detail::logCurrentCaughtException(); + return 1; + } +}