ogre射线数组的拣选
先来看代码。
bool TutorialApplication::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
context.injectMouseButtonDown(convertButton(id));
if (id == OIS::MB_Left)
{
CEGUI::Vector2f mousePos = context.getMouseCursor().getPosition();
Ogre::Ray mouseRay =
mCamera->getCameraToViewportRay(
mousePos.d_x / float(arg.state.width),
mousePos.d_y / float(arg.state.height));
mRayScnQuery->setRay(mouseRay);
mRayScnQuery->setSortByDistance(true);
mRayScnQuery->setQueryMask(mRobotMode? ROBOT_MASK :NINJA_MASK );
Ogre::RaySceneQueryResult& result = mRayScnQuery->execute();
Ogre::RaySceneQueryResult::iterator it = result.begin();
bool movableFound = false;
for (;it != result.end();++it)
{
movableFound =
it->movable &&
it->movable->getName() != ""&&
it->movable->getName() != "PlayerCam";
if (movableFound)
{
mCurObject = it->movable->getParentSceneNode();
if (mCurObject)
mCurObject->showBoundingBox(true);
break;
}
}
if(!movableFound)
{
Ogre::TerrainGroup::RayResult terrainResult = mTerrainGroup->rayIntersects(mouseRay);
if (terrainResult.terrain)
{
Ogre::Entity* ent = NULL;
if(!mRobotMode)
{
ent = mSceneMgr->createEntity("ninja.mesh");
ent->setQueryFlags(NINJA_MASK);
}
else
{
ent = mSceneMgr->createEntity("robot.mesh");
ent->setQueryFlags(ROBOT_MASK);
}
mCurObject = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mCurObject->setPosition(terrainResult.position);
mCurObject->setScale(0.2, 0.2, 0.2);
mCurObject->attachObject(ent);
}
}
else
{
CEGUI::Window* terrainLabel =
CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->getChild(
"TerrainLabel");
std::string name = "robot";
if(!mRobotMode)
name = "ninja";
std::string tips = "pick" + name;
terrainLabel->setText(tips);
}
}
else if (id == OIS::MB_Right)
{
mRobotMode = !mRobotMode;
}
//关联鼠标
if(CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown(convertButton(id))) return true;
return BaseApplication::mousePressed(arg,id);
}
在创建物体时,我们可以设置标志码。
enum QueryFlags
{
NINJA_MASK = 1 << 0,
ROBOT_MASK = 1 << 1
};
mRayScnQuery = mSceneMgr->createRayQuery(Ogre::Ray());
这样我们就可以按标志位来查找物体。