44 #include <visp3/core/vpConfig.h>
48 #include <visp3/ar/vpAROgre.h>
49 #include <visp3/core/vpIoTools.h>
51 #include <OgreRectangle2D.h>
70 const char *pluginsPath)
71 : name(
"ViSP - Augmented Reality"), mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0), mResourcePath(resourcePath),
72 mPluginsPath(pluginsPath),
74 mInputManager(0), mKeyboard(0),
77 mImageRGBA(), mImage(), mPixelBuffer(), mBackground(nullptr), mBackgroundHeight(0), mBackgroundWidth(0),
78 mWindowHeight(height), mWindowWidth(width), windowHidden(false), mNearClipping(0.001), mFarClipping(200), mcam(cam),
79 mshowConfigDialog(true), mOptionalResourceLocation()
215 bool pluginsFileExists =
false;
216 std::string pluginFile;
218 for (
size_t i = 0; i < plugingsPaths.size(); i++) {
219 #if defined(NDEBUG) || !defined(_WIN32)
220 pluginFile = plugingsPaths[i] +
"/plugins.cfg";
222 pluginFile = plugingsPaths[i] +
"/plugins_d.cfg";
226 pluginsFileExists =
true;
230 if (!pluginsFileExists) {
231 std::string errorMsg = std::string(
"Error: the requested plugins file \"")
232 #if defined(NDEBUG) || !defined(_WIN32)
233 +std::string(
"plugins.cfg")
235 + std::string(
"plugins_d.cfg")
237 + std::string(
"\" doesn't exist in ") + std::string(
mPluginsPath);
238 std::cout << errorMsg << std::endl;
242 std::cout <<
"######################### Load plugin file: " << pluginFile << std::endl;
244 if (Ogre::Root::getSingletonPtr() ==
nullptr) {
245 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
248 mRoot = Ogre::Root::getSingletonPtr();
263 bool resourcesFileExists =
false;
264 std::string resourceFile;
266 for (
size_t i = 0; i < resourcesPaths.size(); i++) {
267 resourceFile = resourcesPaths[i] +
"/resources.cfg";
269 resourcesFileExists =
true;
273 if (!resourcesFileExists) {
274 std::string errorMsg = std::string(
"Error: the requested resource file \"resources.cfg\"") +
275 std::string(
"doesn't exist in ") + std::string(
mResourcePath);
277 std::cout << errorMsg << std::endl;
281 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
283 cf.load(resourceFile);
286 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
288 Ogre::String secName, typeName, archName;
289 while (seci.hasMoreElements()) {
290 secName = seci.peekNextKey();
291 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
292 Ogre::ConfigFile::SettingsMultiMap::iterator i;
293 for (i = settings->begin(); i != settings->end(); ++i) {
295 archName = i->second;
296 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
299 std::cout <<
"##################### add resources" << std::endl;
303 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
304 *iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
310 mRoot->restoreConfig();
311 if (!
mRoot->showConfigDialog()) {
316 if (!
mRoot->restoreConfig()) {
321 if (!
mRoot->isInitialised()) {
323 const Ogre::RenderSystemList &lRenderSystemList =
mRoot->getAvailableRenderers();
324 if (lRenderSystemList.size() == 0) {
325 throw "ConfigDialog aborted";
328 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
329 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
330 mRoot->setRenderSystem(lRenderSystem);
333 mRoot->initialise(
false);
336 bool fullscreen =
false;
337 Ogre::NameValuePairList misc;
338 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
339 Ogre::ConfigOptionMap::const_iterator it = config.begin();
341 while (it != config.end()) {
342 Ogre::String leftconf = (*it).first;
343 Ogre::String rightconf = (*it).second.currentValue;
345 if (leftconf ==
"Video Mode") {
347 std::stringstream ss(rightconf.c_str());
351 std::cout <<
"Cannot read Ogre video mode" << std::endl;
359 else if (leftconf ==
"Full Screen") {
360 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)
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();
668 Ogre::Entity *newEntity =
mSceneMgr->createEntity(entityName, model);
669 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(entityName);
670 newNode->attachObject(newEntity);
682 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(sceneName);
683 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
693 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(sceneName)->getPosition();
694 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
705 mSceneMgr->getSceneNode(sceneName)->resetOrientation();
707 Ogre::Matrix3 rotationOgre = 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 = Ogre::Matrix3((Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
723 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
724 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
725 Ogre::Quaternion q(rotationOgre);
726 mSceneMgr->getSceneNode(sceneName)->rotate(q);
756 mSceneMgr->getSceneNode(sceneName)->setVisible(isVisible);
769 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(1, 1, 1) /
mSceneMgr->getSceneNode(sceneName)->getScale());
771 mSceneMgr->getSceneNode(sceneName)->scale(Ogre::Vector3(factorx, factory, factorz));
790 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
791 100000.0 * Ogre::Vector3::UNIT_SCALE));
794 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
795 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
800 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
801 Ogre::TextureManager::getSingleton().createManual(
802 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
806 Ogre::PF_BYTE_L, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
809 Ogre::TextureManager::getSingleton().createManual(
810 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
814 Ogre::PF_BYTE_L, Ogre::TU_DEFAULT);
818 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
827 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
828 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
834 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
835 Backgroundtechnique->createPass();
836 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
837 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
838 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
839 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
841 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
844 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
859 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE,
860 100000.0 * Ogre::Vector3::UNIT_SCALE));
863 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
864 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
869 if (
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
870 Ogre::TextureManager::getSingleton().createManual(
871 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
876 Ogre::PF_BYTE_BGRA, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
880 Ogre::TextureManager::getSingleton().createManual(
881 "BackgroundTexture", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
886 Ogre::PF_BYTE_BGRA, Ogre::TU_DEFAULT);
890 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
901 Ogre::MaterialPtr Backgroundmaterial = Ogre::MaterialManager::getSingleton().create(
902 "BackgroundMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
908 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
909 Backgroundtechnique->createPass();
910 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
911 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
912 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
913 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
915 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
918 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
949 Ogre::Real f, n, f_m_n, f_p_n, px, py, u0, v0;
952 f_m_n = (Ogre::Real)(f - n);
953 f_p_n = (Ogre::Real)(f + n);
958 Ogre::Matrix4 Projection = Ogre::Matrix4(
961 (Ogre::Real)(-1.0 * f_p_n / f_m_n), (Ogre::Real)(-2.0 * f * n / f_m_n), 0, 0, -1.0, 0);
962 mCamera->setCustomProjectionMatrix(
true, Projection);
976 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
978 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
996 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
998 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
1008 *pDest++ = I[i][j].B;
1009 *pDest++ = I[i][j].G;
1010 *pDest++ = I[i][j].R;
1030 Ogre::Matrix4 ModelView
1033 = Ogre::Matrix4((Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1034 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1035 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1036 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1037 mCamera->setCustomViewMatrix(
true, ModelView);
1049 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1055 Ogre::RenderTexture *RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1063 const Ogre::PixelBox &pixelBox =
mPixelBuffer->getCurrentLock();
1064 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1065 Ogre::uint8 *pDest =
static_cast<Ogre::uint8 *
>(pixelBox.data);
1067 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
1068 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
1070 I[i][j].B = *pDest++;
1071 I[i][j].G = *pDest++;
1072 I[i][j].R = *pDest++;
1073 I[i][j].A = *pDest++;
1084 #elif !defined(VISP_BUILD_SHARED_LIBS)
1086 void dummy_vpAROgre() { };
OIS::InputManager * mInputManager
void setCameraParameters(const vpCameraParameters &cameraP)
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)
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)
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)
std::list< std::string > mOptionalResourceLocation
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 emitted 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.