Can someone point me to a link on how to do this? I thought I had a good algorithm but as soon as I introduce translation into the modelview matrix I start getting the wrong results.
Canvas3D has some utility methods to help with this. Snippet below might give an idea: ( xpix, ypix ) is the screen input, and vwPt is the (View) World output. Double check, though !
`
Point3d implPt = new Point3d();
canvas.getPixelLocationInImagePlate( xpix, ypix, implPt );
Transform3D vw2impl = new Transform3D();
canvas.getVworldToImagePlate( vw2impl );
Transform3D impl2vw = new Transform3D();
impl2vw.invert( vw2impl );
Point3d vwPt = new Point3d();
impl2vw.transform( implPt, vwPt );
`
Sorry I should have clarified that I am not using java3d. This is more of a 3d question and I happed to be using java(jogl). I need a more generic answer. I thought all I had to do was multiply the modelview and projection matricies together and invert the result. That seems to work as long as the modelview matrix is identity. As soon as I introduce some translation, because my camera moves, my results are no longer correct.
GLU has a gluUnproject() to convert screen coords back to world coords. Although I don’t see anything wrong with what you mention you’re doing.
The OGL pipeline terminology is a little loose, but shouldn’t you have a viewport transformation too in consideration - the proj. xform gets you only upto NDC ? I may probably mess with parallel proj. first to sort things out .
I am trying to avoid using gluUnproject due to the standard jogl threading issues. I do convert the screen pixel coordinates into [-1,1] ranges before transforming the vector with the inverted viewmatrix. It works just fine when the modelview matrix is identity. I am using a orthographic view matrix. I was hoping someone had a link to a tutorial that would have step by step instructions on picking but they all use dx or glu utility functions to hide all the fun stuff. I guess I will try to debug this with gluUnProject and see if that give me the same result.
This may be a bad guess but:
If it works with an identity modelview but an actual projection matrix, then odds are your matrix inversion is working. If it works with an identity matrix as the modelview, are you sure you’re multiplying the two together in the right order?
You could try looking at the source code for gluUnProject. It sounds like thats what you want. There is a port posted here:
http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=xith3d;action=display;num=1068252077;start=19#19
Or you could try downloading the mesa source.
good luck!!
Turns out I implemented my matrix.transformpoint() function as row-major rather than column-major. The ortho projection doesn’t have any nonzeros off the diagonal which is why I got the correct numbers with a identity modelview. So now I am wondering if the rest of my math is correct so does anyone has a link to a column major math lib that I could verify my code against? I think it is all good now but I just want to make sure.