52 #include "visp/vpConfig.h"
56 #include "visp/vpAROgre.h"
57 #include "visp/vpIoTools.h"
76 unsigned int width,
unsigned int height,
77 const char *resourcePath,
const char *pluginsPath)
78 : mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0)
80 , mInputManager(0), mKeyboard(0)
100 name =
"ViSP - Augmented Reality";
237 #if defined(NDEBUG) || !defined(WIN32)
243 std::string errorMsg =
"Error: the requested plugins file \""
244 + pluginFile +
"\" doesn't exist.";
245 std::cout << errorMsg << std::endl;
249 std::cout <<
"######################### Load plugin file: " << pluginFile << std::endl;
251 if(Ogre::Root::getSingletonPtr() == NULL)
252 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
254 mRoot = Ogre::Root::getSingletonPtr();
269 std::string errorMsg =
"Error: the requested resource file \""
270 + resourceFile +
"\" doesn't exist.";
271 std::cout << errorMsg << std::endl;
275 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
276 cf.load(resourceFile);
279 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
281 Ogre::String secName, typeName, archName;
282 while (seci.hasMoreElements())
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)
290 archName = i->second;
291 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
292 archName, typeName, secName);
295 std::cout <<
"##################### add resources" << std::endl;
298 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
304 mRoot->restoreConfig();
305 if(!
mRoot->showConfigDialog())
309 if(!
mRoot->restoreConfig())
313 if(!
mRoot->isInitialised()){
315 const Ogre::RenderSystemList& lRenderSystemList =
mRoot->getAvailableRenderers();
316 if( lRenderSystemList.size() == 0 )
317 throw "ConfigDialog aborted";
319 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
320 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
321 mRoot->setRenderSystem(lRenderSystem);
324 mRoot->initialise(
false);
327 bool fullscreen =
false;
328 Ogre::NameValuePairList misc;
329 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
330 Ogre::ConfigOptionMap::const_iterator it = config.begin();
332 while( it != config.end() ){
333 Ogre::String leftconf = (*it).first;
334 Ogre::String rightconf = (*it).second.currentValue;
336 if(leftconf ==
"Video Mode"){
346 else if( leftconf ==
"Full Screen" ){
348 if(rightconf ==
"Yes") fullscreen =
true;
352 misc[leftconf] = rightconf;
358 if( hidden && ((OGRE_VERSION_MAJOR << 16 | OGRE_VERSION_MINOR << 8 | OGRE_VERSION_PATCH) >= (1 << 16 | 8 << 8 | 1)) ){
359 misc[
"hidden"] =
"true";
367 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
386 viewPort->setClearEveryFrame(
true);
394 mRoot->addFrameListener(
this);
397 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
401 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
404 size_t windowHnd = 0;
405 std::ostringstream windowHndStr;
407 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
408 windowHndStr << windowHnd;
409 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
412 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
419 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
420 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
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);
431 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
433 RTarget->getViewport(0)->setClearEveryFrame(
true);
434 RTarget->getViewport(0)->setOverlaysEnabled(
false);
448 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
460 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
479 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
485 Ogre::WindowEventUtilities::messagePump();
489 if(result)
return stopTest(evt);
500 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
506 if(result)
return stopTest(evt);
521 if(
mWindow->isClosed())
return false;
571 return mRoot->renderOneFrame();
589 return mRoot->renderOneFrame();
649 Ogre::Entity *newEntity =
mSceneMgr->createEntity(name, model);
650 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
651 newNode->attachObject(newEntity);
664 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(name);
665 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
675 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(name)->getPosition();
676 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
687 mSceneMgr->getSceneNode(name)->resetOrientation();
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);
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);
744 mSceneMgr->getSceneNode(name)->setVisible(isVisible);
754 void vpAROgre::setScale(
const std::string &name,
const float factorx,
const float factory,
const float factorz)
757 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(name)->getScale());
759 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
780 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
783 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
784 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
788 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
789 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
790 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
796 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
799 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
800 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
810 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture").
811 dynamicCast<Ogre::Texture>();
817 Ogre::MaterialPtr Backgroundmaterial
818 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
819 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME).
820 dynamicCast<Ogre::Material>();
821 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
822 Backgroundtechnique->createPass();
823 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
824 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
825 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
826 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
828 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
831 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
845 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
848 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
849 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
853 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
854 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
855 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
862 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
865 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
866 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
878 Ogre::TexturePtr dynTexPtr =
879 Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture").dynamicCast<Ogre::Texture>();
882 mPixelBuffer = dynTexPtr->getBuffer();
885 Ogre::MaterialPtr Backgroundmaterial
886 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
887 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME).
888 dynamicCast<Ogre::Material>();
889 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
890 Backgroundtechnique->createPass();
891 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
892 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
893 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
894 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
896 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
899 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
928 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
929 f = (Ogre::Real)200.0;
930 n = (Ogre::Real)0.001;
931 f_m_n = (Ogre::Real)(f-n);
932 f_p_n = (Ogre::Real)(f+n);
937 Ogre::Matrix4 Projection
940 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
942 mCamera->setCustomProjectionMatrix(
true, Projection);
953 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
954 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
956 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
961 mPixelBuffer->unlock();
972 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
973 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
975 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
977 #if 1 // if texture in BGRa format
992 #else // if texture in RGBa format which is the format of the input image
997 mPixelBuffer->unlock();
1006 Ogre::Matrix4 ModelView
1008 = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1009 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1010 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1011 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1012 mCamera->setCustomViewMatrix(
true, ModelView);
1024 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf").
1025 dynamicCast<Ogre::Texture>();
1026 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1032 Ogre::HardwarePixelBufferSharedPtr mPixelBuffer = dynTexPtr->getBuffer();
1033 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1034 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1035 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1036 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1037 #if 1 // if texture in BGRa format
1038 for(
unsigned int i=0; i<I.
getHeight(); i++){
1039 for(
unsigned int j=0; j<I.
getWidth(); j++){
1041 I[i][j].B = *pDest++;
1042 I[i][j].G = *pDest++;
1043 I[i][j].R = *pDest++;
1044 I[i][j].A = *pDest++;
1047 #else // if texture in RGBa format which is the format of the input image
1052 mPixelBuffer->unlock();