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