c++ - 如何从 gluUnProject 重新定位近平面和远平面以考虑相机位置和角度?

标签 c++ opengl graphics matrix ray-picking

我试图将鼠标在屏幕上的点击追踪到 3D 空间中的光线。据我了解,默认位置和方向如下所示:

gluUnprojection

从此代码:

    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/

相关文章:

php - 从 PHP 安全地编译、运行和返回 C++ 程序的输出

c++ - 在 OpenCV 中从 RGB 转换为 YUYV

java - OpenGL Z 顺序困惑

c++ - 在 OpenGL 4 中绘制一个简单的矩形

java - 最小化窗口时自动重绘

performance - `friday` 包很慢

matlab - MATLAB中如何控制点删除?

python - 将 pybind11 绑定(bind)标记为已弃用的最佳方法

c++ - 让 freeglut 库与 opengl 一起工作

c++ - 编译嵌入在 C++ 代码中的 Julia