openscenegraph
SlideShowConstructor
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2018 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef SLIDESHOWCONSTRUCTOR
15#define SLIDESHOWCONSTRUCTOR
16
17#include <osg/Vec3>
18#include <osg/Vec4>
19#include <osg/ImageUtils>
20#include <osg/Group>
21#include <osg/ClearNode>
22#include <osg/Switch>
23#include <osg/AnimationPath>
24#include <osg/TransferFunction>
25#include <osg/ImageStream>
26#include <osg/ImageSequence>
27#include <osg/ScriptEngine>
28#include <osgText/Text>
29#include <osgGA/GUIEventAdapter>
30
31#include <osgDB/FileUtils>
32
33#include <osgVolume/VolumeTile>
34#include <osgVolume/VolumeSettings>
35
36#include <osgPresentation/AnimationMaterial>
37#include <osgPresentation/SlideEventHandler>
38#include <osgPresentation/PropertyManager>
39#include <osgPresentation/Timeout>
40
41namespace osgPresentation
42{
43
44
45
46class OSGPRESENTATION_EXPORT HUDTransform : public osg::Transform
47{
48 public:
49
50 HUDTransform(HUDSettings* hudSettings);
51
52 virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const;
53
54 virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor*) const;
55
56protected:
57
58 virtual ~HUDTransform();
59
60 osg::ref_ptr<HUDSettings> _hudSettings;
61
62};
63
64class OSGPRESENTATION_EXPORT SlideShowConstructor
65{
66public:
67
68
69 enum CoordinateFrame { SLIDE, MODEL };
70
71
72
73 LayerAttributes* getOrCreateLayerAttributes(osg::Node* node);
74
75 void setDuration(osg::Node* node,double duration)
76 {
77 getOrCreateLayerAttributes(node)->setDuration(duration);
78 }
79
80 void addKey(osg::Node* node,const KeyPosition& kp)
81 {
82 getOrCreateLayerAttributes(node)->addKey(kp);
83 }
84
85 void addRunString(osg::Node* node, const std::string& runString)
86 {
87 getOrCreateLayerAttributes(node)->addRunString(runString);
88 }
89
90 void setJump(osg::Node* node, const JumpData& jumpData)
91 {
92 getOrCreateLayerAttributes(node)->setJump(jumpData);
93 }
94
95 void addPresentationKey(const KeyPosition& kp)
96 {
97 if (!_presentationSwitch) createPresentation();
98 if (_presentationSwitch.valid()) addKey( _presentationSwitch.get(), kp);
99 }
100
101 void addPresentationRunString(const std::string& runString)
102 {
103 if (!_presentationSwitch) createPresentation();
104 if (_presentationSwitch.valid()) addRunString( _presentationSwitch.get(),runString);
105 }
106
107 void addSlideKey(const KeyPosition& kp)
108 {
109 if (!_slide) addSlide();
110 if (_slide.valid()) addKey(_slide.get(),kp);
111 }
112
113 void addSlideRunString(const std::string& runString)
114 {
115 if (!_slide) addSlide();
116 if (_slide.valid()) addRunString(_slide.get(),runString);
117 }
118
119 void setSlideJump(const JumpData& jumpData)
120 {
121 if (!_slide) addSlide();
122 if (_slide.valid()) setJump(_slide.get(),jumpData);
123 }
124
125 void addLayerKey(const KeyPosition& kp)
126 {
127 if (!_currentLayer) addLayer();
128 if (_currentLayer.valid()) addKey(_currentLayer.get(),kp);
129 }
130
131 void addLayerRunString(const std::string& runString)
132 {
133 if (!_currentLayer) addLayer();
134 if (_currentLayer.valid()) addRunString(_currentLayer.get(),runString);
135 }
136
137
138 void setLayerJump(const JumpData& jumpData)
139 {
140 if (!_currentLayer) addLayer();
141 if (_currentLayer.valid()) setJump(_currentLayer.get(),jumpData);
142 }
143
144
145
146 struct PositionData
147 {
148 PositionData():
149 frame(SlideShowConstructor::SLIDE),
150 position(0.0f,1.0f,0.0f),
151 //position(0.5f,0.5f,0.0f),
152 scale(1.0f,1.0f,1.0f),
153 rotate(0.0f,0.0f,0.0f,1.0f),
154 rotation(0.0f,0.0f,1.0f,0.0f),
155 absolute_path(false),
156 inverse_path(false),
157 path_time_offset(0.0),
158 path_time_multiplier(1.0f),
159 path_loop_mode(osg::AnimationPath::NO_LOOPING),
160 animation_material_time_offset(0.0),
161 animation_material_time_multiplier(1.0),
162 animation_material_loop_mode(AnimationMaterial::NO_LOOPING),
163 autoRotate(false),
164 autoScale(false),
165 hud(false) {}
166
167
168 bool requiresPosition() const
169 {
170 return (position[0]!=0.0f || position[1]!=1.0f || position[2]!=1.0f);
171 }
172
173 bool requiresScale() const
174 {
175 return (scale[0]!=1.0f || scale[1]!=1.0f || scale[2]!=1.0f);
176 }
177
178 bool requiresRotate() const
179 {
180 return rotate[0]!=0.0f;
181 }
182
183 bool requiresAnimation() const
184 {
185 return (rotation[0]!=0.0f || !path.empty());
186 }
187
188 bool requiresMaterialAnimation() const
189 {
190 return !animation_material_filename.empty() || !fade.empty();
191 }
192
193 CoordinateFrame frame;
194 osg::Vec3 position;
195 osg::Vec3 scale;
196 osg::Vec4 rotate;
197 osg::Vec4 rotation;
198 std::string animation_name;
199 bool absolute_path;
200 bool inverse_path;
201 double path_time_offset;
202 double path_time_multiplier;
203 osg::AnimationPath::LoopMode path_loop_mode;
204 std::string path;
205 double animation_material_time_offset;
206 double animation_material_time_multiplier;
207 AnimationMaterial::LoopMode animation_material_loop_mode;
208 std::string animation_material_filename;
209 std::string fade;
210 bool autoRotate;
211 bool autoScale;
212 bool hud;
213 };
214
215 struct ModelData
216 {
217 ModelData() {}
218
219 std::string region;
220 std::string effect;
221 std::string options;
222 };
223
224 struct ImageData
225 {
226 ImageData():
227 width(1.0f),
228 height(1.0f),
229 region(0.0f,0.0f,1.0f,1.0f),
230 region_in_pixel_coords(false),
231 texcoord_rotate(0.0f),
232 loopingMode(osg::ImageStream::NO_LOOPING),
233 page(-1),
234 backgroundColor(1.0f,1.0f,1.0f,1.0f),
235 fps(30.0),
236 duration(-1.0),
237 imageSequence(false),
238 imageSequencePagingMode(osg::ImageSequence::PAGE_AND_DISCARD_USED_IMAGES),
239 imageSequenceInteractionMode(PLAY_AUTOMATICALLY_LIKE_MOVIE),
240 blendingHint(USE_IMAGE_ALPHA),
241 delayTime(0.0),
242 startTime(0.0),
243 stopTime(-1.0),
244 volume("")
245 {}
246
247 std::string options;
248 float width;
249 float height;
250 osg::Vec4 region;
251 bool region_in_pixel_coords;
252 float texcoord_rotate;
253 osg::ImageStream::LoopingMode loopingMode;
254 int page;
255 osg::Vec4 backgroundColor;
256 double fps;
257 double duration;
258
259 bool imageSequence;
260 osg::ImageSequence::Mode imageSequencePagingMode;
261
262 enum ImageSequenceInteractionMode
263 {
264 PLAY_AUTOMATICALLY_LIKE_MOVIE,
265 USE_MOUSE_X_POSITION,
266 USE_MOUSE_Y_POSITION
267 };
268 ImageSequenceInteractionMode imageSequenceInteractionMode;
269
270 enum BlendingHint
271 {
272 USE_IMAGE_ALPHA,
273 OFF,
274 ON
275 };
276
277 BlendingHint blendingHint;
278
279 double delayTime;
280 double startTime;
281 double stopTime;
282 std::string volume;
283
284 };
285
286 struct VolumeData
287 {
288 typedef osgVolume::VolumeSettings::ShadingModel ShadingModel;
289 typedef osgVolume::VolumeSettings::Technique Technique;
290
291 VolumeData():
292 shadingModel(osgVolume::VolumeSettings::Standard),
293 useTabbedDragger(false),
294 useTrackballDragger(false),
295 region_in_pixel_coords(false),
296 alphaValue(),
297 cutoffValue(),
298 exteriorTransparencyFactorValue(),
299 sampleDensityValue(),
300 sampleRatioValue(),
301 colorSpaceOperation(osg::NO_COLOR_SPACE_OPERATION),
302 colorModulate(1.0f,1.0f,1.0f,1.0f),
303 technique(osgVolume::VolumeSettings::RayTraced)
304 {
305 hullPositionData.position = osg::Vec3(0.0,0.0,0.0);
306 hullPositionData.frame = osgPresentation::SlideShowConstructor::MODEL;
307 }
308
309 osg::ref_ptr<osgVolume::VolumeSettings> volumeSettings;
310
311 std::string options;
312 ShadingModel shadingModel;
313 osg::ref_ptr<osg::TransferFunction1D> transferFunction;
314 bool useTabbedDragger;
315 bool useTrackballDragger;
316 std::string region;
317 bool region_in_pixel_coords;
318 std::string alphaValue;
319 std::string cutoffValue;
320 std::string exteriorTransparencyFactorValue;
321 std::string sampleDensityValue;
322 std::string sampleDensityWhenMovingValue;
323
324 std::string sampleRatioValue;
325 std::string sampleRatioWhenMovingValue;
326
327 osg::ColorSpaceOperation colorSpaceOperation;
328 osg::Vec4 colorModulate;
329 Technique technique;
330 std::string hull;
331 PositionData hullPositionData;
332 };
333
334
335 struct FontData
336 {
337 FontData():
338 font("fonts/arial.ttf"),
339 layout(osgText::Text::LEFT_TO_RIGHT),
340 alignment(osgText::Text::LEFT_BASE_LINE),
341 axisAlignment(osgText::Text::XZ_PLANE),
342 characterSizeMode(osgText::Text::OBJECT_COORDS),
343 characterSize(0.04f),
344 maximumHeight(1.0f),
345 maximumWidth(1.0f),
346 color(1.0f,1.0f,1.0f,1.0f) {}
347
348 std::string font;
349 osgText::Text::Layout layout;
350 osgText::Text::AlignmentType alignment;
351 osgText::Text::AxisAlignment axisAlignment;
352 osgText::Text::CharacterSizeMode characterSizeMode;
353 float characterSize;
354 float maximumHeight;
355 float maximumWidth;
356 osg::Vec4 color;
357 };
358
359 enum ScriptCallbackType
360 {
361 UPDATE_SCRIPT,
362 EVENT_SCRIPT
363 };
364
365 typedef std::pair<ScriptCallbackType, std::string> ScriptPair;
366
367 struct ScriptData
368 {
369 ScriptData() {}
370
371 typedef std::vector<ScriptPair> Scripts;
372 Scripts scripts;
373
374 bool hasScripts() const { return !scripts.empty(); }
375 };
376
377 SlideShowConstructor(osgDB::Options* options);
378
379 void createPresentation();
380
381 void setBackgroundColor(const osg::Vec4& color, bool updateClearNode);
382 const osg::Vec4& getBackgroundColor() const { return _backgroundColor; }
383
384 void setTextColor(const osg::Vec4& color);
385 const osg::Vec4& getTextColor() const { return _textFontDataDefault.color; }
386
387 void setPresentationName(const std::string& name);
388
389 void setPresentationAspectRatio(float aspectRatio);
390
391 void setPresentationAspectRatio(const std::string& str);
392
393 void setPresentationDuration(double duration);
394
395 void addScriptEngine(const std::string& scriptEngineName);
396
397 void addScriptFile(const std::string& name, const std::string& filename);
398
399 void addScript(const std::string& name, const std::string& language, const std::string& script);
400
401 void addSlide();
402
403 void selectSlide(int slideNum);
404
405
406 void setSlideTitle(const std::string& name, PositionData& positionData, FontData& fontData)
407 {
408 _titlePositionData = positionData;
409 _titleFontData = fontData;
410 _slideTitle = name;
411 }
412
413 void setSlideBackgrondHUD(bool hud) { _slideBackgroundAsHUD = hud; }
414 void setSlideBackground(const std::string& name) { _slideBackgroundImageFileName = name; }
415
416 void setSlideDuration(double duration);
417
418 Timeout* addTimeout();
419
420 void addLayer(bool inheritPreviousLayers=true, bool defineAsBaseLayer=false);
421
422 void selectLayer(int layerNum);
423
424 void setLayerDuration(double duration);
425
426
427 // title settings
428 FontData& getTitleFontData() { return _titleFontData; }
429 FontData& getTitleFontDataDefault() { return _titleFontDataDefault; }
430
431 PositionData& getTitlePositionData() { return _titlePositionData; }
432 PositionData& getTitlePositionDataDefault() { return _titlePositionDataDefault; }
433
434 // text settings
435 FontData& getTextFontData() { return _textFontData; }
436 FontData& getTextFontDataDefault() { return _textFontDataDefault; }
437
438 PositionData& getTextPositionData() { return _textPositionData; }
439 PositionData& getTextPositionDataDefault() { return _textPositionDataDefault; }
440
441 void translateTextCursor(const osg::Vec3& delta) { _textPositionData.position += delta; }
442
443 // image settings
444 PositionData& getImagePositionData() { return _imagePositionData; }
445 PositionData& getImagePositionDataDefault() { return _imagePositionDataDefault; }
446
447 // model settings
448 PositionData& getModelPositionData() { return _modelPositionData; }
449 PositionData& getModelPositionDataDefault() { return _modelPositionDataDefault; }
450
451
452 enum PresentationContext {
453 CURRENT_PRESENTATION,
454 CURRENT_SLIDE,
455 CURRENT_LAYER
456 };
457
458 void addEventHandler(PresentationContext presentationContext, osg::ref_ptr<osgGA::GUIEventHandler> handler);
459
460 void keyToDoOperation(PresentationContext presentationContext, int key, Operation operation, const JumpData& jumpData=JumpData());
461 void keyToDoOperation(PresentationContext presentationContext, int key, const std::string& command, Operation operation, const JumpData& jumpData=JumpData());
462 void keyEventOperation(PresentationContext presentationContext, int key, const KeyPosition& keyPos, const JumpData& jumpData=JumpData());
463
464 void layerClickToDoOperation(Operation operation, const JumpData& jumpData=JumpData());
465 void layerClickToDoOperation(const std::string& command, Operation operation, const JumpData& jumpData=JumpData());
466 void layerClickEventOperation(const KeyPosition& keyPos, const JumpData& jumpData=JumpData());
467
468 void addPropertyAnimation(PresentationContext presentationContext, PropertyAnimation* propertyAnimation);
469
470 void addScriptCallback(PresentationContext presentationContext, ScriptCallbackType scriptCallbackType, const std::string& functionName);
471
472 void addScriptToNode(ScriptCallbackType scriptCallbackType, const std::string& name, osg::Node* node);
473
474 void addScriptsToNode(const ScriptData& scriptData, osg::Node* node);
475
476 void addToCurrentLayer(osg::Node* subgraph);
477
478 void addBullet(const std::string& bullet, PositionData& positionData, FontData& fontData, const ScriptData& scriptData);
479
480 void addParagraph(const std::string& paragraph, PositionData& positionData, FontData& fontData, const ScriptData& scriptData);
481
482 osg::ref_ptr<osg::Image> readImage(const std::string& filename, const ImageData& imageData);
483
484 void addImage(const std::string& filename,const PositionData& positionData, const ImageData& imageData, const ScriptData& scriptData);
485
486 void addStereoImagePair(const std::string& filenameLeft, const ImageData& imageDataLeft, const std::string& filenameRight,const ImageData& imageDataRight, const PositionData& positionData, const ScriptData& scriptData);
487
488 void addGraph(const std::string& filename,const PositionData& positionData, const ImageData& imageData, const ScriptData& scriptData);
489 void addVNC(const std::string& filename,const PositionData& positionData, const ImageData& imageData, const std::string& password, const ScriptData& scriptData);
490 void addBrowser(const std::string& filename,const PositionData& positionData, const ImageData& imageData, const ScriptData& scriptData);
491 void addPDF(const std::string& filename,const PositionData& positionData, const ImageData& imageData, const ScriptData& scriptData);
492 osg::ref_ptr<osg::Image> addInteractiveImage(const std::string& filename,const PositionData& positionData, const ImageData& imageData, const ScriptData& scriptData);
493
494 void addModel(osg::Node* subgraph, const PositionData& positionData, const ModelData& modelData, const ScriptData& scriptData);
495
496 void addModel(const std::string& filename, const PositionData& positionData, const ModelData& modelData, const ScriptData& scriptData);
497
498 void setUpVolumeScalarProperty(osgVolume::VolumeTile* tile, osgVolume::ScalarProperty* property, const std::string& source);
499
500 void addVolume(const std::string& filename, const PositionData& positionData, const VolumeData& volumeData, const ScriptData& scriptData);
501
502 osg::Group* takePresentation() { return _root.release(); }
503
504 osg::Group* getPresentation() { return _root.get(); }
505
506 osg::Switch* getPresentationSwitch() { return _presentationSwitch.get(); }
507
508 osg::Switch* getCurrentSlide() { return _slide.get(); }
509
510 void pushCurrentLayer(osg::Group* newLayerGroup);
511 void popCurrentLayer();
512
513 osg::Group* getCurrentLayer() { return _currentLayer.get(); }
514
515 void setLoopPresentation(bool loop) { _loopPresentation = loop; }
516 bool getLoopPresentation() const { return _loopPresentation; }
517
518 void setAutoSteppingActive(bool flag = true) { _autoSteppingActive = flag; }
519 bool getAutoSteppingActive() const { return _autoSteppingActive; }
520
521 void setHUDSettings(HUDSettings* hudSettings) { _hudSettings = hudSettings; }
522 HUDSettings* getHUDSettings() { return _hudSettings.get(); }
523 const HUDSettings* getHUDSettings() const { return _hudSettings.get(); }
524
525 osg::ScriptEngine* getOrCreateScriptEngine(const std::string& language);
526
527protected:
528
529 void findImageStreamsAndAddCallbacks(osg::Node* node);
530
531 osg::Geometry* createTexturedQuadGeometry(const osg::Vec3& pos, const osg::Vec4& rotation, float width,float height, osg::Image* image, bool& usedTextureRectangle);
532 void setUpMovieVolume(osg::Node* subgraph, osg::ImageStream* imageStream, const ImageData& imageData);
533
534 osg::Vec3 computePositionInModelCoords(const PositionData& positionData) const;
535 void updatePositionFromInModelCoords(const osg::Vec3& vertex, PositionData& positionData) const;
536
537 osg::Vec3 convertSlideToModel(const osg::Vec3& position) const;
538 osg::Vec3 convertModelToSlide(const osg::Vec3& position) const;
539
540 osg::AnimationPathCallback* getAnimationPathCallback(const PositionData& positionData);
541
542 osg::Node* attachMaterialAnimation(osg::Node* model, const PositionData& positionData);
543 bool attachTexMat(osg::StateSet* stateset, const ImageData& imageData, float s, float t, bool textureRectangle);
544
545 osg::StateSet* createTransformStateSet()
546 {
547 osg::StateSet* stateset = new osg::StateSet;
548 #if !defined(OSG_GLES2_AVAILABLE)
549 stateset->setMode(GL_NORMALIZE,osg::StateAttribute::ON);
550 #endif
551 return stateset;
552 }
553
554 osg::Node* decorateSubgraphForPosition(osg::Node* node, PositionData& positionData);
555 osg::Node* decorateSubgraphForPositionAndAnimation(osg::Node* node, const PositionData& positionData);
556
557 osg::ref_ptr<osgDB::Options> _options;
558
559 osg::Vec3 _slideOrigin;
560 osg::Vec3 _eyeOrigin;
561 double _slideWidth;
562 double _slideHeight;
563 double _slideDistance;
564 unsigned int _leftEyeMask;
565 unsigned int _rightEyeMask;
566
567 osg::ref_ptr<HUDSettings> _hudSettings;
568
569 // title settings
570 FontData _titleFontData;
571 FontData _titleFontDataDefault;
572
573 PositionData _titlePositionData;
574 PositionData _titlePositionDataDefault;
575
576 // text settings
577 FontData _textFontData;
578 FontData _textFontDataDefault;
579
580 PositionData _textPositionData;
581 PositionData _textPositionDataDefault;
582
583 // image settings
584 PositionData _imagePositionData;
585 PositionData _imagePositionDataDefault;
586
587 // model settings
588 PositionData _modelPositionData;
589 PositionData _modelPositionDataDefault;
590
591
592 bool _loopPresentation;
593 bool _autoSteppingActive;
594 osg::Vec4 _backgroundColor;
595 std::string _presentationName;
596 double _presentationDuration;
597
598 osg::ref_ptr<osgPresentation::PropertyManager> _propertyManager;
599 osg::ref_ptr<osgPresentation::PropertyEventCallback> _propertyEventCallback;
600
601 osg::ref_ptr<osg::Group> _root;
602 osg::ref_ptr<osg::Switch> _presentationSwitch;
603
604 typedef std::map< std::string, osg::ref_ptr<osg::ScriptEngine> > ScriptEngineMap;
605 ScriptEngineMap _scriptEngines;
606
607 typedef std::map< std::string, osg::ref_ptr<osg::Script> > ScriptMap;
608 ScriptMap _scripts;
609
610 osg::ref_ptr<osg::ClearNode> _slideClearNode;
611 osg::ref_ptr<osg::Switch> _slide;
612 std::string _slideTitle;
613 std::string _slideBackgroundImageFileName;
614 bool _slideBackgroundAsHUD;
615
616 osg::ref_ptr<osg::Group> _previousLayer;
617 osg::ref_ptr<osg::Group> _currentLayer;
618 typedef std::vector< osg::ref_ptr<osg::Group> > LayerStack;
619 LayerStack _layerStack;
620
621 osg::ref_ptr<FilePathData> _filePathData;
622
623 osg::ref_ptr<osg::Group> _layerToApplyEventCallbackTo;
624
625 typedef std::list< osg::ref_ptr<osgGA::GUIEventHandler> > EventHandlerList;
626 EventHandlerList _currentEventCallbacksToApply;
627
628
629 std::string findFileAndRecordPath(const std::string& filename);
630
631 void recordOptionsFilePath(const osgDB::Options* options);
632
633};
634
635}
636
637#endif