Hi Robert, Thanks for your reply. What I'm trying to do is to map window 
coordinates to object coordinates, but I got incorrect results using the 
matrices given by viewer.getCamera().getViewport().computeWindowMatrix(), 
viewer.getCamera().getProjectionMatrix() and 
viewer.getCamera().getViewMatrix(). So I referred to the OSG source code and 
tried the code in my initial email. I didn't know about the matrix stack 
methods then.
 
After obtaining the mapped object coordinates, I need to construct a ray from 
the viewer's position. The position and the ray then can be used to create a 
mouse picking line segment.
 
My code is like this:
 
protected unsafe bool pick(OsgGA.GUIEventAdapter ea, OsgViewer.Viewer viewer){
......
Osg.Matrix VPW = new Matrix();

Osg.Matrix inverseVPW = new Matrix();
Vec3d windowPositionStart = new Vec3d((double)(ea.getXnormalized()), 
(double)(ea.getYnormalized()), -1.0d);
Vec3d windowPositionEnd = new Vec3d((double)(ea.getXnormalized()), 
(double)(ea.getYnormalized()), 1.0d);
Vec3d worldPositionStart = new Vec3d();
Vec3d worldPositionEnd = new Vec3d();
 
VPW.preMult(viewer.getCamera().getProjectionMatrix());
VPW.preMult(viewer.getCamera().getViewMatrix());
 
inverseVPW = Osg.Matrix.inverse(VPW);
worldPositionStart = windowPositionStart * inverseVPW;
worldPositionEnd = windowPositionEnd * inverseVPW;
 
Vec3d ray1 = new Vec3d(worldPositionEnd[0]-worldPositionStart[0], 
worldPositionEnd[1]-worldPositionStart[1], 
worldPositionEnd[2]-worldPositionStart[2]);
ray1.normalize();
 
Vec3 eye = new Vec3();
Vec3 center = new Vec3();
Vec3 up = new Vec3();
viewer.getCamera().getViewMatrixAsLookAt(ref eye, ref center,ref up);
 
Vec3d ray2 = new Vec3d(eye[0]-worldPositionStart[0], 
eye[1]-worldPositionStart[1], eye[2]-worldPositionStart[2]);
ray2.normalize();
....
}
 
results: 
 
1. ray1 always equals {0.0, 0.0, 1.0} after any zooming, rotating, and panning;
2. ray2 remains almost the same after any zooming, rotating, and panning;
3. worldPositionStart looks like the mapping of the screen position. Since I'm 
using perspective projection, worldPositionStart and worldPositionEnd are 
slightly different.
 
questions:
1. why ray1 remains constant?
2. is ray2 what I need in this case?
3. is the near clipping plane at z=-1.0 in OSG?
 
I know what I'm trying to do is kind of simple, but I have been struggling for 
a while and cannot get the correct results yet.
 
Thanks, and regards,
 
Ke Li



> Date: Sat, 15 Mar 2008 10:19:05 +0000> From: [EMAIL PROTECTED]> To: 
> [email protected]> Subject: Re: [osg-users] Problem with 
> using IntersectionVisitor to obtain the viewing matrices> > Hi Ke Li,> > I'm 
> confused by your code and what you are trying to do.> > The matrix stack 
> methods in IntersectionVisitor are there for internal> usage during the 
> traversal of the scene graph, as the traversal goes> down matrices are pushed 
> on the respective stacks, and on the return> up these matrices are popped. 
> Once the traversal is complete the> stacks will be empty unless you've pushed 
> ones on yourself.> > So the quesition still has to be what are you trying to 
> do? What do> you need the matrices for?> > Robert.> > > On Fri, Mar 14, 2008 
> at 9:31 PM, Ke Li <[EMAIL PROTECTED]> wrote:> >> > Dear all,> >> > I'm trying 
> to obtain the model matrix, the view matrix, the projection> > matrix and the 
> window matrix. I used the following piece of code in my pick> > event 
> handler.> >> > protected unsafe bool pick(OsgGA.GUIEventAdapter ea, 
> OsgViewer.Viewer> > viewer)> > {> > ......> > picker = new> > 
> OsgUtil.LineSegmentIntersector(OsgUtil.Intersector.CoordinateFrame.PROJECTION,>
>  > ea.getXnormalized(), ea.getYnormalized());> > OsgUtil.IntersectionVisitor 
> iv = new> > OsgUtil.IntersectionVisitor(picker);> > 
> viewer.getCamera().accept(iv);> >> > Osg.Matrix VPW = new Matrix();> > if 
> (iv.getWindowMatrix()!=null)> > {> > Osg.Matrix mm = new> > 
> Osg.Matrix(iv.getWindowMatrix().ptr());> > VPW.preMult(mm);> > }> > if 
> (iv.getProjectionMatrix() != null)> > {> > Osg.Matrix mm = new> > 
> Osg.Matrix(iv.getProjectionMatrix().ptr());> > VPW.preMult(mm);> > }> > if 
> (iv.getViewMatrix() != null)> > {> > Osg.Matrix mm = new> > 
> Osg.Matrix(iv.getViewMatrix().ptr());> > VPW.preMult(mm);> > }> > if 
> (iv.getModelMatrix() != null)> > {> > Osg.Matrix mm = new> > 
> Osg.Matrix(iv.getModelMatrix().ptr());> > VPW.preMult(mm);> > }> > ......> > 
> }> >> > But the iv object could never return any matrix and all the Boolean> 
> > expression in the if statement always equal to false.> >> > I'm wondering 
> what is wrong here. Is there any thing else that I need to do> > before I can 
> obtain the matrices?> >> > Any help is appreciated!> >> > Regards,> >> > Ke 
> Li> >> >> >> > ________________________________> > Connect and share in new 
> ways with Windows Live. Get it now!> > 
> _______________________________________________> > osg-users mailing list> > 
> [email protected]> > 
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org> >> 
> >> _______________________________________________> osg-users mailing list> 
> [email protected]> 
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
_________________________________________________________________
Shed those extra pounds with MSN and The Biggest Loser!
http://biggestloser.msn.com/
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to