Skip to content
Snippets Groups Projects
Commit e4e5071d authored by Pascal Engeler's avatar Pascal Engeler
Browse files

Added screen to world coordinates conversion (broken)

parent f5a3a766
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@
#include <glm/gtc/matrix_transform.hpp>
#define _USE_MATH_DEFINES
#include <math.h>
#include <screendefs.hpp>
class Camera {
public:
......@@ -25,6 +26,30 @@ public:
glm::vec3 GetViewDir() const { return normalize(m_lookAt-m_eye); }
glm::vec3 GetRightVector() const { return glm::normalize(glm::cross(m_lookAt - m_eye, m_upVector)); }
glm::vec2 GetWorldXyFromMouse(float mx, float my) const {
//calculate point in center of screen
glm::vec3 p_eye = m_eye;
glm::vec3 p_sCenter = m_eye + 0.1f * GetViewDir();
glm::vec3 curr_up = normalize(glm::cross(GetRightVector(), GetViewDir()));
//extent of screen in width and height
//0.1 away from eye, spans 45 (see call to glm::perspective in main.cpp)
//float s_extent_h = 0.2 * std::tan(3.14159265 * 45 / 180.);
float s_extent_h = 0.2f * std::tan(3.14159265 * 45 / 180. / 2.);
float s_extent_w = WIDTH / static_cast<float>(HEIGHT) * s_extent_h;
glm::vec3 p_mouse = p_sCenter + (mx - 0.5f) * s_extent_w * 0.5f * GetRightVector() + (my - 0.5f) * s_extent_h * 0.5f * curr_up; //TODO: Potentially needs (1-my) instead of my
glm::vec3 r = glm::normalize(p_mouse - p_eye);
if (std::abs(r.z) > 0.00001) {
float t = -p_mouse.z / r.z;
glm::vec3 res_p = p_mouse + t * r;
return glm::vec2(res_p.x, res_p.y);
}
else {
return glm::vec2(std::copysign(r.x, 999999.), std::copysign(r.y, 999999.));
}
}
void SetCameraView(glm::vec3 eye, glm::vec3 lookat, glm::vec3 up) {
m_eye = std::move(eye);
m_lookAt = std::move(lookat);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment