-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
416 lines (338 loc) · 13.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <osgART/Foundation>
#include <osgART/VideoLayer>
#include <osgART/PluginManager>
#include <osgART/VideoGeode>
#include <osgART/Utils>
#include <osgART/GeometryUtils>
#include <osgART/MarkerCallback>
#include <osgART/TransformFilterCallback>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/FileUtils>
#include <osgDB/ReadFile>
#include <sys/stat.h> // Used to know if files exist or not
#include "CText.h"
#include "CPicture.h"
#include "HUD.h"
#include "MyMarkerVisibilityCallback.h"
#include "MarkerProximityUpdateCallback.h"
#include "XMLReader.h"
// Used to configure the geode mode of the video background
// You should ALWAYS use TEXTURE_RECTANGLE
enum VideoGeodeMode
{
TEXTURE_2D,
TEXTURE_RECTANGLE,
};
#define VIDEO_GEODE_MODE TEXTURE_RECTANGLE
int currentSlide = 0;
int totalSlides = 0;
int position = 0;
std::map<int, std::string> slides;
osg::ref_ptr<HUD> hud; // heads-up display for on screen debug data
bool FileExists(std::string strFilename) {
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0) {
blnReturn = true;
} else {
blnReturn = false;
}
return(blnReturn);
}
osg::ref_ptr<osg::Node> loadNode(std::string file) {
std::string extension = file.substr(file.length()-3, 3);
if (extension == "osg" || extension == "3ds" || extension == "obj" || extension == "dae" || extension == "fbx") {
osg::ref_ptr<osg::Node> n = osgDB::readNodeFile(file);
n->setName(extension);
return n;
} else if (extension == "png" || extension == "tga" || extension == "jpg" || extension == "jpeg" || extension == "gif") {
osg::ref_ptr<CPicture> p = new CPicture(file, 180);
p->setName(extension);
return p;
} else {
// It should never reach here, but if it does a simple cube is shown (this also prevents a warning)
return osgART::testCube();
}
}
void loadNextObject(osg::ref_ptr<osg::Switch> arSwitch) {
if (++currentSlide >= totalSlides)
currentSlide = 0;
if(slides[currentSlide] == "data/models/flores/flor2.osg") {
if (++currentSlide >= totalSlides)
currentSlide = 0;
}
arSwitch->setSingleChildOn(currentSlide);
}
void loadPreviousObject(osg::ref_ptr<osg::Switch> arSwitch) {
if (--currentSlide < 0)
currentSlide = totalSlides - 1;
if(slides[currentSlide] == "data/models/flores/flor2.osg") {
if (--currentSlide < 0)
currentSlide = totalSlides - 1;
}
arSwitch->setSingleChildOn(currentSlide);
}
class MyKeyboardEventHandler : public osgGA::GUIEventHandler {
private:
osg::ref_ptr<osg::MatrixTransform> arTransform;
osg::ref_ptr<osg::Switch> arSwitch;
osg::ref_ptr<osg::Group> object;
osg::ref_ptr<osg::Group> currentObject;
osg::ref_ptr<osg::Group> oldObject;
public:
MyKeyboardEventHandler() : osgGA::GUIEventHandler() { }
void setMatrixTransform(osg::ref_ptr<osg::MatrixTransform> mt) {
arTransform = mt;
}
void setSwitch(osg::ref_ptr<osg::Switch> sw) {
arSwitch = sw;
}
void setObject(osg::ref_ptr<osg::Group> g) {
object = g;
}
void setOldObject() {
currentObject = oldObject;
this->setCurrentObject();
}
void setNewObject(osg::ref_ptr<osg::Group> g) {
oldObject = currentObject;
currentObject = g;
this->setCurrentObject();
}
void setCurrentObject() {
}
/**
OVERRIDE THE HANDLE METHOD:
The handle() method should return true if the event has been dealt with
and we do not wish it to be handled by any other handler we may also have
defined. Whether you return true or false depends on the behaviour you
want - here we have no other handlers defined so return true.
**/
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa,
osg::Object* obj, osg::NodeVisitor* nv) {
switch (ea.getEventType()) {
/** KEY EVENTS:
Key events have an associated key and event names.
In this example, we are interested in keys up/down/right/left arrow
and space bar.
If we detect a press then we modify the transformation matrix
of the local transform node. **/
case osgGA::GUIEventAdapter::KEYDOWN: {
switch (ea.getKey()) {
case osgGA::GUIEventAdapter::KEY_Up:
std::cout << "+ UP key pressed" << std::endl;
return true;
case osgGA::GUIEventAdapter::KEY_Down:
std::cout << "+ DOWN key pressed" << std::endl;
return true;
case osgGA::GUIEventAdapter::KEY_Right:
std::cout << "+ RIGHT key pressed" << std::endl;
loadNextObject(arSwitch);
return true;
case osgGA::GUIEventAdapter::KEY_Left:
std::cout << "+ LEFT key pressed" << std::endl;
loadPreviousObject(arSwitch);
return true;
}
default: return false;
}
}
}
};
osg::Group* createImageBackground(osg::Image* video) {
osgART::VideoLayer* _layer = new osgART::VideoLayer();
_layer->setSize(*video);
osgART::VideoGeode* _geode;
if (VIDEO_GEODE_MODE == TEXTURE_RECTANGLE)
_geode = new osgART::VideoGeode(osgART::VideoGeode::USE_TEXTURE_RECTANGLE, video);
else if (VIDEO_GEODE_MODE == TEXTURE_2D)
_geode = new osgART::VideoGeode(osgART::VideoGeode::USE_TEXTURE_2D, video); // This can't be used to view video data on slides
else {
std::cout << "ERROR: Video Geode Mode not defined!!!" << std::endl;
exit (-1);
}
addTexturedQuad(*_geode,video->s(),video->t());
_layer->addChild(_geode);
return _layer;
}
int main(int argc, char* argv[]) {
// create a root node
osg::ref_ptr<osg::Group> root = new osg::Group;
osgViewer::Viewer viewer;
// attach root node to the viewer
viewer.setSceneData(root.get());
// add relevant handlers to the viewer
viewer.addEventHandler(new osgViewer::StatsHandler);
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
viewer.addEventHandler(new osgViewer::ThreadingHandler);
viewer.addEventHandler(new osgViewer::HelpHandler);
MyKeyboardEventHandler *myKeyboardEventHandler = new MyKeyboardEventHandler();
viewer.addEventHandler(myKeyboardEventHandler);
// preload the video and tracker
int _video_id = osgART::PluginManager::instance()->load("osgart_video_artoolkit2");
int _tracker_id = osgART::PluginManager::instance()->load("osgart_tracker_artoolkit2");
// Load a video plugin.
osg::ref_ptr<osgART::Video> video =
dynamic_cast<osgART::Video*>(osgART::PluginManager::instance()->get(_video_id));
// check if an instance of the video stream could be started
if (!video.valid())
{
// Without video an AR application can not work. Quit if none found.
osg::notify(osg::FATAL) << "Could not initialize video plugin!" << std::endl;
exit(-1);
}
// Read configuration from XML file
XMLReader* configXML = new XMLReader("data/config.xml");
// found video - configure now
osgART::VideoConfiguration* _config = video->getVideoConfiguration();
// Use any existing configuration.
if (_config)
{
if (argc > 1) {
_config->deviceconfig = argv[1];
} else {
if (std::string("").compare(configXML->getSource()) == 0) {
osg::notify(osg::FATAL) << "Video source not defined in configuration XML" << std::endl;
exit(-1);
}
if (std::string("USB").compare(configXML->getDevice()) == 0) {
// Height and Width are used only if both are defined
std::string w = configXML->hasWidth()?",width=" + configXML->getWidth():"";
std::string h = configXML->hasHeight()?",height=" + configXML->getHeight():"";
std::string wh = (std::string("").compare(w+h) != 0) ? "" : w+h;
_config->deviceconfig = "v4l2src device=" + configXML->getSource() + " use-fixed-fps=false ! ffmpegcolorspace ! capsfilter caps=video/x-raw-rgb,bpp=24" + wh + " ! identity name=artoolkit ! fakesink";
} else if (std::string("file").compare(configXML->getDevice()) == 0) {
_config->deviceconfig = "filesrc location=" + configXML->getSource() + " ! decodebin ! ffmpegcolorspace ! capsfilter caps=video/x-raw-rgb,bpp=24 ! identity name=artoolkit ! fakesink";
} else {
osg::notify(osg::FATAL) << "Incorrect video mode in XML file: " << configXML->getDevice() << std::endl;
exit(-1);
}
}
}
// Open the video. This will not yet start the video stream but will
// get information about the format of the video which is essential
// for the connected tracker
video->open();
osg::ref_ptr<osgART::Tracker> tracker =
dynamic_cast<osgART::Tracker*>(osgART::PluginManager::instance()->get(_tracker_id));
if (!tracker.valid())
{
// Without tracker an AR application can not work. Quit if none found.
osg::notify(osg::FATAL) << "Could not initialize tracker plugin!" << std::endl;
exit(-1);
}
// get the tracker calibration object
osg::ref_ptr<osgART::Calibration> calibration = tracker->getOrCreateCalibration();
// load a calibration file
if (!calibration->load(std::string("data/camera_para.dat")))
{
// the calibration file was non-existing or couldn't be loaded
osg::notify(osg::FATAL) << "Non existing or incompatible calibration file" << std::endl;
exit(-1);
}
// set the image source for the tracker
tracker->setImage(video.get());
osgART::TrackerCallback::addOrSet(root.get(), tracker.get());
// Tracker parameters are read and written via a field mechanism.
// Init access to a field within the tracker, in this case, the binarization threshhold.
osg::ref_ptr< osgART::TypedField<int> > _threshold =
reinterpret_cast< osgART::TypedField<int>* >(tracker->get("threshold"));
// Values are be accessed through a get()/set() mechanism on the field pointer.
if (_threshold.valid()) {
// Set the threshold, and read back.
_threshold->set(100);
osg::notify(osg::WARN) << "Field 'threshold' = " << _threshold->get() << std::endl;
} else {
osg::notify(osg::WARN) << "Field 'threshold' not supported for this tracker" << std::endl;
}
std::map<int, std::string> markers = configXML->getMarkers();
int numMarkers = markers.size();
if (numMarkers > 2)
{
osg::notify(osg::FATAL) << "You can only define 2 markers in XML configuration file" << std::endl;
exit(-1);
}
osg::ref_ptr<osgART::Marker> marker = tracker->addMarker(markers[0]);
osg::ref_ptr<osgART::Marker> marker2 = tracker->addMarker(markers[1]);
if (!marker.valid() || !marker2.valid())
{
// Without marker an AR application can not work. Quit if none found.
osg::notify(osg::FATAL) << "Could not add marker!" << std::endl;
exit(-1);
}
marker->setActive(true);
marker2->setActive(true);
osg::ref_ptr<osg::MatrixTransform> arTransform = new osg::MatrixTransform();
osg::ref_ptr<osg::MatrixTransform> arTransform2 = new osg::MatrixTransform();
osgART::attachDefaultEventCallbacks(arTransform.get(), marker.get());
osgART::attachDefaultEventCallbacks(arTransform2.get(), marker2.get());
osg::ref_ptr<MyMarkerVisibilityCallback> myMarkerVisibilityCallback = new MyMarkerVisibilityCallback(marker);
osgART::addEventCallback(arTransform.get(), myMarkerVisibilityCallback);
bool debugOnHUD = configXML->getDebug();
if (debugOnHUD) {
hud = new HUD(640, 480, 100);
hud->setText("");
hud->setHUDColor(1.0, 0.0, 0.0, 0.5);
myMarkerVisibilityCallback->setText(hud->getTextPointer());
}
currentSlide = 0;
slides = configXML->getSlides();
totalSlides = slides.size();
osg::ref_ptr<osg::Switch> sw = new osg::Switch();
for (int i = 0 ; i < totalSlides ; i++) {
if (!FileExists(slides[i])) { // If the file does not exist
osg::notify(osg::FATAL) << "The " << slides[i] << " file does not exist" << std::endl;
} else {
if (slides[i] == "data/models/flores/flor2.osg")
position = i;
if (i == 0) { // Only the first slide is visible at first
sw->addChild(loadNode(slides[i]), true);
} else {
sw->addChild(loadNode(slides[i]), false);
}
}
}
osg::ref_ptr<osg::MatrixTransform> mTransform = new osg::MatrixTransform();
/*
osg::ref_ptr<osg::Matrix> mRotationMatrix; // coefficient of matrix multiplication
// Make a rotation matrix:
mRotationMatrix->makeRotate( 5.0, osg::Vec3f( 1.0f, 0.0f, 0.0f ) ); // X axis
// Rotate matrix every frame (matrix of OSG::MatrixTransform):
osg::Matrix m = mTransform->getMatrix();
m = mRotationMatrix->identity() * m;
mTransform->setMatrix( m );
*/
mTransform->addChild(sw.get());
arTransform->addChild(sw.get());
arTransform2->addChild(osgDB::readNodeFile("data/models/flores/flores.osg"));
myKeyboardEventHandler->setSwitch(sw);
arTransform->getOrCreateStateSet()->setRenderBinDetails(100, "RenderBin");
arTransform2->getOrCreateStateSet()->setRenderBinDetails(100, "RenderBin");
osg::ref_ptr<osg::Group> videoBackground = createImageBackground(video.get());
videoBackground->getOrCreateStateSet()->setRenderBinDetails(0, "RenderBin");
osg::ref_ptr<osg::Camera> cam = calibration->createCamera();
cam->addChild(arTransform.get());
cam->addChild(arTransform2.get());
cam->addChild(videoBackground.get());
root->addChild(cam.get());
if (debugOnHUD)
root->addChild(hud);
// Used to know the distance between the 2 markers
root->setUpdateCallback(new MarkerProximityUpdateCallback(marker, marker2, arTransform, arTransform2, sw.get(), 300, ¤tSlide, position));
video->start();
if (!configXML->getFullscreen() && configXML->hasWidth() && configXML->hasHeight()) {
viewer.setUpViewInWindow( 0, 0, atoi(configXML->getWidth().c_str()), atoi(configXML->getHeight().c_str()) ); // Hace que se ejecute en una ventana en vez de a pantalla completa (parametros: posición y tamaño de la ventana)
}
if (std::string("all").compare(configXML->getMonitor()) != 0) {
int monitor = atoi(configXML->getMonitor().c_str()); // A non number string will generate a 0
viewer.setUpViewOnSingleScreen(monitor); // Output to monitor number defined in XML configuration file
}
viewer.run();
video->stop();
video->close();
return 0;
}