我试图将鼠标在屏幕上的点击追踪到 3D 空间中的光线。据我了解,默认位置和方向如下所示:
从此代码:
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY;
GLdouble near_x, near_y, near_z;
GLdouble far_x, far_y, far_z;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)mouse_x;
winY = (float)viewport[3] - (float)mouse_y;
gluUnProject(winX, winY, 0.0, modelview, projection, viewport, &near_x, &near_y, &near_z);
gluUnProject(winX, winY, 1.0, modelview, projection, viewport, &far_x, &far_y, &far_z);
我的问题是如何更改近平面和远平面的位置和角度,以便所有鼠标点击看起来都来自相机在 3D 空间中的位置和角度?
具体来说,我正在寻找一种方法,将两个投影平面的中心沿着 (0,0,1) -> (0,0,-tan(65°)) 线定位在 Z 轴下方 65° 处[-tan(65°) ~= -2.145] 近平面和远平面均垂直于通过这些点的视线。
最佳答案
My question is how do you change the position and the angle of the near and far planes so that all mouse clicks will appear to be from the camera's position and angle in 3D space?
将投影和模型 View 矩阵输入 gluUnProject 即可解决此问题。具体来说,您将用于渲染的投影和模型 View 矩阵传递到 gluUnProject 中,以便未投影的坐标与这些矩阵描述的局部坐标系的坐标相匹配。
关于c++ - 如何从 gluUnProject 重新定位近平面和远平面以考虑相机位置和角度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17075510/