1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2018 Robert Osfield
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.
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.
14#ifndef SLIDESHOWCONSTRUCTOR
15#define SLIDESHOWCONSTRUCTOR
19#include <osg/ImageUtils>
21#include <osg/ClearNode>
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>
31#include <osgDB/FileUtils>
33#include <osgVolume/VolumeTile>
34#include <osgVolume/VolumeSettings>
36#include <osgPresentation/AnimationMaterial>
37#include <osgPresentation/SlideEventHandler>
38#include <osgPresentation/PropertyManager>
39#include <osgPresentation/Timeout>
41namespace osgPresentation
46class OSGPRESENTATION_EXPORT HUDTransform : public osg::Transform
50 HUDTransform(HUDSettings* hudSettings);
52 virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const;
54 virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor*) const;
58 virtual ~HUDTransform();
60 osg::ref_ptr<HUDSettings> _hudSettings;
64class OSGPRESENTATION_EXPORT SlideShowConstructor
69 enum CoordinateFrame { SLIDE, MODEL };
73 LayerAttributes* getOrCreateLayerAttributes(osg::Node* node);
75 void setDuration(osg::Node* node,double duration)
77 getOrCreateLayerAttributes(node)->setDuration(duration);
80 void addKey(osg::Node* node,const KeyPosition& kp)
82 getOrCreateLayerAttributes(node)->addKey(kp);
85 void addRunString(osg::Node* node, const std::string& runString)
87 getOrCreateLayerAttributes(node)->addRunString(runString);
90 void setJump(osg::Node* node, const JumpData& jumpData)
92 getOrCreateLayerAttributes(node)->setJump(jumpData);
95 void addPresentationKey(const KeyPosition& kp)
97 if (!_presentationSwitch) createPresentation();
98 if (_presentationSwitch.valid()) addKey( _presentationSwitch.get(), kp);
101 void addPresentationRunString(const std::string& runString)
103 if (!_presentationSwitch) createPresentation();
104 if (_presentationSwitch.valid()) addRunString( _presentationSwitch.get(),runString);
107 void addSlideKey(const KeyPosition& kp)
109 if (!_slide) addSlide();
110 if (_slide.valid()) addKey(_slide.get(),kp);
113 void addSlideRunString(const std::string& runString)
115 if (!_slide) addSlide();
116 if (_slide.valid()) addRunString(_slide.get(),runString);
119 void setSlideJump(const JumpData& jumpData)
121 if (!_slide) addSlide();
122 if (_slide.valid()) setJump(_slide.get(),jumpData);
125 void addLayerKey(const KeyPosition& kp)
127 if (!_currentLayer) addLayer();
128 if (_currentLayer.valid()) addKey(_currentLayer.get(),kp);
131 void addLayerRunString(const std::string& runString)
133 if (!_currentLayer) addLayer();
134 if (_currentLayer.valid()) addRunString(_currentLayer.get(),runString);
138 void setLayerJump(const JumpData& jumpData)
140 if (!_currentLayer) addLayer();
141 if (_currentLayer.valid()) setJump(_currentLayer.get(),jumpData);
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),
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),
168 bool requiresPosition() const
170 return (position[0]!=0.0f || position[1]!=1.0f || position[2]!=1.0f);
173 bool requiresScale() const
175 return (scale[0]!=1.0f || scale[1]!=1.0f || scale[2]!=1.0f);
178 bool requiresRotate() const
180 return rotate[0]!=0.0f;
183 bool requiresAnimation() const
185 return (rotation[0]!=0.0f || !path.empty());
188 bool requiresMaterialAnimation() const
190 return !animation_material_filename.empty() || !fade.empty();
193 CoordinateFrame frame;
198 std::string animation_name;
201 double path_time_offset;
202 double path_time_multiplier;
203 osg::AnimationPath::LoopMode path_loop_mode;
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;
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),
234 backgroundColor(1.0f,1.0f,1.0f,1.0f),
237 imageSequence(false),
238 imageSequencePagingMode(osg::ImageSequence::PAGE_AND_DISCARD_USED_IMAGES),
239 imageSequenceInteractionMode(PLAY_AUTOMATICALLY_LIKE_MOVIE),
240 blendingHint(USE_IMAGE_ALPHA),
251 bool region_in_pixel_coords;
252 float texcoord_rotate;
253 osg::ImageStream::LoopingMode loopingMode;
255 osg::Vec4 backgroundColor;
260 osg::ImageSequence::Mode imageSequencePagingMode;
262 enum ImageSequenceInteractionMode
264 PLAY_AUTOMATICALLY_LIKE_MOVIE,
265 USE_MOUSE_X_POSITION,
268 ImageSequenceInteractionMode imageSequenceInteractionMode;
277 BlendingHint blendingHint;
288 typedef osgVolume::VolumeSettings::ShadingModel ShadingModel;
289 typedef osgVolume::VolumeSettings::Technique Technique;
292 shadingModel(osgVolume::VolumeSettings::Standard),
293 useTabbedDragger(false),
294 useTrackballDragger(false),
295 region_in_pixel_coords(false),
298 exteriorTransparencyFactorValue(),
299 sampleDensityValue(),
301 colorSpaceOperation(osg::NO_COLOR_SPACE_OPERATION),
302 colorModulate(1.0f,1.0f,1.0f,1.0f),
303 technique(osgVolume::VolumeSettings::RayTraced)
305 hullPositionData.position = osg::Vec3(0.0,0.0,0.0);
306 hullPositionData.frame = osgPresentation::SlideShowConstructor::MODEL;
309 osg::ref_ptr<osgVolume::VolumeSettings> volumeSettings;
312 ShadingModel shadingModel;
313 osg::ref_ptr<osg::TransferFunction1D> transferFunction;
314 bool useTabbedDragger;
315 bool useTrackballDragger;
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;
324 std::string sampleRatioValue;
325 std::string sampleRatioWhenMovingValue;
327 osg::ColorSpaceOperation colorSpaceOperation;
328 osg::Vec4 colorModulate;
331 PositionData hullPositionData;
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),
346 color(1.0f,1.0f,1.0f,1.0f) {}
349 osgText::Text::Layout layout;
350 osgText::Text::AlignmentType alignment;
351 osgText::Text::AxisAlignment axisAlignment;
352 osgText::Text::CharacterSizeMode characterSizeMode;
359 enum ScriptCallbackType
365 typedef std::pair<ScriptCallbackType, std::string> ScriptPair;
371 typedef std::vector<ScriptPair> Scripts;
374 bool hasScripts() const { return !scripts.empty(); }
377 SlideShowConstructor(osgDB::Options* options);
379 void createPresentation();
381 void setBackgroundColor(const osg::Vec4& color, bool updateClearNode);
382 const osg::Vec4& getBackgroundColor() const { return _backgroundColor; }
384 void setTextColor(const osg::Vec4& color);
385 const osg::Vec4& getTextColor() const { return _textFontDataDefault.color; }
387 void setPresentationName(const std::string& name);
389 void setPresentationAspectRatio(float aspectRatio);
391 void setPresentationAspectRatio(const std::string& str);
393 void setPresentationDuration(double duration);
395 void addScriptEngine(const std::string& scriptEngineName);
397 void addScriptFile(const std::string& name, const std::string& filename);
399 void addScript(const std::string& name, const std::string& language, const std::string& script);
403 void selectSlide(int slideNum);
406 void setSlideTitle(const std::string& name, PositionData& positionData, FontData& fontData)
408 _titlePositionData = positionData;
409 _titleFontData = fontData;
413 void setSlideBackgrondHUD(bool hud) { _slideBackgroundAsHUD = hud; }
414 void setSlideBackground(const std::string& name) { _slideBackgroundImageFileName = name; }
416 void setSlideDuration(double duration);
418 Timeout* addTimeout();
420 void addLayer(bool inheritPreviousLayers=true, bool defineAsBaseLayer=false);
422 void selectLayer(int layerNum);
424 void setLayerDuration(double duration);
428 FontData& getTitleFontData() { return _titleFontData; }
429 FontData& getTitleFontDataDefault() { return _titleFontDataDefault; }
431 PositionData& getTitlePositionData() { return _titlePositionData; }
432 PositionData& getTitlePositionDataDefault() { return _titlePositionDataDefault; }
435 FontData& getTextFontData() { return _textFontData; }
436 FontData& getTextFontDataDefault() { return _textFontDataDefault; }
438 PositionData& getTextPositionData() { return _textPositionData; }
439 PositionData& getTextPositionDataDefault() { return _textPositionDataDefault; }
441 void translateTextCursor(const osg::Vec3& delta) { _textPositionData.position += delta; }
444 PositionData& getImagePositionData() { return _imagePositionData; }
445 PositionData& getImagePositionDataDefault() { return _imagePositionDataDefault; }
448 PositionData& getModelPositionData() { return _modelPositionData; }
449 PositionData& getModelPositionDataDefault() { return _modelPositionDataDefault; }
452 enum PresentationContext {
453 CURRENT_PRESENTATION,
458 void addEventHandler(PresentationContext presentationContext, osg::ref_ptr<osgGA::GUIEventHandler> handler);
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());
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());
468 void addPropertyAnimation(PresentationContext presentationContext, PropertyAnimation* propertyAnimation);
470 void addScriptCallback(PresentationContext presentationContext, ScriptCallbackType scriptCallbackType, const std::string& functionName);
472 void addScriptToNode(ScriptCallbackType scriptCallbackType, const std::string& name, osg::Node* node);
474 void addScriptsToNode(const ScriptData& scriptData, osg::Node* node);
476 void addToCurrentLayer(osg::Node* subgraph);
478 void addBullet(const std::string& bullet, PositionData& positionData, FontData& fontData, const ScriptData& scriptData);
480 void addParagraph(const std::string& paragraph, PositionData& positionData, FontData& fontData, const ScriptData& scriptData);
482 osg::ref_ptr<osg::Image> readImage(const std::string& filename, const ImageData& imageData);
484 void addImage(const std::string& filename,const PositionData& positionData, const ImageData& imageData, const ScriptData& scriptData);
486 void addStereoImagePair(const std::string& filenameLeft, const ImageData& imageDataLeft, const std::string& filenameRight,const ImageData& imageDataRight, const PositionData& positionData, const ScriptData& scriptData);
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);
494 void addModel(osg::Node* subgraph, const PositionData& positionData, const ModelData& modelData, const ScriptData& scriptData);
496 void addModel(const std::string& filename, const PositionData& positionData, const ModelData& modelData, const ScriptData& scriptData);
498 void setUpVolumeScalarProperty(osgVolume::VolumeTile* tile, osgVolume::ScalarProperty* property, const std::string& source);
500 void addVolume(const std::string& filename, const PositionData& positionData, const VolumeData& volumeData, const ScriptData& scriptData);
502 osg::Group* takePresentation() { return _root.release(); }
504 osg::Group* getPresentation() { return _root.get(); }
506 osg::Switch* getPresentationSwitch() { return _presentationSwitch.get(); }
508 osg::Switch* getCurrentSlide() { return _slide.get(); }
510 void pushCurrentLayer(osg::Group* newLayerGroup);
511 void popCurrentLayer();
513 osg::Group* getCurrentLayer() { return _currentLayer.get(); }
515 void setLoopPresentation(bool loop) { _loopPresentation = loop; }
516 bool getLoopPresentation() const { return _loopPresentation; }
518 void setAutoSteppingActive(bool flag = true) { _autoSteppingActive = flag; }
519 bool getAutoSteppingActive() const { return _autoSteppingActive; }
521 void setHUDSettings(HUDSettings* hudSettings) { _hudSettings = hudSettings; }
522 HUDSettings* getHUDSettings() { return _hudSettings.get(); }
523 const HUDSettings* getHUDSettings() const { return _hudSettings.get(); }
525 osg::ScriptEngine* getOrCreateScriptEngine(const std::string& language);
529 void findImageStreamsAndAddCallbacks(osg::Node* node);
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);
534 osg::Vec3 computePositionInModelCoords(const PositionData& positionData) const;
535 void updatePositionFromInModelCoords(const osg::Vec3& vertex, PositionData& positionData) const;
537 osg::Vec3 convertSlideToModel(const osg::Vec3& position) const;
538 osg::Vec3 convertModelToSlide(const osg::Vec3& position) const;
540 osg::AnimationPathCallback* getAnimationPathCallback(const PositionData& positionData);
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);
545 osg::StateSet* createTransformStateSet()
547 osg::StateSet* stateset = new osg::StateSet;
548 #if !defined(OSG_GLES2_AVAILABLE)
549 stateset->setMode(GL_NORMALIZE,osg::StateAttribute::ON);
554 osg::Node* decorateSubgraphForPosition(osg::Node* node, PositionData& positionData);
555 osg::Node* decorateSubgraphForPositionAndAnimation(osg::Node* node, const PositionData& positionData);
557 osg::ref_ptr<osgDB::Options> _options;
559 osg::Vec3 _slideOrigin;
560 osg::Vec3 _eyeOrigin;
563 double _slideDistance;
564 unsigned int _leftEyeMask;
565 unsigned int _rightEyeMask;
567 osg::ref_ptr<HUDSettings> _hudSettings;
570 FontData _titleFontData;
571 FontData _titleFontDataDefault;
573 PositionData _titlePositionData;
574 PositionData _titlePositionDataDefault;
577 FontData _textFontData;
578 FontData _textFontDataDefault;
580 PositionData _textPositionData;
581 PositionData _textPositionDataDefault;
584 PositionData _imagePositionData;
585 PositionData _imagePositionDataDefault;
588 PositionData _modelPositionData;
589 PositionData _modelPositionDataDefault;
592 bool _loopPresentation;
593 bool _autoSteppingActive;
594 osg::Vec4 _backgroundColor;
595 std::string _presentationName;
596 double _presentationDuration;
598 osg::ref_ptr<osgPresentation::PropertyManager> _propertyManager;
599 osg::ref_ptr<osgPresentation::PropertyEventCallback> _propertyEventCallback;
601 osg::ref_ptr<osg::Group> _root;
602 osg::ref_ptr<osg::Switch> _presentationSwitch;
604 typedef std::map< std::string, osg::ref_ptr<osg::ScriptEngine> > ScriptEngineMap;
605 ScriptEngineMap _scriptEngines;
607 typedef std::map< std::string, osg::ref_ptr<osg::Script> > ScriptMap;
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;
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;
621 osg::ref_ptr<FilePathData> _filePathData;
623 osg::ref_ptr<osg::Group> _layerToApplyEventCallbackTo;
625 typedef std::list< osg::ref_ptr<osgGA::GUIEventHandler> > EventHandlerList;
626 EventHandlerList _currentEventCallbacksToApply;
629 std::string findFileAndRecordPath(const std::string& filename);
631 void recordOptionsFilePath(const osgDB::Options* options);