1
votes

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?

1
Do you forget the perspective divide? (v.x, v.y, v.z) / v.wRabbid76
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 on github. You may use this algorithm to check the inverted QMatrix4x4.Scheff's Cat
I just had a look into source code of QMatrix4x4::inverted(). 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
What z coordinate for your mouse click position are you using? Zero?Danny Ruijters
see back raytrace through 3D mesh and back raytrace through 3D volume both are casting rays in vertex shader with perspective projection camera ... focal length is your z_near parameterSpektre

1 Answers

0
votes

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