48 #include "visp3/core/vpConfig.h"
52 #include "visp3/ar/vpAROgre.h"
53 #include "visp3/core/vpIoTools.h"
72 unsigned int width,
unsigned int height,
73 const char *resourcePath,
const char *pluginsPath)
74 : name(
"ViSP - Augmented Reality"),mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0),
75 mResourcePath(resourcePath), mPluginsPath(pluginsPath),
77 mInputManager(0), mKeyboard(0),
80 mImageRGBA(), mImage(), mPixelBuffer(NULL), mBackground(NULL), mBackgroundHeight(0),
81 mBackgroundWidth(0), mWindowHeight(height), mWindowWidth(width), windowHidden(false),
82 mNearClipping(0.001), mFarClipping(200), mcam(cam), mshowConfigDialog(true),
83 mOptionnalResourceLocation()
222 bool pluginsFileExists =
false;
223 std::string pluginFile;
225 for (
size_t i=0; i<plugingsPaths.size(); i++) {
226 #if defined(NDEBUG) || !defined(_WIN32)
227 pluginFile = plugingsPaths[i]+
"/plugins.cfg";
229 pluginFile = plugingsPaths[i]+
"/plugins_d.cfg";
233 pluginsFileExists =
true;
237 if (! pluginsFileExists) {
238 std::string errorMsg = std::string(
"Error: the requested plugins file \"")
239 #if defined(NDEBUG) || !defined(_WIN32)
240 + std::string(
"plugins.cfg")
242 + std::string(
"plugins_d.cfg")
244 + std::string(
"\" doesn't exist in ")
246 std::cout << errorMsg << std::endl;
250 std::cout <<
"######################### Load plugin file: " << pluginFile << std::endl;
252 if(Ogre::Root::getSingletonPtr() == NULL)
253 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
255 mRoot = Ogre::Root::getSingletonPtr();
269 bool resourcesFileExists =
false;
270 std::string resourceFile;
272 for (
size_t i=0; i<resourcesPaths.size(); i++) {
273 resourceFile = resourcesPaths[i]+
"/resources.cfg";
275 resourcesFileExists =
true;
279 if (! resourcesFileExists) {
280 std::string errorMsg = std::string(
"Error: the requested resource file \"resources.cfg\"")
281 + std::string(
"doesn't exist in ")
284 std::cout << errorMsg << std::endl;
288 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
290 cf.load(resourceFile);
293 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
295 Ogre::String secName, typeName, archName;
296 while (seci.hasMoreElements())
298 secName = seci.peekNextKey();
299 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
300 Ogre::ConfigFile::SettingsMultiMap::iterator i;
301 for (i = settings->begin(); i != settings->end(); ++i)
304 archName = i->second;
305 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
306 archName, typeName, secName);
309 std::cout <<
"##################### add resources" << std::endl;
312 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
318 mRoot->restoreConfig();
319 if(!
mRoot->showConfigDialog())
323 if(!
mRoot->restoreConfig())
327 if(!
mRoot->isInitialised()){
329 const Ogre::RenderSystemList& lRenderSystemList =
mRoot->getAvailableRenderers();
330 if( lRenderSystemList.size() == 0 )
331 throw "ConfigDialog aborted";
333 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
334 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
335 mRoot->setRenderSystem(lRenderSystem);
338 mRoot->initialise(
false);
341 bool fullscreen =
false;
342 Ogre::NameValuePairList misc;
343 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
344 Ogre::ConfigOptionMap::const_iterator it = config.begin();
346 while( it != config.end() ){
347 Ogre::String leftconf = (*it).first;
348 Ogre::String rightconf = (*it).second.currentValue;
350 if(leftconf ==
"Video Mode"){
352 std::stringstream ss(rightconf.c_str());
356 std::cout <<
"Cannot read Ogre video mode" << std::endl;
365 else if( leftconf ==
"Full Screen" ){
367 if(rightconf ==
"Yes") fullscreen =
true;
371 misc[leftconf] = rightconf;
378 #if ( OGRE_VERSION >= (1 << 16 | 8 << 8 | 1) )
379 misc[
"hidden"] =
"true";
386 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
405 viewPort->setClearEveryFrame(
true);
413 mRoot->addFrameListener(
this);
416 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
420 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
423 size_t windowHnd = 0;
424 std::ostringstream windowHndStr;
426 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
427 windowHndStr << windowHnd;
428 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
430 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
431 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
438 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
439 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
443 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
"rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
444 mWindow->getWidth(),
mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
450 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
452 RTarget->getViewport(0)->setClearEveryFrame(
true);
453 RTarget->getViewport(0)->setOverlaysEnabled(
false);
467 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
472 if(Ogre::Root::getSingletonPtr() && !Ogre::Root::getSingletonPtr()->getSceneManagerIterator().hasMoreElements()) {
485 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
504 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
510 Ogre::WindowEventUtilities::messagePump();
514 if(result)
return stopTest(evt);
525 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
531 if(result)
return stopTest(evt);
546 if(
mWindow->isClosed())
return false;
596 return mRoot->renderOneFrame();
614 return mRoot->renderOneFrame();
674 Ogre::Entity *newEntity =
mSceneMgr->createEntity(entityName, model);
675 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(entityName);
676 newNode->attachObject(newEntity);
689 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(sceneName);
690 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
700 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(sceneName)->getPosition();
701 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
712 mSceneMgr->getSceneNode(sceneName)->resetOrientation();
714 Ogre::Matrix3 rotationOgre
715 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
716 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
717 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
718 Ogre::Quaternion q(rotationOgre);
719 mSceneMgr->getSceneNode(sceneName)->rotate(q);
730 Ogre::Matrix3 rotationOgre
731 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
732 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
733 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
734 Ogre::Quaternion q(rotationOgre);
735 mSceneMgr->getSceneNode(sceneName)->rotate(q);
765 mSceneMgr->getSceneNode(sceneName)->setVisible(isVisible);
775 void vpAROgre::setScale(
const std::string &sceneName,
const float factorx,
const float factory,
const float factorz)
778 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(sceneName)->getScale());
780 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(factorx, factory, factorz));
801 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
804 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
805 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
809 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
810 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
811 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
817 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
820 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
821 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
831 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
840 Ogre::MaterialPtr Backgroundmaterial
841 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
842 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
848 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
849 Backgroundtechnique->createPass();
850 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
851 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
852 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
853 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
855 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
858 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
872 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
875 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
876 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
880 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
881 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
882 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
889 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
892 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
893 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
905 Ogre::TexturePtr dynTexPtr =
906 Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
917 Ogre::MaterialPtr Backgroundmaterial
918 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
919 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
925 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
926 Backgroundtechnique->createPass();
927 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
928 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
929 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
930 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
932 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
935 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
967 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
970 f_m_n = (Ogre::Real)(f-n);
971 f_p_n = (Ogre::Real)(f+n);
976 Ogre::Matrix4 Projection
979 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
981 mCamera->setCustomProjectionMatrix(
true, Projection);
994 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
996 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1013 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
1015 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1017 #if 1 // if texture in BGRa format
1032 #else // if texture in RGBa format which is the format of the input image
1046 Ogre::Matrix4 ModelView
1048 = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1049 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1050 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1051 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1052 mCamera->setCustomViewMatrix(
true, ModelView);
1064 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1070 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1076 Ogre::HardwarePixelBufferSharedPtr
mPixelBuffer = dynTexPtr->getBuffer();
1077 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1078 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1079 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1080 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1081 #if 1 // if texture in BGRa format
1082 for(
unsigned int i=0; i<I.
getHeight(); i++){
1083 for(
unsigned int j=0; j<I.
getWidth(); j++){
1085 I[i][j].B = *pDest++;
1086 I[i][j].G = *pDest++;
1087 I[i][j].R = *pDest++;
1088 I[i][j].A = *pDest++;
1091 #else // if texture in RGBa format which is the format of the input image
1096 mPixelBuffer->unlock();
1101 #elif !defined(VISP_BUILD_SHARED_LIBS)
1103 void dummy_vpAROgre() {};
virtual void updateBackgroundTexture(const vpImage< unsigned char > &I)
void setRotation(const std::string &sceneName, const vpRotationMatrix &wRo)
void addRotation(const std::string &sceneName, const vpRotationMatrix &wRo)
unsigned int getWidth() const
vpTranslationVector getPosition(const std::string &sceneName) const
unsigned int mWindowWidth
Ogre::String mPluginsPath
unsigned int mBackgroundWidth
Implementation of an homogeneous matrix and operations on such kind of matrices.
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)
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)
Ogre::RenderWindow * mWindow
OIS::InputManager * mInputManager
Implementation of a rotation matrix and operations on such kind of matrices.
virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo)
void setVisibility(const std::string &sceneName, bool isVisible)
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
virtual bool processInputEvent(const Ogre::FrameEvent &)
void setCameraParameters(const vpCameraParameters &cameraP)
void setScale(const std::string &sceneName, const float factorx, const float factory, const float factorz)
Generic class defining intrinsic camera parameters.
virtual void closeOIS(void)
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)
resize the image : Image initialization
void extract(vpRotationMatrix &R) const
void getRenderingOutput(vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo)
void load(const std::string &entityName, const std::string &model)
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
unsigned int mBackgroundHeight
virtual void updateCameraProjection(void)
Ogre::HardwarePixelBufferSharedPtr mPixelBuffer
Ogre::Rectangle2D * mBackground
virtual void createScene(void)
void setPosition(const std::string &sceneName, const vpTranslationVector &wTo)
Ogre::SceneManager * mSceneMgr
unsigned int getHeight() const
std::list< std::string > mOptionnalResourceLocation
Class that consider the case of a translation vector.