FPS Control working, minor UI cleanup.

master
Martin Felis 2021-10-29 15:52:32 +02:00
parent 23d7cf03f6
commit 1848fd14cf
3 changed files with 143 additions and 93 deletions

View File

@ -23,6 +23,14 @@ inline void CameraState_Init(CameraState* camera_state) {
camera_state->up[1] = 1.f; camera_state->up[1] = 1.f;
camera_state->up[2] = 0.f; camera_state->up[2] = 0.f;
camera_state->pos[0] = 0.f;
camera_state->pos[1] = 0.f;
camera_state->pos[2] = 0.f;
camera_state->vel[0] = 0.f;
camera_state->vel[1] = 0.f;
camera_state->vel[2] = 0.f;
memcpy(&camera_state->mtxView, &mtx_identity, sizeof(camera_state->mtxView)); memcpy(&camera_state->mtxView, &mtx_identity, sizeof(camera_state->mtxView));
CameraState_CalcFromMatrix(camera_state, &camera_state->mtxView); CameraState_CalcFromMatrix(camera_state, &camera_state->mtxView);
} }
@ -93,9 +101,9 @@ inline void CameraState_Update(
camera_state->heading -= dt * mouse_dx * mouse_sensitivity * M_PI / 180.f; camera_state->heading -= dt * mouse_dx * mouse_sensitivity * M_PI / 180.f;
if (camera_state->heading < -M_PI) { if (camera_state->heading < -M_PI) {
camera_state->heading += M_PI; camera_state->heading += M_PI * 2.f;
} else if (camera_state->heading > M_PI) { } else if (camera_state->heading > M_PI) {
camera_state->heading -= M_PI; camera_state->heading -= M_PI * 2.f;
} }
camera_state->pitch += dt * mouse_dy * mouse_sensitivity * M_PI / 180.f; camera_state->pitch += dt * mouse_dy * mouse_sensitivity * M_PI / 180.f;
if (camera_state->pitch < -M_PI * 0.49) { if (camera_state->pitch < -M_PI * 0.49) {

View File

@ -118,8 +118,6 @@ void simulator_reset() {
void simulator_gui() { void simulator_gui() {
ZoneScoped; ZoneScoped;
ImGui::Begin("Simulator");
sStateHistoryCurrent = sStateHistoryCurrent =
std::min(sStateHistoryCurrent, sthstry_get_num_states(sStateHistory) - 1); std::min(sStateHistoryCurrent, sthstry_get_num_states(sStateHistory) - 1);
@ -184,8 +182,6 @@ void simulator_gui() {
ImGui::PopID(); ImGui::PopID();
body.updateCollisionShapes(); body.updateCollisionShapes();
} }
ImGui::End();
} }
void simulator_update(double dt) { void simulator_update(double dt) {

View File

@ -4,10 +4,10 @@
// clang-format on // clang-format on
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <csignal>
#include <unistd.h> #include <unistd.h>
#include <Tracy.hpp> #include <Tracy.hpp>
#include <csignal>
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
@ -20,10 +20,10 @@
#define IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS
#endif #endif
#include "imgui_internal.h" #include "imgui_internal.h"
#include "render_utils.h"
#include "simulator.h" #include "simulator.h"
#include "srender.h" #include "srender.h"
#include "utils.h" #include "utils.h"
#include "render_utils.h"
GLFWwindow* gWindow = nullptr; GLFWwindow* gWindow = nullptr;
GuiInputState* gGuiInputState = nullptr; GuiInputState* gGuiInputState = nullptr;
@ -64,6 +64,14 @@ bool gGizmoEnabled = true;
enum ControlMode { ControlModeNone = 0, ControlModeFirstPersonView = 1 }; enum ControlMode { ControlModeNone = 0, ControlModeFirstPersonView = 1 };
ControlMode gControlMode = ControlModeNone; ControlMode gControlMode = ControlModeNone;
struct Dialogs {
bool mCameraDialog = false;
bool mImGuiDemo = false;
bool mSimulator = false;
bool mImGuizmoTransform = false;
};
Dialogs* gDialogs = nullptr;
using namespace std; using namespace std;
static void error_callback(int error, const char* description) { static void error_callback(int error, const char* description) {
@ -254,78 +262,82 @@ void ShowTransformManip(
window_size.x -= imgui_style.WindowPadding.x * 2; window_size.x -= imgui_style.WindowPadding.x * 2;
window_size.y -= imgui_style.WindowPadding.y * 2 + title_height; window_size.y -= imgui_style.WindowPadding.y * 2 + title_height;
ImGui::Begin("Guizmo"); if (gDialogs->mImGuizmoTransform) {
ImGui::Begin("Guizmo", &gDialogs->mImGuizmoTransform);
if (editTransformDecomposition) { if (editTransformDecomposition) {
if (ImGui::IsKeyPressed(90)) mCurrentGizmoOperation = ImGuizmo::TRANSLATE; if (ImGui::IsKeyPressed(90)) mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
if (ImGui::IsKeyPressed(69)) mCurrentGizmoOperation = ImGuizmo::ROTATE; if (ImGui::IsKeyPressed(69)) mCurrentGizmoOperation = ImGuizmo::ROTATE;
if (ImGui::IsKeyPressed(82)) // r Key if (ImGui::IsKeyPressed(82)) // r Key
mCurrentGizmoOperation = ImGuizmo::SCALE; mCurrentGizmoOperation = ImGuizmo::SCALE;
if (ImGui::RadioButton( if (ImGui::RadioButton(
"Translate", "Translate",
mCurrentGizmoOperation == ImGuizmo::TRANSLATE)) mCurrentGizmoOperation == ImGuizmo::TRANSLATE))
mCurrentGizmoOperation = ImGuizmo::TRANSLATE; mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
ImGui::SameLine();
if (ImGui::RadioButton(
"Rotate",
mCurrentGizmoOperation == ImGuizmo::ROTATE))
mCurrentGizmoOperation = ImGuizmo::ROTATE;
ImGui::SameLine();
if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE))
mCurrentGizmoOperation = ImGuizmo::SCALE;
if (ImGui::RadioButton(
"Universal",
mCurrentGizmoOperation == ImGuizmo::UNIVERSAL))
mCurrentGizmoOperation = ImGuizmo::UNIVERSAL;
float matrixTranslation[3], matrixRotation[3], matrixScale[3];
ImGuizmo::DecomposeMatrixToComponents(
matrix,
matrixTranslation,
matrixRotation,
matrixScale);
ImGui::InputFloat3("Tr", matrixTranslation);
ImGui::InputFloat3("Rt", matrixRotation);
ImGui::InputFloat3("Sc", matrixScale);
ImGuizmo::RecomposeMatrixFromComponents(
matrixTranslation,
matrixRotation,
matrixScale,
matrix);
if (mCurrentGizmoOperation != ImGuizmo::SCALE) {
if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL))
mCurrentGizmoMode = ImGuizmo::LOCAL;
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD)) if (ImGui::RadioButton(
mCurrentGizmoMode = ImGuizmo::WORLD; "Rotate",
} mCurrentGizmoOperation == ImGuizmo::ROTATE))
if (ImGui::IsKeyPressed(83)) useSnap = !useSnap; mCurrentGizmoOperation = ImGuizmo::ROTATE;
ImGui::Checkbox("", &useSnap);
ImGui::SameLine();
switch (mCurrentGizmoOperation) {
case ImGuizmo::TRANSLATE:
ImGui::InputFloat3("Snap", &snap[0]);
break;
case ImGuizmo::ROTATE:
ImGui::InputFloat("Angle Snap", &snap[0]);
break;
case ImGuizmo::SCALE:
ImGui::InputFloat("Scale Snap", &snap[0]);
break;
}
ImGui::Checkbox("Bound Sizing", &boundSizing);
if (boundSizing) {
ImGui::PushID(3);
ImGui::Checkbox("", &boundSizingSnap);
ImGui::SameLine(); ImGui::SameLine();
ImGui::InputFloat3("Snap", boundsSnap); if (ImGui::RadioButton(
ImGui::PopID(); "Scale",
mCurrentGizmoOperation == ImGuizmo::SCALE))
mCurrentGizmoOperation = ImGuizmo::SCALE;
if (ImGui::RadioButton(
"Universal",
mCurrentGizmoOperation == ImGuizmo::UNIVERSAL))
mCurrentGizmoOperation = ImGuizmo::UNIVERSAL;
float matrixTranslation[3], matrixRotation[3], matrixScale[3];
ImGuizmo::DecomposeMatrixToComponents(
matrix,
matrixTranslation,
matrixRotation,
matrixScale);
ImGui::InputFloat3("Tr", matrixTranslation);
ImGui::InputFloat3("Rt", matrixRotation);
ImGui::InputFloat3("Sc", matrixScale);
ImGuizmo::RecomposeMatrixFromComponents(
matrixTranslation,
matrixRotation,
matrixScale,
matrix);
if (mCurrentGizmoOperation != ImGuizmo::SCALE) {
if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL))
mCurrentGizmoMode = ImGuizmo::LOCAL;
ImGui::SameLine();
if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD))
mCurrentGizmoMode = ImGuizmo::WORLD;
}
if (ImGui::IsKeyPressed(83)) useSnap = !useSnap;
ImGui::Checkbox("", &useSnap);
ImGui::SameLine();
switch (mCurrentGizmoOperation) {
case ImGuizmo::TRANSLATE:
ImGui::InputFloat3("Snap", &snap[0]);
break;
case ImGuizmo::ROTATE:
ImGui::InputFloat("Angle Snap", &snap[0]);
break;
case ImGuizmo::SCALE:
ImGui::InputFloat("Scale Snap", &snap[0]);
break;
}
ImGui::Checkbox("Bound Sizing", &boundSizing);
if (boundSizing) {
ImGui::PushID(3);
ImGui::Checkbox("", &boundSizingSnap);
ImGui::SameLine();
ImGui::InputFloat3("Snap", boundsSnap);
ImGui::PopID();
}
} }
ImGui::End();
} }
ImGui::End();
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
float viewManipulateRight = io.DisplaySize.x; float viewManipulateRight = io.DisplaySize.x;
float viewManipulateTop = 0; float viewManipulateTop = 0;
@ -407,7 +419,12 @@ void DoRender() {
if (gViewMode == ViewModeSimulation) { if (gViewMode == ViewModeSimulation) {
simulator_draw(gRndrCmds); simulator_draw(gRndrCmds);
simulator_gui();
if (gDialogs->mSimulator) {
ImGui::Begin("Simulator", &gDialogs->mSimulator);
simulator_gui();
ImGui::End();
}
} else { } else {
RenderGuizmoTest(gRndrCmds); RenderGuizmoTest(gRndrCmds);
} }
@ -426,9 +443,10 @@ void DoRender() {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
ShowViewManip(); ShowViewManip();
ShowTransformManip((float*)&(gCameraState->mtxView), ShowTransformManip(
(float*)&(gProjMatrix), (float*)&(gCameraState->mtxView),
(float*)(&gTransformMatrix)); (float*)&(gProjMatrix),
(float*)(&gTransformMatrix));
ImGui::End(); ImGui::End();
} }
@ -516,6 +534,9 @@ int main(void) {
CameraState_Init(&camera_state); CameraState_Init(&camera_state);
gCameraState = &camera_state; gCameraState = &camera_state;
Dialogs dialogs;
gDialogs = &dialogs;
gRndr = srndr_create(); gRndr = srndr_create();
gView = srview_create(); gView = srview_create();
gRndrCmds = srcmdbuf_create(1024); gRndrCmds = srcmdbuf_create(1024);
@ -575,11 +596,13 @@ int main(void) {
ImGui::BeginMainMenuBar(); ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("Dialogs")) { if (ImGui::BeginMenu("Dialogs")) {
ImGui::Checkbox("ImGui Demo", &show_demo_window); ImGui::Checkbox("Camera", &gDialogs->mCameraDialog);
ImGui::Checkbox("Simulator", &gDialogs->mSimulator);
ImGui::Checkbox("ImGuizmo Transform", &gDialogs->mImGuizmoTransform);
ImGui::Checkbox("ImGui Demo", &gDialogs->mImGuiDemo);
ImGui::EndMenu(); ImGui::EndMenu();
} }
float menu_bar_height = ImGui::GetWindowHeight(); float menu_bar_height = ImGui::GetWindowHeight();
ImGui::EndMainMenuBar(); ImGui::EndMainMenuBar();
@ -616,26 +639,49 @@ int main(void) {
camera_accel[2] += 100.f; camera_accel[2] += 100.f;
} }
CameraState_Update(gCameraState, frame_delta_time, gGuiInputState->mousedX, gGuiInputState->mousedY, camera_accel); CameraState_Update(
gCameraState,
frame_delta_time,
gGuiInputState->mousedX,
gGuiInputState->mousedY,
camera_accel);
} }
ImGui::Begin("Camera Controls"); if (gDialogs->mCameraDialog) {
ImGui::Begin("Camera Controls", &gDialogs->mCameraDialog);
bool changed = false; bool changed = false;
changed = ImGui::SliderFloat("Heading", &gCameraState->heading, -M_PI * 0.98, M_PI * 0.98); changed = ImGui::SliderFloat(
changed = ImGui::SliderFloat("Pitch", &gCameraState->pitch, -M_PI * 0.49, M_PI * 0.49) || changed; "Heading",
&gCameraState->heading,
-M_PI * 0.98,
M_PI * 0.98);
changed = ImGui::SliderFloat(
"Pitch",
&gCameraState->pitch,
-M_PI * 0.49,
M_PI * 0.49)
|| changed;
changed = ImGui::SliderFloat3("Forward", gCameraState->forward, -1.f, 1.f) || changed; changed =
changed = ImGui::SliderFloat3("Right", gCameraState->right, -1.f, 1.f) || changed; ImGui::SliderFloat3("Forward", gCameraState->forward, -1.f, 1.f)
changed = ImGui::SliderFloat3("Up", gCameraState->up, -1.f, 1.f) || changed; || changed;
changed = ImGui::SliderFloat3("Pos", gCameraState->pos, -100.f, 100.f) || changed; changed = ImGui::SliderFloat3("Right", gCameraState->right, -1.f, 1.f)
|| changed;
changed =
ImGui::SliderFloat3("Up", gCameraState->up, -1.f, 1.f) || changed;
changed = ImGui::SliderFloat3("Pos", gCameraState->pos, -100.f, 100.f)
|| changed;
if (changed) { if (changed) {
CameraState_CalcToMatrix(gCameraState, &gCameraState->mtxView); CameraState_CalcToMatrix(gCameraState, &gCameraState->mtxView);
}
ImGui::End();
} }
ImGui::End();
if (show_demo_window) ImGui::ShowDemoWindow(); if (gDialogs->mImGuiDemo) {
ImGui::ShowDemoWindow(&gDialogs->mImGuiDemo);
}
ShowDockspace(true); ShowDockspace(true);