ADD: MousePicker
This commit is contained in:
parent
968f6c3a6e
commit
5ab79c8756
@ -55,7 +55,10 @@ add_executable(Dicewars_Siedler src/main.cpp
|
|||||||
src/engine/renderer/loader/OBJLoader.cpp
|
src/engine/renderer/loader/OBJLoader.cpp
|
||||||
src/engine/renderer/loader/OBJLoader.h
|
src/engine/renderer/loader/OBJLoader.h
|
||||||
src/engine/layer/entities/Light.cpp
|
src/engine/layer/entities/Light.cpp
|
||||||
src/engine/layer/entities/Light.h)
|
src/engine/layer/entities/Light.h
|
||||||
|
src/engine/platform/glfw/MousePicker.cpp
|
||||||
|
src/engine/platform/glfw/MousePicker.h
|
||||||
|
)
|
||||||
|
|
||||||
target_include_directories(Dicewars_Siedler PRIVATE
|
target_include_directories(Dicewars_Siedler PRIVATE
|
||||||
lib/glfw/include
|
lib/glfw/include
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
#include "../renderer/loader/OBJLoader.h"
|
#include "../renderer/loader/OBJLoader.h"
|
||||||
#include "../renderer/model/TexturedModel.h"
|
#include "../renderer/model/TexturedModel.h"
|
||||||
#include "../renderer/textures/ModelTexture.h"
|
#include "../renderer/textures/ModelTexture.h"
|
||||||
|
#include "../toolbox/MathUtils.h"
|
||||||
#include "entities/Light.h"
|
#include "entities/Light.h"
|
||||||
|
|
||||||
GameLayer::GameLayer() :texturedModel(0,0) //Platzhalter, echtes Model kommt in onAttach
|
GameLayer::GameLayer() :texturedModel(0,0) //Platzhalter, echtes Model kommt in onAttach
|
||||||
@ -40,6 +41,7 @@ void GameLayer::onAttach()
|
|||||||
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-25), 0,0,0, 1.f));
|
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-25), 0,0,0, 1.f));
|
||||||
camera = std::make_unique<Camera>();
|
camera = std::make_unique<Camera>();
|
||||||
light = std::make_unique<Light>(glm::vec3(0,0,-20), glm::vec3(1,1,1));
|
light = std::make_unique<Light>(glm::vec3(0,0,-20), glm::vec3(1,1,1));
|
||||||
|
mousePicker = std::make_unique<MousePicker>(renderer.getProjectionMatrix(), MathUtils::createViewMatrix(*camera));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameLayer::onDetach()
|
void GameLayer::onDetach()
|
||||||
@ -49,6 +51,8 @@ void GameLayer::onDetach()
|
|||||||
|
|
||||||
void GameLayer::onUpdate()
|
void GameLayer::onUpdate()
|
||||||
{
|
{
|
||||||
|
mousePicker->update(*camera);
|
||||||
|
printf("Mouse Ray: %f, %f, %f\n", mousePicker->getCurrentRay().x, mousePicker->getCurrentRay().y, mousePicker->getCurrentRay().z);
|
||||||
glm::vec3 moveDir = glm::vec3(0,0,0);
|
glm::vec3 moveDir = glm::vec3(0,0,0);
|
||||||
if (InputManager::isKeyPressed(GLFW_KEY_W)) moveDir.z -= 1.0f;
|
if (InputManager::isKeyPressed(GLFW_KEY_W)) moveDir.z -= 1.0f;
|
||||||
if (InputManager::isKeyPressed(GLFW_KEY_S)) moveDir.z += 1.0f;
|
if (InputManager::isKeyPressed(GLFW_KEY_S)) moveDir.z += 1.0f;
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#ifndef DICEWARS_SIEDLER_GAMELAYER_H
|
#ifndef DICEWARS_SIEDLER_GAMELAYER_H
|
||||||
#define DICEWARS_SIEDLER_GAMELAYER_H
|
#define DICEWARS_SIEDLER_GAMELAYER_H
|
||||||
#include "Layer.h"
|
#include "Layer.h"
|
||||||
|
#include "../platform/glfw/MousePicker.h"
|
||||||
#include "../renderer/Renderer.h"
|
#include "../renderer/Renderer.h"
|
||||||
#include "../renderer/loader/Loader.h"
|
#include "../renderer/loader/Loader.h"
|
||||||
#include "../renderer/model/TexturedModel.h"
|
#include "../renderer/model/TexturedModel.h"
|
||||||
@ -26,6 +27,7 @@ private:
|
|||||||
std::unique_ptr<Entity> entity;
|
std::unique_ptr<Entity> entity;
|
||||||
std::unique_ptr<Camera> camera;
|
std::unique_ptr<Camera> camera;
|
||||||
std::unique_ptr<Light> light;
|
std::unique_ptr<Light> light;
|
||||||
|
std::unique_ptr<MousePicker> mousePicker;
|
||||||
Renderer renderer;
|
Renderer renderer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -17,6 +17,13 @@ bool InputManager::isMouseButtonPressed(int button) {
|
|||||||
return glfwGetMouseButton(window, button) == GLFW_PRESS;
|
return glfwGetMouseButton(window, button) == GLFW_PRESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::vec2 InputManager::getMousePosition() {
|
||||||
|
auto window = static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow());
|
||||||
|
double xpos, ypos;
|
||||||
|
glfwGetCursorPos(window, &xpos, &ypos);
|
||||||
|
return {xpos, ypos};
|
||||||
|
}
|
||||||
|
|
||||||
glm::vec2 InputManager::getMouseDelta() {
|
glm::vec2 InputManager::getMouseDelta() {
|
||||||
static double lastX = 0, lastY = 0;
|
static double lastX = 0, lastY = 0;
|
||||||
double xpos, ypos;
|
double xpos, ypos;
|
||||||
|
|||||||
@ -11,6 +11,8 @@ class InputManager {
|
|||||||
public:
|
public:
|
||||||
static bool isKeyPressed(int keycode);
|
static bool isKeyPressed(int keycode);
|
||||||
static bool isMouseButtonPressed(int button);
|
static bool isMouseButtonPressed(int button);
|
||||||
|
|
||||||
|
static glm::vec2 getMousePosition();
|
||||||
glm::vec2 getMouseDelta();
|
glm::vec2 getMouseDelta();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
43
src/engine/platform/glfw/MousePicker.cpp
Normal file
43
src/engine/platform/glfw/MousePicker.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 07.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "MousePicker.h"
|
||||||
|
|
||||||
|
#include "InputManager.h"
|
||||||
|
#include "../../core/Application.h"
|
||||||
|
#include "../../toolbox/MathUtils.h"
|
||||||
|
|
||||||
|
|
||||||
|
void MousePicker::update(const Camera& camera) {
|
||||||
|
viewMatrix = MathUtils::createViewMatrix(camera);
|
||||||
|
currentRay = calculateMouseRay();
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 MousePicker::calculateMouseRay() {
|
||||||
|
glm::vec2 mouseXY = InputManager::getMousePosition();
|
||||||
|
|
||||||
|
glm::vec2 normalizedDeviceCoords = getNormalizedDeviceCoords(mouseXY.x, mouseXY.y);
|
||||||
|
glm::vec4 clipCoords = glm::vec4(normalizedDeviceCoords.x, normalizedDeviceCoords.y, -1.0f, 1.0f);
|
||||||
|
glm::vec4 eyeCoords = toEyeSpace(clipCoords);
|
||||||
|
glm::vec3 worldCoords = toWorldSpace(eyeCoords);
|
||||||
|
return glm::normalize(worldCoords);
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec2 MousePicker::getNormalizedDeviceCoords(float mouseX, float mouseY) {
|
||||||
|
float x = (2.f * mouseX) / static_cast<float>(Application::getInstance().getWindow().GetWidth()) - 1.f;
|
||||||
|
float y = (2.f * mouseY) / static_cast<float>(Application::getInstance().getWindow().GetHeight()) - 1.f;
|
||||||
|
return {x, y};
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec4 MousePicker::toEyeSpace(const glm::vec4 clipCoords) const {
|
||||||
|
glm::mat4 inverseProjectionMatrix = glm::inverse(projectionMatrix);
|
||||||
|
glm::vec4 eyeCoords = inverseProjectionMatrix * clipCoords;
|
||||||
|
return {eyeCoords.x, eyeCoords.y, -1.0f, 0.0f};
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 MousePicker::toWorldSpace(glm::vec4 eyeCoords) const {
|
||||||
|
glm::mat4 inverseViewMatrix = glm::inverse(viewMatrix);
|
||||||
|
glm::vec4 worldCoords = inverseViewMatrix * eyeCoords;
|
||||||
|
return {worldCoords.x, worldCoords.y, worldCoords.z};
|
||||||
|
}
|
||||||
36
src/engine/platform/glfw/MousePicker.h
Normal file
36
src/engine/platform/glfw/MousePicker.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 07.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MOUSEPICKER_H
|
||||||
|
#define MOUSEPICKER_H
|
||||||
|
#include "glm/mat4x4.hpp"
|
||||||
|
#include "glm/vec3.hpp"
|
||||||
|
|
||||||
|
class Camera;
|
||||||
|
|
||||||
|
class MousePicker {
|
||||||
|
private:
|
||||||
|
glm::vec3 currentRay = glm::vec3(0.0f);
|
||||||
|
glm::mat4 projectionMatrix;
|
||||||
|
glm::mat4 viewMatrix;
|
||||||
|
|
||||||
|
glm::vec3 calculateMouseRay();
|
||||||
|
|
||||||
|
static glm::vec2 getNormalizedDeviceCoords(float mouseX, float mouseY);
|
||||||
|
[[nodiscard]] glm::vec4 toEyeSpace(glm::vec4 clipCoords) const;
|
||||||
|
[[nodiscard]] glm::vec3 toWorldSpace(glm::vec4 eyeCoords) const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
MousePicker(const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix) : projectionMatrix(projectionMatrix), viewMatrix(viewMatrix) {};
|
||||||
|
|
||||||
|
[[nodiscard]] glm::vec3 getCurrentRay() const {
|
||||||
|
return currentRay;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(const Camera& camera);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //MOUSEPICKER_H
|
||||||
@ -22,6 +22,7 @@ public:
|
|||||||
};
|
};
|
||||||
void prepare(const ::Camera &camera, const Light& light);
|
void prepare(const ::Camera &camera, const Light& light);
|
||||||
void renderFrame(const ::Entity &entity, const ::Camera &camera, const Light& light);
|
void renderFrame(const ::Entity &entity, const ::Camera &camera, const Light& light);
|
||||||
|
[[nodiscard]] glm::mat4 getProjectionMatrix() const {return projectionMatrix;}
|
||||||
private:
|
private:
|
||||||
void renderRawModel(const Entity &entity);
|
void renderRawModel(const Entity &entity);
|
||||||
StaticShader staticShader;
|
StaticShader staticShader;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user