Visual Servoing Platform  version 3.0.1
HelloWorldOgre.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 int main()
55 {
56  try {
57 #if defined(VISP_HAVE_OGRE)
58 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)
59 
60  // Image to stock gathered data
61  // Here we acquire a color image. The consequence will be that
62  // the background texture used in Ogre renderer will be also in color.
64 
65  // Now we try to find an available framegrabber
66 #if defined(VISP_HAVE_V4L2)
67  // Video for linux 2 grabber
68  vpV4l2Grabber grabber;
69  grabber.open(I);
70  grabber.acquire(I);
71 #elif defined(VISP_HAVE_DC1394)
72  // libdc1394-2
73  vp1394TwoGrabber grabber;
74  grabber.open(I);
75  grabber.acquire(I);
76 #elif defined(VISP_HAVE_OPENCV)
77  // OpenCV to gather images
78  cv::VideoCapture grabber(0); // open the default camera
79  if(!grabber.isOpened()) { // check if we succeeded
80  std::cout << "Failed to open the camera" << std::endl;
81  return -1;
82  }
83  cv::Mat frame;
84  grabber >> frame; // get a new frame from camera
85  vpImageConvert::convert(frame, I);
86 #endif
87 
88  // Parameters of our camera
89  double px = 565;
90  double py = 565;
91  double u0 = I.getWidth() / 2;
92  double v0 = I.getHeight() / 2;
93  vpCameraParameters cam(px,py,u0,v0);
94  // The matrix with our pose
95  // Defines the pose of the object in the camera frame
97 
98  // Our object
99  // A simulator with the camera parameters defined above,
100  // a grey level background image and of the good size
101  vpAROgre ogre(cam, I.getWidth(), I.getHeight());
102  // Initialisation
103  // Here we load the requested plugins specified in the "plugins.cfg" file
104  // and the resources specified in the "resources.cfg" file
105  // These two files can be found respectively in ViSP_HAVE_OGRE_PLUGINS_PATH
106  // and ViSP_HAVE_OGRE_RESOURCES_PATH folders
107  ogre.init(I);
108 
109  // Create a basic scene
110  // -----------------------------------
111  // Loading things
112  // -----------------------------------
113  // As you will see in section 5, our
114  // application knows locations where
115  // it can search for medias.
116  // Here we use a mesh included in
117  // the installation files : a robot.
118  // -----------------------------------
119  // Here we load the "robot.mesh" model that is found thanks to the resources locations
120  // specified in the "resources.cfg" file
121  ogre.load("Robot", "robot.mesh");
122  // Modify robot scale and orientation
123  // - downscale the size to have something completly visible in the image
124  // - rotation of 180 deg along robot x axis to have head over feet
125  // - rotation of -90 deg along y axis to have robot facing the camera
126  ogre.setScale("Robot", 0.001f, 0.001f, 0.001f);
127  ogre.setRotation("Robot", vpRotationMatrix(vpRxyzVector(M_PI, -M_PI/2, 0)));
128 
129  // Update projection matrix
130  cMo[2][3] = 0.5; // Z = 0.5 meter
131 
132  std::cout << "cMo:\n" << cMo << std::endl;
133 
134  // Rendering loop, ended with on escape
135  while(ogre.continueRendering()){
136  // Acquire a new image
137 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394)
138  grabber.acquire(I);
139 #elif defined(VISP_HAVE_OPENCV)
140  grabber >> frame;
141  vpImageConvert::convert(frame, I);
142 #endif
143  // Pose computation
144  // ...
145  // cMo updated
146  // Display the robot at the position specified by cMo with vpAROgre
147  ogre.display(I,cMo);
148  }
149 #else
150  std::cout << "You need an available framegrabber to run this example" << std::endl;
151 #endif
152 #else
153  std::cout << "You need Ogre3D to run this example" << std::endl;
154 #endif
155  return 0;
156  }
157  catch(vpException &e) {
158  std::cout << "Catch an exception: " << e << std::endl;
159  return 1;
160  }
161  catch(...) {
162  std::cout << "Catch an exception " << std::endl;
163  return 1;
164  }
165 }
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.
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)
Implementation of a rotation matrix and operations on such kind of matrices.
void open(vpImage< unsigned char > &I)
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:115
Generic class defining intrinsic camera parameters.
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:175
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:154
Class for firewire ieee1394 video devices using libdc1394-2.x api.
unsigned int getWidth() const
Definition: vpImage.h:226