Emulating gluUnproject

Started by
2 comments, last by Geometrian 12 years, 8 months ago
Hi,

I set up a view.
The modelview matrix is the identity matrix:[source]1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1[/source]
The projection matrix is 45 degrees, near 0.1, far 100.0, resulting in a matrix:[source]1.8107 0.0000 0.0000 0.0000
0.0000 2.4142 0.0000 0.0000
0.0000 0.0000 -1.0020 -0.2002
0.0000 0.0000 -1.0000 0.0000[/source]
By the documentation on gluUnproject, I multiply (MV*P)[sup]-1[/sup] by a normalized device coordinate to get an object coordinate. Because in my case, MV is I, that means that MV*P = P, and (MV*P)[sup]-1[/sup] = P[sup]-1[/sup]. Matrix P[sup]-1[/sup] is:[source]0.5523 0.0000 0.0000 0.0000
0.0000 0.4142 0.0000 0.0000
0.0000 0.0000 0.0000 -1.0000
0.0000 0.0000 -4.9950 5.0050[/source]
Now, this is a problem. In the next step, the normalized device coordinate is multiplied by this matrix to get the object coordinate. However, notice that any Z value in the NDC will always be zero, because index 2,2 of this matrix, (MV*P)[sup]-1[/sup], is zero! Clearly, something is going wrong. Help?

Thanks,
-G

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement

Now, this is a problem. In the next step, the normalized device coordinate is multiplied by this matrix to get the object coordinate. However, notice that any Z value in the NDC will always be zero, because index 2,2 of this matrix, (MV*P)[sup]-1[/sup], is zero! Clearly, something is going wrong. Help?


Nothing is going wrong, you've just miscalculated something. With this matrix, z value in the result will only be zero if NDC.z is zero (and NDC.w is zero, which it shouldn't be). Yes, M2,2 is zero, but M2,3 is not.

Oh, yeah, and don't forget perspective division (divide result vector by it's w component after matrix multiplication).
WBW, capricorn
There is an app for that

GluProject and gluUnProject code

http://www.opengl.org/wiki/GluProject_and_gluUnProject_code
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Ah thanks everyone! I see now that the problem wasn't that the zero was screwing it up. The problem was that I had forgotten to divide by the "w", which confused me, until I realized that the orders of magnitude differences between the GL code and my emulating code were due to my using 0.1 and 100.0 as clipping planes! Thanks for the tips,
-G

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

This topic is closed to new replies.

Advertisement