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";
241 #if defined(NDEBUG) || !defined(_WIN32)
247 std::string errorMsg =
"Error: the requested plugins file \""
248 + pluginFile +
"\" doesn't exist.";
249 std::cout << errorMsg << std::endl;
253 std::cout <<
"######################### Load plugin file: " << pluginFile << std::endl;
255 if(Ogre::Root::getSingletonPtr() == NULL)
256 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
258 mRoot = Ogre::Root::getSingletonPtr();
273 std::string errorMsg =
"Error: the requested resource file \""
274 + resourceFile +
"\" doesn't exist.";
275 std::cout << errorMsg << std::endl;
279 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
280 cf.load(resourceFile);
283 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
285 Ogre::String secName, typeName, archName;
286 while (seci.hasMoreElements())
288 secName = seci.peekNextKey();
289 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
290 Ogre::ConfigFile::SettingsMultiMap::iterator i;
291 for (i = settings->begin(); i != settings->end(); ++i)
294 archName = i->second;
295 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
296 archName, typeName, secName);
299 std::cout <<
"##################### add resources" << std::endl;
302 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
308 mRoot->restoreConfig();
309 if(!
mRoot->showConfigDialog())
313 if(!
mRoot->restoreConfig())
317 if(!
mRoot->isInitialised()){
319 const Ogre::RenderSystemList& lRenderSystemList =
mRoot->getAvailableRenderers();
320 if( lRenderSystemList.size() == 0 )
321 throw "ConfigDialog aborted";
323 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
324 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
325 mRoot->setRenderSystem(lRenderSystem);
328 mRoot->initialise(
false);
331 bool fullscreen =
false;
332 Ogre::NameValuePairList misc;
333 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
334 Ogre::ConfigOptionMap::const_iterator it = config.begin();
336 while( it != config.end() ){
337 Ogre::String leftconf = (*it).first;
338 Ogre::String rightconf = (*it).second.currentValue;
340 if(leftconf ==
"Video Mode"){
350 else if( leftconf ==
"Full Screen" ){
352 if(rightconf ==
"Yes") fullscreen =
true;
356 misc[leftconf] = rightconf;
363 #if ( OGRE_VERSION >= (1 << 16 | 8 << 8 | 1) )
364 misc[
"hidden"] =
"true";
371 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
390 viewPort->setClearEveryFrame(
true);
398 mRoot->addFrameListener(
this);
401 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
405 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
408 size_t windowHnd = 0;
409 std::ostringstream windowHndStr;
411 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
412 windowHndStr << windowHnd;
413 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
415 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
416 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
423 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
424 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
428 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
"rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
429 mWindow->getWidth(),
mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
435 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
437 RTarget->getViewport(0)->setClearEveryFrame(
true);
438 RTarget->getViewport(0)->setOverlaysEnabled(
false);
452 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
464 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
483 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
489 Ogre::WindowEventUtilities::messagePump();
493 if(result)
return stopTest(evt);
504 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
510 if(result)
return stopTest(evt);
525 if(
mWindow->isClosed())
return false;
575 return mRoot->renderOneFrame();
593 return mRoot->renderOneFrame();
653 Ogre::Entity *newEntity =
mSceneMgr->createEntity(name, model);
654 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
655 newNode->attachObject(newEntity);
668 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(name);
669 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
679 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(name)->getPosition();
680 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
691 mSceneMgr->getSceneNode(name)->resetOrientation();
693 Ogre::Matrix3 rotationOgre
694 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
695 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
696 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
697 Ogre::Quaternion q(rotationOgre);
698 mSceneMgr->getSceneNode(name)->rotate(q);
710 Ogre::Matrix3 rotationOgre
711 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
712 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
713 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
714 Ogre::Quaternion q(rotationOgre);
715 mSceneMgr->getSceneNode(name)->rotate(q);
748 mSceneMgr->getSceneNode(name)->setVisible(isVisible);
758 void vpAROgre::setScale(
const std::string &name,
const float factorx,
const float factory,
const float factorz)
761 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(name)->getScale());
763 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
784 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
787 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
788 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
792 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
793 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
794 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
800 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
803 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
804 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
814 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
823 Ogre::MaterialPtr Backgroundmaterial
824 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
825 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
831 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
832 Backgroundtechnique->createPass();
833 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
834 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
835 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
836 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
838 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
841 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
855 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
858 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
859 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
863 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
864 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
865 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
872 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
875 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
876 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
888 Ogre::TexturePtr dynTexPtr =
889 Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
900 Ogre::MaterialPtr Backgroundmaterial
901 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
902 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
908 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
909 Backgroundtechnique->createPass();
910 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
911 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
912 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
913 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
915 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
918 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
947 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
948 f = (Ogre::Real)200.0;
949 n = (Ogre::Real)0.001;
950 f_m_n = (Ogre::Real)(f-n);
951 f_p_n = (Ogre::Real)(f+n);
956 Ogre::Matrix4 Projection
959 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
961 mCamera->setCustomProjectionMatrix(
true, Projection);
973 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
975 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
992 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
994 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
996 #if 1 // if texture in BGRa format
1011 #else // if texture in RGBa format which is the format of the input image
1025 Ogre::Matrix4 ModelView
1027 = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1028 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1029 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1030 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1031 mCamera->setCustomViewMatrix(
true, ModelView);
1043 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1049 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1055 Ogre::HardwarePixelBufferSharedPtr
mPixelBuffer = dynTexPtr->getBuffer();
1056 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1057 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1058 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1059 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1060 #if 1 // if texture in BGRa format
1061 for(
unsigned int i=0; i<I.
getHeight(); i++){
1062 for(
unsigned int j=0; j<I.
getWidth(); j++){
1064 I[i][j].B = *pDest++;
1065 I[i][j].G = *pDest++;
1066 I[i][j].R = *pDest++;
1067 I[i][j].A = *pDest++;
1070 #else // if texture in RGBa format which is the format of the input image
1075 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
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 resize(const unsigned int h, const unsigned int w)
set the size of the image
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.