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();
267 bool resourcesFileExists =
false;
268 std::string resourceFile;
270 for (
size_t i = 0; i < resourcesPaths.size(); i++) {
271 resourceFile = resourcesPaths[i] +
"/resources.cfg";
273 resourcesFileExists =
true;
277 if (!resourcesFileExists) {
278 std::string errorMsg = std::string(
"Error: the requested resource file \"resources.cfg\"") +
279 std::string(
"doesn't exist in ") + std::string(
mResourcePath);
281 std::cout << errorMsg << std::endl;
285 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
287 cf.load(resourceFile);
290 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
292 Ogre::String secName, typeName, archName;
293 while (seci.hasMoreElements()) {
294 secName = seci.peekNextKey();
295 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
296 Ogre::ConfigFile::SettingsMultiMap::iterator i;
297 for (i = settings->begin(); i != settings->end(); ++i) {
299 archName = i->second;
300 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
303 std::cout <<
"##################### add resources" << std::endl;
307 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
308 *iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
314 mRoot->restoreConfig();
315 if (!
mRoot->showConfigDialog()) {
319 if (!
mRoot->restoreConfig()) {
324 if (!
mRoot->isInitialised()) {
326 const Ogre::RenderSystemList &lRenderSystemList =
mRoot->getAvailableRenderers();
327 if (lRenderSystemList.size() == 0) {
328 throw "ConfigDialog aborted";
331 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
332 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
333 mRoot->setRenderSystem(lRenderSystem);
336 mRoot->initialise(
false);
339 bool fullscreen =
false;
340 Ogre::NameValuePairList misc;
341 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
342 Ogre::ConfigOptionMap::const_iterator it = config.begin();
344 while (it != config.end()) {
345 Ogre::String leftconf = (*it).first;
346 Ogre::String rightconf = (*it).second.currentValue;
348 if (leftconf ==
"Video Mode") {
350 std::stringstream ss(rightconf.c_str());
354 std::cout <<
"Cannot read Ogre video mode" << std::endl;
360 }
else if (leftconf ==
"Full Screen") {
361 if (canInit && (rightconf ==
"Yes")) {
365 misc[leftconf] = rightconf;
373 #if (OGRE_VERSION >= (1 << 16 | 8 << 8 | 1))
374 misc[
"hidden"] =
"true";
381 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
400 viewPort->setClearEveryFrame(
true);
408 mRoot->addFrameListener(
this);
411 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
415 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
418 size_t windowHnd = 0;
419 std::ostringstream windowHndStr;
421 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
422 windowHndStr << windowHnd;
423 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
425 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
426 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
433 mKeyboard =
static_cast<OIS::Keyboard *
>(
mInputManager->createInputObject(OIS::OISKeyboard, bufferedKeys));
440 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
441 "rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
mWindow->getWidth(),
442 mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
449 Ogre::RenderTexture *RTarget = Texture->getBuffer()->getRenderTarget();
451 RTarget->getViewport(0)->setClearEveryFrame(
true);
452 RTarget->getViewport(0)->setOverlaysEnabled(
false);
466 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
471 if (Ogre::Root::getSingletonPtr() && !Ogre::Root::getSingletonPtr()->getSceneManagerIterator().hasMoreElements() &&
483 bool vpAROgre::stopTest(
const Ogre::FrameEvent &evt)
502 bool vpAROgre::frameStarted(
const Ogre::FrameEvent &evt)
508 Ogre::WindowEventUtilities::messagePump();
513 return stopTest(evt);
524 bool vpAROgre::frameEnded(
const Ogre::FrameEvent &evt)
531 return stopTest(evt);
553 if (
mKeyboard->isKeyDown(OIS::KC_ESCAPE))
597 return mRoot->renderOneFrame();
614 return mRoot->renderOneFrame();
665 Ogre::Entity *newEntity =
mSceneMgr->createEntity(entityName, model);
666 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(entityName);
667 newNode->attachObject(newEntity);
679 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(sceneName);
680 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
690 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(sceneName)->getPosition();
691 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
702 mSceneMgr->getSceneNode(sceneName)->resetOrientation();
704 Ogre::Matrix3 rotationOgre = Ogre::Matrix3((Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
705 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
706 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
707 Ogre::Quaternion q(rotationOgre);
708 mSceneMgr->getSceneNode(sceneName)->rotate(q);
719 Ogre::Matrix3 rotationOgre = Ogre::Matrix3((Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
720 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
721 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
722 Ogre::Quaternion q(rotationOgre);
723 mSceneMgr->getSceneNode(sceneName)->rotate(q);
753 mSceneMgr->getSceneNode(sceneName)->setVisible(isVisible);
766 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(1, 1, 1) /
mSceneMgr->getSceneNode(sceneName)->getScale());
768 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(factorx, factory, factorz));
787 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
788 100000.0 * Ogre::Vector3::UNIT_SCALE));
791 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
792 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
797 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
798 Ogre::TextureManager::getSingleton().createManual(
799 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
803 Ogre::PF_BYTE_L, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
805 Ogre::TextureManager::getSingleton().createManual(
806 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
810 Ogre::PF_BYTE_L, Ogre::TU_DEFAULT);
814 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
823 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
824 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
830 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
831 Backgroundtechnique->createPass();
832 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
833 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
834 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
835 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
837 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
840 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
855 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
856 100000.0 * Ogre::Vector3::UNIT_SCALE));
859 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
860 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
865 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
866 Ogre::TextureManager::getSingleton().createManual(
867 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
872 Ogre::PF_BYTE_BGRA, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
875 Ogre::TextureManager::getSingleton().createManual(
876 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
881 Ogre::PF_BYTE_BGRA, Ogre::TU_DEFAULT);
885 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
896 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
897 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
903 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
904 Backgroundtechnique->createPass();
905 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
906 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
907 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
908 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
910 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
913 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
944 Ogre::Real f, n, f_m_n, f_p_n, px, py, u0, v0;
947 f_m_n = (Ogre::Real)(f - n);
948 f_p_n = (Ogre::Real)(f + n);
953 Ogre::Matrix4 Projection = Ogre::Matrix4(
956 (Ogre::Real)(-1.0 * f_p_n / f_m_n), (Ogre::Real)(-2.0 * f * n / f_m_n), 0, 0, -1.0, 0);
957 mCamera->setCustomProjectionMatrix(
true, Projection);
971 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
973 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
991 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
993 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
1003 *pDest++ = I[i][j].B;
1004 *pDest++ = I[i][j].G;
1005 *pDest++ = I[i][j].R;
1025 Ogre::Matrix4 ModelView
1028 = Ogre::Matrix4((Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1029 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1030 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1031 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1032 mCamera->setCustomViewMatrix(
true, ModelView);
1044 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1050 Ogre::RenderTexture *RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1058 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
1059 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1060 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
1062 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
1063 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
1065 I[i][j].B = *pDest++;
1066 I[i][j].G = *pDest++;
1067 I[i][j].R = *pDest++;
1068 I[i][j].A = *pDest++;
1079 #elif !defined(VISP_BUILD_SHARED_LIBS)
1081 void dummy_vpAROgre(){};
OIS::InputManager * mInputManager
void setCameraParameters(const vpCameraParameters &cameraP)
bool continueRendering(void)
virtual bool updateScene(const Ogre::FrameEvent &)
virtual bool customframeEnded(const Ogre::FrameEvent &evt)
OIS::Keyboard * mKeyboard
virtual bool processInputEvent(const Ogre::FrameEvent &)
void addRotation(const std::string &sceneName, const vpRotationMatrix &wRo)
unsigned int mBackgroundWidth
void getRenderingOutput(vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo)
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)
std::list< std::string > mOptionnalResourceLocation
virtual bool destroyScene(void)
virtual void updateCameraProjection(void)
void setRotation(const std::string &sceneName, const vpRotationMatrix &wRo)
virtual void closeOIS(void)
unsigned int mBackgroundHeight
virtual void windowClosed(Ogre::RenderWindow *rw)
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
Ogre::String mPluginsPath
Ogre::Rectangle2D * mBackground
vpTranslationVector getPosition(const std::string &sceneName) const
virtual void createScene(void)
unsigned int mWindowHeight
void setVisibility(const std::string &sceneName, bool isVisible)
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
void load(const std::string &entityName, const std::string &model)
virtual void createCamera(void)
virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo)
Ogre::SceneManager * mSceneMgr
Ogre::HardwarePixelBufferSharedPtr mPixelBuffer
Ogre::String mResourcePath
void setPosition(const std::string &sceneName, const vpTranslationVector &wTo)
Ogre::RenderWindow * mWindow
unsigned int mWindowWidth
virtual void updateBackgroundTexture(const vpImage< unsigned char > &I)
void setScale(const std::string &sceneName, float factorx, float factory, float factorz)
Generic class defining intrinsic camera parameters.
error that can be emited by ViSP classes.
Implementation of an homogeneous matrix and operations on such kind of matrices.
void extract(vpRotationMatrix &R) const
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.