Files
CDAG/Research/core/Controls.cpp

212 lines
6.8 KiB
C++

#include "../inc/gl/glfw3.h"
#include "../inc/glm/glm.hpp"
#include "../Renderer.h"
#include "Defines.h"
#include "Camera.h"
#include "Controls.h"
Controls* Controls::mInstance = NULL;
const float Controls::RUNNINGSPEEDRATIO = 2.f;
const float Controls::CRAWLSPEEDRATIO = 0.5f;
const float Controls::SPEEDINCREASERATIO = 1.25f;
void Controls::Create(GLFWwindow* window, bool fullscreen) {
if (mInstance == NULL)
mInstance = new Controls(window, fullscreen);
}
void Controls::Destroy() {
if (mInstance != NULL)
delete mInstance;
mInstance = NULL;
}
Controls* Controls::Instance() {
return mInstance;
}
void Controls::Run() {
if (mLastTime == 0)
mLastTime = float(glfwGetTime());
mCurrentTime = float(glfwGetTime());
mDeltaTime = mCurrentTime - mLastTime;
mLastTime = mCurrentTime;
mOldMouseX = mMouseX;
mOldMouseY = mMouseY;
glfwGetCursorPos(mWindow, &mMouseX, &mMouseY);
ListenToMouse();
ListenToKeys();
}
void Controls::ListenToMouse() {
if (!mDragMouse || glfwGetMouseButton(mWindow, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
mCamera->UpdateMouse(glm::vec2(mOldMouseX - mMouseX, mOldMouseY - mMouseY), mDeltaTime);
}
void Controls::ListenToKeys() {
if (CheckKey(GLFW_KEY_ESCAPE))//QT
mState = State::EXIT;
if (CheckKey(GLFW_KEY_SPACE, true))//QT
mState = mState == State::PAUSED ? State::RUNNING : State::PAUSED;
glm::vec3 movement(0);
movement.x += CheckKey(GLFW_KEY_UP) || CheckKey(GLFW_KEY_W);//QT
movement.x -= CheckKey(GLFW_KEY_DOWN) || CheckKey(GLFW_KEY_S);
movement.y += CheckKey(GLFW_KEY_RIGHT) || CheckKey(GLFW_KEY_D);
movement.y -= CheckKey(GLFW_KEY_LEFT) || CheckKey(GLFW_KEY_A);
movement.z += CheckKey(GLFW_KEY_Q);
movement.z -= CheckKey(GLFW_KEY_E);
if (CheckKey(GLFW_KEY_LEFT_SHIFT))
movement *= RUNNINGSPEEDRATIO;
if (CheckKey(GLFW_KEY_LEFT_CONTROL))
movement *= CRAWLSPEEDRATIO;
if (CheckKey(GLFW_KEY_MINUS, true))
{
mCamera->SetMovingSpeed(mCamera->GetMovingSpeed() * (1.f / SPEEDINCREASERATIO));
//mCamera->SetRotationSpeed(mCamera->GetRotationSpeed() * (1.f / SPEEDINCREASERATIO));
}
if (CheckKey(GLFW_KEY_EQUAL, true))
{
mCamera->SetMovingSpeed(mCamera->GetMovingSpeed() * SPEEDINCREASERATIO);
//mCamera->SetRotationSpeed(mCamera->GetRotationSpeed() * SPEEDINCREASERATIO);
}
mCamera->UpdateKeys(movement, mDeltaTime);
if (CheckKey(GLFW_KEY_C, true)) // C: Toggle camera mode
mCamera->ToggleMode();
if (CheckKey(GLFW_KEY_X, true)) // X: Reset the camera to original position
mCamera->Reset();
if (CheckKey(GLFW_KEY_L, true)) // L: Update light direction
Renderer::Instance()->UpdateLightDirection();
if (CheckKey(GLFW_KEY_K, true)) // K: Turn on/off light
Renderer::Instance()->ToggleLight();
if (CheckKey(GLFW_KEY_J, true)) // J: Toggle AO mode
Renderer::Instance()->ToggleAOMode();
if (CheckKey(GLFW_KEY_H, true)) // H: Toggle reflections
Renderer::Instance()->ToggleReflections();
if (CheckKey(GLFW_KEY_R, true)) // R: Reload the shaders
Renderer::Instance()->ReloadShaders();
if (CheckKey(GLFW_KEY_F, true)) // F: Toggles fullscreen
{
mFullScreen = !mFullScreen;
Renderer::Instance()->ToggleFullscreen(mFullScreen);
return;
}
if (CheckKey(GLFW_KEY_LEFT_BRACKET, true)) // [: Decreases the octree rendering depth (for improved performance and debugging)
Renderer::Instance()->DecreaseOctreeRenderLevel();
if (CheckKey(GLFW_KEY_RIGHT_BRACKET, true)) // ]: Increases the octree render depth
Renderer::Instance()->IncreaseOctreeRenderLevel();
if (CheckKey(GLFW_KEY_P, true)) // P: take a screenshot
Renderer::Instance()->TakeScreenshot();
if (CheckKey(GLFW_KEY_V, true)) // V: Starts/Stops recording a camera path
Renderer::Instance()->ToggleRecording();
if (CheckKey(GLFW_KEY_I, true)) // I: Clear the lightpath recording
Renderer::Instance()->ClearLightPath();
if (CheckKey(GLFW_KEY_O, true)) // O: Add node to the lightpath
Renderer::Instance()->AddLightPathNode();
if (CheckKey(GLFW_KEY_T, true))
{
if (CheckKey(GLFW_KEY_LEFT_CONTROL)) // Ctrl + T: Replace recording by rotation recording
Renderer::Instance()->SetRotationRecording();
else // T: Clear the recording
Renderer::Instance()->ClearCameraPath();
}
if (CheckKey(GLFW_KEY_Y, true)) // Y: Add node to the recording path, Ctrl+Y removes the last node from the recording
{
if (CheckKey(GLFW_KEY_LEFT_CONTROL))
Renderer::Instance()->RemoveCameraPathNode();
else
Renderer::Instance()->AddCameraPathNode();
}
if (CheckKey(GLFW_KEY_B, true)) // B: Stores a recording (screenshot at each position along a path)
Renderer::Instance()->StoreRecording();
if (CheckKey(GLFW_KEY_G, true)) // G: Starts/Stops a benchmark (plays back recording and stores all frame times)
Renderer::Instance()->RunBenchmark();
if (CheckKey(GLFW_KEY_N, true)) // N: Stores the current viewpoint to a file
Renderer::Instance()->SaveLastViewpoint();
if (CheckKey(GLFW_KEY_M, true)) // M: Loads the last stored viewpoint
Renderer::Instance()->LoadLastViewpoint();
// 0-9: Go to one of the preset locations from settings
if (CheckKey(GLFW_KEY_0, true)) Renderer::Instance()->SetPresetLocation(0);
if (CheckKey(GLFW_KEY_1, true)) Renderer::Instance()->SetPresetLocation(1);
if (CheckKey(GLFW_KEY_2, true)) Renderer::Instance()->SetPresetLocation(2);
if (CheckKey(GLFW_KEY_3, true)) Renderer::Instance()->SetPresetLocation(3);
if (CheckKey(GLFW_KEY_4, true)) Renderer::Instance()->SetPresetLocation(4);
if (CheckKey(GLFW_KEY_5, true)) Renderer::Instance()->SetPresetLocation(5);
if (CheckKey(GLFW_KEY_6, true)) Renderer::Instance()->SetPresetLocation(6);
if (CheckKey(GLFW_KEY_7, true)) Renderer::Instance()->SetPresetLocation(7);
if (CheckKey(GLFW_KEY_8, true)) Renderer::Instance()->SetPresetLocation(8);
if (CheckKey(GLFW_KEY_9, true)) Renderer::Instance()->SetPresetLocation(9);
}
//************************************
// Check if key is pressed; for lock-sensitive keys only return true if key unlocked
//************************************
bool Controls::CheckKey(int key, bool lock) {
bool result = glfwGetKey(mWindow, key) == GLFW_PRESS && (!lock || !mLocked[key]);
if (glfwGetKey(mWindow, key) == GLFW_RELEASE)
mLocked[key] = false;
if (glfwGetKey(mWindow, key) == GLFW_PRESS)
mLocked[key] = true;
return result;
}
void Controls::Scroll(GLFWwindow* window, double xoffset, double yoffset) {
Controls* controller = Controls::mInstance;
controller->mCamera->UpdateScroll(glm::vec2(xoffset, yoffset), controller->mDeltaTime);
};
Controls::State Controls::GetState() {
return mState;
}
Controls::Controls(GLFWwindow* window, bool fullscreen) {
mWindow = window;
mState = State::RUNNING;//QT
mFullScreen = fullscreen;//QT
mLocked.resize(512);
mMouseX = 0.5;
mMouseY = 0.5;
mDragMouse = true;//QT
mLastTime = 0;
mCurrentTime = 0;
mDeltaTime = 0;
Camera::Create();
mCamera = Camera::Instance();
glfwSetScrollCallback(mWindow, this->Scroll);
}
Controls::~Controls() {
mLocked.clear();
}