Jump to content
  • Advertisement
Sign in to follow this  
Warvstar

Can you tell me whats wrong with my pick function?

This topic is 3414 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

Hey all, just wondering if anyone could help me out with this. Right now I have it so if Vec3f returned is not null then it increases the size of the mesh. I'm getting weird results with it at the moment. I have two meshes, one at 0,0,0 and the other at 100,100,100. Camera is at 200, 100, 1000 and is viewing top down. I would appreciate any help, thanks!
Vec3f D3DRenderer::Pick(Matrix* World, const VertexS* vertexbuffer, const int* indexbuffer, Vec3f screenpos, RenderJob* renderJob )
{
    HRESULT hr;
	D3DXVECTOR3 rayObjOrigin,rayObjDirection;

    D3DVertexBuffer *pVB = (D3DVertexBuffer*)m_pRenderObjectFactory->GetRenderObject(renderJob->GetVertexBufferID());
    D3DIndexBuffer  *pIB = (D3DIndexBuffer*) m_pRenderObjectFactory->GetRenderObject(renderJob->GetIndexBufferID());



	int vc = pVB->GetNumOfVertices();
	int indicec = pIB->GetNumOfIndices();

	unsigned int i[36];

	std::vector<VertexS*> Vert = pVB->GetSimple();
	const uint32** Index = pIB->GetIndices();

	for (int pp = 0; pp < indicec; pp++)
	{
	i[pp] = *Index[pp];
	}
	vertex vv1[8];
	for (int p = 0; p < vc; p++)
	{
	vv1

= vertex(D3DXVECTOR3( Vert

->position.x, Vert

->position.y, Vert

->position.z ), D3DXVECTOR4( Vert

->color.x, Vert

->color.y, Vert

->color.z, 1 )); } //const DXGI_SURFACE_DESC* pd3dsdBackBuffer = DXUTGetDXGIBackBufferSurfaceDesc(); bool g_bAllHits = false; g_nNumIntersections = 0L; POINT ptCursor; GetCursorPos( &ptCursor ); ScreenToClient( this->m_hWnd, &ptCursor ); // Compute the vector of the pick ray in screen space D3DXVECTOR3 v; v.x = ptCursor.x;//screenpos.x; v.y = ptCursor.y;//screenpos.y; v.z = 1.0f; v.x = ( ( ( 2.0f * ptCursor.x ) / 640.0f ) - 1 ) / m_ProjMatrix._11; v.y = -( ( ( 2.0f * ptCursor.y ) / 480.0f ) - 1 ) / m_ProjMatrix._22; v.z = 1.0f; // Get the inverse view matrix D3DXMATRIX matWorld; matWorld._11 = World->_ms._11; matWorld._12 = World->_ms._12; matWorld._13 = World->_ms._13; matWorld._14 = World->_ms._14; matWorld._21 = World->_ms._21; matWorld._22 = World->_ms._22; matWorld._23 = World->_ms._23; matWorld._24 = World->_ms._24; matWorld._31 = World->_ms._31; matWorld._32 = World->_ms._32; matWorld._33 = World->_ms._33; matWorld._34 = World->_ms._34; matWorld._41 = World->_ms._41; matWorld._42 = World->_ms._42; matWorld._43 = World->_ms._43; matWorld._44 = World->_ms._44; D3DXMATRIX* m_WorldMatrix = new D3DXMATRIX;; m_WorldMatrix = &matWorld; //D3DXMatrixInverse(m_WorldMatrix, NULL, &m_ViewMatrix); //D3DXMatrixIdentity(m_WorldMatrix); D3DXVECTOR3* result2 = new D3DXVECTOR3; //D3DXVec3Unproject(result2, &v, &this->viewPort, &m_ProjMatrix, &m_ViewMatrix, m_WorldMatrix ); vPickRayDir.x = result2->x; vPickRayDir.y = result2->y; vPickRayDir.z = result2->z; D3DXMATRIX m; D3DXMatrixInverse( &m, NULL, &m_ViewMatrix ); vPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31; vPickRayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32; vPickRayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33; D3DXVec3Normalize(&vPickRayDir,&vPickRayDir); vPickRayOrig.x = m._41; vPickRayOrig.y = m._42; vPickRayOrig.z = m._43; vPickRayOrig+=vPickRayDir*1.0f; D3DXMATRIX m_worldMat; D3DXMATRIX m_combinedMat; D3DXMatrixIdentity(&m_worldMat); D3DXMatrixMultiply(&m_combinedMat,&m_ObjectWorld,&m_ViewMatrix); D3DXVECTOR3 vNear,vDir; D3DXMATRIX invMat; D3DXMatrixInverse(&invMat,NULL,&m_combinedMat); D3DXVec3TransformCoord(&vNear,&vPickRayOrig,&invMat); D3DXVec3TransformNormal(&vDir,&vPickRayDir,&invMat); UINT nFace; FLOAT fBary1, fBary2, fDist; UINT nHitCount = 0; ID3DX10Mesh* qMesh; D3DX10CreateMesh( m_lpD3DDevice, vertexInputLayout, 2, vertexInputLayout[0].SemanticName, vc, indicec / 3, D3DX10_MESH_32_BIT, &qMesh); qMesh->SetVertexData(0, vv1); qMesh->SetIndexData(i, indicec); qMesh->CommitToDevice(); qMesh->Intersect( &vNear, &vDir, &nHitCount, &nFace, &fBary1, &fBary2, &fDist, NULL); if( nHitCount > 0 ) { return Vec3f(vDir.x,vDir.y,vDir.z); } DWORD dwNumFaces = 36/3; for( DWORD p = 0; p < dwNumFaces; p++ ) { D3DXVECTOR3 v0 = vv1[i[3 * p + 0]].pos; D3DXVECTOR3 v1 = vv1[i[3 * p + 1]].pos; D3DXVECTOR3 v2 = vv1[i[3 * p + 2]].pos; if( IntersectTriangle( Vec3f(vNear.x, vNear.y, vNear.z), Vec3f(vDir.x, vDir.y, vDir.z), Vec3f(v0.x, v0.y, v0.z), Vec3f(v1.x, v1.y, v1.z), Vec3f(v2.x, v2.y, v2.z), &fDist, &fBary1, &fBary2 ) ) { return Vec3f(vDir.x,vDir.y,vDir.z); } } return Vec3f(0,0,0); }

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!