History: ThreeDeeObjectPicking
Source of version: 6
Copy to clipboard
This could be used for 3D object picking from a Win32 aplication, I use it from a OnMouseMove callback :
{CODE(wrap="1", colors="c++")} RECT rc;
this->GetClientRect(&rc);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
float tx = (float)(1.0f / width) * cpt.x;
float ty = (float)(1.0f / height) * cpt.y;
Camera *pCam = ogreEngine::getSingletonPtr()->getCurrentCamera();
// calc the ray using normalised screen coordinates [0-1]
Ray ray = pCam->getCameraToViewportRay(tx - 0.5, 0.5 - ty);
// Set up the ray query
if (!mpRayQuery)
mpRayQuery = ogreEngine::getSingletonPtr()->getSceneManager()->createRayQuery(ray);
else
mpRayQuery->setRay(ray); // update ray
// Sort by distance, and say we're only interested in the first hit
mpRayQuery->setSortByDistance(true, 1);
// Execute
RaySceneQueryResult res = mpRayQuery->execute();
RaySceneQueryResult::iterator it = res.begin();
if (it != res.end())
{
mCurrMO = it->movable;
this->updateProgressListeners("mouse : " + mCurrMO->getName());
}
else
{
this->updateProgressListeners("mouse : " + StringConverter::toString(tx) + "x" + StringConverter::toString(ty));
mCurrMO = NULL;
}
mpRayQuery->clearResults();
{CODE}