Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualVoidAccess

Posted 28 March 2013 - 05:49 PM

Still having issues getting this to work, I tried to get the shortest distance from the mouse click but its not working right. Just want the closest vertex to be selected from click on the triangle

 

void PickingApp::pick(int sx, int sy)
{
	D3DXMATRIX P = GetCamera().proj();

	// Compute picking ray in view space.
	float vx = (+2.0f*sx/mClientWidth  - 1.0f)/P(0,0);
	float vy = (-2.0f*sy/mClientHeight + 1.0f)/P(1,1);

	D3DXVECTOR3 rayOrigin(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 rayDir(vx, vy, 1.0f);

	// Tranform to world space.
	D3DXMATRIX V = GetCamera().view();

	D3DXMATRIX inverseV;
	D3DXMatrixInverse(&inverseV, 0, &V);

	D3DXVec3TransformCoord(&rayOrigin, &rayOrigin, &inverseV);
	D3DXVec3TransformNormal(&rayDir, &rayDir, &inverseV);
	
	// Transform to the mesh's local space.
	D3DXMATRIX inverseW;
	D3DXMatrixInverse(&inverseW, 0, &mBaseWorld);

	D3DXVec3TransformCoord(&rayOrigin, &rayOrigin, &inverseW);
	D3DXVec3TransformNormal(&rayDir, &rayDir, &inverseW);
	
	ID3DX10Mesh* d3dxmesh = mBaseMesh.d3dxMesh();
	
	MouseCoord = rayOrigin;

	UINT hitCount;
	float u,v,t;
	ID3D10Blob* allHits;
	d3dxmesh->Intersect(&rayOrigin, &rayDir, &hitCount, 
		&mPickedTriangle, &u, &v, &t, &allHits);

	ReleaseCOM(allHits);
}
drawScene(){
...
...
DWORD i0 = indices[mPickedTriangle*3+0];
DWORD i1 = indices[mPickedTriangle*3+1];
DWORD i2 = indices[mPickedTriangle*3+2];

HR(ib->Unmap());

// Get the vertices of the picked triangle and copy them into our VB.

MeshVertex* vertices = 0;
HR(vb->Map((void**)&vertices, &size));

D3DXVECTOR3* triVerts = 0;
		
HR(mPickedTriVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&triVerts));

float v1 = sqrt((pow(vertices[i0].pos.x - MouseCoord.x  ,2) + pow(vertices[i0].pos.y - MouseCoord.y,2) + pow(vertices[i0].pos.z - MouseCoord.z,2)));

float v2 = sqrt((pow(vertices[i1].pos.x - MouseCoord.x  ,2) + pow(vertices[i1].pos.y - MouseCoord.y,2) + pow(vertices[i1].pos.z - MouseCoord.z,2)));

float v3 = sqrt((pow(vertices[i2].pos.x - MouseCoord.x  ,2) + pow(vertices[i2].pos.y - MouseCoord.y,2) + pow(vertices[i2].pos.z - MouseCoord.z,2)));

		if(v1 < v2 && v1 < v3)
			triVerts[0] = vertices[i0].pos;
		if(v2 < v1 && v2 < v3)
			triVerts[0] = vertices[i1].pos;
		if(v3 < v1 && v3 < v2)
			triVerts[0] = vertices[i2].pos;
...}

#1VoidAccess

Posted 28 March 2013 - 05:49 PM

Still having issues getting this to work, I tried to get the shortest distance from the mouse click but its not working right. Just want the closest vertex to be selected from click on the triangle

 

void PickingApp::pick(int sx, int sy)
{
	D3DXMATRIX P = GetCamera().proj();

	// Compute picking ray in view space.
	float vx = (+2.0f*sx/mClientWidth  - 1.0f)/P(0,0);
	float vy = (-2.0f*sy/mClientHeight + 1.0f)/P(1,1);

	D3DXVECTOR3 rayOrigin(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 rayDir(vx, vy, 1.0f);

	// Tranform to world space.
	D3DXMATRIX V = GetCamera().view();

	D3DXMATRIX inverseV;
	D3DXMatrixInverse(&inverseV, 0, &V);

	D3DXVec3TransformCoord(&rayOrigin, &rayOrigin, &inverseV);
	D3DXVec3TransformNormal(&rayDir, &rayDir, &inverseV);
	
	// Transform to the mesh's local space.
	D3DXMATRIX inverseW;
	D3DXMatrixInverse(&inverseW, 0, &mBaseWorld);

	D3DXVec3TransformCoord(&rayOrigin, &rayOrigin, &inverseW);
	D3DXVec3TransformNormal(&rayDir, &rayDir, &inverseW);
	
	ID3DX10Mesh* d3dxmesh = mBaseMesh.d3dxMesh();
	
	MouseCoord = rayOrigin;

	UINT hitCount;
	float u,v,t;
	ID3D10Blob* allHits;
	d3dxmesh->Intersect(&rayOrigin, &rayDir, &hitCount, 
		&mPickedTriangle, &u, &v, &t, &allHits);

	ReleaseCOM(allHits);
}
DWORD i0 = indices[mPickedTriangle*3+0];
DWORD i1 = indices[mPickedTriangle*3+1];
DWORD i2 = indices[mPickedTriangle*3+2];

HR(ib->Unmap());

// Get the vertices of the picked triangle and copy them into our VB.

MeshVertex* vertices = 0;
HR(vb->Map((void**)&vertices, &size));

D3DXVECTOR3* triVerts = 0;
		
HR(mPickedTriVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&triVerts));

float v1 = sqrt((pow(vertices[i0].pos.x - MouseCoord.x  ,2) + pow(vertices[i0].pos.y - MouseCoord.y,2) + pow(vertices[i0].pos.z - MouseCoord.z,2)));

float v2 = sqrt((pow(vertices[i1].pos.x - MouseCoord.x  ,2) + pow(vertices[i1].pos.y - MouseCoord.y,2) + pow(vertices[i1].pos.z - MouseCoord.z,2)));

float v3 = sqrt((pow(vertices[i2].pos.x - MouseCoord.x  ,2) + pow(vertices[i2].pos.y - MouseCoord.y,2) + pow(vertices[i2].pos.z - MouseCoord.z,2)));

		if(v1 < v2 && v1 < v3)
			triVerts[0] = vertices[i0].pos;
		if(v2 < v1 && v2 < v3)
			triVerts[0] = vertices[i1].pos;
		if(v3 < v1 && v3 < v2)
			triVerts[0] = vertices[i2].pos;

PARTNERS