Visual Servoing Platform  version 3.6.1 under development (2025-02-15)
vpAROgre.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Augmented Reality viewer using Ogre3D.
32  */
33 
43 #ifndef VP_AR_OGRE_H
44 #define VP_AR_OGRE_H
45 
46 #include <visp3/core/vpConfig.h>
47 
48 #ifdef VISP_HAVE_OGRE
49 #include <list>
50 
51 #include <visp3/core/vpImage.h>
52 #include <visp3/core/vpImageConvert.h>
53 #include <visp3/core/vpRGBa.h>
54 
55 #include <visp3/core/vpHomogeneousMatrix.h>
56 #include <visp3/core/vpImageTools.h>
57 #include <visp3/core/vpRotationMatrix.h>
58 #include <visp3/core/vpRxyzVector.h>
59 
60 #include <Ogre.h>
61 #include <OgreFrameListener.h>
62 
63 #if (VISP_HAVE_OGRE_VERSION >= (1<<16 | 10 <<8 | 0))
64 #include <Bites/OgreBitesConfigDialog.h>
65 #endif
66 
67 #if (VISP_HAVE_OGRE_VERSION >= (1<<16 | 11<<8 | 0))
68 #include <Bites/OgreWindowEventUtilities.h>
69 #endif
70 
71 #if (VISP_HAVE_OGRE_VERSION >= (1<<16 | 12 <<8 | 0))
72 #include <OgreComponents.h>
73 #elif (VISP_HAVE_OGRE_VERSION >= (1<<16 | 10 <<8 | 0))
74 #include <OgreBuildSettings.h>
75 #endif
76 
77 #if defined(OGRE_BUILD_COMPONENT_RTSHADERSYSTEM) & (VISP_HAVE_OGRE_VERSION >= (1<<16 | 10 <<8 | 0))
78 #include <RTShaderSystem/OgreShaderGenerator.h>
79 #include <Bites/OgreSGTechniqueResolverListener.h>
80 #endif // INCLUDE_RTSHADER_SYSTEM
81 
82 #ifdef VISP_HAVE_OIS
83 #include <ois/OIS.h>
84 #endif
85 
86 BEGIN_VISP_NAMESPACE
104 class VISP_EXPORT vpAROgre : public Ogre::FrameListener,
105  public Ogre::WindowEventListener
106 #ifdef VISP_HAVE_OIS
107  ,
108  public OIS::KeyListener
109 #endif
110 {
111 public:
112  vpAROgre(const vpCameraParameters &cam = vpCameraParameters(), unsigned int width = 0, unsigned int height = 0,
113  const char *resourcePath =
114 #ifdef VISP_HAVE_OGRE_RESOURCES_PATH
115  VISP_HAVE_OGRE_RESOURCES_PATH,
116 #else
117  ".",
118 #endif
119  const char *pluginsPath =
120 #ifdef VISP_HAVE_OGRE_PLUGINS_PATH
121  VISP_HAVE_OGRE_PLUGINS_PATH
122 #else
123  "."
124 #endif
125  );
126 
127  virtual ~vpAROgre(void);
128 
140  inline void addResource(const std::string &resourceLocation)
141  {
142  mOptionalResourceLocation.push_back(resourceLocation);
143  }
144 
145  void addRotation(const std::string &sceneName, const vpRotationMatrix &wRo);
146 
147  bool continueRendering(void);
148 
149  virtual bool customframeStarted(const Ogre::FrameEvent &evt);
150 
151  virtual bool customframeEnded(const Ogre::FrameEvent &evt);
152 
153  virtual void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMw);
154 
155  virtual void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMw);
156 
157  inline Ogre::Camera *getCamera() { return mCamera; }
158 
164  inline double getFarClippingDistance() const { return mFarClipping; }
165 
171  inline double getNearClippingDistance() const { return mNearClipping; }
172 
173  vpTranslationVector getPosition(const std::string &sceneName) const;
174 
175  void getRenderingOutput(vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo);
176 
177  inline Ogre::SceneManager *getSceneManager() { return mSceneMgr; }
178 
179  virtual void init(vpImage<unsigned char> &I, bool bufferedKeys = false, bool hidden = false);
180  virtual void init(vpImage<vpRGBa> &I, bool bufferedKeys = false, bool hidden = false);
181 
187  bool isWindowHidden() { return windowHidden; }
188 
189 #ifdef VISP_HAVE_OIS
193  virtual bool keyPressed(const OIS::KeyEvent & /*e*/) { return true; }
197  virtual bool keyReleased(const OIS::KeyEvent & /*e*/) { return true; }
198 #endif
199 
200  void load(const std::string &entityName, const std::string &model);
201 
202  bool renderOneFrame(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMw);
203 
204  bool renderOneFrame(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMw);
205 
206  void setCameraParameters(const vpCameraParameters &cameraP);
207 
213  void setFarClippingDistance(const double &dist)
214  {
215  mFarClipping = dist;
216  updateCameraProjection();
217  }
218 
219  void setMaterial(const std::string &entityName, const std::string &materialName);
220 
226  void setNearClippingDistance(const double &dist)
227  {
228  mNearClipping = dist;
229  updateCameraProjection();
230  }
231 
243  inline void setPluginsPath(const char *pluginsPath) { mPluginsPath = pluginsPath; }
244 
245  void setPosition(const std::string &sceneName, const vpTranslationVector &wTo);
246  void setPosition(const std::string &sceneName, const vpHomogeneousMatrix &wMo);
247 
259  inline void setResourcePath(const char *resourcePath) { mResourcePath = resourcePath; }
260 
261  void setRotation(const std::string &sceneName, const vpRotationMatrix &wRo);
262 
263  void setScale(const std::string &sceneName, float factorx, float factory, float factorz);
264 
274  inline void setShowConfigDialog(bool showConfigDialog) { mshowConfigDialog = showConfigDialog; }
275 
276  void setVisibility(const std::string &sceneName, bool isVisible);
277 
285  inline void setWindowName(const Ogre::String &n) { name = n; }
286 
297  inline void setWindowPosition(unsigned int win_x, unsigned int win_y)
298  {
299  if (mWindow == nullptr) {
300  throw vpException(vpException::notInitialized, "Window not initialised, cannot set its position");
301  }
302  mWindow->reposition(static_cast<int>(win_x), static_cast<int>(win_y));
303  }
304 
305  virtual bool windowClosing(Ogre::RenderWindow *rw) VP_OVERRIDE;
306 
307  virtual void windowClosed(Ogre::RenderWindow *rw) VP_OVERRIDE;
308 
309 protected:
310  virtual void init(bool bufferedKeys = false, bool hidden = false);
311  virtual void createCamera(void);
312 
317  virtual void createScene(void) { };
318 
319  virtual void closeOIS(void);
320 
326  virtual bool updateScene(const Ogre::FrameEvent & /*evt*/) { return true; };
327 
333  virtual bool processInputEvent(const Ogre::FrameEvent & /*evt*/) { return true; };
334 
340  virtual bool destroyScene(void)
341  {
342  if (!mSceneMgr) {
343  return false;
344  }
345 
346  mSceneMgr->destroyAllCameras();
347  mSceneMgr->clearScene();
348  mRoot->destroySceneManager(mSceneMgr);
349  return true;
350  }
351 
352  virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo);
353 
354  virtual void updateCameraProjection(void);
355 
356  virtual void updateBackgroundTexture(const vpImage<unsigned char> &I);
357 
358  virtual void updateBackgroundTexture(const vpImage<vpRGBa> &I);
359 
360 private:
361  void createBackground(vpImage<unsigned char> &I);
362  void createBackground(vpImage<vpRGBa> &I);
363 
364  bool frameStarted(const Ogre::FrameEvent &evt);
365 
366  bool frameEnded(const Ogre::FrameEvent &evt);
367 
368  bool stopTest(const Ogre::FrameEvent &evt);
369 
370  bool initialiseRTShaderSystem();
371 
372  void destroyRTShaderSystem();
373 
374 protected:
375  static unsigned int sID;
376  static unsigned int sRTSSUsers;
377  // Attributes
378  Ogre::String name;
381  // OGRE 3D System
382  Ogre::Root *mRoot;
383  Ogre::Camera *mCamera;
384  Ogre::String mSceneManagerName;
385  Ogre::SceneManager *mSceneMgr;
386  Ogre::RenderWindow *mWindow;
387  Ogre::String mResourcePath;
388  Ogre::String mPluginsPath;
390 #ifdef VISP_HAVE_OIS
391  // OIS Input manager and devices
392  OIS::InputManager *mInputManager;
393  OIS::Keyboard *mKeyboard;
394 #endif
395 
396 #if defined(OGRE_BUILD_COMPONENT_RTSHADERSYSTEM) & (VISP_HAVE_OGRE_VERSION >= (1<<16 | 10 <<8 | 0))
397  Ogre::RTShader::ShaderGenerator *mShaderGenerator; // The Shader generator instance.
398  OgreBites::SGTechniqueResolverListener *mMaterialMgrListener; // Shader generator material manager listener.
399 #endif // INCLUDE_RTSHADER_SYSTEM
400 
401  // ViSP AR System
402  bool keepOn;
406  Ogre::HardwarePixelBufferSharedPtr mPixelBuffer;
407  Ogre::Rectangle2D *mBackground;
408  unsigned int mBackgroundHeight;
409  unsigned int mBackgroundWidth;
410  unsigned int mWindowHeight;
411  unsigned int mWindowWidth;
414  // Camera calculations
415  double mNearClipping;
416  double mFarClipping;
422  std::list<std::string> mOptionalResourceLocation;
424 };
425 END_VISP_NAMESPACE
426 #endif // VISP_HAVE_OGRE
427 
428 #endif
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:110
OIS::InputManager * mInputManager
Definition: vpAROgre.h:392
double getNearClippingDistance() const
Definition: vpAROgre.h:171
virtual bool updateScene(const Ogre::FrameEvent &)
Definition: vpAROgre.h:326
OIS::Keyboard * mKeyboard
Definition: vpAROgre.h:393
bool keepOn
Definition: vpAROgre.h:402
void setWindowName(const Ogre::String &n)
Definition: vpAROgre.h:285
virtual bool processInputEvent(const Ogre::FrameEvent &)
Definition: vpAROgre.h:333
virtual bool keyReleased(const OIS::KeyEvent &)
Definition: vpAROgre.h:197
unsigned int mBackgroundWidth
Definition: vpAROgre.h:409
void setShowConfigDialog(bool showConfigDialog)
Definition: vpAROgre.h:274
vpImage< vpRGBa > mImageRGBA
Definition: vpAROgre.h:404
vpCameraParameters mcam
Definition: vpAROgre.h:417
vpImage< unsigned char > mImage
Definition: vpAROgre.h:405
bool windowHidden
Definition: vpAROgre.h:412
void setFarClippingDistance(const double &dist)
Definition: vpAROgre.h:213
void setNearClippingDistance(const double &dist)
Definition: vpAROgre.h:226
double mNearClipping
Definition: vpAROgre.h:415
Ogre::Root * mRoot
Definition: vpAROgre.h:382
Ogre::String name
Definition: vpAROgre.h:378
Ogre::String mSceneManagerName
Definition: vpAROgre.h:384
virtual bool keyPressed(const OIS::KeyEvent &)
Definition: vpAROgre.h:193
void setWindowPosition(unsigned int win_x, unsigned int win_y)
Definition: vpAROgre.h:297
virtual bool destroyScene(void)
Definition: vpAROgre.h:340
Ogre::Camera * mCamera
Definition: vpAROgre.h:383
static unsigned int sRTSSUsers
Definition: vpAROgre.h:376
bool isWindowHidden()
Definition: vpAROgre.h:187
unsigned int mBackgroundHeight
Definition: vpAROgre.h:408
bool mInitialized
Definition: vpAROgre.h:379
void addResource(const std::string &resourceLocation)
Definition: vpAROgre.h:140
Ogre::String mPluginsPath
Definition: vpAROgre.h:388
Ogre::Rectangle2D * mBackground
Definition: vpAROgre.h:407
double mFarClipping
Definition: vpAROgre.h:416
virtual void createScene(void)
Definition: vpAROgre.h:317
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:177
unsigned int mWindowHeight
Definition: vpAROgre.h:410
Ogre::Camera * getCamera()
Definition: vpAROgre.h:157
void setPluginsPath(const char *pluginsPath)
Definition: vpAROgre.h:243
double getFarClippingDistance() const
Definition: vpAROgre.h:164
std::list< std::string > mOptionalResourceLocation
Definition: vpAROgre.h:422
Ogre::SceneManager * mSceneMgr
Definition: vpAROgre.h:385
Ogre::HardwarePixelBufferSharedPtr mPixelBuffer
Definition: vpAROgre.h:406
bool mshowConfigDialog
Definition: vpAROgre.h:419
static unsigned int sID
Definition: vpAROgre.h:375
Ogre::String mResourcePath
Definition: vpAROgre.h:387
Ogre::RenderWindow * mWindow
Definition: vpAROgre.h:386
void setResourcePath(const char *resourcePath)
Definition: vpAROgre.h:259
unsigned int mWindowWidth
Definition: vpAROgre.h:411
Generic class defining intrinsic camera parameters.
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:74
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.