Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
HelloWorldOgreAdvanced.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
39
40#include <iostream>
41
42#include <visp3/core/vpConfig.h>
43
45// Comment / uncomment following lines to use the specific 3rd party compatible with your camera
46// #undef VISP_HAVE_V4L2
47// #undef VISP_HAVE_DC1394
48// #undef HAVE_OPENCV_HIGHGUI
49// #undef HAVE_OPENCV_VIDEOIO
51
52#include <visp3/ar/vpAROgre.h>
53#include <visp3/core/vpCameraParameters.h>
54#include <visp3/core/vpHomogeneousMatrix.h>
55#include <visp3/core/vpImage.h>
56#include <visp3/sensor/vp1394TwoGrabber.h>
57#include <visp3/sensor/vpV4l2Grabber.h>
58
59#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
60#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
61#elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
62#include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
63#endif
64
65#ifdef ENABLE_VISP_NAMESPACE
66using namespace VISP_NAMESPACE_NAME;
67#endif
68
69#if defined(VISP_HAVE_OGRE)
70
71#ifndef DOXYGEN_SHOULD_SKIP_THIS
72
73class vpAROgreAdvanced : public vpAROgre
74{
75private:
76 // Animation attribute
77 Ogre::AnimationState *mAnimationState;
78
79public:
80 vpAROgreAdvanced(const vpCameraParameters &cam = vpCameraParameters(), unsigned int width = 640,
81 unsigned int height = 480)
82 : vpAROgre(cam, width, height)
83 {
84 mAnimationState = nullptr;
85 }
86
87protected:
88 void createScene() VP_OVERRIDE
89 {
90 mSceneMgr->setAmbientLight(Ogre::ColourValue(static_cast<float>(0.6), static_cast<float>(0.6), static_cast<float>(0.6))); // Default value of lightning
91 Ogre::Light *light = mSceneMgr->createLight();
92 light->setDiffuseColour(1.0, 1.0, 1.0); // scaled RGB values
93 light->setSpecularColour(1.0, 1.0, 1.0); // scaled RGB values
94 // Lumiere ponctuelle
95#if (VISP_HAVE_OGRE_VERSION < (1 << 16 | 10 << 8 | 0))
96 light->setPosition(-5, -5, 10);
97#else
98 Ogre::SceneNode *spotLightNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
99 spotLightNode->attachObject(light);
100 spotLightNode->setPosition(Ogre::Vector3(-5, -5, 10));
101#endif
102 light->setType(Ogre::Light::LT_POINT);
103 light->setAttenuation((Ogre::Real)100, (Ogre::Real)1.0, (Ogre::Real)0.045, (Ogre::Real)0.0075);
104 // Ombres
105 light->setCastShadows(true);
106
107 // Create the Entity
108 Ogre::Entity *robot = mSceneMgr->createEntity("Robot", "robot.mesh");
109 // Attach robot to scene graph
110 Ogre::SceneNode *RobotNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("Robot");
111 // RobotNode->setPosition((Ogre::Real)-0.3, (Ogre::Real)0.2,
112 // (Ogre::Real)0);
113 RobotNode->attachObject(robot);
114 RobotNode->scale((Ogre::Real)0.001, (Ogre::Real)0.001, (Ogre::Real)0.001);
115 RobotNode->pitch(Ogre::Degree(180));
116 RobotNode->yaw(Ogre::Degree(-90));
117 robot->setCastShadows(true);
118 mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_MODULATIVE);
119
120 // The animation
121 // Set the good animation
122 mAnimationState = robot->getAnimationState("Idle");
123 // Start over when finished
124 mAnimationState->setLoop(true);
125 // Animation enabled
126 mAnimationState->setEnabled(true);
127 }
128
129 bool customframeEnded(const Ogre::FrameEvent &evt) VP_OVERRIDE
130 {
131 // Update animation
132 // To move, we add it the time since last frame
133 mAnimationState->addTime(evt.timeSinceLastFrame);
134 return true;
135 }
136}; // End of vpAROgreAdvanced class definition
137#endif
138
139#endif
140
141int main()
142{
143 try {
144#if defined(VISP_HAVE_OGRE)
145#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_OPENCV) && \
146 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
147 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
148
149 // Image to store gathered data
150 // Here we acquire a grey level image. The consequence will be that
151 // the background texture used in Ogre renderer will be also in grey
152 // level.
154
155 // Now we try to find an available framegrabber
156#if defined(VISP_HAVE_V4L2)
157 // Video for linux 2 grabber
158 vpV4l2Grabber grabber;
159 // Open frame grabber
160 // Here we acquire an image from an available framegrabber to update
161 // the image size
162 grabber.open(I);
163 grabber.acquire(I);
164#elif defined(VISP_HAVE_DC1394)
165 // libdc1394-2
166 vp1394TwoGrabber grabber;
167 // Open frame grabber
168 // Here we acquire an image from an available framegrabber to update
169 // the image size
170 grabber.open(I);
171 grabber.acquire(I);
172#elif defined(VISP_HAVE_OPENCV) && \
173 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
174 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
175 // OpenCV to gather images
176 cv::VideoCapture grabber(0); // open the default camera
177 if (!grabber.isOpened()) { // check if we succeeded
178 std::cout << "Failed to open the camera" << std::endl;
179 return EXIT_FAILURE;
180 }
181 cv::Mat frame;
182 grabber >> frame; // get a new frame from camera
183 vpImageConvert::convert(frame, I);
184#endif
185
186 // Parameters of our camera
187 double px = 565;
188 double py = 565;
189 double u0 = I.getWidth() / 2;
190 double v0 = I.getHeight() / 2;
191 vpCameraParameters cam(px, py, u0, v0);
192 // The matrix with our pose
194 cMo[2][3] = 0.5; // Z = 0.5 meter
195
196 // Our object
197 vpAROgreAdvanced ogre(cam, I.getWidth(), I.getHeight());
198 // Initialisation
199 ogre.init(I);
200
201 // Rendering loop
202 while (ogre.continueRendering()) {
203 // Acquire a new image
204#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394)
205 grabber.acquire(I);
206#elif defined(VISP_HAVE_OPENCV) && \
207 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
208 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
209 grabber >> frame;
210 vpImageConvert::convert(frame, I);
211#endif
212 // Pose computation
213 // ...
214 // cMo updated
215 // Display with vpAROgre
216 ogre.display(I, cMo);
217 }
218#else
219 std::cout << "You need an available framegrabber to run this example" << std::endl;
220#endif
221#else
222 std::cout << "You need Ogre3D to run this example" << std::endl;
223#endif
224 return EXIT_SUCCESS;
225 }
226 catch (const vpException &e) {
227 std::cout << "Catch an exception: " << e << std::endl;
228 return EXIT_FAILURE;
229 }
230 catch (...) {
231 std::cout << "Catch an exception " << std::endl;
232 return EXIT_FAILURE;
233 }
234}
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition vpAROgre.h:121
virtual bool customframeEnded(const Ogre::FrameEvent &evt)
Definition vpAROgre.cpp:741
virtual void createScene(void)
Definition vpAROgre.h:328
Generic class defining intrinsic camera parameters.
error that can be emitted by ViSP classes.
Definition vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
Definition vpImage.h:131
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void acquire(vpImage< unsigned char > &I)