我遇到了一个我不知道如何修复的错误。当我点击窗口时,3D 空间中的点不会直接映射到实际窗口 x 轴上的点击。当您离窗口中的 x 轴中心越来越远时,单击的点与 3D 空间中实际创建的光线之间的差异会按比例增加。y 轴工作正常,它只是 x 轴。
这是我单击鼠标在 3D 空间中创建的正方形的图片。我只单击边缘并在窗口周围移动。(有些正方形看起来像六边形,因为相机角度朝下,但这并不重要,左右间隙是)
这是我用来通过单击窗口在 3D 空间中创建射线的代码:
GLfloat window_height = 800;
GLfloat window_width = 800;
void proccess_Mouse(int button, int state, int x, int y) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(camera->getAngle(), window_height / window_width, viewPlane_close, viewPlane_far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera->getIndex_position(0), camera->getIndex_position(1), camera->getIndex_position(2),
camera->getIndex_looking(0), camera->getIndex_looking(1), camera->getIndex_looking(2),
0, -1, 0);
// matrix information
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY; // clicked coords on the screen
GLdouble near_x, near_y, near_z; // position of the mouse on the near view plane
GLdouble far_x, far_y, far_z; // position of the mouse on the far view plane
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
winX = (float)x;
winY = (float)viewport[3] - (float)y; // window height - click position
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);
Vector3f* near = new Vector3f(near_x, near_y, near_z);
Vector3f* far = new Vector3f(far_x, far_y, far_z);
...
}
我相信我错过了什么,或者忘记了什么,但我没有看到它。有任何想法吗?