Skip to main content
SelectionBox.h
Copy to clipboard
#ifndef SELECTIONBOX_H #define SELECTIONBOX_H #include <OgreManualObject.h> class SelectionBox : public Ogre::ManualObject { public: SelectionBox(const Ogre::String& name); virtual ~SelectionBox(); void setCorners(float left, float top, float right, float bottom); void setCorners(const Ogre::Vector2& topLeft, const Ogre::Vector2& bottomRight); }; #endif /* SELECTIONBOX_H */

SelectionBox.cpp
Copy to clipboard
#include "SelectionBox.h" SelectionBox::SelectionBox(const Ogre::String& name) : Ogre::ManualObject(name) { setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY); setUseIdentityProjection(true); setUseIdentityView(true); setQueryFlags(0); } SelectionBox::~SelectionBox() { } void SelectionBox::setCorners(float left, float top, float right, float bottom) { left = 2 * left - 1; right = 2 * right - 1; top = 1 - 2 * top; bottom = 1 - 2 * bottom; clear(); begin("Examples/KnotTexture", Ogre::RenderOperation::OT_LINE_STRIP); position(left, top, -1); position(right, top, -1); position(right, bottom, -1); position(left, bottom, -1); position(left, top, -1); end(); setBoundingBox(Ogre::AxisAlignedBox::BOX_INFINITE); } void SelectionBox::setCorners( const Ogre::Vector2& topLeft, const Ogre::Vector2& bottomRight) { setCorners(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y); }

BasicApp.h
Copy to clipboard
#ifndef BASICAPP_H #define BASICAPP_H #include <OgreRoot.h> #include <OgreCamera.h> #include <OgreViewport.h> #include <OgreSceneManager.h> #include <OgreRenderWindow.h> #include <OgreConfigFile.h> #include <OgreException.h> #include <OgreEntity.h> #include <OgreFrameListener.h> #include <OgreWindowEventUtilities.h> #include <OgreSceneQuery.h> #include <OISEvents.h> #include <OISInputManager.h> #include <OISKeyboard.h> #include <OISMouse.h> #include <CEGUI/CEGUI.h> #include <CEGUI/RendererModules/Ogre/Renderer.h> #include <SdkCameraMan.h> #include "SelectionBox.h" class BasicApp : public Ogre::WindowEventListener, public Ogre::FrameListener, public OIS::KeyListener, public OIS::MouseListener { public: BasicApp(); ~BasicApp(); void go(); private: bool mShutdown; Ogre::Root* mRoot; Ogre::Camera* mCamera; Ogre::SceneManager* mSceneMgr; Ogre::RenderWindow* mWindow; Ogre::String mResourcesCfg; Ogre::String mPluginsCfg; OgreBites::SdkCameraMan* mCameraMan; virtual bool frameRenderingQueued(const Ogre::FrameEvent& fe); virtual bool keyPressed(const OIS::KeyEvent& ke); virtual bool keyReleased(const OIS::KeyEvent& ke); virtual bool mouseMoved(const OIS::MouseEvent& me); virtual bool mousePressed(const OIS::MouseEvent& me, OIS::MouseButtonID id); virtual bool mouseReleased(const OIS::MouseEvent& me, OIS::MouseButtonID id); virtual void windowResized(Ogre::RenderWindow* rw); virtual void windowClosed(Ogre::RenderWindow* rw); bool setup(); bool configure(); void chooseSceneManager(); void createCamera(); void createScene(); void destroyScene(); void createFrameListener(); void createViewports(); void setupResources(); void createResourceListener(); void loadResources(); // CEGUI CEGUI::OgreRenderer* mRenderer; bool setupCEGUI(); // OIS OIS::Mouse* mMouse; OIS::Keyboard* mKeyboard; OIS::InputManager* mInputMgr; ////////////////////// // Tutorial Section // ////////////////////// void performSelection(const Ogre::Vector2& first, const Ogre::Vector2& second); void deselectObjects(); void selectObject(Ogre::MovableObject* obj); static void swap(float& x, float& y); bool mSelecting; bool mLMouseDown, mRMouseDown; float mRotSpd; SelectionBox* mSelectionBox; std::list<Ogre::MovableObject*> mSelected; Ogre::Vector2 mStart, mStop; Ogre::RaySceneQuery* mRayScnQuery; Ogre::PlaneBoundedVolumeListSceneQuery* mVolQuery; }; #endif


BasicApp.cpp
Copy to clipboard
#include "BasicApp.h" BasicApp::BasicApp() : mShutdown(false), mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0), mResourcesCfg(Ogre::StringUtil::BLANK), mPluginsCfg(Ogre::StringUtil::BLANK), mCameraMan(0), mRenderer(0), mMouse(0), mKeyboard(0), mInputMgr(0), mSelecting(false), mLMouseDown(false), mRMouseDown(false), mRotSpd(0.1), mSelectionBox(0), mRayScnQuery(0), mVolQuery(0) { } BasicApp::~BasicApp() { if (mCameraMan) delete mCameraMan; mSceneMgr->destroyQuery(mVolQuery); if (mSelectionBox) delete mSelectionBox; Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this); windowClosed(mWindow); delete mRoot; } void BasicApp::go() { #ifdef _DEBUG mResourcesCfg = "resources_d.cfg"; mPluginsCfg = "plugins_d.cfg"; #else mResourcesCfg = "resources.cfg"; mPluginsCfg = "plugins.cfg"; #endif if (!setup()) return; mRoot->startRendering(); destroyScene(); } bool BasicApp::frameRenderingQueued(const Ogre::FrameEvent& fe) { if (mKeyboard->isKeyDown(OIS::KC_ESCAPE)) mShutdown = true; if (mShutdown) return false; if (mWindow->isClosed()) return false; mKeyboard->capture(); mMouse->capture(); mCameraMan->frameRenderingQueued(fe); CEGUI::System::getSingleton().injectTimePulse(fe.timeSinceLastFrame); return true; } bool BasicApp::keyPressed(const OIS::KeyEvent& ke) { // CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext(); // context.injectKeyDown((CEGUI::Key::Scan)e.key); // context.injectChar((CEGUI::Key::Scan)e.text); mCameraMan->injectKeyDown(ke); return true; } bool BasicApp::keyReleased(const OIS::KeyEvent& ke) { // CEGUI::System::getSingleton().getDefaultGUIContext().injectKeyUp( // (CEGUI::Key::Scan)e.key); mCameraMan->injectKeyUp(ke); return true; } bool BasicApp::mouseMoved(const OIS::MouseEvent& me) { CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext(); context.injectMouseMove(me.state.X.rel, me.state.Y.rel); // mCameraMan->injectMouseMove(me); if (mSelecting) { CEGUI::MouseCursor* mouse = &context.getMouseCursor(); mStop.x = mouse->getPosition().d_x / (float)me.state.width; mStop.y = mouse->getPosition().d_y / (float)me.state.height; mSelectionBox->setCorners(mStart, mStop); } if (mLMouseDown) { } else if (mRMouseDown) { mCamera->yaw(Ogre::Degree(-me.state.X.rel * mRotSpd)); mCamera->pitch(Ogre::Degree(-me.state.Y.rel * mRotSpd)); } return true; } // Helper function for mouse events CEGUI::MouseButton convertButton(OIS::MouseButtonID id) { switch (id) { case OIS::MB_Left: return CEGUI::LeftButton; case OIS::MB_Right: return CEGUI::RightButton; case OIS::MB_Middle: return CEGUI::MiddleButton; default: return CEGUI::LeftButton; } } bool BasicApp::mousePressed(const OIS::MouseEvent& me, OIS::MouseButtonID id) { CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext(); context.injectMouseButtonDown(convertButton(id)); // mCameraMan->injectMouseDown(me, id); if (id == OIS::MB_Left) { CEGUI::MouseCursor* mouse = &context.getMouseCursor(); mStart.x = mouse->getPosition().d_x / (float)me.state.width; mStart.y = mouse->getPosition().d_y / (float)me.state.height; mStop = mStart; mSelecting = true; mSelectionBox->clear(); mSelectionBox->setVisible(true); mLMouseDown = true; } else if (id == OIS::MB_Right) { context.getMouseCursor().hide(); mRMouseDown = true; } return true; } bool BasicApp::mouseReleased(const OIS::MouseEvent& me, OIS::MouseButtonID id) { CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext(); context.injectMouseButtonUp(convertButton(id)); // mCameraMan->injectMouseUp(me, id); if (id == OIS::MB_Left) { performSelection(mStart, mStop); mSelecting = false; mSelectionBox->setVisible(false); mLMouseDown = false; } else if (id == OIS::MB_Right) { context.getMouseCursor().show(); mRMouseDown = false; } return true; } void BasicApp::windowResized(Ogre::RenderWindow* rw) { unsigned int width, height, depth; int left, top; rw->getMetrics(width, height, depth, left, top); const OIS::MouseState& ms = mMouse->getMouseState(); ms.width = width; ms.height = height; } void BasicApp::windowClosed(Ogre::RenderWindow* rw) { if (rw == mWindow) { if (mInputMgr) { mInputMgr->destroyInputObject(mMouse); mInputMgr->destroyInputObject(mKeyboard); OIS::InputManager::destroyInputSystem(mInputMgr); mInputMgr = 0; } } } bool BasicApp::setup() { mRoot = new Ogre::Root(mPluginsCfg); setupResources(); if (!configure()) return false; chooseSceneManager(); createCamera(); createViewports(); Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); createResourceListener(); loadResources(); setupCEGUI(); createScene(); createFrameListener(); return true; } bool BasicApp::configure() { if (!(mRoot->restoreConfig() || mRoot->showConfigDialog())) { return false; } mWindow = mRoot->initialise(true, "ITutorial"); return true; } void BasicApp::chooseSceneManager() { mSceneMgr = mRoot->createSceneManager(Ogre::ST_EXTERIOR_CLOSE); } void BasicApp::createCamera() { mCamera = mSceneMgr->createCamera("PlayerCam"); mCamera->setPosition(Ogre::Vector3(0, 0, 80)); mCamera->lookAt(Ogre::Vector3(0, 0, -300)); mCamera->setNearClipDistance(5); mCameraMan = new OgreBites::SdkCameraMan(mCamera); } void BasicApp::createScene() { 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); mVolQuery = mSceneMgr->createPlaneBoundedVolumeQuery(Ogre::PlaneBoundedVolumeList()); CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); CEGUI::Window* rootWin = wmgr.loadLayoutFromFile("test.layout"); CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(rootWin); } void BasicApp::destroyScene() { } void BasicApp::createFrameListener() { Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***"); OIS::ParamList pl; size_t windowHnd = 0; std::ostringstream windowHndStr; mWindow->getCustomAttribute("WINDOW", &windowHnd); windowHndStr << windowHnd; pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); mInputMgr = OIS::InputManager::createInputSystem(pl); mKeyboard = static_cast<OIS::Keyboard*>( mInputMgr->createInputObject(OIS::OISKeyboard, true)); mMouse = static_cast<OIS::Mouse*>( mInputMgr->createInputObject(OIS::OISMouse, true)); mKeyboard->setEventCallback(this); mMouse->setEventCallback(this); windowResized(mWindow); Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this); mRoot->addFrameListener(this); Ogre::LogManager::getSingletonPtr()->logMessage("Finished"); } void BasicApp::createViewports() { Ogre::Viewport* vp = mWindow->addViewport(mCamera); vp->setBackgroundColour(Ogre::ColourValue(0, 0, 0)); mCamera->setAspectRatio( Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight())); } void BasicApp::setupResources() { Ogre::ConfigFile cf; cf.load(mResourcesCfg); Ogre::String secName, typeName, archName; Ogre::ConfigFile::SectionIterator secIt = cf.getSectionIterator(); while (secIt.hasMoreElements()) { secName = secIt.peekNextKey(); Ogre::ConfigFile::SettingsMultiMap* settings = secIt.getNext(); Ogre::ConfigFile::SettingsMultiMap::iterator setIt; for (setIt = settings->begin(); setIt != settings->end(); ++setIt) { typeName = setIt->first; archName = setIt->second; Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); } } } void BasicApp::createResourceListener() { } void BasicApp::loadResources() { Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); } bool BasicApp::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::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext(); context.setDefaultFont("DejaVuSans-10"); context.getMouseCursor().setDefaultImage("TaharezLook/MouseArrow"); Ogre::LogManager::getSingletonPtr()->logMessage("Finished"); return true; } void BasicApp::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 BasicApp::deselectObjects() { std::list<Ogre::MovableObject*>::iterator it; for (it = mSelected.begin(); it != mSelected.end(); ++it) (*it)->getParentSceneNode()->showBoundingBox(false); } void BasicApp::selectObject(Ogre::MovableObject* obj) { obj->getParentSceneNode()->showBoundingBox(true); mSelected.push_back(obj); } void BasicApp::swap(float& x, float& y) { float temp = x; x = y; y = temp; } #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include "windows.h" #endif #ifdef __cplusplus extern "C" { #endif #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) #else int main(int argc, char *argv[]) #endif { BasicApp app; try { app.go(); } catch(Ogre::Exception& e) { #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "An exception has occured: " << e.getFullDescription().c_str() << std::endl; #endif } return 0; } #ifdef __cplusplus } #endif