ViSP  2.8.0
vpAROgre.cpp
1 /****************************************************************************
2  *
3  * $Id: vpAROgre.cpp 4251 2013-05-14 12:19:56Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
132  bool
133 #ifdef VISP_HAVE_OIS
134  bufferedKeys
135 #endif
136  ,bool hidden
137  )
138 {
141 
142  init(
143 #ifdef VISP_HAVE_OIS
144  bufferedKeys,
145 #else
146  false,
147 #endif
148  hidden
149  );
150  // Create the background image which will come from the grabber
151  createBackground(I);
152 }
153 
183  bool
184 #ifdef VISP_HAVE_OIS
185  bufferedKeys
186 #endif
187  ,bool hidden
188  )
189 {
192 
193  init(
194 #ifdef VISP_HAVE_OIS
195  bufferedKeys,
196 #else
197  false,
198 #endif
199  hidden
200  );
201  // Create the background image which will come from the grabber
202  createBackground(I);
203 }
204 
229 void vpAROgre::init(bool
230 #ifdef VISP_HAVE_OIS
231  bufferedKeys
232 #endif
233  ,bool hidden
234  )
235 {
236  // Create the root
237 #if defined(NDEBUG) || !defined(WIN32)
238  std::string pluginFile = mPluginsPath+"/plugins.cfg";
239 #else
240  std::string pluginFile = mPluginsPath+"/plugins_d.cfg";
241 #endif
242  if(!vpIoTools::checkFilename(pluginFile)){
243  std::string errorMsg = "Error: the requested plugins file \""
244  + pluginFile + "\" doesn't exist.";
245  std::cout << errorMsg << std::endl;
246 
247  throw (vpException(vpException::ioError, errorMsg));
248  }
249  std::cout << "######################### Load plugin file: " << pluginFile << std::endl;
250 
251  if(Ogre::Root::getSingletonPtr() == NULL)
252  mRoot = new Ogre::Root(pluginFile, "ogre.cfg", "Ogre.log");
253  else
254  mRoot = Ogre::Root::getSingletonPtr();
255 
256  // Load resource paths from config file
257 
258  // File format is:
259  // [ResourceGroupName]
260  // ArchiveType=Path
261  // .. repeat
262  // For example:
263  // [General]
264  // FileSystem=media/
265  // Zip=packages/level1.zip
266  Ogre::ConfigFile cf;
267  std::string resourceFile = mResourcePath+"/resources.cfg";
268  if(!vpIoTools::checkFilename(resourceFile)){
269  std::string errorMsg = "Error: the requested resource file \""
270  + resourceFile + "\" doesn't exist.";
271  std::cout << errorMsg << std::endl;
272 
273  throw (vpException(vpException::ioError, errorMsg));
274  }
275  std::cout << "######################### Load resource file: " << resourceFile << std::endl;
276  cf.load(resourceFile);
277 
278  // Go through all sections & settings in the file
279  Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
280 
281  Ogre::String secName, typeName, archName;
282  while (seci.hasMoreElements())
283  {
284  secName = seci.peekNextKey();
285  Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
286  Ogre::ConfigFile::SettingsMultiMap::iterator i;
287  for (i = settings->begin(); i != settings->end(); ++i)
288  {
289  typeName = i->first;
290  archName = i->second;
291  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
292  archName, typeName, secName);
293  }
294  }
295  std::cout << "##################### add resources" << std::endl;
296  //Add optionnal resources (given by the user).
297  for(std::list<std::string>::const_iterator iter = mOptionnalResourceLocation.begin(); iter != mOptionnalResourceLocation.end(); ++iter){
298  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter, "FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
299  }
300 
301  // Create the window
302  bool canInit = true;
303  if(mshowConfigDialog){
304  mRoot->restoreConfig();
305  if(!mRoot->showConfigDialog())
306  canInit = false;
307  }
308  else{
309  if(!mRoot->restoreConfig())
310  canInit = false;
311  }
312 
313  if(!mRoot->isInitialised()){
314  if(!canInit){ //We set the default renderer system
315  const Ogre::RenderSystemList& lRenderSystemList = mRoot->getAvailableRenderers();
316  if( lRenderSystemList.size() == 0 )
317  throw "ConfigDialog aborted"; // Exit the application on cancel
318 
319  Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
320  std::cout << "Using " << lRenderSystem->getName() << " as renderer." << std::endl;
321  mRoot->setRenderSystem(lRenderSystem);
322  }
323 
324  mRoot->initialise(false);
325  }
326 
327  bool fullscreen = false;
328  Ogre::NameValuePairList misc;
329  Ogre::ConfigOptionMap config = mRoot->getRenderSystem()->getConfigOptions();
330  Ogre::ConfigOptionMap::const_iterator it = config.begin();
331 
332  while( it != config.end() ){
333  Ogre::String leftconf = (*it).first;
334  Ogre::String rightconf = (*it).second.currentValue;
335 
336  if(leftconf == "Video Mode"){
337  if(canInit)
338  sscanf(rightconf.c_str(), "%d %*s %d", &mWindowWidth, &mWindowHeight);
339  else{
340  if(mWindowWidth == 0 && mWindowHeight == 0){
343  }
344  }
345  }
346  else if( leftconf == "Full Screen" ){
347  if(canInit){
348  if(rightconf == "Yes") fullscreen = true;
349  }
350  }
351  else
352  misc[leftconf] = rightconf;
353 
354  it++;
355  }
356 
357  // With Ogre version >= 1.8.1 we hide the window
358  if( hidden && ((OGRE_VERSION_MAJOR << 16 | OGRE_VERSION_MINOR << 8 | OGRE_VERSION_PATCH) >= (1 << 16 | 8 << 8 | 1)) ){
359  misc["hidden"] = "true";
360  windowHidden = true;
361  mWindow = mRoot->createRenderWindow(name, mWindowWidth, mWindowHeight, fullscreen, &misc);
362  }
363  else
364  mWindow = mRoot->createRenderWindow(name, mWindowWidth, mWindowHeight, fullscreen, &misc);
365 
366  // Initialise resources
367  Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
368  //-----------------------------------------------------
369  // 4 Create the SceneManager
370  //
371  // ST_GENERIC = octree
372  // ST_EXTERIOR_CLOSE = simple terrain
373  // ST_EXTERIOR_FAR = nature terrain (depreciated)
374  // ST_EXTERIOR_REAL_FAR = paging landscape
375  // ST_INTERIOR = Quake3 BSP
376  //-----------------------------------------------------
377 
378  mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
379 
380  // Create the camera
381  createCamera();
382 
383  // Create a viewport
384  Ogre::Viewport* viewPort = mWindow->addViewport(mCamera);
385 // Ogre::Viewport* viewPort = mCamera->getViewport();
386  viewPort->setClearEveryFrame(true);
387  // Set the projection parameters to match the camera intrinsic parameters
389 
390  // Create the 3D scene
391  createScene();
392 
393  // Initialise and register event handlers
394  mRoot->addFrameListener(this);
395 
396  // Register as a Window listener
397  Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
398 
399 #ifdef VISP_HAVE_OIS
400  // Initialise OIS
401  Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
402  OIS::ParamList pl;
403 
404  size_t windowHnd = 0;
405  std::ostringstream windowHndStr;
406  // Initialise window
407  mWindow->getCustomAttribute("WINDOW", &windowHnd);
408  windowHndStr << windowHnd;
409  pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
410  // Let the user use the keyboard elsewhere
411 #if defined (UNIX)
412  pl.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
413 #endif
414 
415  mInputManager = OIS::InputManager::createInputSystem( pl );
416 
417  //Create all devices
418  // Here we only consider the keyboard input
419  mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
420  if ( !bufferedKeys ) mKeyboard->setEventCallback ( this);
421 #endif
422 
423  // Initialise a render to texture to be able to retrieve a screenshot
424  Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual("rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
425  mWindow->getWidth(),mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
426 
427 
428 
429 // Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual("rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
430 // 640,480, 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
431  Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
432  /*Ogre::Viewport* Viewport =*/ RTarget->addViewport(mCamera);
433  RTarget->getViewport(0)->setClearEveryFrame(true);
434  RTarget->getViewport(0)->setOverlaysEnabled(false);
435 }
436 
441 {
442  // Destroy 3D scene
443  destroyScene();
444  // Close OIS
445  closeOIS();
446 
447  if ( mWindow) {
448  Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
450  }
451  // Delete root
452  if (mRoot) delete mRoot;
453 }
454 
460 bool vpAROgre::stopTest(const Ogre::FrameEvent& evt)
461 {
462  // Always keep this part
463  if(keepOn){
464  return updateScene(evt);
465  }
466  else
467  return keepOn;
468 }
469 
479 bool vpAROgre::frameStarted(const Ogre::FrameEvent& evt)
480 {
481  // custom method telling what to do at the beginning of each frame
482  bool result = customframeStarted(evt);
483 
484  // Listen to the window
485  Ogre::WindowEventUtilities::messagePump();
486  processInputEvent(evt);
487 
488  // See if we have to stop rendering
489  if(result) return stopTest(evt);
490  else return result;
491 }
492 
493 
500 bool vpAROgre::frameEnded(const Ogre::FrameEvent& evt)
501 {
502  // custom method telling what to do at the end of each frame
503  bool result = customframeEnded(evt);
504 
505  // See if we have to stop rendering
506  if(result) return stopTest(evt);
507  else return result;
508 }
509 
518 bool vpAROgre::customframeStarted(const Ogre::FrameEvent& /*evt*/)
519 {
520  // See if window was closed
521  if(mWindow->isClosed()) return false;
522 
523 #ifdef VISP_HAVE_OIS
524  // Get keyboard input
525  mKeyboard->capture();
526  if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
527  return false;
528 #endif
529  return true;
530 }
531 
532 
538 bool vpAROgre::customframeEnded(const Ogre::FrameEvent& /*evt*/){return true;}
539 
550 void vpAROgre::windowClosed(Ogre::RenderWindow* rw)
551 {
552  //Only close for window that created OIS (the main window in these demos)
553  if( rw == mWindow ) closeOIS();
554 }
555 
562  const vpHomogeneousMatrix &cMw)
563 {
564  // Update the background to match the situation
566 
567  // Update the camera parameters to match the grabbed image
569 
570  // Display on Ogre Window
571  return mRoot->renderOneFrame();
572 }
573 
580  const vpHomogeneousMatrix &cMw)
581 {
582  // Update the background to match the situation
584 
585  // Update the camera parameters to match the grabbed image
587 
588  // Display on Ogre Window
589  return mRoot->renderOneFrame();
590 }
591 
598  const vpHomogeneousMatrix &cMw)
599 {
600  // Display on Ogre Window
601  if(renderOneFrame(I,cMw)){
602  mWindow->update();
603  keepOn = true;
604  }
605  else
606  keepOn = false;
607 }
608 
615 {
616  // Display on Ogre Window
617  if(renderOneFrame(I,cMw)){
618  mWindow->update();
619  keepOn = true;
620  }
621  else
622  keepOn = false;
623 }
624 
630 {
631  return keepOn;
632 }
633 
638 {
639  mcam = cameraP;
640 }
641 
647 void vpAROgre::load(const std::string &name, const std::string &model)
648 {
649  Ogre::Entity *newEntity = mSceneMgr->createEntity(name, model);
650  Ogre::SceneNode *newNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
651  newNode->attachObject(newEntity);
652 }
653 
660 void vpAROgre::setPosition(const std::string &name,
661  const vpTranslationVector &wTo)
662 {
663  // Reset the position
664  Ogre::SceneNode *node = mSceneMgr->getSceneNode(name);
665  node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
666 }
667 
673 vpTranslationVector vpAROgre::getPosition(const std::string &name)const
674 {
675  Ogre::Vector3 translation = mSceneMgr->getSceneNode(name)->getPosition();
676  return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
677 }
678 
684 void vpAROgre::setRotation(const std::string &name, const vpRotationMatrix &wRo)
685 {
686  // Get the node in its original position
687  mSceneMgr->getSceneNode(name)->resetOrientation();
688  // Apply the new rotation
689  Ogre::Matrix3 rotationOgre
690  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
691  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
692  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
693  Ogre::Quaternion q(rotationOgre);
694  mSceneMgr->getSceneNode(name)->rotate(q);
695 }
696 
702 void vpAROgre::addRotation(const std::string &name,
703  const vpRotationMatrix &wRo)
704 {
705  // Apply the new rotation
706  Ogre::Matrix3 rotationOgre
707  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
708  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
709  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
710  Ogre::Quaternion q(rotationOgre);
711  mSceneMgr->getSceneNode(name)->rotate(q);
712 
713 
714 }
715 
724 void vpAROgre::setPosition(const std::string &name,
725  const vpHomogeneousMatrix &wMo)
726 {
727  // Extract the position and orientation data
728  vpRotationMatrix rotations;
729  vpTranslationVector translation;
730  wMo.extract(rotations);
731  wMo.extract(translation);
732  // Apply them to the node
733  setPosition(name, translation);
734  setRotation(name, rotations);
735 }
736 
742 void vpAROgre::setVisibility(const std::string &name, bool isVisible)
743 {
744  mSceneMgr->getSceneNode(name)->setVisible(isVisible);
745 }
746 
754 void vpAROgre::setScale(const std::string &name, const float factorx, const float factory, const float factorz)
755 {
756  // Reset the scale to its original value
757  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/mSceneMgr->getSceneNode(name)->getScale());
758  // Apply the new scale
759  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
760 }
761 
766 {
767  mCamera = mSceneMgr->createCamera("Camera");
768 }
769 
775 void vpAROgre::createBackground(vpImage<unsigned char> & /* I */)
776 {
777  // Create a rectangle to show the incoming images from the camera
778  mBackground = new Ogre::Rectangle2D(true); // true = textured
779  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
780  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
781 
782  // Texture options
783  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
784  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
785 
786  // Dynamic texture
787  // If we are using opengl we can boost a little bit performances with a dynamic texture
788  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
789  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
790  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
791  Ogre::TEX_TYPE_2D,
792  mBackgroundWidth,//width
793  mBackgroundHeight,//height
794  0, // num of mip maps
795  Ogre::PF_BYTE_L,
796  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
797  }
798  else{
799  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
800  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
801  Ogre::TEX_TYPE_2D,
802  mBackgroundWidth,//width
803  mBackgroundHeight,//height
804  0, // num of mip maps
805  Ogre::PF_BYTE_L,
806  Ogre::TU_DEFAULT);
807  }
808 
809  // Pointer to the dynamic texture
810  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
811 
812  // Get the pixel buffer
813  mPixelBuffer = dynTexPtr->getBuffer();
814 
815  // Material to apply the texture to the background
816  Ogre::MaterialPtr Backgroundmaterial
817  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
818  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
819  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
820  Backgroundtechnique->createPass();
821  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
822  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
823  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
824  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
825  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
826  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
827 
828  // Add the background to the Scene Graph so it will be rendered
829  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
830  BackgroundNode->attachObject(mBackground);
831 }
832 
838 void vpAROgre::createBackground(vpImage<vpRGBa> & /* I */)
839 {
840  // Create a rectangle to show the incoming images from the camera
841  mBackground = new Ogre::Rectangle2D(true); // true = textured
842  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
843  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
844 
845  // Texture options
846  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
847  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
848 
849  // Dynamic texture
850  // If we are using opengl we can boost a little bit performances with a dynamic texture
851  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
852  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
853  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
854  Ogre::TEX_TYPE_2D,
855  mBackgroundWidth,//width
856  mBackgroundHeight,//height
857  0, // num of mip maps
858  //Ogre::PF_BYTE_RGBA,
859  Ogre::PF_BYTE_BGRA,
860  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
861  }
862  else{ // As that texture does not seem to work properly with direct3D we use a default texture
863  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
864  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
865  Ogre::TEX_TYPE_2D,
866  mBackgroundWidth,//width
867  mBackgroundHeight,//height
868  0, // num of mip maps
869  //Ogre::PF_BYTE_RGBA,
870  Ogre::PF_BYTE_BGRA,
871  Ogre::TU_DEFAULT);
872  }
873 
874 
875  // Pointer to the dynamic texture
876  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
877 
878  // Get the pixel buffer
879  mPixelBuffer = dynTexPtr->getBuffer();
880 
881  // Material to apply the texture to the background
882  Ogre::MaterialPtr Backgroundmaterial
883  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
884  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
885  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
886  Backgroundtechnique->createPass();
887  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
888  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
889  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
890  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
891  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
892  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
893 
894  // Add the background to the Scene Graph so it will be rendered
895  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
896  BackgroundNode->attachObject(mBackground);
897 }
898 
907 {
908 #ifdef VISP_HAVE_OIS
909  if( mInputManager )
910  {
911  mInputManager->destroyInputObject( mKeyboard );
912 
913  OIS::InputManager::destroyInputSystem(mInputManager);
914  mInputManager = 0;
915  }
916 #endif
917 }
918 
923 {
924  Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
925  f = (Ogre::Real)200.0; // Far clip distance
926  n = (Ogre::Real)0.001; // Near clip distance
927  f_m_n = (Ogre::Real)(f-n);
928  f_p_n = (Ogre::Real)(f+n);
929  px = (Ogre::Real)mcam.get_px();
930  py = (Ogre::Real)mcam.get_py();
931  u0 = (Ogre::Real)mcam.get_u0();
932  v0 = (Ogre::Real)mcam.get_v0();
933  Ogre::Matrix4 Projection
934  = Ogre::Matrix4( (Ogre::Real)(2.0*px/mBackgroundWidth), 0, (Ogre::Real)(2.0*(u0/mBackgroundWidth)-1.0), 0,
935  0, (Ogre::Real)(2.0*py/mBackgroundHeight), (Ogre::Real)(2.0*(v0/mBackgroundHeight)-1.0),0,
936  0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
937  0, 0, -1.0, 0);
938  mCamera->setCustomProjectionMatrix(true, Projection);
939 }
940 
945 {
946  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
947  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
948  // performance than HBL_NORMAL
949  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
950  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
951  // Buffer data
952  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
953  // Fill in the data in the grey level texture
954  memcpy(pDest, I.bitmap, mBackgroundHeight*mBackgroundWidth);
955 
956  // Unlock the pixel buffer
957  mPixelBuffer->unlock();
958 }
959 
964 {
965  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
966  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
967  // performance than HBL_NORMAL
968  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
969  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
970  // Buffer data
971  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
972  // Fill in the data in the grey level texture
973 #if 1 // if texture in BGRa format
974  for(unsigned int i=0; i<mBackgroundHeight; i++){
975  for(unsigned int j=0; j<mBackgroundWidth; j++){
976  // Color Image
977 // *pDest++=I[i][mBackgroundWidth-j].B; // Blue component
978 // *pDest++=I[i][mBackgroundWidth-j].G; // Green component
979 // *pDest++=I[i][mBackgroundWidth-j].R; // Red component
980 
981  *pDest++=I[i][j].B; // Blue component
982  *pDest++=I[i][j].G; // Green component
983  *pDest++=I[i][j].R; // Red component
984 
985  *pDest++ = 255; // Alpha component
986  }
987  }
988 #else // if texture in RGBa format which is the format of the input image
989  memcpy(pDest, I.bitmap, mBackgroundHeight*mBackgroundWidth*sizeof(vpRGBa));
990 #endif
991 
992  // Unlock the pixel buffer
993  mPixelBuffer->unlock();
994 }
995 
1000 {
1001  // The matrix is given to Ogre with some changes to fit with the world projection
1002  Ogre::Matrix4 ModelView
1003 // = Ogre::Matrix4( (Ogre::Real)-cMo[0][0], (Ogre::Real)-cMo[0][1], (Ogre::Real)-cMo[0][2], (Ogre::Real)-cMo[0][3],
1004  = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1005  (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1006  (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1007  (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1008  mCamera->setCustomViewMatrix(true, ModelView);
1009 }
1010 
1018 {
1020  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("rtf");
1021  Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1022  mWindow->update();
1023  RTarget->update();
1024  if(I.getHeight() != mWindow->getHeight() || I.getWidth() != mWindow->getWidth()){
1025  I.resize(mWindow->getHeight(), mWindow->getWidth());
1026  }
1027  Ogre::HardwarePixelBufferSharedPtr mPixelBuffer = dynTexPtr->getBuffer();
1028  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1029  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1030  dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1031  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
1032 #if 1 // if texture in BGRa format
1033  for(unsigned int i=0; i<I.getHeight(); i++){
1034  for(unsigned int j=0; j<I.getWidth(); j++){
1035  // Color Image
1036  I[i][j].B = *pDest++; // Blue component
1037  I[i][j].G = *pDest++; // Green component
1038  I[i][j].R = *pDest++; // Red component
1039  I[i][j].A = *pDest++; // Alpha component
1040  }
1041  }
1042 #else // if texture in RGBa format which is the format of the input image
1043  memcpy(I.bitmap, pDest, I.getHeight()*I.getWidth()*sizeof(vpRGBa));
1044 #endif
1045 
1046  // Unlock the pixel buffer
1047  mPixelBuffer->unlock();
1048 
1049 }
1050 
1051 
1052 #endif
1053 
virtual void updateBackgroundTexture(const vpImage< unsigned char > &I)
Definition: vpAROgre.cpp:944
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:673
Ogre::String name
Definition: vpAROgre.h:314
void setRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:684
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:538
Type * bitmap
points toward the bitmap
Definition: vpImage.h:120
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:535
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:75
virtual void createCamera(void)
Definition: vpAROgre.cpp:765
void addRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:702
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:561
bool continueRendering(void)
Definition: vpAROgre.cpp:629
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:597
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:999
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:131
virtual bool processInputEvent(const Ogre::FrameEvent &)
Definition: vpAROgre.h:283
vpCameraParameters mcam
Definition: vpAROgre.h:343
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:637
virtual ~vpAROgre(void)
Definition: vpAROgre.cpp:440
Ogre::Camera * mCamera
Definition: vpAROgre.h:318
Generic class defining intrinsic camera parameters.
virtual void closeOIS(void)
Definition: vpAROgre.cpp:906
void setVisibility(const std::string &name, bool isVisible)
Definition: vpAROgre.cpp:742
virtual bool updateScene(const Ogre::FrameEvent &)
Definition: vpAROgre.h:276
virtual void windowClosed(Ogre::RenderWindow *rw)
Definition: vpAROgre.cpp:550
unsigned int mWindowHeight
Definition: vpAROgre.h:338
bool windowHidden
Definition: vpAROgre.h:340
void extract(vpRotationMatrix &R) const
void getRenderingOutput(vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo)
Definition: vpAROgre.cpp:1017
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
Definition: vpAROgre.cpp:518
unsigned int mBackgroundHeight
Definition: vpAROgre.h:336
double get_px() const
virtual void updateCameraProjection(void)
Definition: vpAROgre.cpp:922
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:754
unsigned int getHeight() const
Definition: vpImage.h:150
void setPosition(const std::string &name, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:660
std::list< std::string > mOptionnalResourceLocation
Definition: vpAROgre.h:347
void load(const std::string &name, const std::string &model)
Definition: vpAROgre.cpp:647
Class that consider the case of a translation vector.