Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Mesh Picking

Started by Dharshi Feb 27, 2008 at 3:57 AM 0 replies 1.2k views
Original Post
Dharshi
Dharshi
Hai, I am new to DirectX.I wrote a code to render directx file and its working fine.But what I need now is to pick the mesh.I used the following way, Note: I call below MeshPick function in MouseDown function private bool MeshPick(Mesh mesh, int x, int y) { bool intersects; string status; private float vert1x,vert1y,vert1z,vert2x,vert2y,vert2z,vert3x,vert3y,vert3z; private CustomVertex.TransformedColored[] vert; Vector3 v3Near = new Vector3(x, y, 0); Vector3 v3Far = new Vector3(x, y, 1); v3Near.Unproject(device.Viewport,device.Transform.Projection,device.Transform.View,device.Transform.World); v3Far.Unproject(device.Viewport,device.Transform.Projection,device.Transform.View,device.Transform.World); v3Far.Subtract(v3Near); // Retrieve intersection information IntersectInformation closestIntersection = new IntersectInformation(); intersects = mesh.Intersect(v3Near,v3Far,out closestIntersection); if (intersects) { status = string.Format("Face={0},tu={1},tv={2}",closestIntersection.FaceIndex,closestIntersection.U,closestIntersection.V); HighlightIntersectedFace(closestIntersection); return true; } else return false; } private void HighlightIntersectedFace(IntersectInformation ii) { // create an array to hold the indices for the intersected face short[] intersectedIndices = new short[3]; // fetch indices for the intersected face from the mesh short[] indices = (short[])mesh.LockIndexBuffer(typeof(short), LockFlags.ReadOnly, mesh.NumberFaces * 3); Array.Copy(indices, ii.FaceIndex * 3, intersectedIndices, 0, 3); mesh.UnlockIndexBuffer(); CustomVertex.PositionNormalTextured[] tempIntersectedVertices = new CustomVertex.PositionNormalTextured[3]; // extract vertex data from mesh, using our indices we obtained earlier CustomVertex.PositionNormalTextured[] meshVertices = (CustomVertex.PositionNormalTextured[])mesh.LockVertexBuffer(typeof(CustomVertex.PositionNormalTextured), LockFlags.ReadOnly, mesh.NumberVertices); tempIntersectedVertices[0] = meshVertices[intersectedIndices[0]]; tempIntersectedVertices[1] = meshVertices[intersectedIndices[1]]; tempIntersectedVertices[2] = meshVertices[intersectedIndices[2]]; mesh.UnlockVertexBuffer(); this.intersectedVertices = tempIntersectedVertices; vert =new CustomVertex.TransformedColored[3]; //Get vertices1 x,y and z position vert1x = this.intersectedVertices[0].Position.X; vert1y = this.intersectedVertices[0].Position.Y; vert1z = this.intersectedVertices[0].Position.Z; //Get vertices2 x,y and z position vert2x = this.intersectedVertices[1].Position.X; vert2y = this.intersectedVertices[1].Position.Y; vert2z = this.intersectedVertices[1].Position.Z; //Get vertices3 x,y and z position vert3x = this.intersectedVertices[2].Position.X; vert3y = this.intersectedVertices[2].Position.Y; vert3z = this.intersectedVertices[2].Position.Z; vert[0].Position = new Vector3(vert1x,vert1y,vert1z); vert[0].Color = Color.Yellow.ToArgb(); vert[1].Position = new Vector3(vert2x,vert2y,vert2z); vert[1].Color = Color.Yellow.ToArgb(); vert[2].Position = new Vector3(vert3x,vert3y,vert3z); vert[2].Color = Color.Yellow.ToArgb(); device.VertexFormat = CustomVertex.TransformedColored.Format; device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vert); } I get the ray intersected position vertices from this.intersectedVertices .But Triangle not drawn. I don't know whether the vertices are the correct one or not. I want to get ray intersected near triangle vertices.By those vertices draw triangle andthen highlight that triangle using color.Please help me. How to do it?
Buckeye
Buckeye
A general guideline for writing good code is to start out with simple code and make sure it works before you add more routines.

You've written two routines and you don't know if either of them work correctly:

- the intersection routine
- the triangle rendering routine

Pick just one routine and make sure it works.

For instance, it appears that you don't know whether the DrawTriangles routine is correct. You should make sure that routine works before you try to render data that you aren't sure of.

- instead of rendering the intersected vertices, render a triangle you know is correct. When you get the rendering routine to work correctly, see if the intersected vertices render correctly.

- if the intersected vertices do not render correctly, use debugging techniques to determine what in your pick routine isn't correct.

There are several debugging techniques you can use:

- PRIMARY: check return codes from function calls to make sure there are no errors. Use the deubgger!
- output debug strings displaying the vertex coordinates to see if they are correct.
- setup breakpoints in your code and check variables and return codes to see if the variables you pass to your Intersect call are correct.

Looking at the code you've posted, I suggest making sure your routine to render a triangle is correct before you try the intersection routine. Look at examples in the SDK and copy them or imitate them.

When you've checked out your code further, let us know if you have further problems.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.