开发者

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.

I calculate the MVP for the shader thus:

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. Anyway this works开发者_如何学JAVA because the OpenGL side of the viewports appear and operate fine.

The other side of my viewport output is 2D overlay painting via Qt's paintEvent() system, grid labelling for example. So I use the same matrix to find the 3D location in the camera's clip space:

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. My GLSL vertex shader performs the calculation like this:

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, but can anyone see where I'm going wrong?

Update:

I reimplemented the old (working) Qt code for comparison purposes:

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!


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...).

It's amazing how much Qt and OpenGL do in the background for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜