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"){
354 std::cout <<
"Cannot read Ogre video mode" << std::endl;
363 else if( leftconf ==
"Full Screen" ){
365 if(rightconf ==
"Yes") fullscreen =
true;
369 misc[leftconf] = rightconf;
376 #if ( OGRE_VERSION >= (1 << 16 | 8 << 8 | 1) )
377 misc[
"hidden"] =
"true";
384 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
403 viewPort->setClearEveryFrame(
true);
411 mRoot->addFrameListener(
this);
414 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
418 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
421 size_t windowHnd = 0;
422 std::ostringstream windowHndStr;
424 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
425 windowHndStr << windowHnd;
426 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
428 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
429 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
436 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
437 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
441 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
"rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
442 mWindow->getWidth(),
mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
448 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
450 RTarget->getViewport(0)->setClearEveryFrame(
true);
451 RTarget->getViewport(0)->setOverlaysEnabled(
false);
465 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
477 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
496 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
502 Ogre::WindowEventUtilities::messagePump();
506 if(result)
return stopTest(evt);
517 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
523 if(result)
return stopTest(evt);
538 if(
mWindow->isClosed())
return false;
588 return mRoot->renderOneFrame();
606 return mRoot->renderOneFrame();
666 Ogre::Entity *newEntity =
mSceneMgr->createEntity(entityName, model);
667 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(entityName);
668 newNode->attachObject(newEntity);
681 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(sceneName);
682 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
692 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(sceneName)->getPosition();
693 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
704 mSceneMgr->getSceneNode(sceneName)->resetOrientation();
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(sceneName)->rotate(q);
722 Ogre::Matrix3 rotationOgre
723 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
724 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
725 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
726 Ogre::Quaternion q(rotationOgre);
727 mSceneMgr->getSceneNode(sceneName)->rotate(q);
757 mSceneMgr->getSceneNode(sceneName)->setVisible(isVisible);
767 void vpAROgre::setScale(
const std::string &sceneName,
const float factorx,
const float factory,
const float factorz)
770 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(sceneName)->getScale());
772 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(factorx, factory, factorz));
793 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
796 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
797 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
801 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
802 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
803 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
809 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
812 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
813 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
823 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
832 Ogre::MaterialPtr Backgroundmaterial
833 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
834 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
840 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
841 Backgroundtechnique->createPass();
842 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
843 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
844 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
845 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
847 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
850 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
864 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
867 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
868 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
872 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
873 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
874 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
881 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
884 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
885 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
897 Ogre::TexturePtr dynTexPtr =
898 Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
909 Ogre::MaterialPtr Backgroundmaterial
910 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
911 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
917 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
918 Backgroundtechnique->createPass();
919 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
920 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
921 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
922 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
924 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
927 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
959 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
962 f_m_n = (Ogre::Real)(f-n);
963 f_p_n = (Ogre::Real)(f+n);
968 Ogre::Matrix4 Projection
971 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
973 mCamera->setCustomProjectionMatrix(
true, Projection);
986 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
988 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1005 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
1007 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1009 #if 1 // if texture in BGRa format
1024 #else // if texture in RGBa format which is the format of the input image
1038 Ogre::Matrix4 ModelView
1040 = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1041 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1042 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1043 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1044 mCamera->setCustomViewMatrix(
true, ModelView);
1056 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1062 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1068 Ogre::HardwarePixelBufferSharedPtr
mPixelBuffer = dynTexPtr->getBuffer();
1069 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1070 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1071 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1072 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1073 #if 1 // if texture in BGRa format
1074 for(
unsigned int i=0; i<I.
getHeight(); i++){
1075 for(
unsigned int j=0; j<I.
getWidth(); j++){
1077 I[i][j].B = *pDest++;
1078 I[i][j].G = *pDest++;
1079 I[i][j].R = *pDest++;
1080 I[i][j].A = *pDest++;
1083 #else // if texture in RGBa format which is the format of the input image
1088 mPixelBuffer->unlock();
1093 #elif !defined(VISP_BUILD_SHARED_LIBS)
1095 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)
Class that defines a RGB 32 bits structure.
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)
set the size of the image without initializing it.
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.