i have been spending a lot of time to track a bug in my projection pipeline. I am implementing ray picking for my diploma thesis - actually just a UI thing
I did not plan to waste so much time on it.
I used two different methods to unproject:
1) Place an imageplane in front of the camera and shoot a ray
2) Invert the projection matrix multiply and homogenize
In both cases i observed that the generated rays were a little of and missed the scene parts they were suppossed to hit slightly. Just because I ran out of ideas
I translated the ray origin to (0,0, -1) and suddenly it worked perfectly.
I could ignore that little glitch, but it leaves me with a bad feeling as I do not understand what happens behind the curtains. The ray origin should be (0,0,0) - right ?
As I ran into the problem twice, I suppose I did fail to understand all aspects of homogenous clip coordinates. I will post my projection matrices and maybe you guys could
give me some advice what I am doing wrong - This is really starting to frustrate me.
My viewport to NDC matrix (row vectors):
x: (1.0f / (width / 2.0f)), 0, 0 ,0)
y: (0, (1.0f / (height / 2.0f)), 0 ,0)
z: (0, 0, 1 ,0)
w:(-1.0f 1.0f 0 ,1)
My projection matrix: (symmetric frustum)
x: (near / right, 0 0 0)
y: (0 near/top 0 0)
z:(0, 0 (far + near)) / (far - near) 1) //Camera is looking in positive z, opposed to vanilla OpenGL
w(0, 0 -(2 * far * near) / (far - near) 1)
The inverse projection matrix (after Mouse Ray Picking Explained):
x: (right / near, 0 0 0)
y: (0 top / near 0 0)
z: (0 0 0 (far - near) / (-(2*far*near))
w:(0 0 1 (far + near) / ( 2*far*near))
I am not sure the matrices will help, but maybe you got an idea what i might have forgot ?
The actual method (java) looks like this:
/*
* Returns a ray in eye space
*/
public Ray raycast(Point2 position) {
//Viewport to NDC
Point3 pickPoint = paintingSurface.normalise(new Point3(position, 0));
//NDC to eye coordinates
Point3 reprojectedPoint = inverseProjection.multiply(pickPoint);
reprojectedPoint.homogenousDivide();
Vector3 rayDir = reprojectedPoint.subtract(Point3.ZERO);
rayDir.normalize();
return new Ray(new Point3(0,0,-1), rayDir); /// :-(((((( Why???
}
I would be glad if anybody could point out my error.
Thanks a lot,
Frederick