您的位置:首页 > 其它

Ogre立方体查询

2015-07-29 14:35 295 查看
Ogre立方体查询
ogre还有一个立方体查询。
//---------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void):mRenderer(0),mSelectionBox(0),mSelecting(false)
{
}
//---------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void)
{
CEGUI::OgreRenderer::destroySystem();
if(mSelectionBox)
delete mSelectionBox;
}

//---------------------------------------------------------------------------
void TutorialApplication::createScene(void)
{
// Create your scene here :)
setupCEGui();
// 设置环境光
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.7,0.7,0.7));

for (int i= 0; i<10;++i)
for (int j = 0;j < 10; ++j)
{
Ogre::Entity* ent = mSceneMgr->createEntity("robot.mesh");
Ogre::SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
node->setPosition(Ogre::Vector3(i*15,0,j*15));
node->attachObject(ent);
node->setScale(0.2,0.2,0.2);
}

mCamera->setPosition(-60,100,-60);
mCamera->lookAt(60,0,60);

mSelectionBox = new SelectionBox("SelectionBox");
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(mSelectionBox);

CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
CEGUI::Window* rootWin = wmgr.createWindow("DefaultWindow", "CEGUIDemo/Sheet");
CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(rootWin);

mVolQuery = mSceneMgr->createPlaneBoundedVolumeQuery(Ogre::PlaneBoundedVolumeList());
}

bool TutorialApplication::quit(const CEGUI::EventArgs &e)
{
mShutDown = true;
return true;
}

//---------------------------------------------------------------------------
bool TutorialApplication::mouseMoved(const OIS::MouseEvent &arg)
{
//关联鼠标
if(CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseMove(arg.state.X.rel, arg.state.Y.rel))
{
if(mSelecting)
{
CEGUI::MouseCursor* mouse = &CEGUI::System::getSingleton().getDefaultGUIContext().getMouseCursor();
mStop.x = mouse->getPosition().d_x/(float)arg.state.width;
mStop.y = mouse->getPosition().d_y/(float)arg.state.height;

mSelectionBox->setCorners(mStart,mStop);
}

return true;
}

return BaseApplication::mouseMoved(arg);
}
//---------------------------------------------------------------------------
bool TutorialApplication::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
if(context.injectMouseButtonDown(convertButton(id)))
{
if (id == OIS::MB_Left)
{
CEGUI::MouseCursor* mouse = &context.getMouseCursor();
mStart.x = mouse->getPosition().d_x/(float)arg.state.width;
mStart.y = mouse->getPosition().d_y/(float)arg.state.height;

mStop = mStart;

mSelecting = true;

mSelectionBox->clear();
mSelectionBox->setVisible(true);

}
else if (id == OIS::MB_Right)
{

}
return true;
}

//关联鼠标
return BaseApplication::mousePressed(arg,id);
}
//---------------------------------------------------------------------------
bool TutorialApplication::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
if (id == OIS::MB_Left)
{
performSelection(mStart, mStop);
mSelecting = false;
mSelectionBox->setVisible(false);
}

//关联鼠标
if(CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp(convertButton(id))) return true;
return BaseApplication::mouseReleased(arg,id);
}

CEGUI::MouseButton TutorialApplication::convertButton(OIS::MouseButtonID buttonID)
{
//关联鼠标按键
switch (buttonID)
{
case OIS::MB_Left:
return CEGUI::LeftButton;
break;

case OIS::MB_Right:
return CEGUI::RightButton;
break;

case OIS::MB_Middle:
return CEGUI::MiddleButton;
break;

default:
return CEGUI::LeftButton;
break;
}
}

void TutorialApplication::setupCEGui()
{
Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing CEGUI ***");

mRenderer = &CEGUI::OgreRenderer::bootstrapSystem();

//设置资源路径
CEGUI::ImageManager::setImagesetDefaultResourceGroup("Imagesets");
CEGUI::Font::setDefaultResourceGroup("Fonts");
CEGUI::Scheme::setDefaultResourceGroup("Schemes");
CEGUI::WidgetLookManager::setDefaultResourceGroup("LookNFeel");
CEGUI::WindowManager::setDefaultResourceGroup("Layouts");

//设置鼠标图片
CEGUI::SchemeManager::getSingleton().createFromFile("TaharezLook.scheme");
CEGUI::FontManager::getSingleton().createFromFile("DejaVuSans-10.font");
CEGUI::System::getSingleton().getDefaultGUIContext().getMouseCursor().setDefaultImage("TaharezLook/MouseArrow");

CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();

context.setDefaultFont("DejaVuSans-10");
context.getMouseCursor().setDefaultImage("TaharezLook/MouseArrow");

Ogre::LogManager::getSingletonPtr()->logMessage("Finished");
}

void TutorialApplication::destroyScene()
{

}

void TutorialApplication::swap(float& x,float& y)
{
float temp = x;
x = y;
y = temp;
}

void TutorialApplication::performSelection(const Ogre::Vector2& first,const Ogre::Vector2& second)
{
float left = first.x,right = second.x;
float top = first.y, bottom = second.y;
if(left > right)
swap(left,right);
if (top > bottom)
swap(top,bottom);
if ((right - left)*(bottom - top) < 0.0001)
return;

Ogre::Ray topLeft = mCamera->getCameraToViewportRay(left,top);
Ogre::Ray topRight = mCamera->getCameraToViewportRay(right,top);
Ogre::Ray bottomLeft = mCamera->getCameraToViewportRay(left,bottom);
Ogre::Ray bottomRight = mCamera->getCameraToViewportRay(right,bottom);

Ogre::Plane frontPlane, topPlane, leftPlane, bottomPlane, rightPlane;

frontPlane = Ogre::Plane(
topLeft.getOrigin(),
topRight.getOrigin(),
bottomRight.getOrigin());

topPlane = Ogre::Plane(
topLeft.getOrigin(),
topLeft.getPoint(10),
topRight.getPoint(10));

leftPlane = Ogre::Plane(
topLeft.getOrigin(),
bottomLeft.getPoint(10),
topLeft.getPoint(10));

bottomPlane = Ogre::Plane(
bottomLeft.getOrigin(),
bottomRight.getPoint(10),
bottomLeft.getPoint(10));

rightPlane = Ogre::Plane(
topRight.getOrigin(),
topRight.getPoint(10),
bottomRight.getPoint(10));

Ogre::PlaneBoundedVolume vol;

vol.planes.push_back(frontPlane);
vol.planes.push_back(topPlane);
vol.planes.push_back(leftPlane);
vol.planes.push_back(bottomPlane);
vol.planes.push_back(rightPlane);

Ogre::PlaneBoundedVolumeList volList;
volList.push_back(vol);

mVolQuery->setVolumes(volList);
Ogre::SceneQueryResult result = mVolQuery->execute();

deselectObjects();

Ogre::SceneQueryResultMovableList::iterator it;
for (it = result.movables.begin(); it != result.movables.end(); ++it)
{
std::cout << (*it)->getParentSceneNode()->getPosition() << std::endl;;
selectObject(*it);
}
}

void TutorialApplication::deselectObjects()
{
std::list<Ogre::MovableObject*>::iterator it;

for (it = mSelected.begin(); it != mSelected.end(); ++it)
(*it)->getParentSceneNode()->showBoundingBox(false);
}

void TutorialApplication::selectObject(Ogre::MovableObject* obj)
{
obj->getParentSceneNode()->showBoundingBox(true);
mSelected.push_back(obj);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: