ViSP  2.6.2
vpAROgre.cpp
1 /****************************************************************************
2  *
3  * $Id: vpAROgre.cpp 3747 2012-05-30 07:39:39Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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  mWidth =width;
95  mHeight = height;
96  mshowConfigDialog = true;
98 }
99 
126  bool
127 #ifdef VISP_HAVE_OIS
128  bufferedKeys
129 #endif
130  )
131 {
132  init(
133 #ifdef VISP_HAVE_OIS
134  bufferedKeys
135 #endif
136  );
137  // Create the background image which will come from the grabber
138  createBackground(I);
139 }
140 
167  bool
168 #ifdef VISP_HAVE_OIS
169  bufferedKeys
170 #endif
171  )
172 {
173  init(
174 #ifdef VISP_HAVE_OIS
175  bufferedKeys
176 #endif
177  );
178  // Create the background image which will come from the grabber
179  createBackground(I);
180 }
181 
203 void vpAROgre::init(bool
204 #ifdef VISP_HAVE_OIS
205  bufferedKeys
206 #endif
207  )
208 {
209  // Create the root
210 #if defined(NDEBUG) || !defined(WIN32)
211  std::string pluginFile = mPluginsPath+"/plugins.cfg";
212 #else
213  std::string pluginFile = mPluginsPath+"/plugins_d.cfg";
214 #endif
215  if(!vpIoTools::checkFilename(pluginFile)){
216  std::string errorMsg = "Error: the requested plugins file \""
217  + pluginFile + "\" doesn't exist.";
218  std::cout << errorMsg << std::endl;
219 
220  throw (vpException(vpException::ioError, errorMsg));
221  }
222  std::cout << "Load plugin file: " << pluginFile << std::endl;
223  mRoot = new Ogre::Root(pluginFile, "ogre.cfg", "Ogre.log");
224 
225  // Load resource paths from config file
226 
227  // File format is:
228  // [ResourceGroupName]
229  // ArchiveType=Path
230  // .. repeat
231  // For example:
232  // [General]
233  // FileSystem=media/
234  // Zip=packages/level1.zip
235  Ogre::ConfigFile cf;
236  std::string resourceFile = mResourcePath+"/resources.cfg";
237  if(!vpIoTools::checkFilename(resourceFile)){
238  std::string errorMsg = "Error: the requested resource file \""
239  + resourceFile + "\" doesn't exist.";
240  std::cout << errorMsg << std::endl;
241 
242  throw (vpException(vpException::ioError, errorMsg));
243  }
244  std::cout << "Load resource file: " << resourceFile << std::endl;
245  cf.load(resourceFile);
246 
247  // Go through all sections & settings in the file
248  Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
249 
250  Ogre::String secName, typeName, archName;
251  while (seci.hasMoreElements())
252  {
253  secName = seci.peekNextKey();
254  Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
255  Ogre::ConfigFile::SettingsMultiMap::iterator i;
256  for (i = settings->begin(); i != settings->end(); ++i)
257  {
258  typeName = i->first;
259  archName = i->second;
260  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
261  archName, typeName, secName);
262  }
263  }
264 
265  //Add optionnal resources (given by the user).
266  for(std::list<std::string>::const_iterator iter = mOptionnalResourceLocation.begin(); iter != mOptionnalResourceLocation.end(); ++iter){
267  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter, "FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
268  }
269 
270  // Create the window
271  if(!mRoot->restoreConfig() || mshowConfigDialog)
272  if(!mRoot->showConfigDialog())
273  throw "ConfigDialog aborted"; // Exit the application on cancel
274 
275  mWindow = mRoot->initialise(true, "Visp - Augmented Reality");
276 
277  // Initialise resources
278  Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
279 
280  //-----------------------------------------------------
281  // 4 Create the SceneManager
282  //
283  // ST_GENERIC = octree
284  // ST_EXTERIOR_CLOSE = simple terrain
285  // ST_EXTERIOR_FAR = nature terrain (depreciated)
286  // ST_EXTERIOR_REAL_FAR = paging landscape
287  // ST_INTERIOR = Quake3 BSP
288  //-----------------------------------------------------
289  mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
290 
291  // Create the camera
292  createCamera();
293 
294  // Create a viewport
295  Ogre::Viewport* viewPort = mWindow->addViewport(mCamera);
296  viewPort->setClearEveryFrame(true);
297 
298  // Set the projection parameters to match the camera intrinsic parameters
300 
301  // Create the 3D scene
302  createScene();
303 
304  // Initialise and register event handlers
305  mRoot->addFrameListener( this);
306 
307  // Register as a Window listener
308  Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
309 
310 #ifdef VISP_HAVE_OIS
311  // Initialise OIS
312  Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
313  OIS::ParamList pl;
314 #endif
315  size_t windowHnd = 0;
316  std::ostringstream windowHndStr;
317 
318  // Initialise window
319  mWindow->getCustomAttribute("WINDOW", &windowHnd);
320  windowHndStr << windowHnd;
321 #ifdef VISP_HAVE_OIS
322  pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
323 
324  mInputManager = OIS::InputManager::createInputSystem( pl );
325 
326  //Create all devices
327  // Here we only consider the keyboard input
328  mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
329  if ( !bufferedKeys ) mKeyboard->setEventCallback ( this);
330 #endif
331 
332  // Initialise a render to texture to be able to retrieve a screenshot
333  Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual("rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
334  mWindow->getWidth(),mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
335  Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
336  /*Ogre::Viewport* Viewport =*/ RTarget->addViewport(mCamera);
337  RTarget->getViewport(0)->setClearEveryFrame(true);
338  RTarget->getViewport(0)->setOverlaysEnabled(false);
339 }
340 
345 {
346  // Destroy 3D scene
347  destroyScene();
348  // Close OIS
349  closeOIS();
350 
351  if ( mWindow) {
352  Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
354  }
355  // Delete root
356  if (mRoot) delete mRoot;
357 }
358 
364 bool vpAROgre::stopTest(const Ogre::FrameEvent& evt)
365 {
366  // Always keep this part
367  if(keepOn){
368  return updateScene(evt);
369  }
370  else
371  return keepOn;
372 }
373 
383 bool vpAROgre::frameStarted(const Ogre::FrameEvent& evt)
384 {
385  // custom method telling what to do at the beginning of each frame
386  bool result = customframeStarted(evt);
387 
388  // Listen to the window
389  Ogre::WindowEventUtilities::messagePump();
390  processInputEvent(evt);
391 
392  // See if we have to stop rendering
393  if(result) return stopTest(evt);
394  else return result;
395 }
396 
397 
404 bool vpAROgre::frameEnded(const Ogre::FrameEvent& evt)
405 {
406  // custom method telling what to do at the end of each frame
407  bool result = customframeEnded(evt);
408 
409  // See if we have to stop rendering
410  if(result) return stopTest(evt);
411  else return result;
412 }
413 
422 bool vpAROgre::customframeStarted(const Ogre::FrameEvent& /*evt*/)
423 {
424  // See if window was closed
425  if(mWindow->isClosed()) return false;
426 
427 #ifdef VISP_HAVE_OIS
428  // Get keyboard input
429  mKeyboard->capture();
430  if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
431  return false;
432 #endif
433  return true;
434 }
435 
436 
442 bool vpAROgre::customframeEnded(const Ogre::FrameEvent& /*evt*/){return true;}
443 
454 void vpAROgre::windowClosed(Ogre::RenderWindow* rw)
455 {
456  //Only close for window that created OIS (the main window in these demos)
457  if( rw == mWindow ) closeOIS();
458 }
459 
466  const vpHomogeneousMatrix &cMw)
467 {
468  // Update the background to match the situation
470 
471  // Update the camera parameters to match the grabbed image
473 
474  // Display on Ogre Window
475  if(mRoot->renderOneFrame()){
476  mWindow->update();
477  keepOn = true;
478  }
479  else
480  keepOn = false;
481 }
482 
489 {
490  // Update the background to match the situation
492 
493  // Update the camera parameters to match the grabbed image
495 
496  // Display on Ogre Window
497  if(mRoot->renderOneFrame()){
498  mWindow->update();
499  keepOn = true;
500  }
501  else
502  keepOn = false;
503 }
504 
510 {
511  return keepOn;
512 }
513 
518 {
519  mcam = cameraP;
520 }
521 
527 void vpAROgre::load(const std::string &name, const std::string &model)
528 {
529  Ogre::Entity *newEntity = mSceneMgr->createEntity(name, model);
530  Ogre::SceneNode *newNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
531  newNode->attachObject(newEntity);
532 }
533 
540 void vpAROgre::setPosition(const std::string &name,
541  const vpTranslationVector &wTo)
542 {
543  // Reset the position
544  Ogre::SceneNode *node = mSceneMgr->getSceneNode(name);
545  node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
546 }
547 
553 vpTranslationVector vpAROgre::getPosition(const std::string &name)const
554 {
555  Ogre::Vector3 translation = mSceneMgr->getSceneNode(name)->getPosition();
556  return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
557 }
558 
564 void vpAROgre::setRotation(const std::string &name, const vpRotationMatrix &wRo)
565 {
566  // Get the node in its original position
567  mSceneMgr->getSceneNode(name)->resetOrientation();
568  // Apply the new rotation
569  Ogre::Matrix3 rotationOgre
570  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
571  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
572  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
573  Ogre::Quaternion q(rotationOgre);
574  mSceneMgr->getSceneNode(name)->rotate(q);
575 }
576 
582 void vpAROgre::addRotation(const std::string &name,
583  const vpRotationMatrix &wRo)
584 {
585  // Apply the new rotation
586  Ogre::Matrix3 rotationOgre
587  = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
588  (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
589  (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
590  Ogre::Quaternion q(rotationOgre);
591  mSceneMgr->getSceneNode(name)->rotate(q);
592 
593 
594 }
595 
604 void vpAROgre::setPosition(const std::string &name,
605  const vpHomogeneousMatrix &wMo)
606 {
607  // Extract the position and orientation data
608  vpRotationMatrix rotations;
609  vpTranslationVector translation;
610  wMo.extract(rotations);
611  wMo.extract(translation);
612  // Apply them to the node
613  setPosition(name, translation);
614  setRotation(name, rotations);
615 }
616 
622 void vpAROgre::setVisibility(const std::string &name, bool isVisible)
623 {
624  mSceneMgr->getSceneNode(name)->setVisible(isVisible);
625 }
626 
634 void vpAROgre::setScale(const std::string &name, const float factorx, const float factory, const float factorz)
635 {
636  // Reset the scale to its original value
637  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/mSceneMgr->getSceneNode(name)->getScale());
638  // Apply the new scale
639  mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
640 }
641 
646 {
647  mCamera = mSceneMgr->createCamera("Camera");
648 }
649 
655 void vpAROgre::createBackground(vpImage<unsigned char> & /* I */)
656 {
657  // Create a rectangle to show the incoming images from the camera
658  mBackground = new Ogre::Rectangle2D(true); // true = textured
659  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
660  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
661 
662  // Texture options
663  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
664  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
665 
666  // Dynamic texture
667  // If we are using opengl we can boost a little bit performances with a dynamic texture
668  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
669  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
670  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
671  Ogre::TEX_TYPE_2D,
672  mWidth,//width
673  mHeight,//height
674  0, // num of mip maps
675  Ogre::PF_BYTE_L,
676  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
677  }
678  else{
679  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
680  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
681  Ogre::TEX_TYPE_2D,
682  mWidth,//width
683  mHeight,//height
684  0, // num of mip maps
685  Ogre::PF_BYTE_L,
686  Ogre::TU_DEFAULT);
687  }
688 
689  // Pointer to the dynamic texture
690  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
691 
692  // Get the pixel buffer
693  mPixelBuffer = dynTexPtr->getBuffer();
694 
695  // Material to apply the texture to the background
696  Ogre::MaterialPtr Backgroundmaterial
697  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
698  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
699  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
700  Backgroundtechnique->createPass();
701  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
702  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
703  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
704  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
705  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
706  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
707 
708  // Add the background to the Scene Graph so it will be rendered
709  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
710  BackgroundNode->attachObject(mBackground);
711 }
712 
718 void vpAROgre::createBackground(vpImage<vpRGBa> & /* I */)
719 {
720  // Create a rectangle to show the incoming images from the camera
721  mBackground = new Ogre::Rectangle2D(true); // true = textured
722  mBackground->setCorners(-1.0, 1.0, 1.0, -1.0); // Spread all over the window
723  mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE)); // To be shown everywhere
724 
725  // Texture options
726  Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
727  Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
728 
729  // Dynamic texture
730  // If we are using opengl we can boost a little bit performances with a dynamic texture
731  if(mRoot->getRenderSystem()->getName() == "OpenGL Rendering Subsystem") {
732  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
733  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
734  Ogre::TEX_TYPE_2D,
735  mWidth,//width
736  mHeight,//height
737  0, // num of mip maps
738  //Ogre::PF_BYTE_RGBA,
739  Ogre::PF_BYTE_BGRA,
740  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
741  }
742  else{ // As that texture does not seem to work properly with direct3D we use a default texture
743  Ogre::TextureManager::getSingleton().createManual("BackgroundTexture",
744  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
745  Ogre::TEX_TYPE_2D,
746  mWidth,//width
747  mHeight,//height
748  0, // num of mip maps
749  //Ogre::PF_BYTE_RGBA,
750  Ogre::PF_BYTE_BGRA,
751  Ogre::TU_DEFAULT);
752  }
753 
754 
755  // Pointer to the dynamic texture
756  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("BackgroundTexture");
757 
758  // Get the pixel buffer
759  mPixelBuffer = dynTexPtr->getBuffer();
760 
761  // Material to apply the texture to the background
762  Ogre::MaterialPtr Backgroundmaterial
763  = Ogre::MaterialManager::getSingleton().create("BackgroundMaterial",
764  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
765  Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
766  Backgroundtechnique->createPass();
767  Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
768  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false); // Background
769  Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false); // Background
770  Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTexture");
771  mBackground->setMaterial("BackgroundMaterial"); // Attach the material to the rectangle
772  mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); // To be rendered in Background
773 
774  // Add the background to the Scene Graph so it will be rendered
775  Ogre::SceneNode *BackgroundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BackgoundNode");
776  BackgroundNode->attachObject(mBackground);
777 }
778 
787 {
788 #ifdef VISP_HAVE_OIS
789  if( mInputManager )
790  {
791  mInputManager->destroyInputObject( mKeyboard );
792 
793  OIS::InputManager::destroyInputSystem(mInputManager);
794  mInputManager = 0;
795  }
796 #endif
797 }
798 
803 {
804  Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
805  f = (Ogre::Real)200.0; // Far clip distance
806  n = (Ogre::Real)0.001; // Near clip distance
807  f_m_n = (Ogre::Real)(f-n);
808  f_p_n = (Ogre::Real)(f+n);
809  px = (Ogre::Real)mcam.get_px();
810  py = (Ogre::Real)mcam.get_py();
811  u0 = (Ogre::Real)mcam.get_u0();
812  v0 = (Ogre::Real)mcam.get_v0();
813  Ogre::Matrix4 Projection
814  = Ogre::Matrix4( (Ogre::Real)(2.0*px/mWidth), 0, (Ogre::Real)(2.0*(u0/mWidth)-1.0), 0,
815  0, (Ogre::Real)(2.0*py/mHeight), (Ogre::Real)(2.0*(v0/mHeight)-1.0),0,
816  0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
817  0, 0, -1.0, 0);
818  mCamera->setCustomProjectionMatrix(true, Projection);
819 }
820 
825 {
826  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
827  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
828  // performance than HBL_NORMAL
829  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
830  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
831  // Buffer data
832  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
833  // Fill in the data in the grey level texture
834  memcpy(pDest, I.bitmap, mHeight*mWidth);
835 
836  // Unlock the pixel buffer
837  mPixelBuffer->unlock();
838 }
839 
844 {
845  // Inspired from Ogre wiki : http://www.ogre3d.org/tikiwiki/Creating+dynamic+textures
846  // Lock the pixel buffer and get a pixel box. HBL_DISCARD is to use for best
847  // performance than HBL_NORMAL
848  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); // Lock the buffer
849  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
850  // Buffer data
851  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
852  // Fill in the data in the grey level texture
853 #if 1 // if texture in BGRa format
854  for(unsigned int i=0; i<mHeight; i++){
855  for(unsigned int j=0; j<mWidth; j++){
856  // Color Image
857 // *pDest++=I[i][mWidth-j].B; // Blue component
858 // *pDest++=I[i][mWidth-j].G; // Green component
859 // *pDest++=I[i][mWidth-j].R; // Red component
860 
861  *pDest++=I[i][j].B; // Blue component
862  *pDest++=I[i][j].G; // Green component
863  *pDest++=I[i][j].R; // Red component
864 
865  *pDest++ = 255; // Alpha component
866  }
867  }
868 #else // if texture in RGBa format which is the format of the input image
869  memcpy(pDest, I.bitmap, mHeight*mWidth*sizeof(vpRGBa));
870 #endif
871 
872  // Unlock the pixel buffer
873  mPixelBuffer->unlock();
874 }
875 
880 {
881  // The matrix is given to Ogre with some changes to fit with the world projection
882  Ogre::Matrix4 ModelView
883 // = Ogre::Matrix4( (Ogre::Real)-cMo[0][0], (Ogre::Real)-cMo[0][1], (Ogre::Real)-cMo[0][2], (Ogre::Real)-cMo[0][3],
884  = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
885  (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
886  (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
887  (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
888  mCamera->setCustomViewMatrix(true, ModelView);
889 
890 }
891 
899 {
901  Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName("rtf");
902  Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
903  mWindow->update();
904  RTarget->update();
905  Ogre::HardwarePixelBufferSharedPtr mPixelBuffer = dynTexPtr->getBuffer();
906  mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
907  const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
908  dynTexPtr->getBuffer()->blitToMemory(pixelBox);
909  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
910  if(I.getHeight() != mWindow->getHeight() || I.getWidth() != mWindow->getWidth()){
911  I.resize(mWindow->getHeight(), mWindow->getWidth());
912  }
913  memcpy(I.bitmap, pDest, mWindow->getHeight()*mWindow->getWidth()*sizeof(vpRGBa));
914 
915  // Unlock the pixel buffer
916  mPixelBuffer->unlock();
917 
918 }
919 
920 
921 #endif
922 
virtual void updateBackgroundTexture(const vpImage< unsigned char > &I)
Definition: vpAROgre.cpp:824
bool keepOn
Definition: vpAROgre.h:326
bool mshowConfigDialog
Definition: vpAROgre.h:337
double get_u0() const
vpTranslationVector getPosition(const std::string &name) const
Definition: vpAROgre.cpp:553
void setRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:564
unsigned int getWidth() const
Definition: vpImage.h:154
Ogre::String mPluginsPath
Definition: vpAROgre.h:317
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:442
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:287
Ogre::String mResourcePath
Definition: vpAROgre.h:316
virtual void createCamera(void)
Definition: vpAROgre.cpp:645
void addRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:582
OIS::Keyboard * mKeyboard
Definition: vpAROgre.h:322
double get_py() const
bool continueRendering(void)
Definition: vpAROgre.cpp:509
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:465
Class that defines a RGB 32 bits structure.
Definition: vpRGBa.h:68
Ogre::RenderWindow * mWindow
Definition: vpAROgre.h:315
OIS::InputManager * mInputManager
Definition: vpAROgre.h:321
The vpRotationMatrix considers the particular case of a rotation matrix.
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false)
Definition: vpAROgre.cpp:125
virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo)
Definition: vpAROgre.cpp:879
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:439
double get_v0() const
virtual bool processInputEvent(const Ogre::FrameEvent &)
Definition: vpAROgre.h:280
vpCameraParameters mcam
Definition: vpAROgre.h:335
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:517
virtual ~vpAROgre(void)
Definition: vpAROgre.cpp:344
Ogre::Camera * mCamera
Definition: vpAROgre.h:313
Generic class defining intrinsic camera parameters.
virtual void closeOIS(void)
Definition: vpAROgre.cpp:786
void setVisibility(const std::string &name, bool isVisible)
Definition: vpAROgre.cpp:622
virtual bool updateScene(const Ogre::FrameEvent &)
Definition: vpAROgre.h:273
virtual void windowClosed(Ogre::RenderWindow *rw)
Definition: vpAROgre.cpp:454
void extract(vpRotationMatrix &R) const
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
Definition: vpAROgre.cpp:422
double get_px() const
virtual void updateCameraProjection(void)
Definition: vpAROgre.cpp:802
Ogre::Root * mRoot
Definition: vpAROgre.h:312
unsigned int mHeight
Definition: vpAROgre.h:331
vpAROgre(const vpCameraParameters &cam=vpCameraParameters(), unsigned int width=640, unsigned int height=480, const char *resourcePath=VISP_HAVE_OGRE_RESOURCES_PATH, const char *pluginsPath=VISP_HAVE_OGRE_PLUGINS_PATH)
Definition: vpAROgre.cpp:75
Ogre::HardwarePixelBufferSharedPtr mPixelBuffer
Definition: vpAROgre.h:329
Ogre::Rectangle2D * mBackground
Definition: vpAROgre.h:330
virtual void createScene(void)
Definition: vpAROgre.h:264
Ogre::SceneManager * mSceneMgr
Definition: vpAROgre.h:314
void setScale(const std::string &name, const float factorx, const float factory, const float factorz)
Definition: vpAROgre.cpp:634
unsigned int getHeight() const
Definition: vpImage.h:145
void setPosition(const std::string &name, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:540
unsigned int mWidth
Definition: vpAROgre.h:332
std::list< std::string > mOptionnalResourceLocation
Definition: vpAROgre.h:339
void getRenderingOutput(vpImage< vpRGBa > &I, vpHomogeneousMatrix &cMo)
Definition: vpAROgre.cpp:898
void load(const std::string &name, const std::string &model)
Definition: vpAROgre.cpp:527
Class that consider the case of a translation vector.