I’m writing a routine to intersect the line from eye-point to the point on the screen with a plane (a plane is given in the world’s coordinate system).
Here’s the pseudocode:
Vector3d
intersect(windowX, windowY, plane) {
Line line = new Line(
project(modelview, windowX, windowY, 3),
project(modelview, windowX, windowY, 10)
);
return line.intersect(plane);
}
The code that implements this pseudocode works correctly with orthographic projection, but returns incorrect results with perspective projection.
The second parameter for gluUnProject should be viewport[3] - winY - 1, but this is just a one pixel distance error.
I notice that you pass only the modelview matrix for your project function. I think you don’t need to pass it because you can call getDoublev(GL_MODELVIEW_MATRIX,…) inside your function.
You need to subtract -1 because if the height of your viewport is 480, the range of y is [0 479].
I think you need the most recent modelview and projection matrix when you unproject a screen coord to object coord.
My guess is when you are in orthographic projection, if you get the modelview matrix and then you translate forward or backward, the result is correct because all line are parallel. But this is not the case in perspective view, so the first point of the line is very important.
No, you need to send the modelview matrix with which the object was drawn
Yes, the problem is actually that
Line (
project(modelview, windowX, windowY, 0),
project(modelview, windowX, windowY, 10)
)
and
Line (
project(modelview, windowX, windowY, 3),
project(modelview, windowX, windowY, 10)
)
are different
Does anyone by any chance has an idea on how to solve this problem?