ViSP  2.7.0
vpAROgre.cpp
1 /****************************************************************************
2  *
3  * $Id: vpAROgre.cpp 4093 2013-02-04 17:49: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  if( hidden && ((OGRE_VERSION_MAJOR << 16 | OGRE_VERSION_MINOR << 8 | OGRE_VERSION_PATCH) >= (1 << 16 | 8 << 8 | 1)) ){
358  misc["hidden"] = "true";
359  windowHidden = true;
360  mWindow = mRoot->createRenderWindow(name, mWindowWidth, mWindowHeight, fullscreen, &misc);
361  }
362  else
363  mWindow = mRoot->createRenderWindow(name, mWindowWidth, mWindowHeight, fullscreen, &misc);
364 
365  // Initialise resources
366  Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
367  //-----------------------------------------------------
368  // 4 Create the SceneManager
369  //
370  // ST_GENERIC = octree
371  // ST_EXTERIOR_CLOSE = simple terrain
372  // ST_EXTERIOR_FAR = nature terrain (depreciated)
373  // ST_EXTERIOR_REAL_FAR = paging landscape
374  // ST_INTERIOR = Quake3 BSP
375  //-----------------------------------------------------
376 
377  mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
378 
379  // Create the camera
380  createCamera();
381 
382  // Create a viewport
383  Ogre::Viewport* viewPort = mWindow->addViewport(mCamera);
384 // Ogre::Viewport* viewPort = mCamera->getViewport();
385  viewPort->setClearEveryFrame(true);
386  // Set the projection parameters to match the camera intrinsic parameters
388 
389  // Create the 3D scene
390  createScene();
391 
392  // Initialise and register event handlers
393  mRoot->addFrameListener(this);
394 
395  // Register as a Window listener
396  Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
397 
398 #ifdef VISP_HAVE_OIS
399  // Initialise OIS
400  Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
401  OIS::ParamList pl;
402 
403  size_t windowHnd = 0;
404  std::ostringstream windowHndStr;
405  // Initialise window
406  mWindow->getCustomAttribute("WINDOW", &windowHnd);
407  windowHndStr << windowHnd;
408  pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
409  // Let the user use the keyboard elsewhere
410 #if defined (UNIX)
411  pl.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
412 #endif
413 
414  mInputManager = OIS::InputManager::createInputSystem( pl );
415 
416  //Create all devices
417  // Here we only consider the keyboard input
418  mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
419  if ( !bufferedKeys ) mKeyboard->setEventCallback ( this);
420 #endif
421 
422  // Initialise a render to texture to be able to retrieve a screenshot
423  Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual("rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
424  mWindow->getWidth(),mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
425 
426 
427 
428 // Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual("rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
429 // 640,480, 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
430  Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
431  /*Ogre::Viewport* Viewport =*/ RTarget->addViewport(mCamera);
432  RTarget->getViewport(0)->setClearEveryFrame(true);
433  RTarget->getViewport(0)->setOverlaysEnabled(false);
434 }
435 
440 {
441  // Destroy 3D scene
442  destroyScene();
443  // Close OIS
444  closeOIS();
445 
446  if ( mWindow) {
447  Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
449  }
450  // Delete root
451  if (mRoot) delete mRoot;
452 }
453 
459 bool vpAROgre::stopTest(const Ogre::FrameEvent& evt)
460 {
461  // Always keep this part
462  if(keepOn){
463  return updateScene(evt);
464  }
465  else
466  return keepOn;
467 }
468 
478 bool vpAROgre::frameStarted(const Ogre::FrameEvent& evt)
479 {
480  // custom method telling what to do at the beginning of each frame
481  bool result = customframeStarted(evt);
482 
483  // Listen to the window
484  Ogre::WindowEventUtilities::messagePump();
485  processInputEvent(evt);
486 
487  // See if we have to stop rendering
488  if(result) return stopTest(evt);
489  else return result;
490 }
491 
492 
499 bool vpAROgre::frameEnded(const Ogre::FrameEvent& evt)
500 {
501  // custom method telling what to do at the end of each frame
502  bool result = customframeEnded(evt);
503 
504  // See if we have to stop rendering
505  if(result) return stopTest(evt);
506  else return result;
507 }
508 
517 bool vpAROgre::customframeStarted(const Ogre::FrameEvent& /*evt*/)
518 {
519  // See if window was closed
520  if(mWindow->isClosed()) return false;
521 
522 #ifdef VISP_HAVE_OIS
523  // Get keyboard input
524  mKeyboard->capture();
525  if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
526  return false;
527 #endif
528  return true;
529 }
530 
531 
537 bool vpAROgre::customframeEnded(const Ogre::FrameEvent& /*evt*/){return true;}
538 
549 void vpAROgre::windowClosed(Ogre::RenderWindow* rw)
550 {
551  //Only close for window that created OIS (the main window in these demos)
552  if( rw == mWindow ) closeOIS();
553 }
554 
561  const vpHomogeneousMatrix &cMw)
562 {
563  // Update the background to match the situation
565 
566  // Update the camera parameters to match the grabbed image
568 
569  // Display on Ogre Window
570  return mRoot->renderOneFrame();
571 }
572 
579  const vpHomogeneousMatrix &cMw)
580 {
581  // Update the background to match the situation
583 
584  // Update the camera parameters to match the grabbed image
586 
587  // Display on Ogre Window
588  return mRoot->renderOneFrame();
589 }
590 
597  const vpHomogeneousMatrix &cMw)
598 {
599  // Display on Ogre Window
600  if(renderOneFrame(I,cMw)){
601  mWindow->update();
602  keepOn = true;
603  }
604  else
605  keepOn = false;
606 }
607 
614 {
615  // Display on Ogre Window
616  if(renderOneFrame(I,cMw)){
617  mWindow->update();
618  keepOn = true;
619  }
620  else
621  keepOn = false;
622 }
623 
629 {
630  return keepOn;
631 }
632 
637 {
638  mcam = cameraP;
639 }
640 
646 void vpAROgre::load(const std::string &name, const std::string &model)
647 {
648  Ogre::Entity *newEntity = mSceneMgr->createEntity(name, model);
649  Ogre::SceneNode *newNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
650  newNode->attachObject(newEntity);
651 }
652 
659 void vpAROgre::setPosition(const std::string &name,
660  const vpTranslationVector &wTo)
661 {
662  // Reset the position
663  Ogre::SceneNode *node = mSceneMgr->getSceneNode(name);
664  node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
665 }
666 
672 vpTranslationVector vpAROgre::getPosition(const std::string &name)const
673 {
674  Ogre::Vector3 translation = mSceneMgr->getSceneNode(name)->getPosition();
675  return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
676 }
677 
683 void vpAROgre::setRotation(const std::string &name, const vpRotationMatrix &wRo)
684 {
685  // Get the node in its original position
686  mSceneMgr->getSceneNode(name)->resetOrientation();
687  // Apply the new rotation
688  Ogre::Matrix3 rotationOgre
689  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
690  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
691  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
692  Ogre::Quaternion q(rotationOgre);
693  mSceneMgr->getSceneNode(name)->rotate(q);
694 }
695 
701 void vpAROgre::addRotation(const std::string &name,
702  const vpRotationMatrix &wRo)
703 {
704  // Apply the new rotation
705  Ogre::Matrix3 rotationOgre
706  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
707  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
708  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
709  Ogre::Quaternion q(rotationOgre);
710  mSceneMgr->getSceneNode(name)->rotate(q);
711 
712 
713 }
714 
723 void vpAROgre::setPosition(const std::string &name,
724  const vpHomogeneousMatrix &wMo)
725 {
726  // Extract the position and orientation data
727  vpRotationMatrix rotations;
728  vpTranslationVector translation;
729  wMo.extract(rotations);
730  wMo.extract(translation);
731  // Apply them to the node
732  setPosition(name, translation);
733  setRotation(name, rotations);
734 }
735 
741 void vpAROgre::setVisibility(const std::string &name, bool isVisible)
742 {
743  mSceneMgr->getSceneNode(name)->setVisible(isVisible);
744 }
745 
753 void vpAROgre::setScale(const std::string &name, const float factorx, const float factory, const float factorz)
754 {
755  // Reset the scale to its original value
756  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/mSceneMgr->getSceneNode(name)->getScale());
757  // Apply the new scale
758  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
759 }
760 
765 {
766  mCamera = mSceneMgr->createCamera("Camera");
767 }
768 
774 void vpAROgre::createBackground(vpImage<unsigned char> & /* I */)
775 {
776  // Create a rectangle to show the incoming images from the camera
777  mBackground = new Ogre::Rectangle2D(true); // true = textured
778  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
779  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
780 
781  // Texture options
782  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
783  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
784 
785  // Dynamic texture
786  // If we are using opengl we can boost a little bit performances with a dynamic texture
787  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
788  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
789  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
790  Ogre::TEX_TYPE_2D,
791  mBackgroundWidth,//width
792  mBackgroundHeight,//height
793  0, // num of mip maps
794  Ogre::PF_BYTE_L,
795  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
796  }
797  else{
798  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
799  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
800  Ogre::TEX_TYPE_2D,
801  mBackgroundWidth,//width
802  mBackgroundHeight,//height
803  0, // num of mip maps
804  Ogre::PF_BYTE_L,
805  Ogre::TU_DEFAULT);
806  }
807 
808  // Pointer to the dynamic texture
809  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
810 
811  // Get the pixel buffer
812  mPixelBuffer = dynTexPtr->getBuffer();
813 
814  // Material to apply the texture to the background
815  Ogre::MaterialPtr Backgroundmaterial
816  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
817  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
818  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
819  Backgroundtechnique->createPass();
820  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
821  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
822  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
823  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
824  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
825  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
826 
827  // Add the background to the Scene Graph so it will be rendered
828  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
829  BackgroundNode->attachObject(mBackground);
830 }
831 
837 void vpAROgre::createBackground(vpImage<vpRGBa> & /* I */)
838 {
839  // Create a rectangle to show the incoming images from the camera
840  mBackground = new Ogre::Rectangle2D(true); // true = textured
841  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
842  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
843 
844  // Texture options
845  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
846  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
847 
848  // Dynamic texture
849  // If we are using opengl we can boost a little bit performances with a dynamic texture
850  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
851  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
852  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
853  Ogre::TEX_TYPE_2D,
854  mBackgroundWidth,//width
855  mBackgroundHeight,//height
856  0, // num of mip maps
857  //Ogre::PF_BYTE_RGBA,
858  Ogre::PF_BYTE_BGRA,
859  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
860  }
861  else{ // As that texture does not seem to work properly with direct3D we use a default texture
862  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
863  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
864  Ogre::TEX_TYPE_2D,
865  mBackgroundWidth,//width
866  mBackgroundHeight,//height
867  0, // num of mip maps
868  //Ogre::PF_BYTE_RGBA,
869  Ogre::PF_BYTE_BGRA,
870  Ogre::TU_DEFAULT);
871  }
872 
873 
874  // Pointer to the dynamic texture
875  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
876 
877  // Get the pixel buffer
878  mPixelBuffer = dynTexPtr->getBuffer();
879 
880  // Material to apply the texture to the background
881  Ogre::MaterialPtr Backgroundmaterial
882  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
883  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
884  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
885  Backgroundtechnique->createPass();
886  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
887  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
888  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
889  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
890  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
891  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
892 
893  // Add the background to the Scene Graph so it will be rendered
894  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
895  BackgroundNode->attachObject(mBackground);
896 }
897 
906 {
907 #ifdef VISP_HAVE_OIS
908  if( mInputManager )
909  {
910  mInputManager->destroyInputObject( mKeyboard );
911 
912  OIS::InputManager::destroyInputSystem(mInputManager);
913  mInputManager = 0;
914  }
915 #endif
916 }
917 
922 {
923  Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
924  f = (Ogre::Real)200.0; // Far clip distance
925  n = (Ogre::Real)0.001; // Near clip distance
926  f_m_n = (Ogre::Real)(f-n);
927  f_p_n = (Ogre::Real)(f+n);
928  px = (Ogre::Real)mcam.get_px();
929  py = (Ogre::Real)mcam.get_py();
930  u0 = (Ogre::Real)mcam.get_u0();
931  v0 = (Ogre::Real)mcam.get_v0();
932  Ogre::Matrix4 Projection
933  = Ogre::Matrix4( (Ogre::Real)(2.0*px/mBackgroundWidth), 0, (Ogre::Real)(2.0*(u0/mBackgroundWidth)-1.0), 0,
934  0, (Ogre::Real)(2.0*py/mBackgroundHeight), (Ogre::Real)(2.0*(v0/mBackgroundHeight)-1.0),0,
935  0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
936  0, 0, -1.0, 0);
937  mCamera->setCustomProjectionMatrix(true, Projection);
938 }
939 
944 {
945  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
946  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
947  // performance than HBL_NORMAL
948  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
949  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
950  // Buffer data
951  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
952  // Fill in the data in the grey level texture
953  memcpy(pDest, I.bitmap, mBackgroundHeight*mBackgroundWidth);
954 
955  // Unlock the pixel buffer
956  mPixelBuffer->unlock();
957 }
958 
963 {
964  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
965  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
966  // performance than HBL_NORMAL
967  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
968  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
969  // Buffer data
970  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
971  // Fill in the data in the grey level texture
972 #if 1 // if texture in BGRa format
973  for(unsigned int i=0; i<mBackgroundHeight; i++){
974  for(unsigned int j=0; j<mBackgroundWidth; j++){
975  // Color Image
976 // *pDest++=I[i][mBackgroundWidth-j].B; // Blue component
977 // *pDest++=I[i][mBackgroundWidth-j].G; // Green component
978 // *pDest++=I[i][mBackgroundWidth-j].R; // Red component
979 
980  *pDest++=I[i][j].B; // Blue component
981  *pDest++=I[i][j].G; // Green component
982  *pDest++=I[i][j].R; // Red component
983 
984  *pDest++ = 255; // Alpha component
985  }
986  }
987 #else // if texture in RGBa format which is the format of the input image
988  memcpy(pDest, I.bitmap, mBackgroundHeight*mBackgroundWidth*sizeof(vpRGBa));
989 #endif
990 
991  // Unlock the pixel buffer
992  mPixelBuffer->unlock();
993 }
994 
999 {
1000  // The matrix is given to Ogre with some changes to fit with the world projection
1001  Ogre::Matrix4 ModelView
1002 // = Ogre::Matrix4( (Ogre::Real)-cMo[0][0], (Ogre::Real)-cMo[0][1], (Ogre::Real)-cMo[0][2], (Ogre::Real)-cMo[0][3],
1003  = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1004  (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1005  (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1006  (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1007  mCamera->setCustomViewMatrix(true, ModelView);
1008 }
1009 
1017 {
1019  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("rtf");
1020  Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1021  mWindow->update();
1022  RTarget->update();
1023  Ogre::HardwarePixelBufferSharedPtr mPixelBuffer = dynTexPtr->getBuffer();
1024  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1025  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1026  dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1027  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
1028  if(I.getHeight() != mWindow->getHeight() || I.getWidth() != mWindow->getWidth()){
1029  I.resize(mWindow->getHeight(), mWindow->getWidth());
1030  }
1031  memcpy(I.bitmap, pDest, 640*480*sizeof(vpRGBa));
1032 
1033  // Unlock the pixel buffer
1034  mPixelBuffer->unlock();
1035 
1036 }
1037 
1038 
1039 #endif
1040 
virtual void updateBackgroundTexture(const vpImage< unsigned char > &I)
Definition: vpAROgre.cpp:943
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:672
Ogre::String name
Definition: vpAROgre.h:314
void setRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:683
unsigned int getWidth() const
Definition: vpImage.h:154
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:537
Type * bitmap
points toward the bitmap
Definition: vpImage.h:115
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:530
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:764
void addRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:701
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:560
bool continueRendering(void)
Definition: vpAROgre.cpp:628
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:596
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:998
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:636
virtual ~vpAROgre(void)
Definition: vpAROgre.cpp:439
Ogre::Camera * mCamera
Definition: vpAROgre.h:318
Generic class defining intrinsic camera parameters.
virtual void closeOIS(void)
Definition: vpAROgre.cpp:905
void setVisibility(const std::string &name, bool isVisible)
Definition: vpAROgre.cpp:741
virtual bool updateScene(const Ogre::FrameEvent &)
Definition: vpAROgre.h:276
virtual void windowClosed(Ogre::RenderWindow *rw)
Definition: vpAROgre.cpp:549
unsigned int mWindowHeight
Definition: vpAROgre.h:338
bool windowHidden
Definition: vpAROgre.h:340
void extract(vpRotationMatrix &R) const
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
Definition: vpAROgre.cpp:517
unsigned int mBackgroundHeight
Definition: vpAROgre.h:336
double get_px() const
virtual void updateCameraProjection(void)
Definition: vpAROgre.cpp:921
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:753
unsigned int getHeight() const
Definition: vpImage.h:145
void setPosition(const std::string &name, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:659
std::list< std::string > mOptionnalResourceLocation
Definition: vpAROgre.h:347
void getRenderingOutput(vpImage< vpRGBa > &I, vpHomogeneousMatrix &cMo)
Definition: vpAROgre.cpp:1016
void load(const std::string &name, const std::string &model)
Definition: vpAROgre.cpp:646
Class that consider the case of a translation vector.