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 : mRoot(0), mCamera(0), mSceneMgr(0), mWindow(0)
80 , mInputManager(0), mKeyboard(0)
88 std::cout <<
"mPluginsPath: " <<
mPluginsPath<< std::endl;
210 #if defined(NDEBUG) || !defined(WIN32)
216 std::string errorMsg =
"Error: the requested plugins file \""
217 + pluginFile +
"\" doesn't exist.";
218 std::cout << errorMsg << std::endl;
222 std::cout <<
"Load plugin file: " << pluginFile << std::endl;
223 mRoot =
new Ogre::Root(pluginFile,
"ogre.cfg",
"Ogre.log");
238 std::string errorMsg =
"Error: the requested resource file \""
239 + resourceFile +
"\" doesn't exist.";
240 std::cout << errorMsg << std::endl;
244 std::cout <<
"Load resource file: " << resourceFile << std::endl;
245 cf.load(resourceFile);
248 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
250 Ogre::String secName, typeName, archName;
251 while (seci.hasMoreElements())
253 secName = seci.peekNextKey();
254 Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
255 Ogre::ConfigFile::SettingsMultiMap::iterator i;
256 for (i = settings->begin(); i != settings->end(); ++i)
259 archName = i->second;
260 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
261 archName, typeName, secName);
267 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(*iter,
"FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
272 if(!
mRoot->showConfigDialog())
273 throw "ConfigDialog aborted";
275 mWindow =
mRoot->initialise(
true,
"Visp - Augmented Reality");
278 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
296 viewPort->setClearEveryFrame(
true);
305 mRoot->addFrameListener(
this);
308 Ogre::WindowEventUtilities::addWindowEventListener(
mWindow,
this);
312 Ogre::LogManager::getSingletonPtr()->logMessage(
"*** Initializing OIS ***");
315 size_t windowHnd = 0;
316 std::ostringstream windowHndStr;
319 mWindow->getCustomAttribute(
"WINDOW", &windowHnd);
320 windowHndStr << windowHnd;
322 pl.insert(std::make_pair(std::string(
"WINDOW"), windowHndStr.str()));
328 mKeyboard =
static_cast<OIS::Keyboard*
>(
mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
329 if ( !bufferedKeys )
mKeyboard->setEventCallback (
this);
333 Ogre::TexturePtr Texture = Ogre::TextureManager::getSingleton().createManual(
"rtf", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,Ogre::TEX_TYPE_2D,
334 mWindow->getWidth(),
mWindow->getHeight(), 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);
335 Ogre::RenderTexture* RTarget = Texture->getBuffer()->getRenderTarget();
337 RTarget->getViewport(0)->setClearEveryFrame(
true);
338 RTarget->getViewport(0)->setOverlaysEnabled(
false);
352 Ogre::WindowEventUtilities::removeWindowEventListener(
mWindow,
this);
364 bool vpAROgre::stopTest(
const Ogre::FrameEvent& evt)
383 bool vpAROgre::frameStarted(
const Ogre::FrameEvent& evt)
389 Ogre::WindowEventUtilities::messagePump();
393 if(result)
return stopTest(evt);
404 bool vpAROgre::frameEnded(
const Ogre::FrameEvent& evt)
410 if(result)
return stopTest(evt);
425 if(
mWindow->isClosed())
return false;
475 if(
mRoot->renderOneFrame()){
497 if(
mRoot->renderOneFrame()){
529 Ogre::Entity *newEntity =
mSceneMgr->createEntity(name, model);
530 Ogre::SceneNode *newNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(name);
531 newNode->attachObject(newEntity);
544 Ogre::SceneNode *node =
mSceneMgr->getSceneNode(name);
545 node->setPosition((Ogre::Real)wTo[0], (Ogre::Real)wTo[1], (Ogre::Real)wTo[2]);
555 Ogre::Vector3 translation =
mSceneMgr->getSceneNode(name)->getPosition();
556 return vpTranslationVector((Ogre::Real)translation[0], (Ogre::Real)translation[1], (Ogre::Real)translation[2]);
567 mSceneMgr->getSceneNode(name)->resetOrientation();
569 Ogre::Matrix3 rotationOgre
570 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
571 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
572 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
573 Ogre::Quaternion q(rotationOgre);
574 mSceneMgr->getSceneNode(name)->rotate(q);
586 Ogre::Matrix3 rotationOgre
587 = Ogre::Matrix3( (Ogre::Real)wRo[0][0], (Ogre::Real)wRo[0][1], (Ogre::Real)wRo[0][2],
588 (Ogre::Real)wRo[1][0], (Ogre::Real)wRo[1][1], (Ogre::Real)wRo[1][2],
589 (Ogre::Real)wRo[2][0], (Ogre::Real)wRo[2][1], (Ogre::Real)wRo[2][2]);
590 Ogre::Quaternion q(rotationOgre);
591 mSceneMgr->getSceneNode(name)->rotate(q);
624 mSceneMgr->getSceneNode(name)->setVisible(isVisible);
634 void vpAROgre::setScale(
const std::string &name,
const float factorx,
const float factory,
const float factorz)
637 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(1,1,1)/
mSceneMgr->getSceneNode(name)->getScale());
639 mSceneMgr->getSceneNode(name)->scale(Ogre::Vector3(factorx, factory, factorz));
660 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
663 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
664 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
668 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
669 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
670 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
676 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
679 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
680 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
690 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
696 Ogre::MaterialPtr Backgroundmaterial
697 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
698 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
699 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
700 Backgroundtechnique->createPass();
701 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
702 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
703 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
704 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
706 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
709 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
723 mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
726 Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_NONE);
727 Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(1);
731 if(
mRoot->getRenderSystem()->getName() ==
"OpenGL Rendering Subsystem") {
732 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
733 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
740 Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
743 Ogre::TextureManager::getSingleton().createManual(
"BackgroundTexture",
744 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
756 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"BackgroundTexture");
762 Ogre::MaterialPtr Backgroundmaterial
763 = Ogre::MaterialManager::getSingleton().create(
"BackgroundMaterial",
764 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
765 Ogre::Technique *Backgroundtechnique = Backgroundmaterial->createTechnique();
766 Backgroundtechnique->createPass();
767 Backgroundmaterial->getTechnique(0)->getPass(0)->setLightingEnabled(
false);
768 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(
false);
769 Backgroundmaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(
false);
770 Backgroundmaterial->getTechnique(0)->getPass(0)->createTextureUnitState(
"BackgroundTexture");
772 mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
775 Ogre::SceneNode *BackgroundNode =
mSceneMgr->getRootSceneNode()->createChildSceneNode(
"BackgoundNode");
804 Ogre::Real f,n,f_m_n,f_p_n,px,py,u0,v0;
805 f = (Ogre::Real)200.0;
806 n = (Ogre::Real)0.001;
807 f_m_n = (Ogre::Real)(f-n);
808 f_p_n = (Ogre::Real)(f+n);
813 Ogre::Matrix4 Projection
814 = Ogre::Matrix4( (Ogre::Real)(2.0*px/
mWidth), 0, (Ogre::Real)(2.0*(u0/
mWidth)-1.0), 0,
815 0, (Ogre::Real)(2.0*py/
mHeight), (Ogre::Real)(2.0*(v0/
mHeight)-1.0),0,
816 0, 0, (Ogre::Real)(-1.0*f_p_n/f_m_n), (Ogre::Real)(-2.0*f*n/f_m_n),
818 mCamera->setCustomProjectionMatrix(
true, Projection);
830 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
832 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
849 const Ogre::PixelBox& pixelBox =
mPixelBuffer->getCurrentLock();
851 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
853 #if 1 // if texture in BGRa format
854 for(
unsigned int i=0; i<
mHeight; i++){
855 for(
unsigned int j=0; j<
mWidth; j++){
868 #else // if texture in RGBa format which is the format of the input image
882 Ogre::Matrix4 ModelView
884 = Ogre::Matrix4( (Ogre::Real)cMw[0][0], (Ogre::Real)cMw[0][1], (Ogre::Real)cMw[0][2], (Ogre::Real)cMw[0][3],
885 (Ogre::Real)-cMw[1][0], (Ogre::Real)-cMw[1][1], (Ogre::Real)-cMw[1][2], (Ogre::Real)-cMw[1][3],
886 (Ogre::Real)-cMw[2][0], (Ogre::Real)-cMw[2][1], (Ogre::Real)-cMw[2][2], (Ogre::Real)-cMw[2][3],
887 (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)0, (Ogre::Real)1);
888 mCamera->setCustomViewMatrix(
true, ModelView);
901 Ogre::TexturePtr dynTexPtr = Ogre::TextureManager::getSingleton().getByName(
"rtf");
902 Ogre::RenderTexture* RTarget = dynTexPtr->getBuffer()->getRenderTarget();
905 Ogre::HardwarePixelBufferSharedPtr
mPixelBuffer = dynTexPtr->getBuffer();
906 mPixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
907 const Ogre::PixelBox& pixelBox = mPixelBuffer->getCurrentLock();
908 dynTexPtr->getBuffer()->blitToMemory(pixelBox);
909 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
916 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
Ogre::String mPluginsPath
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
void resize(const unsigned int height, const unsigned int width)
set the size of the image
virtual bool destroyScene(void)
Ogre::String mResourcePath
virtual void createCamera(void)
void addRotation(const std::string &name, const vpRotationMatrix &wRo)
OIS::Keyboard * mKeyboard
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 init(vpImage< unsigned char > &I, bool bufferedKeys=false)
virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo)
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)
void extract(vpRotationMatrix &R) const
virtual bool customframeStarted(const Ogre::FrameEvent &evt)
virtual void updateCameraProjection(void)
vpAROgre(const vpCameraParameters &cam=vpCameraParameters(), unsigned int width=640, unsigned int height=480, const char *resourcePath=VISP_HAVE_OGRE_RESOURCES_PATH, const char *pluginsPath=VISP_HAVE_OGRE_PLUGINS_PATH)
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 getRenderingOutput(vpImage< vpRGBa > &I, vpHomogeneousMatrix &cMo)
void load(const std::string &name, const std::string &model)
Class that consider the case of a translation vector.