QMatrix4x4 map 函数可以将一组坐标从一个坐标系映射到另一个坐标系。在 Qt 中,我们可以使用它将 Widget 局部坐标映射到 OpenGL 内部坐标。
示例代码如下:
// 假设 widget 是一个 QWidget 对象,openglWidget 是一个继承自 QOpenGLWidget 的 OpenGL 视图
// 获取 widget 上的鼠标位置
QPoint pos = widget->mapFromGlobal(QCursor::pos());
// 将 widget 的局部坐标映射到 openglWidget 上
QVector4D result = openglWidget->projectionMatrix() * openglWidget->viewMatrix() *
openglWidget->modelMatrix() * QVector4D(pos.x(), pos.y(), 0.0f, 1.0f);
// 获得 opengl 内部坐标
QVector3D openglPos(result.x() / result.w(), result.y() / result.w(), result.z() / result.w());
在这个示例中,我们首先获取了鼠标在 widget 上的位置,并将其转换为局部坐标。然后,我们使用 openglWidget 的 projectionMatrix、viewMatrix 和 modelMatrix 将局部坐标映射到 opengl 内部坐标。最后,我们将结果保存为 QVector3D 类型的 openglPos。