PDA

View Full Version : Projection matrix for picking.



skynet
02-01-2006, 04:08 AM
I guess, this is more a math-problem. But since the math forum is hardly visited, I try my luck here...

I have to generate a projection matrix for picking. This matrix builds the frustum around a pixel at a given 2D viewport coordinate. Until now I could generate this matrix from knowns like fov, aspect ratio, near and farplane (symmetric projection), just like I would use glFrustum().

Now Im facing the problem that I do not know aspect, fov etc. anymore. The only thing I got left is a projection matrix (arbitrary; could be projective, orthographic symmetric or asymmetric)that I get handed over. The challenge is to _manipulate_ that given projection matrix in order to achieve the same effect (i.e. bound the frustum around the pixel that the user clicked on.)

Im not good at maths in homogeneous coordinates. But I was hoping that I could easily "move" the frustum-planes in clipspace by just manipulating some of the values in the projection matrix. Any hints are welcome.

Relic
02-01-2006, 05:36 AM
What about using the gluPickMatrix function?

Picking projection looks something like this

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPickMatrix(x, viewport[3] - y, pickWidth, pickHeight, viewport);
gluPerspective(...); // or whatever your projection matrix setup is.You'll find examples on the web.

skynet
02-01-2006, 05:44 AM
Thank you, I just figured this out. The thing is that I need the maths in order to calculate the matrices myself.

But now as I know how gluPickMatrix is supposed to work, I can come up with the maths myself:

1. map the given viewport-subregion into clipspace (assuming that the given viewport covers -1..1)
2. compute a matrix which scales and translates this clipspace-subregion to cover the whole area of -1..1 in X and Y
3. no changes are made to the depthvalues

Am I right?

Relic
02-01-2006, 07:37 AM
Source is here:
http://oss.sgi.com/cgi-bin/cvsweb.cgi/~c...=text%2Fplain (http://oss.sgi.com/cgi-bin/cvsweb.cgi/~checkout~/projects/ogl-sample/main/gfx/lib/glu/libutil/project.c?rev=1.4;content-type=text%2Fplain)

skynet
02-01-2006, 08:32 AM
The cool thing is, Ive come to the same math and understand how/why it works.

The bad thing is, its so easy that I feel kinda dumb now :-D