Selecting Meshes with mouse

Started by
4 comments, last by Odiee 18 years, 11 months ago
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.
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,
Learn about game programming!Games Programming in C++: Start to Finish
The SDK sample might not be that good for you. Perhaps try the picking articles Robert Dunlop has written on his website. (Look them up in the articles section)

With optimizations aside, you "pick" both and select the one with shorter distance.
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 directionD3DXVECTOR3   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 identityD3DXVec3TransformNormal(&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 ^^
cool, TNX

This topic is closed to new replies.

Advertisement