简体   繁体   English

MVP矩阵不能在着色器之外工作?

[英]MVP matrix not working outside of shader?

Odd problem here, I've been converting my current project from Qt's native matrix/vector classes to Eigen's, but I've come across an issue that I can't work out.奇怪的问题在这里,我一直在将我当前的项目从 Qt 的本机矩阵/向量类转换为 Eigen 的,但我遇到了一个我无法解决的问题。

I calculate the MVP for the shader thus:我这样计算着色器的 MVP:

DiagonalMatrix< double, 4 > diag( Vector4d( 1.0, 1.0, -1.0, 1.0 ) );
scrMatrix_.noalias() = projMatrix_ * diag * camMatrix_.inverse();

The diag matrix inverts the Z-axis because all my maths sees the camera's aim vector pointing into the screen, but OpenGL does the opposite. diag矩阵反转 Z 轴,因为我所有的数学运算都看到相机的瞄准矢量指向屏幕,但 OpenGL 则相反。 Anyway this works because the OpenGL side of the viewports appear and operate fine.无论如何,这是可行的,因为视口的 OpenGL 侧出现并且运行良好。

The other side of my viewport output is 2D overlay painting via Qt's paintEvent() system, grid labelling for example.我的视口 output 的另一面是通过 Qt 的paintEvent()系统进行的 2D 叠加绘制,例如网格标记。 So I use the same matrix to find the 3D location in the camera's clip space:所以我使用相同的矩阵在相机的剪辑空间中找到 3D 位置:

Vector4d outVec( scrMatrix_ * ( Vector4d() << inVec, 1.0 ).finished() );

Except I get totally wrong results:除了我得到完全错误的结果:

inVec: 0 0 10
outVec: 11.9406 -7.20796

In this example I expected something more like outVec: 0.55 -0.15 .在这个例子中,我预计会更像outVec: 0.55 -0.15 My GLSL vertex shader performs the calculation like this:我的 GLSL 顶点着色器执行如下计算:

gl_Position = scrMatrix_ * transform * vec4( inVec, 1.0 );

In the examples above transform is the identity, so I can't see any difference between the two projections, and yet the outcomes are totally different, I know this is a long shot?在上面的例子中, transform是恒等式,所以我看不出两个投影之间有什么区别,但结果却完全不同,我知道这是一个远射吗? but can anyone see where I'm going wrong?但谁能看到我哪里出错了?

Update:更新:

I reimplemented the old (working) Qt code for comparison purposes:我重新实现了旧的(工作)Qt 代码以进行比较:

QVector3D qvec( vector( 0 ), vector( 1 ), vector( 2 ) );
QMatrix4x4 qmat( Affine3d( scrMatrix_ ).data() );
QPointF pnt = ( qvec * qmat ).toPointF() / 2.0;

Vs:对比:

Vector4d vec( scrMatrix_ * ( Vector4d() << vector, 1.0 ).finished() );
QPointF pnt = QPointF( vec( 0 ), vec( 1 ) ) / 2.0;

To me they are identical, but only the Qt version works!对我来说,它们是相同的,但只有 Qt 版本有效!

Well I sussed it out, you need to scale the XYZ axes of the resulting vector by the W axis scale factor (clues in the name...).好吧,我猜出来了,您需要通过 W 轴比例因子(名称中的线索......)来缩放结果向量的 XYZ 轴。

It's amazing how much Qt and OpenGL do in the background for you.令人惊讶的是 Qt 和 OpenGL 在后台为您做了多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM