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