ViSP  3.0.0
HelloWorldOgre.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  ogre.setPosition("Robot", vpTranslationVector(-0.3, 0.2, 0));
123  ogre.setScale("Robot", 0.001f,0.001f,0.001f);
124  ogre.setRotation("Robot", vpRotationMatrix(vpRxyzVector(M_PI, 0, 0)));
125 
126  cMo[2][3] = 1.; // Z = 1 meter
127 
128  std::cout << "cMo:\n" << cMo << std::endl;
129 
130  // Rendering loop, ended with on escape
131  while(ogre.continueRendering()){
132  // Acquire a new image
133 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394)
134  grabber.acquire(I);
135 #elif defined(VISP_HAVE_OPENCV)
136  grabber >> frame;
137  vpImageConvert::convert(frame, I);
138 #endif
139  //Pose computation
140  // ...
141  // cMo updated
142  // Display the robot at the position specified by cMo with vpAROgre
143  ogre.display(I,cMo);
144  }
145 #else
146  std::cout << "You need an available framegrabber to run this example" << std::endl;
147 #endif
148 #else
149  std::cout << "You need Ogre3D to run this example" << std::endl;
150 #endif
151  return 0;
152  }
153  catch(vpException e) {
154  std::cout << "Catch an exception: " << e << std::endl;
155  return 1;
156  }
157  catch(...) {
158  std::cout << "Catch an exception " << std::endl;
159  return 1;
160  }
161 }
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:161
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.
Definition: vpAROgre.h:86
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 for the Video4Linux2 video device.
unsigned int getHeight() const
Definition: vpImage.h:152
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.
Class that consider the case of a translation vector.