ViSP  2.9.0
vpAROgre.cpp
1 /****************************************************************************
2  *
3  * $Id: vpAROgre.cpp 4604 2014-01-21 14:15:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Augmented Reality viewer using Ogre3D.
36  *
37  * Authors:
38  * Bertrand Delabarre
39  *
40  *****************************************************************************/
41 
52 #include "visp/vpConfig.h"
53 
54 #ifdef VISP_HAVE_OGRE
55 
56 #include "visp/vpAROgre.h"
57 #include "visp/vpIoTools.h"
58 
59 
76  unsigned int width, unsigned int height,
77  const char *resourcePath, const char *pluginsPath)
78  : mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0)
79 #ifdef VISP_HAVE_OIS
80  , mInputManager(0), mKeyboard(0)
81 #endif
82 {
83  // Get resources.cfg path
84  mResourcePath = resourcePath;
85  //std::cout << "mResourcePath: " << mResourcePath<< std::endl;
86  // Get plugins.cfg path
87  mPluginsPath = pluginsPath;
88  //std::cout << "mPluginsPath: " << mPluginsPath<< std::endl;
89  // Set intrinsic camera parameters
90  mcam = cam;
91  // When created no reason to stop displaying
92  keepOn = true;
93  // Set Dimensions
94  mWindowWidth = width;
95  mWindowHeight = height;
96  windowHidden = false;
97  mshowConfigDialog = true;
99 
100  name = "ViSP - Augmented Reality";
101 
102  mBackground = NULL;
103  mBackgroundHeight = 0;
104  mBackgroundWidth = 0;
105 }
106 
136  bool
137 #ifdef VISP_HAVE_OIS
138  bufferedKeys
139 #endif
140  ,bool hidden
141  )
142 {
145 
146  init(
147 #ifdef VISP_HAVE_OIS
148  bufferedKeys,
149 #else
150  false,
151 #endif
152  hidden
153  );
154  // Create the background image which will come from the grabber
155  createBackground(I);
156 }
157 
187  bool
188 #ifdef VISP_HAVE_OIS
189  bufferedKeys
190 #endif
191  ,bool hidden
192  )
193 {
196 
197  init(
198 #ifdef VISP_HAVE_OIS
199  bufferedKeys,
200 #else
201  false,
202 #endif
203  hidden
204  );
205  // Create the background image which will come from the grabber
206  createBackground(I);
207 }
208 
233 void vpAROgre::init(bool
234 #ifdef VISP_HAVE_OIS
235  bufferedKeys
236 #endif
237  ,bool hidden
238  )
239 {
240  // Create the root
241 #if defined(NDEBUG) || !defined(_WIN32)
242  std::string pluginFile = mPluginsPath+"/plugins.cfg";
243 #else
244  std::string pluginFile = mPluginsPath+"/plugins_d.cfg";
245 #endif
246  if(!vpIoTools::checkFilename(pluginFile)){
247  std::string errorMsg = "Error: the requested plugins file \""
248  + pluginFile + "\" doesn't exist.";
249  std::cout << errorMsg << std::endl;
250 
251  throw (vpException(vpException::ioError, errorMsg));
252  }
253  std::cout << "######################### Load plugin file: " << pluginFile << std::endl;
254 
255  if(Ogre::Root::getSingletonPtr() == NULL)
256  mRoot = new Ogre::Root(pluginFile, "ogre.cfg", "Ogre.log");
257  else
258  mRoot = Ogre::Root::getSingletonPtr();
259 
260  // Load resource paths from config file
261 
262  // File format is:
263  // [ResourceGroupName]
264  // ArchiveType=Path
265  // .. repeat
266  // For example:
267  // [General]
268  // FileSystem=media/
269  // Zip=packages/level1.zip
270  Ogre::ConfigFile cf;
271  std::string resourceFile = mResourcePath+"/resources.cfg";
272  if(!vpIoTools::checkFilename(resourceFile)){
273  std::string errorMsg = "Error: the requested resource file \""
274  + resourceFile + "\" doesn't exist.";
275  std::cout << errorMsg << std::endl;
276 
277  throw (vpException(vpException::ioError, errorMsg));
278  }
279  std::cout << "######################### Load resource file: " << resourceFile << std::endl;
280  cf.load(resourceFile);
281 
282  // Go through all sections & settings in the file
283  Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
284 
285  Ogre::String secName, typeName, archName;
286  while (seci.hasMoreElements())
287  {
288  secName = seci.peekNextKey();
289  Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
290  Ogre::ConfigFile::SettingsMultiMap::iterator i;
291  for (i = settings->begin(); i != settings->end(); ++i)
292  {
293  typeName = i->first;
294  archName = i->second;
295  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
296  archName, typeName, secName);
297  }
298  }
299  std::cout << "##################### add resources" << std::endl;
300  //Add optionnal resources (given by the user).
301  for(std::list<std::string>::const_iterator iter = mOptionnalResourceLocation.begin(); iter != mOptionnalResourceLocation.end(); ++iter){
302  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter, "FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
303  }
304 
305  // Create the window
306  bool canInit = true;
307  if(mshowConfigDialog){
308  mRoot->restoreConfig();
309  if(!mRoot->showConfigDialog())
310  canInit = false;
311  }
312  else{
313  if(!mRoot->restoreConfig())
314  canInit = false;
315  }
316 
317  if(!mRoot->isInitialised()){
318  if(!canInit){ //We set the default renderer system
319  const Ogre::RenderSystemList& lRenderSystemList = mRoot->getAvailableRenderers();
320  if( lRenderSystemList.size() == 0 )
321  throw "ConfigDialog aborted"; // Exit the application on cancel
322 
323  Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
324  std::cout << "Using " << lRenderSystem->getName() << " as renderer." << std::endl;
325  mRoot->setRenderSystem(lRenderSystem);
326  }
327 
328  mRoot->initialise(false);
329  }
330 
331  bool fullscreen = false;
332  Ogre::NameValuePairList misc;
333  Ogre::ConfigOptionMap config = mRoot->getRenderSystem()->getConfigOptions();
334  Ogre::ConfigOptionMap::const_iterator it = config.begin();
335 
336  while( it != config.end() ){
337  Ogre::String leftconf = (*it).first;
338  Ogre::String rightconf = (*it).second.currentValue;
339 
340  if(leftconf == "Video Mode"){
341  if(canInit)
342  sscanf(rightconf.c_str(), "%d %*s %d", &mWindowWidth, &mWindowHeight);
343  else{
344  if(mWindowWidth == 0 && mWindowHeight == 0){
347  }
348  }
349  }
350  else if( leftconf == "Full Screen" ){
351  if(canInit){
352  if(rightconf == "Yes") fullscreen = true;
353  }
354  }
355  else
356  misc[leftconf] = rightconf;
357 
358  it++;
359  }
360 
361  // With Ogre version >= 1.8.1 we hide the window
362  if( hidden ){
363 #if ( OGRE_VERSION >= (1 << 16 | 8 << 8 | 1) )
364  misc["hidden"] = "true";
365  windowHidden = true;
366 #endif
367  }
368  mWindow = mRoot->createRenderWindow(name, mWindowWidth, mWindowHeight, fullscreen, &misc);
369 
370  // Initialise resources
371  Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
372  //-----------------------------------------------------
373  // 4 Create the SceneManager
374  //
375  // ST_GENERIC = octree
376  // ST_EXTERIOR_CLOSE = simple terrain
377  // ST_EXTERIOR_FAR = nature terrain (depreciated)
378  // ST_EXTERIOR_REAL_FAR = paging landscape
379  // ST_INTERIOR = Quake3 BSP
380  //-----------------------------------------------------
381 
382  mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
383 
384  // Create the camera
385  createCamera();
386 
387  // Create a viewport
388  Ogre::Viewport* viewPort = mWindow->addViewport(mCamera);
389 // Ogre::Viewport* viewPort = mCamera->getViewport();
390  viewPort->setClearEveryFrame(true);
391  // Set the projection parameters to match the camera intrinsic parameters
393 
394  // Create the 3D scene
395  createScene();
396 
397  // Initialise and register event handlers
398  mRoot->addFrameListener(this);
399 
400  // Register as a Window listener
401  Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
402 
403 #ifdef VISP_HAVE_OIS
404  // Initialise OIS
405  Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
406  OIS::ParamList pl;
407 
408  size_t windowHnd = 0;
409  std::ostringstream windowHndStr;
410  // Initialise window
411  mWindow->getCustomAttribute("WINDOW", &windowHnd);
412  windowHndStr << windowHnd;
413  pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
414  // Let the user use the keyboard elsewhere
415 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
416  pl.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
417 #endif
418 
419  mInputManager = OIS::InputManager::createInputSystem( pl );
420 
421  //Create all devices
422  // Here we only consider the keyboard input
423  mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
424  if ( !bufferedKeys ) mKeyboard->setEventCallback ( this);
425 #endif
426 
427  // Initialise a render to texture to be able to retrieve a screenshot
428  Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual("rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
429  mWindow->getWidth(),mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
430 
431 
432 
433 // Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual("rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
434 // 640,480, 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
435  Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
436  /*Ogre::Viewport* Viewport =*/ RTarget->addViewport(mCamera);
437  RTarget->getViewport(0)->setClearEveryFrame(true);
438  RTarget->getViewport(0)->setOverlaysEnabled(false);
439 }
440 
445 {
446  // Destroy 3D scene
447  destroyScene();
448  // Close OIS
449  closeOIS();
450 
451  if ( mWindow) {
452  Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
454  }
455  // Delete root
456  if (mRoot) delete mRoot;
457 }
458 
464 bool vpAROgre::stopTest(const Ogre::FrameEvent& evt)
465 {
466  // Always keep this part
467  if(keepOn){
468  return updateScene(evt);
469  }
470  else
471  return keepOn;
472 }
473 
483 bool vpAROgre::frameStarted(const Ogre::FrameEvent& evt)
484 {
485  // custom method telling what to do at the beginning of each frame
486  bool result = customframeStarted(evt);
487 
488  // Listen to the window
489  Ogre::WindowEventUtilities::messagePump();
490  processInputEvent(evt);
491 
492  // See if we have to stop rendering
493  if(result) return stopTest(evt);
494  else return result;
495 }
496 
497 
504 bool vpAROgre::frameEnded(const Ogre::FrameEvent& evt)
505 {
506  // custom method telling what to do at the end of each frame
507  bool result = customframeEnded(evt);
508 
509  // See if we have to stop rendering
510  if(result) return stopTest(evt);
511  else return result;
512 }
513 
522 bool vpAROgre::customframeStarted(const Ogre::FrameEvent& /*evt*/)
523 {
524  // See if window was closed
525  if(mWindow->isClosed()) return false;
526 
527 #ifdef VISP_HAVE_OIS
528  // Get keyboard input
529  mKeyboard->capture();
530  if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
531  return false;
532 #endif
533  return true;
534 }
535 
536 
542 bool vpAROgre::customframeEnded(const Ogre::FrameEvent& /*evt*/){return true;}
543 
554 void vpAROgre::windowClosed(Ogre::RenderWindow* rw)
555 {
556  //Only close for window that created OIS (the main window in these demos)
557  if( rw == mWindow ) closeOIS();
558 }
559 
566  const vpHomogeneousMatrix &cMw)
567 {
568  // Update the background to match the situation
570 
571  // Update the camera parameters to match the grabbed image
573 
574  // Display on Ogre Window
575  return mRoot->renderOneFrame();
576 }
577 
584  const vpHomogeneousMatrix &cMw)
585 {
586  // Update the background to match the situation
588 
589  // Update the camera parameters to match the grabbed image
591 
592  // Display on Ogre Window
593  return mRoot->renderOneFrame();
594 }
595 
602  const vpHomogeneousMatrix &cMw)
603 {
604  // Display on Ogre Window
605  if(renderOneFrame(I,cMw)){
606  mWindow->update();
607  keepOn = true;
608  }
609  else
610  keepOn = false;
611 }
612 
619 {
620  // Display on Ogre Window
621  if(renderOneFrame(I,cMw)){
622  mWindow->update();
623  keepOn = true;
624  }
625  else
626  keepOn = false;
627 }
628 
634 {
635  return keepOn;
636 }
637 
642 {
643  mcam = cameraP;
644 }
645 
651 void vpAROgre::load(const std::string &name, const std::string &model)
652 {
653  Ogre::Entity *newEntity = mSceneMgr->createEntity(name, model);
654  Ogre::SceneNode *newNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
655  newNode->attachObject(newEntity);
656 }
657 
664 void vpAROgre::setPosition(const std::string &name,
665  const vpTranslationVector &wTo)
666 {
667  // Reset the position
668  Ogre::SceneNode *node = mSceneMgr->getSceneNode(name);
669  node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
670 }
671 
677 vpTranslationVector vpAROgre::getPosition(const std::string &name)const
678 {
679  Ogre::Vector3 translation = mSceneMgr->getSceneNode(name)->getPosition();
680  return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
681 }
682 
688 void vpAROgre::setRotation(const std::string &name, const vpRotationMatrix &wRo)
689 {
690  // Get the node in its original position
691  mSceneMgr->getSceneNode(name)->resetOrientation();
692  // Apply the new rotation
693  Ogre::Matrix3 rotationOgre
694  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
695  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
696  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
697  Ogre::Quaternion q(rotationOgre);
698  mSceneMgr->getSceneNode(name)->rotate(q);
699 }
700 
706 void vpAROgre::addRotation(const std::string &name,
707  const vpRotationMatrix &wRo)
708 {
709  // Apply the new rotation
710  Ogre::Matrix3 rotationOgre
711  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
712  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
713  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
714  Ogre::Quaternion q(rotationOgre);
715  mSceneMgr->getSceneNode(name)->rotate(q);
716 
717 
718 }
719 
728 void vpAROgre::setPosition(const std::string &name,
729  const vpHomogeneousMatrix &wMo)
730 {
731  // Extract the position and orientation data
732  vpRotationMatrix rotations;
733  vpTranslationVector translation;
734  wMo.extract(rotations);
735  wMo.extract(translation);
736  // Apply them to the node
737  setPosition(name, translation);
738  setRotation(name, rotations);
739 }
740 
746 void vpAROgre::setVisibility(const std::string &name, bool isVisible)
747 {
748  mSceneMgr->getSceneNode(name)->setVisible(isVisible);
749 }
750 
758 void vpAROgre::setScale(const std::string &name, const float factorx, const float factory, const float factorz)
759 {
760  // Reset the scale to its original value
761  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/mSceneMgr->getSceneNode(name)->getScale());
762  // Apply the new scale
763  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
764 }
765 
770 {
771  mCamera = mSceneMgr->createCamera("Camera");
772 }
773 
779 void vpAROgre::createBackground(vpImage<unsigned char> & /* I */)
780 {
781  // Create a rectangle to show the incoming images from the camera
782  mBackground = new Ogre::Rectangle2D(true); // true = textured
783  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
784  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
785 
786  // Texture options
787  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
788  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
789 
790  // Dynamic texture
791  // If we are using opengl we can boost a little bit performances with a dynamic texture
792  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
793  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
794  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
795  Ogre::TEX_TYPE_2D,
796  mBackgroundWidth,//width
797  mBackgroundHeight,//height
798  0, // num of mip maps
799  Ogre::PF_BYTE_L,
800  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
801  }
802  else{
803  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
804  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
805  Ogre::TEX_TYPE_2D,
806  mBackgroundWidth,//width
807  mBackgroundHeight,//height
808  0, // num of mip maps
809  Ogre::PF_BYTE_L,
810  Ogre::TU_DEFAULT);
811  }
812 
813  // Pointer to the dynamic texture
814  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
815 //#if ( OGRE_VERSION >= (1 << 16 | 9 << 8 | 0) )
816 // .dynamicCast<Ogre::Texture>();// Get the pixel buffer
817 //#else
818 // ;
819 //#endif
820  mPixelBuffer = dynTexPtr->getBuffer();
821 
822  // Material to apply the texture to the background
823  Ogre::MaterialPtr Backgroundmaterial
824  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
825  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
826 //#if ( OGRE_VERSION >= (1 << 16 | 9 << 8 | 0) )
827 // .dynamicCast<Ogre::Material>();
828 //#else
829 // ;
830 //#endif
831  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
832  Backgroundtechnique->createPass();
833  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
834  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
835  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
836  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
837  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
838  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
839 
840  // Add the background to the Scene Graph so it will be rendered
841  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
842  BackgroundNode->attachObject(mBackground);
843 }
844 
850 void vpAROgre::createBackground(vpImage<vpRGBa> & /* I */)
851 {
852  // Create a rectangle to show the incoming images from the camera
853  mBackground = new Ogre::Rectangle2D(true); // true = textured
854  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
855  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
856 
857  // Texture options
858  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
859  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
860 
861  // Dynamic texture
862  // If we are using opengl we can boost a little bit performances with a dynamic texture
863  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
864  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
865  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
866  Ogre::TEX_TYPE_2D,
867  mBackgroundWidth,//width
868  mBackgroundHeight,//height
869  0, // num of mip maps
870  //Ogre::PF_BYTE_RGBA,
871  Ogre::PF_BYTE_BGRA,
872  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
873  }
874  else{ // As that texture does not seem to work properly with direct3D we use a default texture
875  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
876  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
877  Ogre::TEX_TYPE_2D,
878  mBackgroundWidth,//width
879  mBackgroundHeight,//height
880  0, // num of mip maps
881  //Ogre::PF_BYTE_RGBA,
882  Ogre::PF_BYTE_BGRA,
883  Ogre::TU_DEFAULT);
884  }
885 
886 
887  // Pointer to the dynamic texture
888  Ogre::TexturePtr dynTexPtr =
889  Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
890 //#if ( OGRE_VERSION >= (1 << 16 | 9 << 8 | 0) )
891 // .dynamicCast<Ogre::Texture>();// Get the pixel buffer
892 //#else
893 // ;
894 //#endif
895 
896  // Get the pixel buffer
897  mPixelBuffer = dynTexPtr->getBuffer();
898 
899  // Material to apply the texture to the background
900  Ogre::MaterialPtr Backgroundmaterial
901  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
902  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
903 //#if ( OGRE_VERSION >= (1 << 16 | 9 << 8 | 0) )
904 // .dynamicCast<Ogre::Material>();
905 //#else
906 // ;
907 //#endif
908  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
909  Backgroundtechnique->createPass();
910  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
911  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
912  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
913  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
914  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
915  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
916 
917  // Add the background to the Scene Graph so it will be rendered
918  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
919  BackgroundNode->attachObject(mBackground);
920 }
921 
930 {
931 #ifdef VISP_HAVE_OIS
932  if( mInputManager )
933  {
934  mInputManager->destroyInputObject( mKeyboard );
935 
936  OIS::InputManager::destroyInputSystem(mInputManager);
937  mInputManager = 0;
938  }
939 #endif
940 }
941 
946 {
947  Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
948  f = (Ogre::Real)200.0; // Far clip distance
949  n = (Ogre::Real)0.001; // Near clip distance
950  f_m_n = (Ogre::Real)(f-n);
951  f_p_n = (Ogre::Real)(f+n);
952  px = (Ogre::Real)mcam.get_px();
953  py = (Ogre::Real)mcam.get_py();
954  u0 = (Ogre::Real)mcam.get_u0();
955  v0 = (Ogre::Real)mcam.get_v0();
956  Ogre::Matrix4 Projection
957  = Ogre::Matrix4( (Ogre::Real)(2.0*px/mBackgroundWidth), 0, (Ogre::Real)(2.0*(u0/mBackgroundWidth)-1.0), 0,
958  0, (Ogre::Real)(2.0*py/mBackgroundHeight), (Ogre::Real)(2.0*(v0/mBackgroundHeight)-1.0),0,
959  0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
960  0, 0, -1.0, 0);
961  mCamera->setCustomProjectionMatrix(true, Projection);
962 }
963 
968 {
969  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
970  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
971  // performance than HBL_NORMAL
972  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
973  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
974  // Buffer data
975  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
976  // Fill in the data in the grey level texture
977  memcpy(pDest, I.bitmap, mBackgroundHeight*mBackgroundWidth);
978 
979  // Unlock the pixel buffer
980  mPixelBuffer->unlock();
981 }
982 
987 {
988  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
989  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
990  // performance than HBL_NORMAL
991  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
992  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
993  // Buffer data
994  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
995  // Fill in the data in the grey level texture
996 #if 1 // if texture in BGRa format
997  for(unsigned int i=0; i<mBackgroundHeight; i++){
998  for(unsigned int j=0; j<mBackgroundWidth; j++){
999  // Color Image
1000 // *pDest++=I[i][mBackgroundWidth-j].B; // Blue component
1001 // *pDest++=I[i][mBackgroundWidth-j].G; // Green component
1002 // *pDest++=I[i][mBackgroundWidth-j].R; // Red component
1003 
1004  *pDest++=I[i][j].B; // Blue component
1005  *pDest++=I[i][j].G; // Green component
1006  *pDest++=I[i][j].R; // Red component
1007 
1008  *pDest++ = 255; // Alpha component
1009  }
1010  }
1011 #else // if texture in RGBa format which is the format of the input image
1012  memcpy(pDest, I.bitmap, mBackgroundHeight*mBackgroundWidth*sizeof(vpRGBa));
1013 #endif
1014 
1015  // Unlock the pixel buffer
1016  mPixelBuffer->unlock();
1017 }
1018 
1023 {
1024  // The matrix is given to Ogre with some changes to fit with the world projection
1025  Ogre::Matrix4 ModelView
1026 // = Ogre::Matrix4( (Ogre::Real)-cMo[0][0], (Ogre::Real)-cMo[0][1], (Ogre::Real)-cMo[0][2], (Ogre::Real)-cMo[0][3],
1027  = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1028  (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1029  (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1030  (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1031  mCamera->setCustomViewMatrix(true, ModelView);
1032 }
1033 
1041 {
1043  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("rtf");
1044 //#if ( OGRE_VERSION >= (1 << 16 | 9 << 8 | 0) )
1045 // .dynamicCast<Ogre::Texture>();
1046 //#else
1047 // ;
1048 //#endif
1049  Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1050  mWindow->update();
1051  RTarget->update();
1052  if(I.getHeight() != mWindow->getHeight() || I.getWidth() != mWindow->getWidth()){
1053  I.resize(mWindow->getHeight(), mWindow->getWidth());
1054  }
1055  Ogre::HardwarePixelBufferSharedPtr mPixelBuffer = dynTexPtr->getBuffer();
1056  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1057  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1058  dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1059  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
1060 #if 1 // if texture in BGRa format
1061  for(unsigned int i=0; i<I.getHeight(); i++){
1062  for(unsigned int j=0; j<I.getWidth(); j++){
1063  // Color Image
1064  I[i][j].B = *pDest++; // Blue component
1065  I[i][j].G = *pDest++; // Green component
1066  I[i][j].R = *pDest++; // Red component
1067  I[i][j].A = *pDest++; // Alpha component
1068  }
1069  }
1070 #else // if texture in RGBa format which is the format of the input image
1071  memcpy(I.bitmap, pDest, I.getHeight()*I.getWidth()*sizeof(vpRGBa));
1072 #endif
1073 
1074  // Unlock the pixel buffer
1075  mPixelBuffer->unlock();
1076 
1077 }
1078 
1079 
1080 #endif
1081 
virtual void updateBackgroundTexture(const vpImage< unsigned char > &I)
Definition: vpAROgre.cpp:967
bool keepOn
Definition: vpAROgre.h:331
bool mshowConfigDialog
Definition: vpAROgre.h:345
double get_u0() const
vpTranslationVector getPosition(const std::string &name) const
Definition: vpAROgre.cpp:677
Ogre::String name
Definition: vpAROgre.h:314
void setRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:688
unsigned int getWidth() const
Definition: vpImage.h:159
unsigned int mWindowWidth
Definition: vpAROgre.h:339
Ogre::String mPluginsPath
Definition: vpAROgre.h:322
unsigned int mBackgroundWidth
Definition: vpAROgre.h:337
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
virtual bool customframeEnded(const Ogre::FrameEvent &evt)
Definition: vpAROgre.cpp:542
Type * bitmap
points toward the bitmap
Definition: vpImage.h:120
virtual bool destroyScene(void)
Definition: vpAROgre.h:290
Ogre::String mResourcePath
Definition: vpAROgre.h:321
error that can be emited by ViSP classes.
Definition: vpException.h:76
virtual void createCamera(void)
Definition: vpAROgre.cpp:769
void addRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:706
vpAROgre(const vpCameraParameters &cam=vpCameraParameters(), unsigned int width=0, unsigned int height=0, const char *resourcePath=VISP_HAVE_OGRE_RESOURCES_PATH, const char *pluginsPath=VISP_HAVE_OGRE_PLUGINS_PATH)
Definition: vpAROgre.cpp:75
OIS::Keyboard * mKeyboard
Definition: vpAROgre.h:327
double get_py() const
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:565
bool continueRendering(void)
Definition: vpAROgre.cpp:633
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:601
Class that defines a RGB 32 bits structure.
Definition: vpRGBa.h:68
Ogre::RenderWindow * mWindow
Definition: vpAROgre.h:320
OIS::InputManager * mInputManager
Definition: vpAROgre.h:326
The vpRotationMatrix considers the particular case of a rotation matrix.
virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo)
Definition: vpAROgre.cpp:1022
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:485
double get_v0() const
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:135
virtual bool processInputEvent(const Ogre::FrameEvent &)
Definition: vpAROgre.h:283
vpCameraParameters mcam
Definition: vpAROgre.h:343
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:641
virtual ~vpAROgre(void)
Definition: vpAROgre.cpp:444
Ogre::Camera * mCamera
Definition: vpAROgre.h:318
Generic class defining intrinsic camera parameters.
virtual void closeOIS(void)
Definition: vpAROgre.cpp:929
void setVisibility(const std::string &name, bool isVisible)
Definition: vpAROgre.cpp:746
virtual bool updateScene(const Ogre::FrameEvent &)
Definition: vpAROgre.h:276
virtual void windowClosed(Ogre::RenderWindow *rw)
Definition: vpAROgre.cpp:554
unsigned int mWindowHeight
Definition: vpAROgre.h:338
void resize(const unsigned int h, const unsigned int w)
set the size of the image
Definition: vpImage.h:532
bool windowHidden
Definition: vpAROgre.h:340
void extract(vpRotationMatrix &R) const
void getRenderingOutput(vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo)
Definition: vpAROgre.cpp:1040
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
Definition: vpAROgre.cpp:522
unsigned int mBackgroundHeight
Definition: vpAROgre.h:336
double get_px() const
virtual void updateCameraProjection(void)
Definition: vpAROgre.cpp:945
Ogre::Root * mRoot
Definition: vpAROgre.h:317
Ogre::HardwarePixelBufferSharedPtr mPixelBuffer
Definition: vpAROgre.h:334
Ogre::Rectangle2D * mBackground
Definition: vpAROgre.h:335
virtual void createScene(void)
Definition: vpAROgre.h:267
Ogre::SceneManager * mSceneMgr
Definition: vpAROgre.h:319
void setScale(const std::string &name, const float factorx, const float factory, const float factorz)
Definition: vpAROgre.cpp:758
unsigned int getHeight() const
Definition: vpImage.h:150
void setPosition(const std::string &name, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:664
std::list< std::string > mOptionnalResourceLocation
Definition: vpAROgre.h:347
void load(const std::string &name, const std::string &model)
Definition: vpAROgre.cpp:651
Class that consider the case of a translation vector.