voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
Handler.hpp
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"
8 
9 #include <OGRE/OgreCamera.h>
10 #include <OGRE/OgreFrameListener.h>
11 #include <OGRE/OgreRenderWindow.h>
12 #include <OGRE/OgreRoot.h>
13 
14 #include <OIS/OISInputManager.h>
15 #include <OIS/OISKeyboard.h>
16 #include <OIS/OISMouse.h>
17 
18 
23 class Handler : public Ogre::FrameListener, public OIS::MouseListener, public OIS::KeyListener
24 {
25 public:
30  : camera(nullptr)
31  , keyboard(nullptr)
32  , mouse(nullptr)
33  , graphicDispatcher(0, true)
34  , m_lookVert(blub::math::pi)
35  , m_lookHor(0.)
36  , m_forward(false)
37  , m_backwards(false)
38  , m_left(false)
39  , m_right(false)
40  , m_fast(false)
41  , m_lookLeft(false)
42  , m_lookRight(false)
43  , m_lookUp(false)
44  , m_lookDown(false)
45  {
46  ;
47  }
48 
52  virtual ~Handler()
53  {
54  inputManager->destroyInputObject(keyboard);
55  inputManager->destroyInputObject(mouse);
56  OIS::InputManager::destroyInputSystem(inputManager);
57  }
58 
64  {
65  // initalise Ogre3d
66 #if defined BLUB_DEBUG
67  renderSystem.reset(new Ogre::Root("plugins_d.cfg"));
68 #else
69  renderSystem.reset(new Ogre::Root("plugins.cfg"));
70 #endif
71  Ogre::LogManager::getSingleton().getDefaultLog()->setDebugOutputEnabled(true);
72  if (!renderSystem->restoreConfig())
73  {
74  if (!renderSystem->showConfigDialog())
75  {
76  return false;
77  }
78  }
79  renderSystem->initialise(true);
80 
81  renderScene = renderSystem->createSceneManager(Ogre::ST_GENERIC);
82 // renderScene = renderSystem->createSceneManager("OctreeSceneManager");
83 
84 // Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../data", "FileSystem");
85 // Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
86 
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);
93 
94  renderScene->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
95 
96  Ogre::Light *light = renderScene->createLight();
97  light->setType(Ogre::Light::LT_DIRECTIONAL);
98  light->setDirection(1., -1., -1.);
99 
100  renderSystem->addFrameListener(this);
101 
102  return true;
103  }
104 
110  {
111  OIS::ParamList pl;
112  size_t windowHandle = 0;
113 
114  renderSystem->getAutoCreatedWindow()->getCustomAttribute("WINDOW", &windowHandle);
115 
116  pl.insert(std::make_pair(std::string("WINDOW"), boost::lexical_cast<std::string>(windowHandle)));
117  #if defined OIS_WIN32_PLATFORM
118  //pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_BACKGROUND" )));
119  //pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_EXCLUSIVE")));
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")));
124 // pl.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
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")));
128  #endif
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));
132 
133  keyboard->setEventCallback(this);
134  mouse->setEventCallback(this);
135 
136  return true;
137  }
138 
139  // FrameListener interface
140 public:
147  bool frameStarted(const Ogre::FrameEvent &evt)
148  {
149  Ogre::RenderWindow *renderWindow(Ogre::Root::getSingletonPtr()->getAutoCreatedWindow());
150  if (renderWindow->isClosed())
151  {
152  return false;
153  }
154 
155  keyboard->capture();
156  mouse->capture();
157 
158  {
159  blub::real speed(5.);
160  if (m_fast)
161  {speed = 20.;}
162  if (m_forward)
163  {
164  camera->moveRelative(Ogre::Vector3::NEGATIVE_UNIT_Z*speed*evt.timeSinceLastFrame);
165  }
166  if (m_backwards)
167  {
168  camera->moveRelative(Ogre::Vector3::UNIT_Z*speed*evt.timeSinceLastFrame);
169  }
170  if (m_left)
171  {
172  camera->moveRelative(Ogre::Vector3::NEGATIVE_UNIT_X*speed*evt.timeSinceLastFrame);
173  }
174  if (m_right)
175  {
176  camera->moveRelative(Ogre::Vector3::UNIT_X*speed*evt.timeSinceLastFrame);
177  }
178  }
179  {
180  const blub::real lookSpeed = (blub::math::piHalf/2.) * evt.timeSinceLastFrame;
181  if (m_lookLeft)
182  {
183  m_lookVert += lookSpeed;
184  }
185  if (m_lookRight)
186  {
187  m_lookVert -= lookSpeed;
188  }
189  if (m_lookUp)
190  {
191  m_lookHor += lookSpeed;
192  }
193  if (m_lookDown)
194  {
195  m_lookHor -= lookSpeed;
196  }
197  m_lookHor = blub::math::clamp<blub::real>(m_lookHor, -blub::math::piHalf, blub::math::piHalf);
198 
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));
201 
202  blub::quaternion rot(rotVert*rotHor);
203 
204  camera->setOrientation(rot);
205  }
206 
207  m_sigFrame(evt.timeSinceLastFrame);
208  graphicDispatcher.start();
209 
210  return true;
211  }
212 
213  // MouseListener interface
214 public:
220  bool mouseMoved(const OIS::MouseEvent &arg)
221  {
222  m_lookHor += static_cast<blub::real>(arg.state.Y.rel) / -500.;
223  m_lookVert += static_cast<blub::real>(arg.state.X.rel) / -500.;
224 
225  return true;
226  }
227 
233  bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID /*id*/)
234  {
235  if (!arg.state.buttonDown(OIS::MB_Left) && !arg.state.buttonDown(OIS::MB_Right))
236  {return true;}
237 
238  m_sigMouseGotPressed(arg.state.buttonDown(OIS::MB_Left));
239 
240  return true;
241  }
246  bool mouseReleased(const OIS::MouseEvent &/*arg*/, OIS::MouseButtonID /*id*/)
247  {
248  return true;
249  }
250 
251  // KeyListener interface
252 public:
253 
259  bool keyPressed(const OIS::KeyEvent &arg)
260  {
261  if (arg.key == OIS::KC_F11)
262  {
263  if (camera->getPolygonMode() == Ogre::PM_WIREFRAME)
264  {
265  camera->setPolygonMode(Ogre::PM_SOLID);
266  }
267  else
268  {
269  camera->setPolygonMode(Ogre::PM_WIREFRAME);
270  }
271  return true;
272  }
273  return handleKeyPress(arg, true);
274  }
280  bool keyReleased(const OIS::KeyEvent &arg)
281  {
282  return handleKeyPress(arg, false);
283  }
290  bool handleKeyPress(const OIS::KeyEvent &arg, const bool& pressed)
291  {
292  switch (arg.key)
293  {
294  case OIS::KC_W:
295  m_forward = pressed;
296  break;
297  case OIS::KC_S:
298  m_backwards = pressed;
299  break;
300  case OIS::KC_A:
301  m_left = pressed;
302  break;
303  case OIS::KC_D:
304  m_right = pressed;
305  break;
306  case OIS::KC_LSHIFT:
307  m_fast = pressed;
308  break;
309  case OIS::KC_LEFT:
310  m_lookLeft = pressed;
311  break;
312  case OIS::KC_RIGHT:
313  m_lookRight = pressed;
314  break;
315  case OIS::KC_UP:
316  m_lookUp = pressed;
317  break;
318  case OIS::KC_DOWN:
319  m_lookDown = pressed;
320  break;
321  default:
322  break;
323  }
324 
325  m_sigKeyGotPressed(arg, pressed);
326 
327  return true;
328  }
329 
330  typedef blub::signal<void (blub::real)> t_sigFrame;
331  t_sigFrame* signalFrame() {return &m_sigFrame;}
332  typedef blub::signal<void (OIS::KeyEvent, bool pressed)> t_sigKeyGotPressed;
333  t_sigKeyGotPressed* signalKeyGotPressed() {return &m_sigKeyGotPressed;}
334  typedef blub::signal<void (bool left)> t_sigMouseGotPressed;
335  t_sigMouseGotPressed* signalMouseGotPressed() {return &m_sigMouseGotPressed;}
336 
337 public:
338  blub::scopedPointer<Ogre::Root> renderSystem;
339  Ogre::SceneManager* renderScene;
340  Ogre::Camera* camera;
341 
342  OIS::InputManager *inputManager;
343  OIS::Keyboard *keyboard;
344  OIS::Mouse *mouse;
345 
346  blub::async::dispatcher graphicDispatcher;
347 
348 protected:
349  blub::real m_lookVert;
350  blub::real m_lookHor;
351  bool m_forward;
352  bool m_backwards;
353  bool m_left;
354  bool m_right;
355  bool m_fast;
356  bool m_lookLeft;
357  bool m_lookRight;
358  bool m_lookUp;
359  bool m_lookDown;
360 
361  t_sigFrame m_sigFrame;
362  t_sigKeyGotPressed m_sigKeyGotPressed;
363  t_sigMouseGotPressed m_sigMouseGotPressed;
364 };
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