Sign in to follow this  
be17_17y

Triangle Selector

Recommended Posts

How to make my heightmap return the point x,z on the terrain based on mouse clicking point ?. i want to try D3DXIntersectTri, but i don't know how to get back the vertexbuffer of every triangle of the heightmap.... help me please...... :)

Share this post


Link to post
Share on other sites
Have you thought about using D3DXIntersect instead? With that function, it will do a ray intersect with a full mesh, not just a single triangle. You just pass in your terrain and the picking ray, and you'll get a face index, a set of barycentric coords and a distance if there's a collision.

From there, you can use the XYZ of the face vertexes along with the barycentric coords it gives you to find the XYZ where the mouse click was, or just normalize your picking ray and mulitply it times the distance to find a less accurate XYZ.


Under the Gun Online

Share this post


Link to post
Share on other sites
hmm... Interesting..
can u show me example to use or where can i find that (don't sdk)?
most importantly to find XYZ...
Thanks for sharing.....:)

Share this post


Link to post
Share on other sites
Here's the source for a utility mesh interect that I'm using. I don't have an example of converting screen coords to world space coords, but you can find that on the net pretty easily.

I'm passing in an object (contains a pointer to a mesh) a ray start location, a ray direction (normalized) and pointers for output: a face index, a distance, and a XYZ vector that gets filled in with the intersection location. I don't think it's going to be as precise as generating the location from the barycentric coords, but for what I'm doing it doesn't matter as much.



bool CManagerInput::RayMeshIntersect( CWorldObject* pObject,
D3DXVECTOR3 *vec3RayOrigin,
D3DXVECTOR3 *vec3RayDest,
DWORD *dwIndex,
float *fDistOut,
D3DXVECTOR3 *vec3WorldSpaceIntersect )
{
if ( pObject->m_pMesh != NULL )
{
// there's a mesh, convert ray to model space
D3DXVECTOR3 vNear,vDir;
D3DXMATRIX invMat;
D3DXMatrixInverse( &invMat,NULL,&pObject->m_matWorld );
D3DXVec3TransformCoord(&vNear,vec3RayOrigin,&invMat);
D3DXVec3TransformNormal(&vDir,vec3RayDest,&invMat);

// test for intersection
BOOL bHit;
//DWORD dwIndex;
float u,v;
float dist;
D3DXIntersect(pObject->m_pMesh->m_pMesh,&vNear,&vDir,&bHit,dwIndex,&u,&v,&dist,NULL,NULL);

// is there an intersection?
if (bHit)
{
*vec3WorldSpaceIntersect = vNear + ( vDir * dist );
*fDistOut = dist;
return true;
}
}


return false;
}


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