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()
225 #if defined(NDEBUG) || !defined(_WIN32)
231 std::string errorMsg =
"Error: the requested plugins file \""
232 + pluginFile +
"\" doesn't exist.";
233 std::cout << errorMsg << std::endl;
237 std::cout <<
"######################### Load plugin file: " << pluginFile << std::endl;
239 if(Ogre::Root::getSingletonPtr() == NULL)
240 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
242 mRoot = Ogre::Root::getSingletonPtr();
257 std::string errorMsg =
"Error: the requested resource file \""
258 + resourceFile +
"\" doesn't exist.";
259 std::cout << errorMsg << std::endl;
263 std::cout <<
"######################### Load resource file: " << resourceFile << std::endl;
264 cf.load(resourceFile);
267 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
269 Ogre::String secName, typeName, archName;
270 while (seci.hasMoreElements())
272 secName = seci.peekNextKey();
273 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
274 Ogre::ConfigFile::SettingsMultiMap::iterator i;
275 for (i = settings->begin(); i != settings->end(); ++i)
278 archName = i->second;
279 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
280 archName, typeName, secName);
283 std::cout <<
"##################### add resources" << std::endl;
286 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
292 mRoot->restoreConfig();
293 if(!
mRoot->showConfigDialog())
297 if(!
mRoot->restoreConfig())
301 if(!
mRoot->isInitialised()){
303 const Ogre::RenderSystemList& lRenderSystemList =
mRoot->getAvailableRenderers();
304 if( lRenderSystemList.size() == 0 )
305 throw "ConfigDialog aborted";
307 Ogre::RenderSystem *lRenderSystem = lRenderSystemList.at(0);
308 std::cout <<
"Using " << lRenderSystem->getName() <<
" as renderer." << std::endl;
309 mRoot->setRenderSystem(lRenderSystem);
312 mRoot->initialise(
false);
315 bool fullscreen =
false;
316 Ogre::NameValuePairList misc;
317 Ogre::ConfigOptionMap config =
mRoot->getRenderSystem()->getConfigOptions();
318 Ogre::ConfigOptionMap::const_iterator it = config.begin();
320 while( it != config.end() ){
321 Ogre::String leftconf = (*it).first;
322 Ogre::String rightconf = (*it).second.currentValue;
324 if(leftconf ==
"Video Mode"){
328 std::cout <<
"Cannot read Ogre video mode" << std::endl;
337 else if( leftconf ==
"Full Screen" ){
339 if(rightconf ==
"Yes") fullscreen =
true;
343 misc[leftconf] = rightconf;
350 #if ( OGRE_VERSION >= (1 << 16 | 8 << 8 | 1) )
351 misc[
"hidden"] =
"true";
358 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
377 viewPort->setClearEveryFrame(
true);
385 mRoot->addFrameListener(
this);
388 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
392 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
395 size_t windowHnd = 0;
396 std::ostringstream windowHndStr;
398 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
399 windowHndStr << windowHnd;
400 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
402 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
403 pl.insert(std::make_pair(std::string(
"x11_keyboard_grab"), std::string(
"false")));
410 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
411 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
415 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
"rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
416 mWindow->getWidth(),
mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
422 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
424 RTarget->getViewport(0)->setClearEveryFrame(
true);
425 RTarget->getViewport(0)->setOverlaysEnabled(
false);
439 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
451 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
470 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
476 Ogre::WindowEventUtilities::messagePump();
480 if(result)
return stopTest(evt);
491 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
497 if(result)
return stopTest(evt);
512 if(
mWindow->isClosed())
return false;
562 return mRoot->renderOneFrame();
580 return mRoot->renderOneFrame();
640 Ogre::Entity *newEntity =
mSceneMgr->createEntity(name, model);
641 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
642 newNode->attachObject(newEntity);
655 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(name);
656 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
666 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(name)->getPosition();
667 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
678 mSceneMgr->getSceneNode(name)->resetOrientation();
680 Ogre::Matrix3 rotationOgre
681 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
682 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
683 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
684 Ogre::Quaternion q(rotationOgre);
685 mSceneMgr->getSceneNode(name)->rotate(q);
697 Ogre::Matrix3 rotationOgre
698 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
699 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
700 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
701 Ogre::Quaternion q(rotationOgre);
702 mSceneMgr->getSceneNode(name)->rotate(q);
735 mSceneMgr->getSceneNode(name)->setVisible(isVisible);
745 void vpAROgre::setScale(
const std::string &name,
const float factorx,
const float factory,
const float factorz)
748 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(name)->getScale());
750 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
771 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
774 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
775 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
779 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
780 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
781 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
787 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
790 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
791 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
801 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
810 Ogre::MaterialPtr Backgroundmaterial
811 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
812 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
818 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
819 Backgroundtechnique->createPass();
820 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
821 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
822 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
823 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
825 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
828 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
842 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
845 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
846 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
850 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
851 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
852 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
859 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
862 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
863 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
875 Ogre::TexturePtr dynTexPtr =
876 Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
887 Ogre::MaterialPtr Backgroundmaterial
888 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
889 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
895 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
896 Backgroundtechnique->createPass();
897 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
898 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
899 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
900 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
902 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
905 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
937 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
940 f_m_n = (Ogre::Real)(f-n);
941 f_p_n = (Ogre::Real)(f+n);
946 Ogre::Matrix4 Projection
949 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
951 mCamera->setCustomProjectionMatrix(
true, Projection);
964 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
966 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
983 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
985 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
987 #if 1 // if texture in BGRa format
1002 #else // if texture in RGBa format which is the format of the input image
1016 Ogre::Matrix4 ModelView
1018 = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
1019 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
1020 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
1021 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
1022 mCamera->setCustomViewMatrix(
true, ModelView);
1034 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
1040 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
1046 Ogre::HardwarePixelBufferSharedPtr
mPixelBuffer = dynTexPtr->getBuffer();
1047 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
1048 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
1049 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
1050 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
1051 #if 1 // if texture in BGRa format
1052 for(
unsigned int i=0; i<I.
getHeight(); i++){
1053 for(
unsigned int j=0; j<I.
getWidth(); j++){
1055 I[i][j].B = *pDest++;
1056 I[i][j].G = *pDest++;
1057 I[i][j].R = *pDest++;
1058 I[i][j].A = *pDest++;
1061 #else // if texture in RGBa format which is the format of the input image
1066 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.