One of them suggested other code for doing the picking with an ortho-matrix, others said I can just reuse my current code and throw a ortho-matrix in there instead of a projective-matrix (Which does not work).
There is one case where my code works somehow (Not 100% correct, but at least a bit): When the mesh I want to pick is almost at the same location as my camera is (Or so).
Can someone point me into the right direction? Here is my code:
/** Recalculates the projection matrix after e.g. a change of the windowsize */
void SceneRenderer::RecalculateProjectionMatrix()
{
if(bPerspectiveMatrix)
{
// Create perspective matrix
Aspect=SizeX/SizeY;
D3DXMatrixPerspectiveFovLH( &ProjectionMatrix, FOV, SizeX/SizeY, NearPlane, FarPlane );
}else
{
// Create ortho matrix
D3DXMatrixOrthoLH( &ProjectionMatrix, SizeX/OrthoZoom, SizeY/OrthoZoom, NearPlane, FarPlane);
}
}
// -- other file ---
/** Gets you the coordinates of the mouse cursor in 3D-World space */
void SceneRenderer_ToSwapchain::ComputeMouseWorldPos(D3DXVECTOR3* outLoc, D3DXVECTOR3* outDir, Scene* Scene)
{
D3DXVECTOR3 vPickRayDir;
D3DXVECTOR3 vPickRayOrig;
Scene->SetSceneCamera(SceneCamera);
D3DXMATRIX* pmatProj = &Scene->GetBasicObjectInfo()->Projection;
//Get the cursor 2D position
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient(SwapChainDesc.OutputWindow, &ptCursor );
RECT r;
GetClientRect(SwapChainDesc.OutputWindow, &r);
// Compute the vector of the Mouse in screen space
D3DXVECTOR3 v;
v.x = ( ( ( 2.0f * (ptCursor.x ) ) / r.right) - 1 ) / pmatProj->_11;
v.y = -( ( ( 2.0f * (ptCursor.y ) ) / r.bottom ) - 1 ) / pmatProj->_22;
v.z = 1;
// Get the inverse view matrix
D3DXMATRIXA16 m;
D3DXMatrixInverse( &m, NULL, &Scene->GetBasicObjectInfo()->View );
// Transform the screen space Mouse pos into 3D space
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;
vPickRayOrig.x = m._41;
vPickRayOrig.y = m._42;
vPickRayOrig.z = m._43;
D3DXVec3Normalize(&vPickRayDir,&vPickRayDir);
//And finally output them
*outLoc=vPickRayOrig;
*outDir=vPickRayDir;
}
Any suggestions are appreciated.






