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