70 lines
1.6 KiB
C++
70 lines
1.6 KiB
C++
#include "InputSystem.h"
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <glm.hpp>
|
|
|
|
#include <ext.hpp>
|
|
|
|
void InputSystem::ObserveKey(int key) {
|
|
_keyCodeDictionaryObserver.emplace(key, KeyObserver(_window, key));
|
|
}
|
|
|
|
void InputSystem::Update() {
|
|
for(auto& observer : _keyCodeDictionaryObserver) {
|
|
observer.second.Update();
|
|
}
|
|
}
|
|
|
|
void InputSystem::SetWindow(GLFWwindow* window) {
|
|
_window = window;
|
|
}
|
|
|
|
bool InputSystem::IsLeftMouseDown() const
|
|
{
|
|
return glfwGetMouseButton(_window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
|
|
}
|
|
|
|
bool InputSystem::IsRightMouseDown() const
|
|
{
|
|
return glfwGetMouseButton(_window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS;
|
|
}
|
|
|
|
void InputSystem::GetMousePos(glm::vec2& position) const
|
|
{
|
|
double x, y;
|
|
|
|
glfwGetCursorPos(_window, &x, &y);
|
|
|
|
position.x = x;
|
|
position.y = y;
|
|
}
|
|
|
|
void InputSystem::GetPickingRay(const glm::mat4& transform, glm::vec3& startingPoint, glm::vec3& direction) const {
|
|
glm::vec2 screenSpacePosition;
|
|
GetMousePos(screenSpacePosition);
|
|
|
|
int screenWidth, screenHeight;
|
|
glfwGetFramebufferSize(_window, &screenWidth, &screenHeight);
|
|
|
|
glm::vec2 clipSpacePosition = glm::vec2(
|
|
( screenSpacePosition.x / screenWidth ) * 2.0 - 1.0,
|
|
1.0 - ( screenSpacePosition.y / screenHeight ) * 2.0
|
|
);
|
|
|
|
glm::vec4 nearPoint = glm::vec4(clipSpacePosition.x, clipSpacePosition.y, 0.01f, 1.0f);
|
|
glm::vec4 farPoint = nearPoint;
|
|
farPoint.z = 0.99f;
|
|
|
|
glm::mat4 inverse = glm::inverse(transform);
|
|
nearPoint = inverse * nearPoint;
|
|
farPoint = inverse * farPoint;
|
|
|
|
nearPoint /= nearPoint.w;
|
|
farPoint /= farPoint.w;
|
|
|
|
startingPoint = nearPoint;
|
|
direction = farPoint - nearPoint;
|
|
} |