Visual Servoing Platform  version 3.0.1
HelloWorldOgreAdvanced.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Ogre example.
32  *
33  * Authors:
34  * Bertrand Delabarre
35  *
36  *****************************************************************************/
44 #include <iostream>
45 
46 #include <visp3/sensor/vpOpenCVGrabber.h>
47 #include <visp3/sensor/vpV4l2Grabber.h>
48 #include <visp3/sensor/vp1394TwoGrabber.h>
49 #include <visp3/core/vpHomogeneousMatrix.h>
50 #include <visp3/core/vpImage.h>
51 #include <visp3/core/vpCameraParameters.h>
52 #include <visp3/ar/vpAROgre.h>
53 
54 #if defined(VISP_HAVE_OGRE)
55 
56 #ifndef DOXYGEN_SHOULD_SKIP_THIS
57 
58 class vpAROgreAdvanced : public vpAROgre
59 {
60 private:
61  // Animation attribute
62  Ogre::AnimationState * mAnimationState;
63 
64 public:
65  vpAROgreAdvanced(const vpCameraParameters &cam = vpCameraParameters(),
66  unsigned int width = 640, unsigned int height = 480)
67  : vpAROgre(cam, width, height)
68  {
69  mAnimationState = NULL;
70  }
71 
72 protected:
73  void createScene()
74  {
75  // Create the Entity
76  Ogre::Entity* robot = mSceneMgr->createEntity("Robot", "robot.mesh");
77  // Attach robot to scene graph
78  Ogre::SceneNode* RobotNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("Robot");
79  //RobotNode->setPosition((Ogre::Real)-0.3, (Ogre::Real)0.2, (Ogre::Real)0);
80  RobotNode->attachObject(robot);
81  RobotNode->scale((Ogre::Real)0.001,(Ogre::Real)0.001,(Ogre::Real)0.001);
82  RobotNode->pitch(Ogre::Degree(180));
83  RobotNode->yaw(Ogre::Degree(-90));
84 
85  // The animation
86  // Set the good animation
87  mAnimationState = robot->getAnimationState( "Idle" );
88  // Start over when finished
89  mAnimationState->setLoop( true );
90  // Animation enabled
91  mAnimationState->setEnabled( true );
92  }
93 
94  bool customframeEnded( const Ogre::FrameEvent& evt)
95  {
96  // Update animation
97  // To move, we add it the time since last frame
98  mAnimationState->addTime( evt.timeSinceLastFrame );
99  return true;
100  }
101 };// End of vpAROgreAdvanced class definition
102 #endif
103 
104 #endif
105 
106 int main()
107 {
108  try {
109 #if defined(VISP_HAVE_OGRE)
110 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)
111 
112  // Image to store gathered data
113  // Here we acquire a grey level image. The consequence will be that
114  // the background texture used in Ogre renderer will be also in grey
115  // level.
117 
118  // Now we try to find an available framegrabber
119 #if defined(VISP_HAVE_V4L2)
120  // Video for linux 2 grabber
121  vpV4l2Grabber grabber;
122  // Open frame grabber
123  // Here we acquire an image from an available framegrabber to update
124  // the image size
125  grabber.open(I);
126  grabber.acquire(I);
127 #elif defined(VISP_HAVE_DC1394)
128  // libdc1394-2
129  vp1394TwoGrabber grabber;
130  // Open frame grabber
131  // Here we acquire an image from an available framegrabber to update
132  // the image size
133  grabber.open(I);
134  grabber.acquire(I);
135 #elif defined(VISP_HAVE_OPENCV)
136  // OpenCV to gather images
137  cv::VideoCapture grabber(0); // open the default camera
138  if(!grabber.isOpened()) { // check if we succeeded
139  std::cout << "Failed to open the camera" << std::endl;
140  return -1;
141  }
142  cv::Mat frame;
143  grabber >> frame; // get a new frame from camera
144  vpImageConvert::convert(frame, I);
145 #endif
146 
147  // Parameters of our camera
148  double px = 565;
149  double py = 565;
150  double u0 = I.getWidth() / 2;
151  double v0 = I.getHeight() / 2;
152  vpCameraParameters cam(px,py,u0,v0);
153  // The matrix with our pose
155  cMo[2][3] = 0.5; // Z = 0.5 meter
156 
157  // Our object
158  vpAROgreAdvanced ogre(cam, I.getWidth(), I.getHeight());
159  // Initialisation
160  ogre.init(I);
161 
162  // Rendering loop
163  while(ogre.continueRendering()){
164  // Acquire a new image
165 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394)
166  grabber.acquire(I);
167 #elif defined(VISP_HAVE_OPENCV)
168  grabber >> frame;
169  vpImageConvert::convert(frame, I);
170 #endif
171  // Pose computation
172  // ...
173  // cMo updated
174  // Display with vpAROgre
175  ogre.display(I, cMo);
176  }
177 #else
178  std::cout << "You need an available framegrabber to run this example" << std::endl;
179 #endif
180 #else
181  std::cout << "You need Ogre3D to run this example" << std::endl;
182 #endif
183  return 0;
184  }
185  catch(vpException &e) {
186  std::cout << "Catch an exception: " << e << std::endl;
187  return 1;
188  }
189  catch(...) {
190  std::cout << "Catch an exception " << std::endl;
191  return 1;
192  }
193 }
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual bool customframeEnded(const Ogre::FrameEvent &evt)
Definition: vpAROgre.cpp:563
error that can be emited by ViSP classes.
Definition: vpException.h:73
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:89
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
vp_deprecated void init()
Generic class defining intrinsic camera parameters.
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:175
virtual void createScene(void)
Definition: vpAROgre.h:299
Class for firewire ieee1394 video devices using libdc1394-2.x api.
unsigned int getWidth() const
Definition: vpImage.h:226