1 #include "blub/async/dispatcher.hpp"
2 #include "blub/core/globals.hpp"
3 #include "blub/core/sharedPointer.hpp"
4 #include "blub/core/scopedPtr.hpp"
5 #include "blub/log/global.hpp"
6 #include "blub/math/quaternion.hpp"
7 #include "blub/math/math.hpp"
9 #include <OGRE/OgreCamera.h>
10 #include <OGRE/OgreFrameListener.h>
11 #include <OGRE/OgreRenderWindow.h>
12 #include <OGRE/OgreRoot.h>
14 #include <OIS/OISInputManager.h>
15 #include <OIS/OISKeyboard.h>
16 #include <OIS/OISMouse.h>
23 class Handler :
public Ogre::FrameListener,
public OIS::MouseListener,
public OIS::KeyListener
33 , graphicDispatcher(0, true)
34 , m_lookVert(
blub::math::pi)
54 inputManager->destroyInputObject(keyboard);
55 inputManager->destroyInputObject(mouse);
56 OIS::InputManager::destroyInputSystem(inputManager);
66 #if defined BLUB_DEBUG
67 renderSystem.reset(
new Ogre::Root(
"plugins_d.cfg"));
69 renderSystem.reset(
new Ogre::Root(
"plugins.cfg"));
71 Ogre::LogManager::getSingleton().getDefaultLog()->setDebugOutputEnabled(
true);
72 if (!renderSystem->restoreConfig())
74 if (!renderSystem->showConfigDialog())
79 renderSystem->initialise(
true);
81 renderScene = renderSystem->createSceneManager(Ogre::ST_GENERIC);
87 camera = renderScene->createCamera(
"camera");
88 camera->setAutoAspectRatio(
true);
89 camera->setNearClipDistance(0.1);
90 camera->setFarClipDistance(1000.);
91 renderSystem->getAutoCreatedWindow()->addViewport(camera);
92 camera->getViewport()->setBackgroundColour(Ogre::ColourValue::Black);
94 renderScene->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
96 Ogre::Light *light = renderScene->createLight();
97 light->setType(Ogre::Light::LT_DIRECTIONAL);
98 light->setDirection(1., -1., -1.);
100 renderSystem->addFrameListener(
this);
112 size_t windowHandle = 0;
114 renderSystem->getAutoCreatedWindow()->getCustomAttribute(
"WINDOW", &windowHandle);
116 pl.insert(std::make_pair(std::string(
"WINDOW"), boost::lexical_cast<std::string>(windowHandle)));
117 #if defined OIS_WIN32_PLATFORM
120 pl.insert(std::make_pair(std::string(
"w32_keyboard"), std::string(
"DISCL_FOREGROUND")));
121 pl.insert(std::make_pair(std::string(
"w32_keyboard"), std::string(
"DISCL_NONEXCLUSIVE")));
122 #elif defined OIS_LINUX_PLATFORM
123 pl.insert(std::make_pair(std::string(
"x11_mouse_grab"), std::string(
"true")));
125 pl.insert(std::make_pair(std::string(
"x11_mouse_hide"), std::string(
"true")));
126 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
127 pl.insert(std::make_pair(std::string(
"XAutoRepeatOn"), std::string(
"true")));
129 inputManager = OIS::InputManager::createInputSystem(pl);
130 keyboard =
static_cast<OIS::Keyboard*
>(inputManager->createInputObject(OIS::OISKeyboard,
true));
131 mouse =
static_cast<OIS::Mouse*
>(inputManager->createInputObject(OIS::OISMouse,
true));
133 keyboard->setEventCallback(
this);
134 mouse->setEventCallback(
this);
149 Ogre::RenderWindow *renderWindow(Ogre::Root::getSingletonPtr()->getAutoCreatedWindow());
150 if (renderWindow->isClosed())
159 blub::real speed(5.);
164 camera->moveRelative(Ogre::Vector3::NEGATIVE_UNIT_Z*speed*evt.timeSinceLastFrame);
168 camera->moveRelative(Ogre::Vector3::UNIT_Z*speed*evt.timeSinceLastFrame);
172 camera->moveRelative(Ogre::Vector3::NEGATIVE_UNIT_X*speed*evt.timeSinceLastFrame);
176 camera->moveRelative(Ogre::Vector3::UNIT_X*speed*evt.timeSinceLastFrame);
180 const blub::real lookSpeed = (blub::math::piHalf/2.) * evt.timeSinceLastFrame;
183 m_lookVert += lookSpeed;
187 m_lookVert -= lookSpeed;
191 m_lookHor += lookSpeed;
195 m_lookHor -= lookSpeed;
197 m_lookHor = blub::math::clamp<blub::real>(m_lookHor, -blub::math::piHalf, blub::math::piHalf);
199 const blub::quaternion rotVert(0, blub::math::sin(m_lookVert / 2.0), 0, blub::math::cos(m_lookVert / 2.0));
200 const blub::quaternion rotHor(blub::math::sin(m_lookHor / 2.0), 0, 0, blub::math::cos(m_lookHor / 2.0));
204 camera->setOrientation(rot);
207 m_sigFrame(evt.timeSinceLastFrame);
208 graphicDispatcher.start();
222 m_lookHor +=
static_cast<blub::real
>(arg.state.Y.rel) / -500.;
223 m_lookVert +=
static_cast<blub::real
>(arg.state.X.rel) / -500.;
235 if (!arg.state.buttonDown(OIS::MB_Left) && !arg.state.buttonDown(OIS::MB_Right))
238 m_sigMouseGotPressed(arg.state.buttonDown(OIS::MB_Left));
261 if (arg.key == OIS::KC_F11)
263 if (camera->getPolygonMode() == Ogre::PM_WIREFRAME)
265 camera->setPolygonMode(Ogre::PM_SOLID);
269 camera->setPolygonMode(Ogre::PM_WIREFRAME);
298 m_backwards = pressed;
310 m_lookLeft = pressed;
313 m_lookRight = pressed;
319 m_lookDown = pressed;
325 m_sigKeyGotPressed(arg, pressed);
331 t_sigFrame* signalFrame() {
return &m_sigFrame;}
333 t_sigKeyGotPressed* signalKeyGotPressed() {
return &m_sigKeyGotPressed;}
335 t_sigMouseGotPressed* signalMouseGotPressed() {
return &m_sigMouseGotPressed;}
338 blub::scopedPointer<Ogre::Root> renderSystem;
339 Ogre::SceneManager* renderScene;
340 Ogre::Camera* camera;
342 OIS::InputManager *inputManager;
343 OIS::Keyboard *keyboard;
349 blub::real m_lookVert;
350 blub::real m_lookHor;
361 t_sigFrame m_sigFrame;
362 t_sigKeyGotPressed m_sigKeyGotPressed;
363 t_sigMouseGotPressed m_sigMouseGotPressed;
bool handleKeyPress(const OIS::KeyEvent &arg, const bool &pressed)
handleKeyPress checks if WSAD or LShift gets pressed and moves the camera.
Definition: Handler.hpp:290
bool keyReleased(const OIS::KeyEvent &arg)
keyReleased Does nothing. Must get implemented because pure virtual in OIS::KeyListener ...
Definition: Handler.hpp:280
Definition: quaternion.hpp:25
bool initialiseOIS()
initialiseOIS initialises OIS
Definition: Handler.hpp:109
Handler()
Handler constructor.
Definition: Handler.hpp:29
bool frameStarted(const Ogre::FrameEvent &evt)
frameStarted gets called after every rendered frame by ogre3d. Method calls the graphic dispatcher us...
Definition: Handler.hpp:147
virtual ~Handler()
~Handler destructor.
Definition: Handler.hpp:52
Definition: dispatcher.hpp:29
The Handler class initialises Ogre3d (graphics) and OIS (input) and derives their callbacks...
Definition: Handler.hpp:23
bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID)
mousePressed Checks if mouse button "left" or "right" got pressed and calls the signal signalKeyGotPr...
Definition: Handler.hpp:233
bool keyPressed(const OIS::KeyEvent &arg)
keyPressed gets calles by OIS. If key F11 got pressed rendering mode gets changed to wireframe...
Definition: Handler.hpp:259
bool mouseReleased(const OIS::MouseEvent &, OIS::MouseButtonID)
mouseReleased Does nothing. Must get implemented because pure virtual in OIS::KeyListener ...
Definition: Handler.hpp:246
bool initialiseOgre()
initialiseOgre initialises ogre3d, including scene and camera.
Definition: Handler.hpp:63
Definition: deadlineTimer.hpp:10
bool mouseMoved(const OIS::MouseEvent &arg)
mouseMoved gets called by OIS. Calculates the camera orientation and sets the results.
Definition: Handler.hpp:220