ViSP
HelloWorldOgreAdvanced.cpp
1 /****************************************************************************
2  *
3  * $Id: HelloWorldOgreAdvanced.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 #if defined(VISP_HAVE_OGRE)
58 
59 #ifndef DOXYGEN_SHOULD_SKIP_THIS
60 
61 class vpAROgreAdvanced : public vpAROgre
62 {
63 private:
64  // Animation attribute
65  Ogre::AnimationState * mAnimationState;
66 
67 public:
68  vpAROgreAdvanced(const vpCameraParameters &cam = vpCameraParameters(),
69  unsigned int width = 640, unsigned int height = 480)
70  : vpAROgre(cam, width, height)
71  {
72  mAnimationState = NULL;
73  }
74 
75 protected:
76  void createScene()
77  {
78  // Create the Entity
79  Ogre::Entity* robot = mSceneMgr->createEntity("Robot", "robot.mesh");
80  // Attach robot to scene graph
81  Ogre::SceneNode* RobotNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("Robot");
82  RobotNode->setPosition((Ogre::Real)-0.3, (Ogre::Real)0.2, (Ogre::Real)0);
83  RobotNode->attachObject(robot);
84  RobotNode->scale((Ogre::Real)0.001,(Ogre::Real)0.001,(Ogre::Real)0.001);
85  RobotNode->pitch(Ogre::Degree(180));
86  RobotNode->yaw(Ogre::Degree(-90));
87 
88  // The animation
89  // Set the good animation
90  mAnimationState = robot->getAnimationState( "Idle" );
91  // Start over when finished
92  mAnimationState->setLoop( true );
93  // Animation enabled
94  mAnimationState->setEnabled( true );
95  }
96 
97  bool customframeEnded( const Ogre::FrameEvent& evt)
98  {
99  // Update animation
100  // To move, we add it the time since last frame
101  mAnimationState->addTime( evt.timeSinceLastFrame );
102  return true;
103  }
104 };// End of vpAROgreAdvanced class definition
105 #endif
106 
107 #endif
108 
109 int main()
110 {
111  try {
112 #if defined(VISP_HAVE_OGRE)
113 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394_2) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)
114 
115  // Image to store gathered data
116  // Here we acquire a grey level image. The consequence will be that
117  // the background texture used in Ogre renderer will be also in grey
118  // level.
120 
121  // Now we try to find an available framegrabber
122 #if defined(VISP_HAVE_V4L2)
123  // Video for linux 2 grabber
124  vpV4l2Grabber grabber;
125  // Open frame grabber
126  // Here we acquire an image from an available framegrabber to update
127  // the image size
128  grabber.open(I);
129  grabber.acquire(I);
130 #elif defined(VISP_HAVE_DC1394_2)
131  // libdc1394-2
132  vp1394TwoGrabber grabber;
133  // Open frame grabber
134  // Here we acquire an image from an available framegrabber to update
135  // the image size
136  grabber.open(I);
137  grabber.acquire(I);
138 #elif defined(VISP_HAVE_OPENCV)
139  // OpenCV to gather images
140  cv::VideoCapture grabber(0); // open the default camera
141  if(!grabber.isOpened()) { // check if we succeeded
142  std::cout << "Failed to open the camera" << std::endl;
143  return -1;
144  }
145  cv::Mat frame;
146  grabber >> frame; // get a new frame from camera
147  vpImageConvert::convert(frame, I);
148 #endif
149 
150  // Parameters of our camera
151  double px = 565;
152  double py = 565;
153  double u0 = I.getWidth() / 2;
154  double v0 = I.getHeight() / 2;
155  vpCameraParameters cam(px,py,u0,v0);
156  // The matrix with our pose
158  cMo[2][3] = 1.; // Z = 1 meter
159 
160  // Our object
161  vpAROgreAdvanced ogre(cam, I.getWidth(), I.getHeight());
162  // Initialisation
163  ogre.init(I);
164 
165  // Rendering loop
166  while(ogre.continueRendering()){
167  // Acquire a new image
168 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394_2)
169  grabber.acquire(I);
170 #elif defined(VISP_HAVE_OPENCV)
171  grabber >> frame;
172  vpImageConvert::convert(frame, I);
173 #endif
174  // Pose computation
175  // ...
176  // cMo updated
177  // Display with vpAROgre
178  ogre.display(I, cMo);
179  }
180 #else
181  std::cout << "You need an available framegrabber to run this example" << std::endl;
182 #endif
183 #else
184  std::cout << "You need Ogre3D to run this example" << std::endl;
185 #endif
186  return 0;
187  }
188  catch(vpException e) {
189  std::cout << "Catch an exception: " << e << std::endl;
190  return 1;
191  }
192  catch(...) {
193  std::cout << "Catch an exception " << std::endl;
194  return 1;
195  }
196 }
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...
virtual bool customframeEnded(const Ogre::FrameEvent &evt)
Definition: vpAROgre.cpp:559
void setPosition(const vpHomogeneousMatrix &cMw)
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)
void open(vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
Class for the Video4Linux2 video device.
virtual void createScene(void)
Definition: vpAROgre.h:301
void init()
Basic initialisation (identity).
unsigned int getHeight() const
Definition: vpImage.h:152
Class for firewire ieee1394 video devices using libdc1394-2.x api.