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");
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);
823 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
824 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
826 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
829 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
843 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
846 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
847 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
851 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
852 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
853 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
860 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
863 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
864 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
876 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
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);
889 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
890 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
892 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
895 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
924 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
925 f = (Ogre::Real)200.0;
926 n = (Ogre::Real)0.001;
927 f_m_n = (Ogre::Real)(f-n);
928 f_p_n = (Ogre::Real)(f+n);
933 Ogre::Matrix4 Projection
936 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
938 mCamera->setCustomProjectionMatrix(
true, Projection);
950 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
952 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
969 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
971 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
973 #if 1 // if texture in BGRa format
988 #else // if texture in RGBa format which is the format of the input image
1002 Ogre::Matrix4 ModelView
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);
1020 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1021 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
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++){
1036 I[i][j].B = *pDest++;
1037 I[i][j].G = *pDest++;
1038 I[i][j].R = *pDest++;
1039 I[i][j].A = *pDest++;
1042 #else // if texture in RGBa format which is the format of the input image
1047 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
void getRenderingOutput(vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo)
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 load(const std::string &name, const std::string &model)
Class that consider the case of a translation vector.