Skip to content

Commit

Permalink
Tweaked orbit_camera and enabled both cameras in the model_loader exa…
Browse files Browse the repository at this point in the history
…mple.
  • Loading branch information
johannesugb committed Jun 1, 2023
1 parent f6ae81e commit 8e06afe
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
9 changes: 3 additions & 6 deletions auto_vk_toolkit/src/orbit_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ namespace avk

void orbit_camera::on_enable()
{
composition_interface::current()->input().set_cursor_mode(cursor::hand_cursor);
calculate_lateral_speed();
}

void orbit_camera::on_disable()
{
composition_interface::current()->input().set_cursor_mode(cursor::arrow_cursor);
}

void orbit_camera::update()
Expand All @@ -51,10 +49,10 @@ namespace avk
const auto rotSpeed = mRotationSpeed
* ((input().key_down(key_code::left_shift) || input().key_down(key_code::right_shift)) ? mFastMultiplier : 1.f)
* ((input().key_down(key_code::left_control) || input().key_down(key_code::right_control)) ? mSlowMultiplier : 1.f);
glm::quat rotHoriz = glm::angleAxis( rotSpeed * static_cast<float>(mouseMoved.x), glm::vec3(0.f, 1.f, 0.f));
glm::quat rotVert = glm::angleAxis(-rotSpeed * static_cast<float>(mouseMoved.y), glm::vec3(1.f, 0.f, 0.f));
glm::quat rotHoriz = glm::angleAxis(rotSpeed * static_cast<float>(mouseMoved.x), up());
glm::quat rotVert = glm::angleAxis(rotSpeed * static_cast<float>(mouseMoved.y), right(*this));
const auto oldPos = back(*this) * mPivotDistance;
const auto newPos = rotHoriz * oldPos * rotVert;
const auto newPos = rotHoriz * (rotVert * oldPos);
translate(*this, newPos - oldPos);
look_along(-newPos);
}
Expand Down Expand Up @@ -107,6 +105,5 @@ namespace avk
auto resy = nullptr != wnd ? static_cast<float>(wnd->resolution().y) : 1000.f;
resy *= 0.5f;
mLateralSpeed = glm::abs(mPivotDistance / projection_matrix()[1][1]) / resy;
LOG_INFO(fmt::format("mPivotDistance[{}], mLateralSpeed[{}]", mPivotDistance - mNear, mLateralSpeed));
}
}
91 changes: 57 additions & 34 deletions examples/model_loader/source/model_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class model_loader_app : public avk::invokee

mUpdater->on(avk::swapchain_resized_event(avk::context().main_window())).invoke([this]() {
this->mQuakeCam.set_aspect_ratio(avk::context().main_window()->aspect_ratio());
this->mOrbitCam.set_aspect_ratio(avk::context().main_window()->aspect_ratio());
});

//first make sure render pass is updated
Expand All @@ -209,22 +210,54 @@ class model_loader_app : public avk::invokee
).update(mPipeline);


// Add the camera to the composition (and let it handle the updates)
// Add the cameras to the composition (and let them handle 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();

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{
bool isEnabled = this->is_enabled();
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("Scale", glm::value_ptr(mScale), 0.005f, 0.01f, 10.0f);
ImGui::Checkbox("Enable/Disable invokee", &isEnabled);
if (isEnabled != this->is_enabled())
Expand All @@ -250,7 +283,9 @@ class model_loader_app : public avk::invokee
auto mainWnd = avk::context().main_window();
auto ifi = 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[ifi]->fill(glm::value_ptr(viewProjMat), 0);

// Get a command pool to allocate command buffers from:
Expand Down Expand Up @@ -352,34 +387,22 @@ class model_loader_app : public avk::invokee
mQuakeCam.look_at(glm::vec3{0.0f, 0.0f, 0.0f});
}

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); }
// Automatic camera path:
if (avk::input().key_pressed(avk::key_code::c)) {
if (avk::input().key_down(avk::key_code::left_shift)) { // => disable
if (mCameraPath.has_value()) {
avk::current_composition()->remove_element_immediately(mCameraPath.value());
mCameraPath.reset();
}
}
else {
mQuakeCam.enable();
if (nullptr != imguiManager) { imguiManager->enable_user_interaction(false); }
else { // => enable
if (mCameraPath.has_value()) {
avk::current_composition()->remove_element_immediately(mCameraPath.value());
}
mCameraPath.emplace(mQuakeCam);
avk::current_composition()->add_element(mCameraPath.value());
}
}

//// Automatic camera path:
//if (avk::input().key_pressed(avk::key_code::c)) {
// if (avk::input().key_down(avk::key_code::left_shift)) { // => disable
// if (mCameraPath.has_value()) {
// avk::current_composition()->remove_element_immediately(mCameraPath.value());
// mCameraPath.reset();
// }
// }
// else { // => enable
// if (mCameraPath.has_value()) {
// avk::current_composition()->remove_element_immediately(mCameraPath.value());
// }
// mCameraPath.emplace(mQuakeCam);
// avk::current_composition()->add_element(mCameraPath.value());
// }
//}
}

private: // v== Member variables ==v
Expand All @@ -395,10 +418,10 @@ class model_loader_app : public avk::invokee

std::vector<data_for_draw_call> mDrawCalls;
avk::graphics_pipeline mPipeline;
avk::orbit_camera mQuakeCam;

glm::vec3 mScale;
glm::vec3 mScale;

avk::orbit_camera mOrbitCam;
avk::quake_camera mQuakeCam;
std::optional<camera_path> mCameraPath;

// imgui elements
Expand Down

0 comments on commit 8e06afe

Please sign in to comment.