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;
357 if( hidden && ((OGRE_VERSION_MAJOR << 16 | OGRE_VERSION_MINOR << 8 | OGRE_VERSION_PATCH) >= (1 << 16 | 8 << 8 | 1)) ){
358 misc[
"hidden"] =
"true";
366 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
385 viewPort->setClearEveryFrame(
true);
393 mRoot->addFrameListener(
this);
396 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
400 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
403 size_t windowHnd = 0;
404 std::ostringstream windowHndStr;
406 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
407 windowHndStr << windowHnd;
408 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
411 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
418 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
419 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
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);
430 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
432 RTarget->getViewport(0)->setClearEveryFrame(
true);
433 RTarget->getViewport(0)->setOverlaysEnabled(
false);
447 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
459 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
478 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
484 Ogre::WindowEventUtilities::messagePump();
488 if(result)
return stopTest(evt);
499 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
505 if(result)
return stopTest(evt);
520 if(
mWindow->isClosed())
return false;
570 return mRoot->renderOneFrame();
588 return mRoot->renderOneFrame();
648 Ogre::Entity *newEntity =
mSceneMgr->createEntity(name, model);
649 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
650 newNode->attachObject(newEntity);
663 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(name);
664 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
674 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(name)->getPosition();
675 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
686 mSceneMgr->getSceneNode(name)->resetOrientation();
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);
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);
743 mSceneMgr->getSceneNode(name)->setVisible(isVisible);
753 void vpAROgre::setScale(
const std::string &name,
const float factorx,
const float factory,
const float factorz)
756 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(name)->getScale());
758 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
779 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
782 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
783 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
787 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
788 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
789 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
795 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
798 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
799 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
809 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
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);
822 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
823 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
825 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
828 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
842 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
845 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
846 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
850 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
851 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
852 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
859 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
862 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
863 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
875 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
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);
888 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
889 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
891 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
894 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
923 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
924 f = (Ogre::Real)200.0;
925 n = (Ogre::Real)0.001;
926 f_m_n = (Ogre::Real)(f-n);
927 f_p_n = (Ogre::Real)(f+n);
932 Ogre::Matrix4 Projection
935 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
937 mCamera->setCustomProjectionMatrix(
true, Projection);
949 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
951 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
968 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
970 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
972 #if 1 // if texture in BGRa format
987 #else // if texture in RGBa format which is the format of the input image
1001 Ogre::Matrix4 ModelView
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);
1019 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1020 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
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);
1034 mPixelBuffer->unlock();
virtual void updateBackgroundTexture(const vpImage< unsigned char > &I)
vpTranslationVector getPosition(const std::string &name) const
void setRotation(const std::string &name, const vpRotationMatrix &wRo)
unsigned int getWidth() const
unsigned int mWindowWidth
Ogre::String mPluginsPath
unsigned int mBackgroundWidth
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)
Type * bitmap
points toward the bitmap
void resize(const unsigned int height, const unsigned int width)
set the size of the image
virtual bool destroyScene(void)
Ogre::String mResourcePath
error that can be emited by ViSP classes.
virtual void createCamera(void)
void addRotation(const std::string &name, const vpRotationMatrix &wRo)
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)
OIS::Keyboard * mKeyboard
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
bool continueRendering(void)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Class that defines a RGB 32 bits structure.
Ogre::RenderWindow * mWindow
OIS::InputManager * mInputManager
The vpRotationMatrix considers the particular case of a rotation matrix.
virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo)
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
virtual bool processInputEvent(const Ogre::FrameEvent &)
void setCameraParameters(const vpCameraParameters &cameraP)
Generic class defining intrinsic camera parameters.
virtual void closeOIS(void)
void setVisibility(const std::string &name, bool isVisible)
virtual bool updateScene(const Ogre::FrameEvent &)
virtual void windowClosed(Ogre::RenderWindow *rw)
unsigned int mWindowHeight
void extract(vpRotationMatrix &R) const
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
unsigned int mBackgroundHeight
virtual void updateCameraProjection(void)
Ogre::HardwarePixelBufferSharedPtr mPixelBuffer
Ogre::Rectangle2D * mBackground
virtual void createScene(void)
Ogre::SceneManager * mSceneMgr
void setScale(const std::string &name, const float factorx, const float factory, const float factorz)
unsigned int getHeight() const
void setPosition(const std::string &name, const vpTranslationVector &wTo)
std::list< std::string > mOptionnalResourceLocation
void getRenderingOutput(vpImage< vpRGBa > &I, vpHomogeneousMatrix &cMo)
void load(const std::string &name, const std::string &model)
Class that consider the case of a translation vector.