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");
253 mRoot = Ogre::Root::getSingletonPtr();
268 bool resourcesFileExists =
false;
269 std::string resourceFile;
271 for (
size_t i = 0; i < resourcesPaths.size(); i++) {
272 resourceFile = resourcesPaths[i] +
"/resources.cfg";
274 resourcesFileExists =
true;
278 if (!resourcesFileExists) {
279 std::string errorMsg = std::string(
"Error: the requested resource file \"resources.cfg\"") +
280 std::string(
"doesn't exist in ") + std::string(
mResourcePath);
282 std::cout << errorMsg << std::endl;
286 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
288 cf.load(resourceFile);
291 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
293 Ogre::String secName, typeName, archName;
294 while (seci.hasMoreElements()) {
295 secName = seci.peekNextKey();
296 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
297 Ogre::ConfigFile::SettingsMultiMap::iterator i;
298 for (i = settings->begin(); i != settings->end(); ++i) {
300 archName = i->second;
301 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
304 std::cout <<
"##################### add resources" << std::endl;
308 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
309 *iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
315 mRoot->restoreConfig();
316 if (!
mRoot->showConfigDialog()) {
320 if (!
mRoot->restoreConfig()) {
325 if (!
mRoot->isInitialised()) {
327 const Ogre::RenderSystemList &lRenderSystemList =
mRoot->getAvailableRenderers();
328 if (lRenderSystemList.size() == 0) {
329 throw "ConfigDialog aborted";
332 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
333 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
334 mRoot->setRenderSystem(lRenderSystem);
337 mRoot->initialise(
false);
340 bool fullscreen =
false;
341 Ogre::NameValuePairList misc;
342 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
343 Ogre::ConfigOptionMap::const_iterator it = config.begin();
345 while (it != config.end()) {
346 Ogre::String leftconf = (*it).first;
347 Ogre::String rightconf = (*it).second.currentValue;
349 if (leftconf ==
"Video Mode") {
351 std::stringstream ss(rightconf.c_str());
355 std::cout <<
"Cannot read Ogre video mode" << std::endl;
361 }
else if (leftconf ==
"Full Screen") {
362 if (canInit && (rightconf ==
"Yes")) {
366 misc[leftconf] = rightconf;
374 #if (OGRE_VERSION >= (1 << 16 | 8 << 8 | 1)) 375 misc[
"hidden"] =
"true";
382 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
401 viewPort->setClearEveryFrame(
true);
409 mRoot->addFrameListener(
this);
412 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
416 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
419 size_t windowHnd = 0;
420 std::ostringstream windowHndStr;
422 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
423 windowHndStr << windowHnd;
424 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
426 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 427 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
434 mKeyboard =
static_cast<OIS::Keyboard *
>(
mInputManager->createInputObject(OIS::OISKeyboard, bufferedKeys));
441 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
442 "rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
mWindow->getWidth(),
443 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() &&
mRoot) {
483 bool vpAROgre::stopTest(
const Ogre::FrameEvent &evt)
503 bool vpAROgre::frameStarted(
const Ogre::FrameEvent &evt)
509 Ogre::WindowEventUtilities::messagePump();
514 return stopTest(evt);
525 bool vpAROgre::frameEnded(
const Ogre::FrameEvent &evt)
532 return stopTest(evt);
554 if (
mKeyboard->isKeyDown(OIS::KC_ESCAPE))
598 return mRoot->renderOneFrame();
615 return mRoot->renderOneFrame();
666 Ogre::Entity *newEntity =
mSceneMgr->createEntity(entityName, model);
667 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(entityName);
668 newNode->attachObject(newEntity);
680 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(sceneName);
681 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
691 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(sceneName)->getPosition();
692 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
703 mSceneMgr->getSceneNode(sceneName)->resetOrientation();
705 Ogre::Matrix3 rotationOgre = Ogre::Matrix3((Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
706 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
707 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
708 Ogre::Quaternion q(rotationOgre);
709 mSceneMgr->getSceneNode(sceneName)->rotate(q);
720 Ogre::Matrix3 rotationOgre = Ogre::Matrix3((Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
721 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
722 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
723 Ogre::Quaternion q(rotationOgre);
724 mSceneMgr->getSceneNode(sceneName)->rotate(q);
754 mSceneMgr->getSceneNode(sceneName)->setVisible(isVisible);
767 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(1, 1, 1) /
mSceneMgr->getSceneNode(sceneName)->getScale());
769 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(factorx, factory, factorz));
788 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
789 100000.0 * Ogre::Vector3::UNIT_SCALE));
792 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
793 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
798 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
799 Ogre::TextureManager::getSingleton().createManual(
800 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
804 Ogre::PF_BYTE_L, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
806 Ogre::TextureManager::getSingleton().createManual(
807 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
811 Ogre::PF_BYTE_L, Ogre::TU_DEFAULT);
815 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
824 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
825 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
831 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
832 Backgroundtechnique->createPass();
833 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
834 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
835 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
836 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
838 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
841 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
856 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
857 100000.0 * Ogre::Vector3::UNIT_SCALE));
860 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
861 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
866 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
867 Ogre::TextureManager::getSingleton().createManual(
868 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
873 Ogre::PF_BYTE_BGRA, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
876 Ogre::TextureManager::getSingleton().createManual(
877 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
882 Ogre::PF_BYTE_BGRA, Ogre::TU_DEFAULT);
886 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
897 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
898 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
904 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
905 Backgroundtechnique->createPass();
906 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
907 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
908 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
909 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
911 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
914 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
945 Ogre::Real f, n, f_m_n, f_p_n, px, py, u0, v0;
948 f_m_n = (Ogre::Real)(f - n);
949 f_p_n = (Ogre::Real)(f + n);
954 Ogre::Matrix4 Projection = Ogre::Matrix4(
957 (Ogre::Real)(-1.0 * f_p_n / f_m_n), (Ogre::Real)(-2.0 * f * n / f_m_n), 0, 0, -1.0, 0);
958 mCamera->setCustomProjectionMatrix(
true, Projection);
972 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
974 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
992 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
994 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
996 #if 1 // if texture in BGRa format 1004 *pDest++ = I[i][j].B;
1005 *pDest++ = I[i][j].G;
1006 *pDest++ = I[i][j].R;
1011 #else // if texture in RGBa format which is the format of the input image 1026 Ogre::Matrix4 ModelView
1029 = Ogre::Matrix4((Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1030 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1031 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1032 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1033 mCamera->setCustomViewMatrix(
true, ModelView);
1045 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1051 Ogre::RenderTexture *RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1059 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
1060 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1061 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
1062 #if 1 // if texture in BGRa format 1063 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
1064 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
1066 I[i][j].B = *pDest++;
1067 I[i][j].G = *pDest++;
1068 I[i][j].R = *pDest++;
1069 I[i][j].A = *pDest++;
1072 #else // if texture in RGBa format which is the format of the input image 1080 #elif !defined(VISP_BUILD_SHARED_LIBS) 1082 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)
void setScale(const std::string &sceneName, float factorx, float factory, float factorz)
vpTranslationVector getPosition(const std::string &sceneName) const
unsigned int mWindowWidth
Ogre::String mPluginsPath
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
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)
void extract(vpRotationMatrix &R) const
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)
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 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
unsigned int getHeight() const
Ogre::Rectangle2D * mBackground
virtual void createScene(void)
void setPosition(const std::string &sceneName, const vpTranslationVector &wTo)
Ogre::SceneManager * mSceneMgr
std::list< std::string > mOptionnalResourceLocation
unsigned int getWidth() const
Class that consider the case of a translation vector.