Skip to content

Commit

Permalink
Added an orbit_camera to further example applications.
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesugb committed Jun 2, 2023
1 parent ce1b08d commit 96d5bfa
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 55 deletions.
3 changes: 1 addition & 2 deletions auto_vk_toolkit/src/orbit_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace avk
{
orbit_camera::orbit_camera(std::string aName, bool aIsEnabled)
: invokee(std::move(aName), aIsEnabled)
, mRotationSpeed(0.0025f)
, mRotationSpeed(0.002f)
, mPivotDistance{ 10.f }
, mPivotDistanceSpeed{ .5f }
, mMinPivotDistance{ 1.f }
Expand Down Expand Up @@ -107,7 +107,6 @@ namespace avk
{
const auto* wnd = context().main_window();
auto resy = nullptr != wnd ? static_cast<float>(wnd->resolution().y) : 1000.f;
resy *= 0.5f;
mLateralSpeed = glm::abs(mPivotDistance / projection_matrix()[1][1]) / resy;
}
}
59 changes: 42 additions & 17 deletions examples/orca_loader/source/orca_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "orca_scene.hpp"
#include "serializer.hpp"
#include "sequential_invoker.hpp"
#include "orbit_camera.hpp"
#include "quake_camera.hpp"
#include "vk_convenience_functions.hpp"

Expand Down Expand Up @@ -418,24 +419,56 @@ class orca_loader_app : public avk::invokee
#endif

// Add the camera to the composition (and let it handle the updates)
mOrbitCam.set_translation({ 0.0f, 0.0f, 0.0f });
mQuakeCam.set_translation({ 0.0f, 0.0f, 0.0f });
mOrbitCam.set_perspective_projection(glm::radians(60.0f), avk::context().main_window()->aspect_ratio(), 0.3f, 1000.0f);
mQuakeCam.set_perspective_projection(glm::radians(60.0f), avk::context().main_window()->aspect_ratio(), 0.3f, 1000.0f);
//mQuakeCam.set_orthographic_projection(-5, 5, -5, 5, 0.5, 100);
avk::current_composition()->add_element(mOrbitCam);
avk::current_composition()->add_element(mQuakeCam);
mQuakeCam.disable();

// UI:
mFileBrowser.SetTitle("Select ORCA scene file");
mFileBrowser.SetTypeFilters({ ".fscene" });

auto imguiManager = avk::current_composition()->element_by_type<avk::imgui_manager>();
if(nullptr != imguiManager) {
imguiManager->add_callback([this](){
imguiManager->add_callback([
this,
windowHoveredLastFrame = false
]() mutable {
ImGui::Begin("Info & Settings");
ImGui::SetWindowPos(ImVec2(1.0f, 1.0f), ImGuiCond_FirstUseEver);
ImGui::Text("%.3f ms/frame", 1000.0f / ImGui::GetIO().Framerate);
ImGui::Text("%.1f FPS", ImGui::GetIO().Framerate);
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), "[F1]: Toggle input-mode");
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), " (UI vs. scene navigation)");

ImGui::Separator();
bool quakeCamEnabled = mQuakeCam.is_enabled();
if (ImGui::Checkbox("Enable Quake Camera", &quakeCamEnabled)) {
if (quakeCamEnabled) { // => should be enabled
mQuakeCam.set_matrix(mOrbitCam.matrix());
mQuakeCam.enable();
mOrbitCam.disable();
}
}
if (quakeCamEnabled) {
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), "[F1] to exit Quake Camera navigation.");
if (avk::input().key_pressed(avk::key_code::f1)) {
mOrbitCam.set_matrix(mQuakeCam.matrix());
mOrbitCam.enable();
mQuakeCam.disable();
}
}
const bool windowHoveredThisFrame = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow);
if (windowHoveredThisFrame && !windowHoveredLastFrame && mOrbitCam.is_enabled()) {
mOrbitCam.disable();
}
if (windowHoveredLastFrame && !windowHoveredThisFrame && !mQuakeCam.is_enabled()) {
mOrbitCam.enable();
}
windowHoveredLastFrame = windowHoveredThisFrame;
ImGui::Separator();

ImGui::DragFloat3("Rotate Objects", glm::value_ptr(mRotateObjects), 0.005f, -glm::pi<float>(), glm::pi<float>());
ImGui::DragFloat3("Rotate Scene", glm::value_ptr(mRotateScene), 0.005f, -glm::pi<float>(), glm::pi<float>());
ImGui::Separator();
Expand Down Expand Up @@ -473,7 +506,7 @@ class orca_loader_app : public avk::invokee
printf("Time from init to fourth frame: %d min, %lld sec %lf ms\n", int_min, int_sec - static_cast<decltype(int_sec)>(int_min) * 60, fp_ms - 1000.0 * int_sec);
}

if (avk::input().key_pressed(avk::key_code::c)) {
if (avk::input().key_pressed(avk::key_code::c)) {,
// Center the cursor:
auto resolution = avk::context().main_window()->resolution();
avk::context().main_window()->set_cursor_pos({ resolution[0] / 2.0, resolution[1] / 2.0 });
Expand All @@ -482,17 +515,6 @@ class orca_loader_app : public avk::invokee
// Stop the current composition:
avk::current_composition()->stop();
}
if (avk::input().key_pressed(avk::key_code::f1)) {
auto imguiManager = avk::current_composition()->element_by_type<avk::imgui_manager>();
if (mQuakeCam.is_enabled()) {
mQuakeCam.disable();
if (nullptr != imguiManager) { imguiManager->enable_user_interaction(true); }
}
else {
mQuakeCam.enable();
if (nullptr != imguiManager) { imguiManager->enable_user_interaction(false); }
}
}
}

void render() override
Expand All @@ -508,7 +530,9 @@ class orca_loader_app : public avk::invokee
mDestroyOldResourcesInFrame.reset();
}

auto viewProjMat = mQuakeCam.projection_matrix() * mQuakeCam.view_matrix();
auto viewProjMat = mQuakeCam.is_enabled()
? mQuakeCam.projection_and_view_matrix()
: mOrbitCam.projection_and_view_matrix();
auto emptyCmd = mViewProjBuffers[ifi]->fill(glm::value_ptr(viewProjMat), 0);

// Get a command pool to allocate command buffers from:
Expand Down Expand Up @@ -587,6 +611,7 @@ class orca_loader_app : public avk::invokee
std::optional<avk::buffer> mOldMaterialBuffer;
std::optional<avk::graphics_pipeline> mOldPipeline;

avk::orbit_camera mOrbitCam;
avk::quake_camera mQuakeCam;

glm::vec3 mRotateObjects = { 0.f, 0.f, 0.f };
Expand Down
57 changes: 40 additions & 17 deletions examples/skinned_meshlets/source/skinned_meshlets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "meshlet_helpers.hpp"
#include "model.hpp"
#include "serializer.hpp"
#include "orbit_camera.hpp"
#include "quake_camera.hpp"
#include "sequential_invoker.hpp"
/**
Expand Down Expand Up @@ -452,9 +453,13 @@ class skinned_meshlets_app : public avk::invokee
}

// Add the camera to the composition (and let it handle the updates)
mOrbitCam.set_translation({ 0.0f, -1.0f, 8.0f });
mQuakeCam.set_translation({ 0.0f, -1.0f, 8.0f });
mOrbitCam.set_perspective_projection(glm::radians(60.0f), avk::context().main_window()->aspect_ratio(), 0.3f, 1000.0f);
mQuakeCam.set_perspective_projection(glm::radians(60.0f), avk::context().main_window()->aspect_ratio(), 0.3f, 1000.0f);
avk::current_composition()->add_element(mOrbitCam);
avk::current_composition()->add_element(mQuakeCam);
mQuakeCam.disable();

auto imguiManager = avk::current_composition()->element_by_type<avk::imgui_manager>();
if (nullptr != imguiManager) {
Expand All @@ -466,7 +471,8 @@ class skinned_meshlets_app : public avk::invokee
return static_cast<double>(props.limits.timestampPeriod);
}),
lastFrameDurationMs = 0.0,
lastDrawMeshTasksDurationMs = 0.0
lastDrawMeshTasksDurationMs = 0.0,
windowHoveredLastFrame = false
]() mutable {
ImGui::Begin("Info & Settings");
ImGui::SetWindowPos(ImVec2(1.0f, 1.0f), ImGuiCond_FirstUseEver);
Expand All @@ -481,9 +487,33 @@ class skinned_meshlets_app : public avk::invokee
ImGui::Text( "mPipelineStats[0] : %llu", mPipelineStats[0]);
ImGui::Text( "mPipelineStats[1] : %llu", mPipelineStats[1]);
ImGui::Text( "mPipelineStats[2] : %llu", mPipelineStats[2]);

ImGui::Separator();
bool quakeCamEnabled = mQuakeCam.is_enabled();
if (ImGui::Checkbox("Enable Quake Camera", &quakeCamEnabled)) {
if (quakeCamEnabled) { // => should be enabled
mQuakeCam.set_matrix(mOrbitCam.matrix());
mQuakeCam.enable();
mOrbitCam.disable();
}
}
if (quakeCamEnabled) {
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), "[F1] to exit Quake Camera navigation.");
if (avk::input().key_pressed(avk::key_code::f1)) {
mOrbitCam.set_matrix(mQuakeCam.matrix());
mOrbitCam.enable();
mQuakeCam.disable();
}
}
const bool windowHoveredThisFrame = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow);
if (windowHoveredThisFrame && !windowHoveredLastFrame && mOrbitCam.is_enabled()) {
mOrbitCam.disable();
}
if (windowHoveredLastFrame && !windowHoveredThisFrame && !mQuakeCam.is_enabled()) {
mOrbitCam.enable();
}
windowHoveredLastFrame = windowHoveredThisFrame;
ImGui::Separator();
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), "[F1]: Toggle input-mode");
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), " (UI vs. scene navigation)");

ImGui::Separator();
if (mUseNvPipeline.has_value()) {
Expand Down Expand Up @@ -523,18 +553,6 @@ class skinned_meshlets_app : public avk::invokee
// Stop the current composition:
avk::current_composition()->stop();
}

if (avk::input().key_pressed(avk::key_code::f1)) {
auto imguiManager = avk::current_composition()->element_by_type<avk::imgui_manager>();
if (mQuakeCam.is_enabled()) {
mQuakeCam.disable();
if (nullptr != imguiManager) { imguiManager->enable_user_interaction(true); }
}
else {
mQuakeCam.enable();
if (nullptr != imguiManager) { imguiManager->enable_user_interaction(false); }
}
}
}

void render() override
Expand All @@ -561,7 +579,9 @@ class skinned_meshlets_app : public avk::invokee
});
}

auto viewProjMat = mQuakeCam.projection_matrix() * mQuakeCam.view_matrix();
auto viewProjMat = mQuakeCam.is_enabled()
? mQuakeCam.projection_and_view_matrix()
: mOrbitCam.projection_and_view_matrix();
auto emptyCmd = mViewProjBuffers[inFlightIndex]->fill(glm::value_ptr(viewProjMat), 0);

// Get a command pool to allocate command buffers from:
Expand Down Expand Up @@ -659,8 +679,11 @@ class skinned_meshlets_app : public avk::invokee
std::vector<data_for_draw_call> mDrawCalls;
avk::graphics_pipeline mPipelineExt;
avk::graphics_pipeline mPipelineNv;

avk::orbit_camera mOrbitCam;
avk::quake_camera mQuakeCam;
uint32_t mNumMeshlets;

uint32_t mNumMeshlets;
uint32_t mTaskInvocationsExt;
uint32_t mTaskInvocationsNv;

Expand Down
61 changes: 42 additions & 19 deletions examples/static_meshlets/source/static_meshlets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "model.hpp"
#include "serializer.hpp"
#include "sequential_invoker.hpp"
#include "orbit_camera.hpp"
#include "quake_camera.hpp"
#include "vk_convenience_functions.hpp"

Expand Down Expand Up @@ -315,9 +316,13 @@ class static_meshlets_app : public avk::invokee
});

// Add the camera to the composition (and let it handle the updates)
mQuakeCam.set_translation({ 0.0f, 0.0f, 0.0f });
mQuakeCam.set_perspective_projection(glm::radians(60.0f), avk::context().main_window()->aspect_ratio(), 0.3f, 1000.0f);
mOrbitCam.set_translation({ 0.0f, 0.0f, 8.0f });
mQuakeCam.set_translation({ 0.0f, 0.0f, 8.0f });
mOrbitCam.set_perspective_projection(glm::radians(45.0f), avk::context().main_window()->aspect_ratio(), 0.3f, 1000.0f);
mQuakeCam.set_perspective_projection(glm::radians(45.0f), avk::context().main_window()->aspect_ratio(), 0.3f, 1000.0f);
avk::current_composition()->add_element(mOrbitCam);
avk::current_composition()->add_element(mQuakeCam);
mQuakeCam.disable();

auto imguiManager = avk::current_composition()->element_by_type<avk::imgui_manager>();
if (nullptr != imguiManager) {
Expand All @@ -329,7 +334,8 @@ class static_meshlets_app : public avk::invokee
return static_cast<double>(props.limits.timestampPeriod);
}),
lastFrameDurationMs = 0.0,
lastDrawMeshTasksDurationMs = 0.0
lastDrawMeshTasksDurationMs = 0.0,
windowHoveredLastFrame = false
]() mutable {
ImGui::Begin("Info & Settings");
ImGui::SetWindowPos(ImVec2(1.0f, 1.0f), ImGuiCond_FirstUseEver);
Expand All @@ -344,9 +350,33 @@ class static_meshlets_app : public avk::invokee
ImGui::Text( "mPipelineStats[0] : %llu", mPipelineStats[0]);
ImGui::Text( "mPipelineStats[1] : %llu", mPipelineStats[1]);
ImGui::Text( "mPipelineStats[2] : %llu", mPipelineStats[2]);

ImGui::Separator();
bool quakeCamEnabled = mQuakeCam.is_enabled();
if (ImGui::Checkbox("Enable Quake Camera", &quakeCamEnabled)) {
if (quakeCamEnabled) { // => should be enabled
mQuakeCam.set_matrix(mOrbitCam.matrix());
mQuakeCam.enable();
mOrbitCam.disable();
}
}
if (quakeCamEnabled) {
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), "[F1] to exit Quake Camera navigation.");
if (avk::input().key_pressed(avk::key_code::f1)) {
mOrbitCam.set_matrix(mQuakeCam.matrix());
mOrbitCam.enable();
mQuakeCam.disable();
}
}
const bool windowHoveredThisFrame = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow);
if (windowHoveredThisFrame && !windowHoveredLastFrame && mOrbitCam.is_enabled()) {
mOrbitCam.disable();
}
if (windowHoveredLastFrame && !windowHoveredThisFrame && !mQuakeCam.is_enabled()) {
mOrbitCam.enable();
}
windowHoveredLastFrame = windowHoveredThisFrame;
ImGui::Separator();
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), "[F1]: Toggle input-mode");
ImGui::TextColored(ImVec4(0.f, .6f, .8f, 1.f), " (UI vs. scene navigation)");

ImGui::Separator();
if (mUseNvPipeline.has_value()) {
Expand Down Expand Up @@ -386,18 +416,6 @@ class static_meshlets_app : public avk::invokee
// Stop the current composition:
avk::current_composition()->stop();
}

if (avk::input().key_pressed(avk::key_code::f1)) {
auto imguiManager = avk::current_composition()->element_by_type<avk::imgui_manager>();
if (mQuakeCam.is_enabled()) {
mQuakeCam.disable();
if (nullptr != imguiManager) { imguiManager->enable_user_interaction(true); }
}
else {
mQuakeCam.enable();
if (nullptr != imguiManager) { imguiManager->enable_user_interaction(false); }
}
}
}

void render() override
Expand All @@ -407,7 +425,9 @@ class static_meshlets_app : public avk::invokee
auto mainWnd = context().main_window();
auto inFlightIndex = mainWnd->current_in_flight_index();

auto viewProjMat = mQuakeCam.projection_matrix() * mQuakeCam.view_matrix();
auto viewProjMat = mQuakeCam.is_enabled()
? mQuakeCam.projection_and_view_matrix()
: mOrbitCam.projection_and_view_matrix();
auto emptyCmd = mViewProjBuffers[inFlightIndex]->fill(glm::value_ptr(viewProjMat), 0);

// Get a command pool to allocate command buffers from:
Expand Down Expand Up @@ -495,8 +515,11 @@ mViewProjBuffers[inFlightIndex]->fill(glm::value_ptr(viewProjMat), 0),
std::vector<data_for_draw_call> mDrawCalls;
avk::graphics_pipeline mPipelineExt;
avk::graphics_pipeline mPipelineNv;

avk::orbit_camera mOrbitCam;
avk::quake_camera mQuakeCam;
uint32_t mNumMeshlets;

uint32_t mNumMeshlets;
uint32_t mTaskInvocationsExt;
uint32_t mTaskInvocationsNv;

Expand Down

0 comments on commit 96d5bfa

Please sign in to comment.