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 : name(
"ViSP - Augmented Reality"),mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0),
79 mResourcePath(resourcePath), mPluginsPath(pluginsPath),
81 mInputManager(0), mKeyboard(0),
84 mImageRGBA(), mImage(), mPixelBuffer(NULL), mBackground(NULL), mBackgroundHeight(0),
85 mBackgroundWidth(0), mWindowHeight(height), mWindowWidth(width), windowHidden(false),
86 mNearClipping(0.001), mFarClipping(200), mcam(cam), mshowConfigDialog(true),
87 mOptionnalResourceLocation()
226 bool pluginsFileExists =
false;
227 std::string pluginFile;
229 for (
size_t i=0; i<plugingsPaths.size(); i++) {
230 #if defined(NDEBUG) || !defined(_WIN32)
231 pluginFile = plugingsPaths[i]+
"/plugins.cfg";
233 pluginFile = plugingsPaths[i]+
"/plugins_d.cfg";
237 pluginsFileExists =
true;
241 if (! pluginsFileExists) {
242 std::string errorMsg = std::string(
"Error: the requested plugins file \"")
243 #if defined(NDEBUG) || !defined(_WIN32)
244 + std::string(
"plugins.cfg")
246 + std::string(
"plugins_d.cfg")
248 + std::string(
"\" doesn't exist in ")
250 std::cout << errorMsg << std::endl;
254 std::cout <<
"######################### Load plugin file: " << pluginFile << std::endl;
256 if(Ogre::Root::getSingletonPtr() == NULL)
257 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
259 mRoot = Ogre::Root::getSingletonPtr();
273 bool resourcesFileExists =
false;
274 std::string resourceFile;
276 for (
size_t i=0; i<resourcesPaths.size(); i++) {
277 resourceFile = resourcesPaths[i]+
"/resources.cfg";
279 resourcesFileExists =
true;
283 if (! resourcesFileExists) {
284 std::string errorMsg = std::string(
"Error: the requested resource file \"resources.cfg\"")
285 + std::string(
"doesn't exist in ")
288 std::cout << errorMsg << std::endl;
292 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
294 cf.load(resourceFile);
297 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
299 Ogre::String secName, typeName, archName;
300 while (seci.hasMoreElements())
302 secName = seci.peekNextKey();
303 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
304 Ogre::ConfigFile::SettingsMultiMap::iterator i;
305 for (i = settings->begin(); i != settings->end(); ++i)
308 archName = i->second;
309 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
310 archName, typeName, secName);
313 std::cout <<
"##################### add resources" << std::endl;
316 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
322 mRoot->restoreConfig();
323 if(!
mRoot->showConfigDialog())
327 if(!
mRoot->restoreConfig())
331 if(!
mRoot->isInitialised()){
333 const Ogre::RenderSystemList& lRenderSystemList =
mRoot->getAvailableRenderers();
334 if( lRenderSystemList.size() == 0 )
335 throw "ConfigDialog aborted";
337 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
338 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
339 mRoot->setRenderSystem(lRenderSystem);
342 mRoot->initialise(
false);
345 bool fullscreen =
false;
346 Ogre::NameValuePairList misc;
347 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
348 Ogre::ConfigOptionMap::const_iterator it = config.begin();
350 while( it != config.end() ){
351 Ogre::String leftconf = (*it).first;
352 Ogre::String rightconf = (*it).second.currentValue;
354 if(leftconf ==
"Video Mode"){
358 std::cout <<
"Cannot read Ogre video mode" << std::endl;
367 else if( leftconf ==
"Full Screen" ){
369 if(rightconf ==
"Yes") fullscreen =
true;
373 misc[leftconf] = rightconf;
380 #if ( OGRE_VERSION >= (1 << 16 | 8 << 8 | 1) )
381 misc[
"hidden"] =
"true";
388 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
407 viewPort->setClearEveryFrame(
true);
415 mRoot->addFrameListener(
this);
418 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
422 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
425 size_t windowHnd = 0;
426 std::ostringstream windowHndStr;
428 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
429 windowHndStr << windowHnd;
430 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
432 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
433 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
440 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
441 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
445 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
"rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
446 mWindow->getWidth(),
mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
452 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
454 RTarget->getViewport(0)->setClearEveryFrame(
true);
455 RTarget->getViewport(0)->setOverlaysEnabled(
false);
469 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
481 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
500 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
506 Ogre::WindowEventUtilities::messagePump();
510 if(result)
return stopTest(evt);
521 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
527 if(result)
return stopTest(evt);
542 if(
mWindow->isClosed())
return false;
592 return mRoot->renderOneFrame();
610 return mRoot->renderOneFrame();
670 Ogre::Entity *newEntity =
mSceneMgr->createEntity(name, model);
671 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
672 newNode->attachObject(newEntity);
685 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(name);
686 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
696 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(name)->getPosition();
697 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
708 mSceneMgr->getSceneNode(name)->resetOrientation();
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);
727 Ogre::Matrix3 rotationOgre
728 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
729 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
730 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
731 Ogre::Quaternion q(rotationOgre);
732 mSceneMgr->getSceneNode(name)->rotate(q);
765 mSceneMgr->getSceneNode(name)->setVisible(isVisible);
775 void vpAROgre::setScale(
const std::string &name,
const float factorx,
const float factory,
const float factorz)
778 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(name)->getScale());
780 mSceneMgr->getSceneNode(name)->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();
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 without initializing it.
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.