【发布时间】:2020-12-24 05:21:16
【问题描述】:
我正在使用 C++/Qt/OpenGL 4.3 来实现 OpenGL 查看器,但我一直在将鼠标坐标转换为世界坐标。
更新:阅读 cmets 并回答后,我发现这段代码可以正常工作:
float depth;
double mouseX;
double mouseY;
_app->getCurPos(mouseX, mouseY);
glReadPixels(mouseX, _h - mouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, (void *)&depth);
float x = (2.0f * mouseX) / _w - 1.0f;
float y = 1.0f - (2.0f * mouseY) / _h;
float z = depth * 2.0 - 1.0; // convert to NDC
QVector4D pos(x, y, z, 1.0);
QMatrix4x4 invVM(_app->camera()->ViewMatrix().inverted());
QMatrix4x4 invPM(_app->camera()->ProjectionMatrix().inverted());
QVector4D world = invVM * invPM * pos;
world /= world.w();
qDebug() << "world" << world;
(来自原始问题...) 这段代码哪里出错了?它的输出包括在下面。
代码:
// mouse coordinates to world coordinates
QVector3D GLCamera::transformScreen(float mouseX, float mouseY)
{
// our 3D view has Y as "UP"
float x = (2.0f * mouseX) / _w - 1.0f;
float y = 0.0f;
float z = 1.0f - (2.0f * mouseY) / _h;
// hard code NDC to upper-right of screen
x = 1.0;
y = 0.0;
z = 1.0;
QVector4D ndc = QVector4D(x, y, z, 1);
QVector4D point1 = ndc * mProjectionMatrix.inverted();
qDebug() << point1;
QVector3D point3D = QVector3D(point1) / point1.w();
qDebug() << point3D;
QVector3D point2 = point3D * mViewMatrix.inverted();
qDebug() << point2;
qDebug() << "mViewMatrix: " << mViewMatrix;
qDebug() << "inv : " << mViewMatrix.inverted();
qDebug() << "mProjMatrix: " << mProjectionMatrix;
qDebug() << "inv : " << mProjectionMatrix.inverted();
return point2;
}
输出:
qDebug: QVector4D(1, 0, 1, 1)
qDebug: QVector4D(0.742599, 0, -49.995, 49.005)
qDebug: QVector3D(0.0151535, 0, -1.0202)
# This is the returned value x seems so much smaller than z
qDebug: QVector3D(-0.000938916, -0.0418856, 0.0473429)
qDebug: mViewMatrix: QMatrix4x4(type:Translation,Rotation
1 0 0 0
0 0.748955 -0.662621 -0.626549
0 0.662621 0.748955 -22.9856
0 0 0 1
)
qDebug: inv : QMatrix4x4(type:Translation,Rotation
1 0 0 0
0 0.748955 0.662621 15.7
0 -0.662621 0.748955 16.8
0 0 0 1
)
qDebug: mProjMatrix: QMatrix4x4(type:General
1.34662 0 0 0
0 2.41421 0 0
0 0 -1.0002 -0.020002
0 0 -1 0
)
qDebug: inv : QMatrix4x4(type:General
0.742599 0 0 0
0 0.414214 0 0
0 0 0 -1
0 0 -49.995 50.005
)
【问题讨论】:
-
您的问题是什么?我假设你没有得到你期望的结果?代码中有几处我认为是错误的。所以让我先问你代码来自哪里。为了将屏幕点转换为世界坐标,您需要点的深度。在这种情况下,您将其硬编码为
1。您是否普遍意识到这个问题以及您想如何解决它? -
您的代码会立即覆盖
(x, y, z)值,这可能不是您的意图。此外,无论哪种情况,您的z坐标都是错误的。如果(x, y)已映射到标准化设备坐标(-1.0, -1.0) -> (+1.0, +1.0),则z = -1.0将产生(x, y, z, 1.0)作为W = - Z剪切平面上的一个点。 -
@nico 我的问题是:有人可以识别我的代码中的编码或逻辑错误。该代码来自我为我的公司编写的产品。我在此行之后为演示目的对 x、y、z 进行了硬编码:
// hard code NDC to upper-right of screen -
@brett 您是在谈论此评论之后的 3 行吗?
// hard code NDC to upper-right of screen是有意为代码 sn-p 设计的。我只想在球场上得到 x 和 z。我可以不关心 y(我现在的上/下矢量)。我将探索将 z 转换为屏幕的真实深度。谢谢。 -
完成最后一个 cmets 后,我想我在将 mouseY 与 z 关联时遇到了问题。我会探索这条路。谢谢!