Sign in to follow this  
GIS

Mesh Picking in Directx

Recommended Posts

GIS    122
Anyone familiar with this? I tried to implement it but I cannot get the intersection checking function to return me true when I click on the mesh. Not sure what went wrong... pls advise. For the code below... the part on conversion of mouse point to 3d point and the calculaation of the world transformation matrix of the 3d mesh should be correct. public void MouseDown(object sender, MouseEventArgs e ) { // The technique used here is to create a ray through the entire // logical 3d space and then perform a bounding box-ray intersection. Matrix proj = m_drawArgs.WorldCamera.ProjectionMatrix; Vector3 v, rayStart, rayDirection; IntersectInformation intInf; // convert mouse pt to 3d point v = convertMousePt(e.X, e.Y); clicked = true; Matrix m = m_drawArgs.WorldCamera.m_ViewMatrix * m_drawArgs.WorldCamera.m_WorldMatrix; m.Invert(); rayDirection.X = v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31; rayDirection.Y = v.X * m.M12 + v.Y * m.M22 + v.Z * m.M32; rayDirection.Z = v.X * m.M13 + v.Y * m.M23 + v.Z * m.M33; rayStart.X = m.M41; rayStart.Y = m.M42; rayStart.Z = m.M43; float minDistance = float.MaxValue; foreach(RenderableObject ro in this.m_children) { if(ro.IsOn) { if (((Shape2Obj)ro).Pick(m_drawArgs, rayStart, rayDirection, out intInf) ) MessageBox.Show("Success"); } } } public bool Pick(DrawArgs drawArgs, Vector3 rayStart, Vector3 rayDirection, out IntersectInformation intInf) { //world matrix of the mesh Matrix World = Matrix.RotationX((float)MathEngine.DegreesToRadians(90-xRotation))* Matrix.RotationZ((float)MathEngine.DegreesToRadians(-Orientation)) * Matrix.Scaling(Scale, Scale, Scale) * Matrix.Translation(0,0,(float)drawArgs.WorldCamera.WorldRadius + Altitude*jpVertExaggeration) * Matrix.RotationY((float)MathEngine.DegreesToRadians(90-Latitude)) * Matrix.RotationZ((float)MathEngine.DegreesToRadians(Longitude)); Matrix inverseWorld = Matrix.Invert(World); Vector3 localStart = Vector3.TransformCoordinate(rayStart, inverseWorld); Vector3 localDirection = Vector3.TransformNormal(rayDirection, inverseWorld); localDirection.Normalize(); return mesh.Intersect(localStart, localDirection, out intInf); }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this