1

I would like to get the ray out of my current view.

I generate the following projection matrix:

QMatrix4x4 m_proj;
...
m_proj.perspective(camFOV, ar, camZnear, camZfar);

when I apply a point in 3d I get the normalized coordinates (range of [-1 1]).

If I get a click event, I normalize the coordinates to [-1 1] and multiply it by when m_proj.inverted(), but I get weird results (a number greater than 1).

In other words, how do I transform a click in the screen (x,y) coordinates to a ray in the camera coordinate system?

EDIT:

This is similar question to Qt OpenGL- How to get the object based on the mouse click But how do I do it with modern opengl?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Mercury
  • 1,886
  • 5
  • 25
  • 44
  • 1
    Do you forget the perspective divide? `(v.x, v.y, v.z) / v.w` – Rabbid76 Feb 21 '18 at 10:58
  • 1
    I'm not sure whether `QMatrix4x4::inverted()` provides the correct result. My "stone-old" red book mentions inverse projection matrix explicitly. (I remember roughly having read once that projection matrix is actually not invertable.) However, I found [Inverse Projection](https://gist.github.com/gyng/8921328) on github. You may use this algorithm to check the inverted `QMatrix4x4`. – Scheff's Cat Feb 21 '18 at 11:02
  • I just had a look into [source code of QMatrix4x4::inverted()](https://code.woboq.org/qt5/qtbase/src/gui/math3d/qmatrix4x4.cpp.html#_ZNK10QMatrix4x48invertedEPb). If I understood it right, for perspective projection, the general matrix inversion is used. (May be, it is applicable for projection matrices properly built with `QMatrix4x4::perspective()`.) However, it couldn't hurt to use/check the `invertible` argument of `QMatrix4x4::inverted()` (just to be sure). – Scheff's Cat Feb 21 '18 at 11:11
  • 2
    What z coordinate for your mouse click position are you using? Zero? – Danny Ruijters Feb 21 '18 at 11:18
  • see [back raytrace through 3D mesh](https://stackoverflow.com/a/45140313/2521214) and [back raytrace through 3D volume](https://stackoverflow.com/a/48092685/2521214) both are casting rays in vertex shader with perspective projection camera ... focal length is your `z_near` parameter – Spektre Feb 21 '18 at 12:53
  • @Scheff I think you're right `InverseProjection(FOV) != Inverse(Projection(FOV))` as the projection matrix is not orthogonal not invertible as it is not `1:1` mapping/transform ... – Spektre Feb 21 '18 at 13:01

1 Answers1

1

For future users:

QVector4D uv(xy.x() / m_w * 2 - 1, -(xy.y() / m_h * 2 - 1),1,1);
QVector3D sv = m_proj.inverted() * uv;

where sv is a point on the camera screen

Mercury
  • 1,886
  • 5
  • 25
  • 44