Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
HelloWorldOgre.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Ogre example.
33  *
34  * Authors:
35  * Bertrand Delabarre
36  *
37  *****************************************************************************/
45 #include <iostream>
46 
47 #include <visp3/ar/vpAROgre.h>
48 #include <visp3/core/vpCameraParameters.h>
49 #include <visp3/core/vpHomogeneousMatrix.h>
50 #include <visp3/core/vpImage.h>
51 #include <visp3/sensor/vp1394TwoGrabber.h>
52 #include <visp3/sensor/vpOpenCVGrabber.h>
53 #include <visp3/sensor/vpV4l2Grabber.h>
54 
55 int main()
56 {
57  try {
58 #if defined(VISP_HAVE_OGRE)
59 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)
60 
61  // Image to stock gathered data
62  // Here we acquire a color image. The consequence will be that
63  // the background texture used in Ogre renderer will be also in color.
65 
66 // Now we try to find an available framegrabber
67 #if defined(VISP_HAVE_V4L2)
68  // Video for linux 2 grabber
69  vpV4l2Grabber grabber;
70  grabber.open(I);
71  grabber.acquire(I);
72 #elif defined(VISP_HAVE_DC1394)
73  // libdc1394-2
74  vp1394TwoGrabber grabber;
75  grabber.open(I);
76  grabber.acquire(I);
77 #elif defined(VISP_HAVE_OPENCV)
78  // OpenCV to gather images
79  cv::VideoCapture grabber(0); // open the default camera
80  if (!grabber.isOpened()) { // check if we succeeded
81  std::cout << "Failed to open the camera" << std::endl;
82  return -1;
83  }
84  cv::Mat frame;
85  grabber >> frame; // get a new frame from camera
86  vpImageConvert::convert(frame, I);
87 #endif
88 
89  // Parameters of our camera
90  double px = 565;
91  double py = 565;
92  double u0 = I.getWidth() / 2;
93  double v0 = I.getHeight() / 2;
94  vpCameraParameters cam(px, py, u0, v0);
95  // The matrix with our pose
96  // Defines the pose of the object in the camera frame
98 
99  // Our object
100  // A simulator with the camera parameters defined above,
101  // a grey level background image and of the good size
102  vpAROgre ogre(cam, I.getWidth(), I.getHeight());
103  // Initialisation
104  // Here we load the requested plugins specified in the "plugins.cfg" file
105  // and the resources specified in the "resources.cfg" file
106  // These two files can be found respectively in
107  // ViSP_HAVE_OGRE_PLUGINS_PATH and ViSP_HAVE_OGRE_RESOURCES_PATH folders
108  ogre.init(I);
109 
110  // Create a basic scene
111  // -----------------------------------
112  // Loading things
113  // -----------------------------------
114  // As you will see in section 5, our
115  // application knows locations where
116  // it can search for medias.
117  // Here we use a mesh included in
118  // the installation files : a robot.
119  // -----------------------------------
120  // Here we load the "robot.mesh" model that is found thanks to the
121  // resources locations specified in the "resources.cfg" file
122  ogre.load("Robot", "robot.mesh");
123  // Modify robot scale and orientation
124  // - downscale the size to have something completly visible in the image
125  // - rotation of 180 deg along robot x axis to have head over feet
126  // - rotation of -90 deg along y axis to have robot facing the camera
127  ogre.setScale("Robot", 0.001f, 0.001f, 0.001f);
128  ogre.setRotation("Robot", vpRotationMatrix(vpRxyzVector(M_PI, -M_PI / 2, 0)));
129 
130  // Update projection matrix
131  cMo[2][3] = 0.5; // Z = 0.5 meter
132 
133  std::cout << "cMo:\n" << cMo << std::endl;
134 
135  // Rendering loop, ended with on escape
136  while (ogre.continueRendering()) {
137 // Acquire a new image
138 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394)
139  grabber.acquire(I);
140 #elif defined(VISP_HAVE_OPENCV)
141  grabber >> frame;
142  vpImageConvert::convert(frame, I);
143 #endif
144  // Pose computation
145  // ...
146  // cMo updated
147  // Display the robot at the position specified by cMo with vpAROgre
148  ogre.display(I, cMo);
149  }
150 #else
151  std::cout << "You need an available framegrabber to run this example" << std::endl;
152 #endif
153 #else
154  std::cout << "You need Ogre3D to run this example" << std::endl;
155 #endif
156  return EXIT_SUCCESS;
157  } catch (const vpException &e) {
158  std::cout << "Catch an exception: " << e << std::endl;
159  return EXIT_FAILURE;
160  } catch (...) {
161  std::cout << "Catch an exception " << std::endl;
162  return EXIT_FAILURE;
163  }
164 }
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:239
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:71
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:90
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:178
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:156
Class for firewire ieee1394 video devices using libdc1394-2.x api.