I need some help on 3D picking.
I am using the way it works here. In short, what I have is:
normalizedPoint[0] = (x * 2 / screenW) -1;
normalizedPoint[1] = 1 - (y * 2 / screenH);
normalizedPoint[2] = ?
normalizedPoint[3] = ?
for 2 and 3, I have no idea what it should be (I put 1, -1 just like the reference, and it doesn’t work)
Then, for my root object (following just psuedo code):
matrix = perspective_matrix x model_matrix
inv_matrix = inverse(matrix)
outpoint = inv_matrix x normalizedPoint
That’s what I have, but it doesn’t work, the outPoint I receive is not even close to the point I am suppose clicking. I’ve been searching in web for more than a week. but no idea how to solve it. HELP!
Oh. I actually solved the problem, sort of.
I first, modify from the source code of glUnproject to have the following:
Input to the above would be the point in the Projected View Frustum Coordinates (i.e., screen input). For example:
will translate the screen input (click) at (30,50) to the world coordinate where the object is sitting at. The third parameter,
winzis actually on which projected plane the click is occured, here, 0 means the nearZ of the projection plane.The way I make picking functions, is by unprojecting two points, using the above function, on the far and near clipping plane, so:
Once we have the picking ray, what I am doing is simply testing the distance between the picking ray and the “center” of those “pickable” objects. (of course, you can have some more sophisticated testing algorithm).