Camera movement

This commit is contained in:
legatt 2026-02-10 16:36:16 +03:00
parent 3f965b2393
commit b3d74c9cd7
7 changed files with 78 additions and 8 deletions

View File

@ -9,7 +9,7 @@ export namespace ee_engine
struct InputHandlers struct InputHandlers
{ {
std::function<void(float dx, float dy)> zooming; std::function<void(float dx, float dy)> zooming;
std::function<void(float dx, float dy)> mapScroll; std::function<void(float x, float y, float lastX, float lastY)> mapScroll;
}; };
class IInput class IInput
@ -21,6 +21,9 @@ export namespace ee_engine
protected: protected:
InputHandlers handlers; InputHandlers handlers;
float mouseX = 0.0f, mouseY = 0.0f;
float lastMouseX = 0.0f, lastMouseY = 0.0f;
explicit IInput(const InputHandlers &handlers) {this->handlers = handlers;}; explicit IInput(const InputHandlers &handlers) {this->handlers = handlers;};
}; };
} }

View File

@ -69,7 +69,8 @@ export namespace ee_engine
void SetCamera(const CameraTransform &camera); void SetCamera(const CameraTransform &camera);
constexpr CameraTransform getCamera() const noexcept {return camera;}; constexpr CameraTransform getCamera() const noexcept {return camera;};
glm::vec2 ScreenToWorld(const float screenx, const float screenY);
glm::vec2 ScreenToWorld(const glm::vec2 &screenPos);
protected: protected:
@ -78,6 +79,7 @@ export namespace ee_engine
float screenAspect; float screenAspect;
CameraTransform camera; CameraTransform camera;
glm::mat4 viewProjection; glm::mat4 viewProjection;
glm::mat4 viewProjectionInversed;
GLFWwindow *window; GLFWwindow *window;
MapMesh *mapMesh; MapMesh *mapMesh;

View File

@ -72,10 +72,28 @@ void ee_engine::IRenderer::SetCamera(const CameraTransform &camera)
); );
viewProjection = P * V; viewProjection = P * V;
viewProjectionInversed = glm::inverse(viewProjection);
ApplyCamera(); ApplyCamera();
} }
glm::vec2 ee_engine::IRenderer::ScreenToWorld(const float screenX, const float screenY)
{
return ScreenToWorld(glm::vec2(screenX, screenY));
}
glm::vec2 ee_engine::IRenderer::ScreenToWorld(const glm::vec2 &screenPos)
{
float x = 2.0f * screenPos.x / screenWidth - 1.0f;
float y = -2.0f * screenPos.y / screenHeight + 1.0f;
glm::vec4 ndc(x, y, 0.0f, 1.0f);
glm::vec4 world = viewProjectionInversed * ndc;
return glm::vec2(world) / world.w;
}
void ee_engine::IRenderer::UpdateRegionCmd(ee_engine::RendererCommand &command) void ee_engine::IRenderer::UpdateRegionCmd(ee_engine::RendererCommand &command)
{ {
if (!regionsMapping.contains(command.region.id)) if (!regionsMapping.contains(command.region.id))

View File

@ -10,7 +10,7 @@ export
private: private:
static void Zoom(float dx, float dy); static void Zoom(float dx, float dy);
static void MapScroll(float dx, float dy); static void MapScroll(float x, float y, float lastX, float lastY);
Actions(); Actions();
}; };

View File

@ -12,5 +12,10 @@ export {
GLFWInput(ee_engine::InputHandlers handlers); GLFWInput(ee_engine::InputHandlers handlers);
void Poll() override; void Poll() override;
private:
void OnLeftMouseButton(int state);
void OnRightMouseButton(int state);
void OnMiddleMouseButton(int state);
}; };
} }

View File

@ -1,6 +1,7 @@
module; module;
#include <cmath> #include <cmath>
#include <glm/glm.hpp>
module Actions; module Actions;
import Engine; import Engine;
@ -17,7 +18,13 @@ void Actions::Zoom(float dx, float dy)
Engine::renderer->SetCamera(cam); Engine::renderer->SetCamera(cam);
} }
void Actions::MapScroll(float dx, float dy) void Actions::MapScroll(float x, float y, float lastX, float lastY)
{ {
ee_engine::CameraTransform cam = Engine::renderer->getCamera();
glm::vec2 pos = Engine::renderer->ScreenToWorld(x, y);
glm::vec2 lastPos = Engine::renderer->ScreenToWorld(lastX, lastY);
cam.position -= pos - lastPos;
Engine::renderer->SetCamera(cam);
} }

View File

@ -3,16 +3,17 @@ module;
#include "Logger.h" #include "Logger.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define GLFW_GET_INPUT reinterpret_cast<GLFWInput*>(glfwGetWindowUserPointer(window))
#define GLFW_CALLBACK(callback, ...) GLFW_GET_INPUT->handlers.callback(__VA_ARGS__);
module GLFWInput; module GLFWInput;
import Engine; import Engine;
GLFWInput::GLFWInput(ee_engine::InputHandlers handlers) : ee_engine::IInput(handlers) GLFWInput::GLFWInput(ee_engine::InputHandlers handlers) : ee_engine::IInput(handlers)
{ {
glfwSetWindowUserPointer(Engine::window, this); glfwSetWindowUserPointer(Engine::window, this);
glfwSetScrollCallback(Engine::window, [](GLFWwindow *window, double dx, double dy) {
glfwSetScrollCallback(Engine::window, [](GLFWwindow *window, double dx, double dy) { GLFW_CALLBACK(zooming, static_cast<float>(dx), static_cast<float>(dy));
reinterpret_cast<GLFWInput*>(glfwGetWindowUserPointer(window))->
handlers.zooming(static_cast<float>(dx), static_cast<float>(dy));
}); });
LOG(INFO, "Input mappings initialized"); LOG(INFO, "Input mappings initialized");
@ -20,5 +21,39 @@ GLFWInput::GLFWInput(ee_engine::InputHandlers handlers) : ee_engine::IInput(hand
void GLFWInput::Poll() void GLFWInput::Poll()
{ {
double mouseX, mouseY;
glfwGetCursorPos(Engine::window, &mouseX, &mouseY);
this->mouseX = static_cast<float>(mouseX);
this->mouseY = static_cast<float>(mouseY);
int LMB = glfwGetMouseButton(Engine::window, GLFW_MOUSE_BUTTON_LEFT);
int RMB = glfwGetMouseButton(Engine::window, GLFW_MOUSE_BUTTON_RIGHT);
int MMB = glfwGetMouseButton(Engine::window, GLFW_MOUSE_BUTTON_MIDDLE);
OnLeftMouseButton(LMB);
OnRightMouseButton(RMB);
OnMiddleMouseButton(MMB);
lastMouseX = this->mouseX;
lastMouseY = this->mouseY;
}
void GLFWInput::OnLeftMouseButton(int state){}
void GLFWInput::OnRightMouseButton(int state){}
void GLFWInput::OnMiddleMouseButton(int state)
{
switch(state)
{
case GLFW_PRESS:
{
handlers.mapScroll(mouseX, mouseY, lastMouseX, lastMouseY);
break;
}
case GLFW_RELEASE:
{
break;
}
}
} }