49 #include <visp3/core/vpConfig.h> 53 #include <visp3/ar/vpAROgre.h> 54 #include <visp3/core/vpIoTools.h> 56 #include <OgreRectangle2D.h> 74 const char *pluginsPath)
75 : name(
"ViSP - Augmented Reality"), mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0), mResourcePath(resourcePath),
76 mPluginsPath(pluginsPath),
78 mInputManager(0), mKeyboard(0),
81 mImageRGBA(), mImage(), mPixelBuffer(), mBackground(NULL), mBackgroundHeight(0), mBackgroundWidth(0),
82 mWindowHeight(height), mWindowWidth(width), windowHidden(false), mNearClipping(0.001), mFarClipping(200), mcam(cam),
83 mshowConfigDialog(true), mOptionnalResourceLocation()
220 bool pluginsFileExists =
false;
221 std::string pluginFile;
223 for (
size_t i = 0; i < plugingsPaths.size(); i++) {
224 #if defined(NDEBUG) || !defined(_WIN32) 225 pluginFile = plugingsPaths[i] +
"/plugins.cfg";
227 pluginFile = plugingsPaths[i] +
"/plugins_d.cfg";
231 pluginsFileExists =
true;
235 if (!pluginsFileExists) {
236 std::string errorMsg = std::string(
"Error: the requested plugins file \"")
237 #if defined(NDEBUG) || !defined(_WIN32) 238 + std::string(
"plugins.cfg")
240 + std::string(
"plugins_d.cfg")
242 + std::string(
"\" doesn't exist in ") + std::string(
mPluginsPath);
243 std::cout << errorMsg << std::endl;
247 std::cout <<
"######################### Load plugin file: " << pluginFile << std::endl;
249 if (Ogre::Root::getSingletonPtr() == NULL)
250 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
252 mRoot = Ogre::Root::getSingletonPtr();
266 bool resourcesFileExists =
false;
267 std::string resourceFile;
269 for (
size_t i = 0; i < resourcesPaths.size(); i++) {
270 resourceFile = resourcesPaths[i] +
"/resources.cfg";
272 resourcesFileExists =
true;
276 if (!resourcesFileExists) {
277 std::string errorMsg = std::string(
"Error: the requested resource file \"resources.cfg\"") +
278 std::string(
"doesn't exist in ") + std::string(
mResourcePath);
280 std::cout << errorMsg << std::endl;
284 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
286 cf.load(resourceFile);
289 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
291 Ogre::String secName, typeName, archName;
292 while (seci.hasMoreElements()) {
293 secName = seci.peekNextKey();
294 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
295 Ogre::ConfigFile::SettingsMultiMap::iterator i;
296 for (i = settings->begin(); i != settings->end(); ++i) {
298 archName = i->second;
299 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
302 std::cout <<
"##################### add resources" << std::endl;
306 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
307 *iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
313 mRoot->restoreConfig();
314 if (!
mRoot->showConfigDialog())
317 if (!
mRoot->restoreConfig())
321 if (!
mRoot->isInitialised()) {
323 const Ogre::RenderSystemList &lRenderSystemList =
mRoot->getAvailableRenderers();
324 if (lRenderSystemList.size() == 0)
325 throw "ConfigDialog aborted";
327 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
328 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
329 mRoot->setRenderSystem(lRenderSystem);
332 mRoot->initialise(
false);
335 bool fullscreen =
false;
336 Ogre::NameValuePairList misc;
337 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
338 Ogre::ConfigOptionMap::const_iterator it = config.begin();
340 while (it != config.end()) {
341 Ogre::String leftconf = (*it).first;
342 Ogre::String rightconf = (*it).second.currentValue;
344 if (leftconf ==
"Video Mode") {
346 std::stringstream ss(rightconf.c_str());
350 std::cout <<
"Cannot read Ogre video mode" << std::endl;
357 }
else if (leftconf ==
"Full Screen") {
359 if (rightconf ==
"Yes")
363 misc[leftconf] = rightconf;
370 #if (OGRE_VERSION >= (1 << 16 | 8 << 8 | 1)) 371 misc[
"hidden"] =
"true";
378 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
397 viewPort->setClearEveryFrame(
true);
405 mRoot->addFrameListener(
this);
408 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
412 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
415 size_t windowHnd = 0;
416 std::ostringstream windowHndStr;
418 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
419 windowHndStr << windowHnd;
420 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
422 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 423 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
430 mKeyboard =
static_cast<OIS::Keyboard *
>(
mInputManager->createInputObject(OIS::OISKeyboard, bufferedKeys));
436 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
437 "rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
mWindow->getWidth(),
438 mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
445 Ogre::RenderTexture *RTarget = Texture->getBuffer()->getRenderTarget();
447 RTarget->getViewport(0)->setClearEveryFrame(
true);
448 RTarget->getViewport(0)->setOverlaysEnabled(
false);
462 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
467 if (Ogre::Root::getSingletonPtr() && !Ogre::Root::getSingletonPtr()->getSceneManagerIterator().hasMoreElements()) {
480 bool vpAROgre::stopTest(
const Ogre::FrameEvent &evt)
498 bool vpAROgre::frameStarted(
const Ogre::FrameEvent &evt)
504 Ogre::WindowEventUtilities::messagePump();
509 return stopTest(evt);
520 bool vpAROgre::frameEnded(
const Ogre::FrameEvent &evt)
527 return stopTest(evt);
549 if (
mKeyboard->isKeyDown(OIS::KC_ESCAPE))
593 return mRoot->renderOneFrame();
610 return mRoot->renderOneFrame();
661 Ogre::Entity *newEntity =
mSceneMgr->createEntity(entityName, model);
662 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(entityName);
663 newNode->attachObject(newEntity);
675 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(sceneName);
676 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
686 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(sceneName)->getPosition();
687 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
698 mSceneMgr->getSceneNode(sceneName)->resetOrientation();
700 Ogre::Matrix3 rotationOgre = Ogre::Matrix3((Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
701 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
702 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
703 Ogre::Quaternion q(rotationOgre);
704 mSceneMgr->getSceneNode(sceneName)->rotate(q);
715 Ogre::Matrix3 rotationOgre = 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);
749 mSceneMgr->getSceneNode(sceneName)->setVisible(isVisible);
759 void vpAROgre::setScale(
const std::string &sceneName,
const float factorx,
const float factory,
const float factorz)
762 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(1, 1, 1) /
mSceneMgr->getSceneNode(sceneName)->getScale());
764 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(factorx, factory, factorz));
783 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
784 100000.0 * Ogre::Vector3::UNIT_SCALE));
787 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
788 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
793 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
794 Ogre::TextureManager::getSingleton().createManual(
795 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
799 Ogre::PF_BYTE_L, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
801 Ogre::TextureManager::getSingleton().createManual(
802 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
806 Ogre::PF_BYTE_L, Ogre::TU_DEFAULT);
810 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
819 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
820 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
826 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
827 Backgroundtechnique->createPass();
828 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
829 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
830 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
831 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
833 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
836 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
851 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
852 100000.0 * Ogre::Vector3::UNIT_SCALE));
855 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
856 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
861 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
862 Ogre::TextureManager::getSingleton().createManual(
863 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
868 Ogre::PF_BYTE_BGRA, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
871 Ogre::TextureManager::getSingleton().createManual(
872 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
877 Ogre::PF_BYTE_BGRA, Ogre::TU_DEFAULT);
881 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
892 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
893 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
899 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
900 Backgroundtechnique->createPass();
901 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
902 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
903 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
904 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
906 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
909 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
940 Ogre::Real f, n, f_m_n, f_p_n, px, py, u0, v0;
943 f_m_n = (Ogre::Real)(f - n);
944 f_p_n = (Ogre::Real)(f + n);
949 Ogre::Matrix4 Projection = Ogre::Matrix4(
952 (Ogre::Real)(-1.0 * f_p_n / f_m_n), (Ogre::Real)(-2.0 * f * n / f_m_n), 0, 0, -1.0, 0);
953 mCamera->setCustomProjectionMatrix(
true, Projection);
967 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
969 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
987 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
989 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
991 #if 1 // if texture in BGRa format 999 *pDest++ = I[i][j].B;
1000 *pDest++ = I[i][j].G;
1001 *pDest++ = I[i][j].R;
1006 #else // if texture in RGBa format which is the format of the input image 1021 Ogre::Matrix4 ModelView
1024 = Ogre::Matrix4((Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1025 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1026 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1027 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1028 mCamera->setCustomViewMatrix(
true, ModelView);
1040 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1046 Ogre::RenderTexture *RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1052 Ogre::HardwarePixelBufferSharedPtr
mPixelBuffer = dynTexPtr->getBuffer();
1053 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1054 const Ogre::PixelBox &pixelBox = mPixelBuffer->getCurrentLock();
1055 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1056 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
1057 #if 1 // if texture in BGRa format 1058 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
1059 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
1061 I[i][j].B = *pDest++;
1062 I[i][j].G = *pDest++;
1063 I[i][j].R = *pDest++;
1064 I[i][j].A = *pDest++;
1067 #else // if texture in RGBa format which is the format of the input image 1072 mPixelBuffer->unlock();
1075 #elif !defined(VISP_BUILD_SHARED_LIBS) 1077 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.