Commit 0dba1ac1 authored by Lukas Tietze's avatar Lukas Tietze

Einfaches interface

parent d585126b
......@@ -4,25 +4,31 @@
namespace app
{
class VkrtApp : public lib::IAppAdapter
{
private:
float currentHDeg = 0;
float colorChangeFactor = 5.f;
float sunAngle = 0;
float sunSpeedFactor = 5.f;
float moveSpeedFactor = 1.f;
float rotateSpeedFactor = 300.f;
class VkrtApp : public lib::IAppAdapter
{
private:
float currentHDeg = 0;
float colorChangeFactor = 5.f;
float sunAngle = 0;
float sunSpeedFactor = 5.f;
float moveSpeedFactor = 1.f;
float rotateSpeedFactor = 300.f;
bool animateLight = true;
bool animateBackground = true;
void HandleCameraMovement(lib::App &app, lib::time_diff_t delta);
void HandleOtherKeys(lib::App &app);
void UpdateBackground(lib::App &app, lib::time_diff_t delta);
void UpdateLight(lib::App &app, lib::time_diff_t delta);
void HandleCameraMovement(lib::App &app, lib::time_diff_t delta);
void HandleOtherKeys(lib::App &app);
void AnimateBackground(lib::App &app, lib::time_diff_t delta);
void UpdateBackground(lib::App &app, lib::time_diff_t delta);
void AnimateLight(lib::App &app, lib::time_diff_t delta);
void UpdateLight(lib::App &app, lib::time_diff_t delta);
void RenderUi(lib::App &app);
public:
void Update(lib::App &app, lib::time_diff_t delta) override;
void AfterInit(lib::App &app) override;
void BeforeClose(lib::App &app) override;
void AfterClose(lib::App &app) override;
};
public:
void Update(lib::App &app, lib::time_diff_t delta) override;
void AfterInit(lib::App &app) override;
void BeforeClose(lib::App &app) override;
void AfterClose(lib::App &app) override;
};
} // namespace app
......@@ -89,8 +89,7 @@ void app::VkrtApp::HandleOtherKeys(lib::App &app)
app.ToogleUi();
}
}
void app::VkrtApp::UpdateBackground(lib::App &app, lib::time_diff_t delta)
void app::VkrtApp::AnimateBackground(lib::App &app, lib::time_diff_t delta)
{
this->currentHDeg += delta.count() * this->colorChangeFactor;
......@@ -98,6 +97,14 @@ void app::VkrtApp::UpdateBackground(lib::App &app, lib::time_diff_t delta)
{
this->currentHDeg -= 360.f;
}
}
void app::VkrtApp::UpdateBackground(lib::App &app, lib::time_diff_t delta)
{
if (this->animateBackground)
{
this->AnimateBackground(app, delta);
}
auto &col = app.GetContext().GetClearColor();
......@@ -128,19 +135,60 @@ void app::VkrtApp::UpdateBackground(lib::App &app, lib::time_diff_t delta)
}
}
void app::VkrtApp::AnimateLight(lib::App &app, lib::time_diff_t delta)
{
this->sunAngle += delta.count() * this->sunSpeedFactor;
if (this->sunAngle > 360.f)
{
this->sunAngle -= 360.f;
}
}
void app::VkrtApp::UpdateLight(lib::App &app, lib::time_diff_t delta)
{
auto &light = app.GetContext().GetMainLightSource();
if (this->animateLight)
{
this->AnimateLight(app, delta);
}
this->sunAngle += delta.count() * this->sunSpeedFactor;
auto &light = app.GetContext().GetMainLightSource();
light.SetPosition(glm::rotate(light.GetPosition(),
glm::radians(delta.count() * this->sunSpeedFactor),
lib::axis::y));
}
void app::VkrtApp::RenderUi(lib::App &app)
{
ImGui::NewFrame();
{
ImGui::Begin("Szenenkonfiguration");
{
ImGui::Text("Lichtquelle:");
ImGui::SliderAngle("Rotation", &this->sunAngle, 0.f);
ImGui::Checkbox("Animieren", &this->animateLight);
ImGui::SliderFloat("Animationsgeschwindigkeit", &this->sunSpeedFactor, 0, 20.f);
}
{
ImGui::Text("Hintergrund:");
ImGui::Checkbox("Animieren", &this->animateBackground);
}
ImGui::End();
}
ImGui::Render();
}
void app::VkrtApp::Update(lib::App &app, lib::time_diff_t delta)
{
if (app.IsUiShown())
{
this->RenderUi(app);
}
this->HandleCameraMovement(app, delta);
this->HandleOtherKeys(app);
this->UpdateBackground(app, delta);
......
......@@ -119,6 +119,7 @@ namespace lib
void SetShowUi(bool show);
void ToogleUi();
bool IsUiShown() const;
GLFWwindow *GetWindow();
const GLFWwindow *GetWindow() const;
......
......@@ -240,7 +240,7 @@ namespace lib
void DestroyImGuiComponents();
void DestroyImGui();
void ReloadImGui();
void ImGuiFrame();
void StartImGuiFrame();
bool CheckAndAdd(const char *name, NameList &buf, bool required);
bool CheckAndAddAll(const char **, uint32_t count, NameList &buf, bool required);
......@@ -262,7 +262,9 @@ namespace lib
bool IsOk() const;
bool IsRecoverable() const;
void UpdateCommandBuffer(uint32_t imageIndex);
void BeginFrame();
void DrawFrame(lib::time_diff_t diff);
void EndFrame();
void WaitDeviceIdle();
const vk::Instance &GetInstance() const;
......
#pragma once
#include <type_traits>
#include <chrono>
#include "glm/glm.hpp"
namespace lib
{
using clock = std::chrono::high_resolution_clock;
using time_diff_t = std::chrono::duration<float>;
using time_point_t = clock::time_point;
using clock = std::chrono::high_resolution_clock;
using time_diff_t = std::chrono::duration<float>;
using time_point_t = clock::time_point;
namespace points {
inline const glm::vec3 center = {0, 0, 0};
}
namespace points
{
inline const glm::vec3 center = {0, 0, 0};
}
namespace axis
{
inline const glm::vec3 x = {1, 0, 0};
inline const glm::vec3 y = {0, 1, 0};
inline const glm::vec3 z = {0, 0, 1};
} // namespace axis
namespace axis
{
inline const glm::vec3 x = {1, 0, 0};
inline const glm::vec3 y = {0, 1, 0};
inline const glm::vec3 z = {0, 0, 1};
} // namespace axis
} // namespace lib
......@@ -4,6 +4,9 @@
#include "VkUtil/VkUtil.hpp"
#include <chrono>
#include <algorithm>
#include <numeric>
#include <cmath>
using lib::log::Log;
using lib::log::LogError;
......@@ -28,13 +31,19 @@ namespace
bool keyState[GLFW_KEY_LAST];
bool lastKeyState[GLFW_KEY_LAST];
int modifiers = 0;
bool uiShown = false;
lib::time_point_t t0;
lib::time_point_t t1;
lib::time_diff_t lastFrameDelta;
uint32_t framesInLastSecond = 0;
uint32_t frameCounter = 0;
lib::time_point_t lastFrameCountStart;
bool uiShown = false;
std::vector<std::chrono::microseconds> lastSecondFrameTimes;
std::chrono::duration<double, std::chrono::milliseconds::period> averageFrameTime;
std::chrono::duration<double, std::chrono::milliseconds::period> varianceFrameTime;
void NextFrame()
{
......@@ -52,13 +61,34 @@ namespace
::lastFrameDelta = ::t1 - ::t0;
::frameCounter++;
::lastSecondFrameTimes.push_back(std::chrono::duration_cast<std::chrono::microseconds>(lastFrameDelta));
const auto timeSinceLastFrameCountStart = std::chrono::duration_cast<std::chrono::milliseconds>(::t1 - ::lastFrameCountStart);
if (timeSinceLastFrameCountStart.count() > 1000)
if (timeSinceLastFrameCountStart.count() > 1000u)
{
::framesInLastSecond = ::frameCounter;
::frameCounter = 0;
::lastFrameCountStart = ::t1;
const auto average = std::reduce(::lastSecondFrameTimes.cbegin(), ::lastSecondFrameTimes.cend()) /
static_cast<std::chrono::microseconds::rep>(::lastSecondFrameTimes.size());
const auto totalDiff = std::transform_reduce(::lastSecondFrameTimes.cbegin(), ::lastSecondFrameTimes.cend(),
std::chrono::microseconds{},
std::plus<std::chrono::microseconds>{},
[=](decltype(::lastSecondFrameTimes)::value_type v) {
const auto aDbl = static_cast<double>(average.count());
const auto bDbl = static_cast<double>(v.count());
const auto diff = std::pow(aDbl - bDbl, 2);
const auto asUs = static_cast<std::chrono::microseconds::rep>(diff);
return std::chrono::microseconds(asUs);
});
const auto variance = totalDiff / static_cast<std::chrono::microseconds::rep>(::lastSecondFrameTimes.size());
::averageFrameTime = std::chrono::duration<double, std::chrono::milliseconds::period>(average);
::varianceFrameTime = std::chrono::duration<double, std::chrono::milliseconds::period>(variance);
lastSecondFrameTimes.clear();
}
}
......@@ -224,8 +254,10 @@ void lib::App::Run()
{
try
{
this->context->BeginFrame();
this->adapter->Update(*this, lastFrameDelta);
this->context->DrawFrame(lastFrameDelta);
this->context->SetShowUi(::uiShown);
}
catch (const std::exception &e)
{
......@@ -278,7 +310,9 @@ std::string lib::App::MakeWindowTitle() const
if (this->titleInfo.showFPS)
{
buf << " (" << ::framesInLastSecond << "fps)";
buf << " (" << ::framesInLastSecond << "fps) ["
<< ::averageFrameTime.count() << "ms pro frame /"
<< ::varianceFrameTime.count() << ']';
}
return buf.str();
......@@ -327,7 +361,6 @@ void lib::App::RegisterCallbacks() const
void lib::App::SetShowUi(bool show)
{
::uiShown = show;
this->context->SetShowUi(::uiShown);
if (::uiShown)
{
......@@ -340,6 +373,11 @@ void lib::App::ToogleUi()
this->SetShowUi(!::uiShown);
}
bool lib::App::IsUiShown() const
{
return ::uiShown;
}
GLFWwindow *lib::App::GetWindow()
{
return this->window;
......
......@@ -169,26 +169,8 @@ void lib::CgContext::ReloadImGui()
this->CreateImGuiComponents();
}
void lib::CgContext::ImGuiFrame()
void lib::CgContext::StartImGuiFrame()
{
static bool close = false;
ImGui_ImplVulkan_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("My Window!");
ImGui::Text("Hello World!");
if (ImGui::Button("Click me!"))
{
lib::log::Log("He clicked me :O");
}
ImGui::End();
ImGui::ShowMetricsWindow();
ImGui::ShowAboutWindow();
ImGui::Render();
}
\ No newline at end of file
......@@ -913,14 +913,21 @@ void lib::CgContext::UpdateUniformBuffers(uint32_t imageIndex, lib::time_diff_t
this->clearColor));
}
void lib::CgContext::DrawFrame(lib::time_diff_t delta)
void lib::CgContext::BeginFrame()
{
this->logicalDevice.waitForFences(this->inFlightFences[this->frameIndex], VK_TRUE, std::numeric_limits<uint64_t>::max());
if (this->imGui.show)
{
this->ImGuiFrame();
this->StartImGuiFrame();
}
}
void lib::CgContext::EndFrame()
{
}
void lib::CgContext::DrawFrame(lib::time_diff_t delta)
{
this->logicalDevice.waitForFences(this->inFlightFences[this->frameIndex], VK_TRUE, std::numeric_limits<uint64_t>::max());
auto nextImageResult = this->logicalDevice.acquireNextImageKHR(
this->swapChain,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment