Jump to content
  • Advertisement
Sign in to follow this  

Selecting Meshes with mouse

This topic is 4757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm wondering if there were more *. file meshes rendering how is the mouse picking (selecting) implemented. e.g. When I click on mesh to select it.

Share this post


Link to post
Share on other sites
Advertisement
If you've got an older version of the SDK...(maybe even last year's?) It's got a "pick" sample Direct3D project which shows how to do this.

I know it's in the DX81 SDK samples for sure.

hth,

Share this post


Link to post
Share on other sites
hi, using D3DX functions, it's not that hard.
The first step is to get the starting position and a direction in world space :




D3DVIEWPORT9 viewPort;
D3DXMATRIXA16 id;

g_pd3dDevice->GetViewport(&viewPort);
D3DXMatrixIdentity(&id);

D3DXVECTOR3 start((float)mousePos.x, (float)mousePos.y, 0.0f);
D3DXVECTOR3 end((float)mousePos.x, (float)mousePos.y, 1.0f);
D3DXVECTOR3 dir;

D3DXVec3Unproject(&start, &start, &viewPort, g_Camera.GetProjectionMatrix(), g_Camera.GetViewMatrix(), &id);
D3DXVec3Unproject(&end, &end, &viewPort, g_Camera.GetProjectionMatrix(), g_Camera.GetViewMatrix(), &id);
dir = end - start;
D3DXVec3Normalize(&dir, &dir);





where mousePos is your mouse position in screen space.

After that, you only need to use D3DX functions like :

D3DXIntersect // intersect a .x mesh
D3DXIntersectSubset // intersect a subset of a .x mesh
D3DXIntersectTri // intersect a single triangle

In my code, I use D3DXIntersectTri, because I don't use .x meshes, so here is the code for the actual intersection test :



D3DXVECTOR3 tDir; // transformed direction
D3DXVECTOR3 tStart; // transformed starting position

// transforme using inverse of the mesh's tranform matrix. If you don't
// do that, the intersection will be wrong if the world matrix for this
// mesh is not identity
D3DXVec3TransformNormal(&tDir, dir, &m_InverseWorldMatrix);
D3DXVec3TransformCoord(&tStart, start, &m_InverseWorldMatrix);

// I skeep the locking of the buffers, the loops, ect. Here, you just give
// the 3 D3DXVECTIR3 of the triangles (p0 ... P2), the starting point and
// direction (transformed ones), pointer to 3 floats : U, V and dist (read
// the doc to know what it is if you don't ^^)
if (D3DXIntersectTri(&p0, &p1, &p2, &tStart, &tDir, &U, &V, dist))
return(true);






[Edit]
Before testing every triangle, I also do a simple bouding sphere test, and then a bouding box test to optimize. You use these 2 functions for that :

D3DXBoxBoundProbe
D3DXSphereBoundProbe
[/Edit]


Ok, I think i didn't forget anything, if you have other questions, feel free to ask ^^

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!