Commit 7c50af4c authored by Philip Trettner's avatar Philip Trettner

Added getViewRay to GenericCamera. Closes #39

parent 6d3dc3ff
......@@ -86,7 +86,7 @@ void GenericCamera::FPSstyleLookAround(float _deltaX, float _deltaY)
fwd = glm::vec3( //
cal * caz, //
sal, //
cal * saz //
cal * saz //
);
auto right = normalize(cross(fwd, glm::vec3(0, 1, 0)));
auto up = cross(right, fwd);
......@@ -516,6 +516,26 @@ glm::vec3 GenericCamera::getForwardDirection() const
return forward;
}
void GenericCamera::getViewRay(glm::vec2 mousePosition, glm::vec3 *pos, glm::vec3 *dir) const
{
glm::vec3 ps[2];
auto i = 0;
for (auto d : {0.5f, -0.5f})
{
glm::vec4 v{mousePosition.x * 2 - 1, 1 - mousePosition.y * 2, d * 2 - 1, 1.0};
v = glm::inverse(getProjectionMatrix()) * v;
v /= v.w;
v = glm::inverse(getViewMatrix()) * v;
ps[i++] = glm::vec3(v);
}
if (pos)
*pos = getPosition();
if (dir)
*dir = normalize(ps[0] - ps[1]);
}
void GenericCamera::setTarget(const glm::vec3 &_target, const glm::vec3 &_up)
{
glm::vec3 forwardVector = _target - mPosition;
......
......@@ -393,6 +393,11 @@ public:
/// Get the unit forward direction
glm::vec3 getForwardDirection() const;
/// Returns the world space ray (pos -> dir) for a given mouse position
/// Mouse position must be in (0,0) .. (1,1)
/// Note that mousePosition (0,0) is top-left (and not bottom-left)
void getViewRay(glm::vec2 mousePosition, glm::vec3* pos, glm::vec3* dir) const;
private:
///
/// States: update the storeStateToString() & setStateFromString() functions whenever adding a new state!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment