add screen to world to camera

This commit is contained in:
Green Sky 2022-02-01 15:24:16 +01:00
parent e64c68d942
commit 6aa7e3899e
2 changed files with 17 additions and 0 deletions

View File

@ -181,6 +181,19 @@ std::array<glm::vec4, 6> Camera3D::getFrustumPlanes(void) const {
return planes;
}
// example use:
// const glm::vec4 screen {
// (
// glm::vec2{mouse_pos.x, window_size.y - mouse_pos.y}
// / (glm::vec2{window_size.x, window_size.y} * 0.5f)
// )
// - glm::vec2{1.f, 1.f}
// , 1.f, 1.f
// };
glm::vec4 Camera3D::screenToWorld(const glm::vec4& screen) const {
return glm::inverse(getViewProjection()) * screen;
}
template<typename T>
static T dot34(glm::vec<3, T> lhs, glm::vec<4, T> rhs) {
return glm::dot(lhs, glm::vec<3, T>(rhs)) + rhs.w;

View File

@ -49,6 +49,10 @@ namespace MM::OpenGL {
// call updateView beforehand, does not cache
std::array<glm::vec4, 6> getFrustumPlanes(void) const;
// uses getViewProjection(), does not cache
// screen has to be in NDC, xy -> [-1, 1]
glm::vec4 screenToWorld(const glm::vec4& screen) const;
};
// test if aabb in frustum