SlideShowConstructor 17.8 KB
Newer Older
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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield 
 *
 * This software is open source and may be redistributed and/or modified under  
 * the terms of the GNU General Public License (GPL) version 2.0.
 * The full license is in LICENSE.txt file included with this distribution,.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * include LICENSE.txt for more details.
*/

#ifndef SLIDESHOWCONSTRUCTOR
#define SLIDESHOWCONSTRUCTOR

#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Group>
#include <osg/ClearNode>
#include <osg/Switch>
#include <osg/AnimationPath>
#include <osg/TransferFunction>
#include <osg/ImageStream>
#include <osgText/Text>
#include <osgGA/GUIEventAdapter>

#include <osgDB/FileUtils>

#include <osgPresentation/AnimationMaterial>
#include <osgPresentation/SlideEventHandler>

namespace osgPresentation
{

class OSGPRESENTATION_EXPORT HUDSettings : public osg::Referenced
{
    public:
        HUDSettings(double slideDistance, float eyeOffset, unsigned int leftMask, unsigned int rightMask);

        virtual bool getModelViewMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const;

        virtual bool getInverseModelViewMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const;

        double          _slideDistance;
        double          _eyeOffset;
        unsigned int    _leftMask;
        unsigned int    _rightMask;

protected:

        virtual ~HUDSettings();
};


class OSGPRESENTATION_EXPORT HUDTransform : public osg::Transform
{
    public:

        HUDTransform(HUDSettings* hudSettings);

        virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const;

        virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor*) const;

protected:

        virtual ~HUDTransform();

        osg::ref_ptr<HUDSettings> _hudSettings;

};

class OSGPRESENTATION_EXPORT SlideShowConstructor
{
public:

    
    enum CoordinateFrame { SLIDE, MODEL };

    
    LayerAttributes* getOrCreateLayerAttributes(osg::Node* node);
    
    void setDuration(osg::Node* node,double duration)
    {
        getOrCreateLayerAttributes(node)->setDuration(duration);
    }
    
    void addKey(osg::Node* node,const KeyPosition& kp)
    {
        getOrCreateLayerAttributes(node)->addKey(kp);
    }

    void addRunString(osg::Node* node, const std::string& runString)
    {
        getOrCreateLayerAttributes(node)->addRunString(runString);
    }

    void setJump(osg::Node* node, bool relativeJump, int slideNum, int layerNum)
    {
        getOrCreateLayerAttributes(node)->setJump(relativeJump, slideNum, layerNum);
    }

    void addPresentationKey(const KeyPosition& kp)
    {
        if (!_presentationSwitch) createPresentation();            
        if (_presentationSwitch.valid()) addKey( _presentationSwitch.get(), kp);
    }

    void addPresentationRunString(const std::string& runString)
    {
        if (!_presentationSwitch) createPresentation();            
        if (_presentationSwitch.valid()) addRunString( _presentationSwitch.get(),runString);
    }

    void addSlideKey(const KeyPosition& kp)
    {
        if (!_slide) addSlide();
        if (_slide.valid()) addKey(_slide.get(),kp);
    }
    
    void addSlideRunString(const std::string& runString)
    {
        if (!_slide) addSlide();
        if (_slide.valid()) addRunString(_slide.get(),runString);
    }
    
    void setSlideJump(bool relativeJump, int switchNum, int layerNum)
    {
        if (!_slide) addSlide();
        if (_slide.valid()) setJump(_slide.get(),relativeJump, switchNum, layerNum);
    }

    void addLayerKey(const KeyPosition& kp)
    {
        if (!_currentLayer) addLayer();
        if (_currentLayer.valid()) addKey(_currentLayer.get(),kp);
    }

    void addLayerRunString(const std::string& runString)
    {
        if (!_currentLayer) addLayer();
        if (_currentLayer.valid()) addRunString(_currentLayer.get(),runString);
    }
    

    void setLayerJump(bool relativeJump, int switchNum, int layerNum)
    {
        if (!_currentLayer) addLayer();
        if (_currentLayer.valid()) setJump(_currentLayer.get(),relativeJump, switchNum, layerNum);
    }



    struct PositionData
    {
        PositionData():
            frame(SlideShowConstructor::SLIDE),
            position(0.0f,1.0f,0.0f),
            //position(0.5f,0.5f,0.0f),
            scale(1.0f,1.0f,1.0f),
            rotate(0.0f,0.0f,0.0f,1.0f),
            rotation(0.0f,0.0f,1.0f,0.0f),
            absolute_path(false),
            inverse_path(false),
            path_time_offset(0.0),
            path_time_multiplier(1.0f),
            path_loop_mode(osg::AnimationPath::NO_LOOPING),
            animation_material_time_offset(0.0),
            animation_material_time_multiplier(1.0),
            animation_material_loop_mode(AnimationMaterial::NO_LOOPING),
            autoRotate(false),
            autoScale(false),
            hud(false) {}


        bool requiresPosition() const
        {
            return (position[0]!=0.0f || position[1]!=1.0f || position[2]!=1.0f);
        }

        bool requiresScale() const
        {
            return (scale[0]!=1.0f || scale[1]!=1.0f || scale[2]!=1.0f);
        }

        bool requiresRotate() const
        {
            return rotate[0]!=0.0f;
        }

        bool requiresAnimation() const
        {
            return (rotation[0]!=0.0f || !path.empty());
        }

        bool requiresMaterialAnimation() const
        {
            return !animation_material_filename.empty() || !fade.empty();
        }

        CoordinateFrame                         frame;
        osg::Vec3                               position;
        osg::Vec3                               scale;
        osg::Vec4                               rotate;
        osg::Vec4                               rotation;
        std::string                             animation_name;
        bool                                    absolute_path;
        bool                                    inverse_path;
        double                                  path_time_offset;
        double                                  path_time_multiplier;
        osg::AnimationPath::LoopMode            path_loop_mode;
        std::string                             path;
        double                                  animation_material_time_offset;
        double                                  animation_material_time_multiplier;
        AnimationMaterial::LoopMode             animation_material_loop_mode;
        std::string                             animation_material_filename;
        std::string                             fade;
        bool                                    autoRotate;
        bool                                    autoScale;
        bool                                    hud;
    };

    struct ModelData
    {
        ModelData():
            effect("") {}
            
        std::string effect;
    };

    struct ImageData
    {
        ImageData():
            width(1.0f),
            height(1.0f),
            region(0.0f,0.0f,1.0f,1.0f),
            region_in_pixel_coords(false),
            texcoord_rotate(0.0f),
            loopingMode(osg::ImageStream::NO_LOOPING),
            page(-1),
            backgroundColor(1.0f,1.0f,1.0f,1.0f) {}
            
        float                           width;
        float                           height;
        osg::Vec4                       region;
        bool                            region_in_pixel_coords;
        float                           texcoord_rotate;
        osg::ImageStream::LoopingMode   loopingMode;
        int                             page;
        osg::Vec4                       backgroundColor;
    };

    struct VolumeData
    {
        enum ShadingModel
        {
            Standard,
            Light,
            Isosurface,
            MaximumIntensityProjection
        };

        VolumeData():
            shadingModel(Standard),
            useTabbedDragger(false),
            useTrackballDragger(false),
            region_in_pixel_coords(false),
            alphaValue(1.0),
            cutoffValue(0.1),
            sampleDensityValue(0.005),
            sampleDensityWhenMovingValue(0.0)
        {
            region[0] = region[1] = region[2] = 0.0f;
            region[3] = region[4] = region[5] = 1.0f;
        }

        ShadingModel                            shadingModel;
        osg::ref_ptr<osg::TransferFunction1D>   transferFunction;
        bool                                    useTabbedDragger;
        bool                                    useTrackballDragger;
        float                                   region[6];
        bool                                    region_in_pixel_coords;
        float                                   alphaValue;
        float                                   cutoffValue;
        float                                   sampleDensityValue;
        float                                   sampleDensityWhenMovingValue;
    };


    struct FontData
    {
        FontData():
            font("fonts/arial.ttf"),
            layout(osgText::Text::LEFT_TO_RIGHT),
            alignment(osgText::Text::LEFT_BASE_LINE),
            axisAlignment(osgText::Text::XZ_PLANE),
            characterSizeMode(osgText::Text::OBJECT_COORDS),
            characterSize(0.04f),
            maximumHeight(1.0f),
            maximumWidth(1.0f),
            color(1.0f,1.0f,1.0f,1.0f) {}

        std::string                         font;
        osgText::Text::Layout               layout;
        osgText::Text::AlignmentType        alignment;
        osgText::Text::AxisAlignment        axisAlignment;
        osgText::Text::CharacterSizeMode    characterSizeMode;
        float                               characterSize;
        float                               maximumHeight;
        float                               maximumWidth;
        osg::Vec4                           color;
    };


    SlideShowConstructor(osgDB::Options* options);

    void createPresentation();
    
    void setBackgroundColor(const osg::Vec4& color, bool updateClearNode);
    const osg::Vec4& getBackgroundColor() const { return _backgroundColor; }
    
    void setTextColor(const osg::Vec4& color);
    const osg::Vec4& getTextColor() const { return _textFontDataDefault.color; }
    
    void setPresentationName(const std::string& name);
    
    void setPresentationAspectRatio(float aspectRatio);

    void setPresentationAspectRatio(const std::string& str);
    
    void setPresentationDuration(double duration);


    void addSlide();
    
    void selectSlide(int slideNum);


    void setSlideTitle(const std::string& name, PositionData& positionData, FontData& fontData)
    {
        _titlePositionData = positionData;
        _titleFontData = fontData;
        _slideTitle = name;
    }

    void setSlideBackgrondHUD(bool hud) { _slideBackgroundAsHUD = hud; }
    void setSlideBackground(const std::string& name) { _slideBackgroundImageFileName = name; }
    
    void setSlideDuration(double duration);


    void addLayer(bool inheritPreviousLayers=true, bool defineAsBaseLayer=false);
    
    void selectLayer(int layerNum);
    
    void setLayerDuration(double duration);

    
    // title settings
    FontData& getTitleFontData() { return _titleFontData; }
    FontData& getTitleFontDataDefault() { return _titleFontDataDefault; }

    PositionData& getTitlePositionData() { return _titlePositionData; }
    PositionData& getTitlePositionDataDefault() { return _titlePositionDataDefault; }

    // text settings
    FontData& getTextFontData() { return _textFontData; }
    FontData& getTextFontDataDefault() { return _textFontDataDefault; }
    
    PositionData& getTextPositionData() { return _textPositionData; }
    PositionData& getTextPositionDataDefault() { return _textPositionDataDefault; }

    void translateTextCursor(const osg::Vec3& delta) { _textPositionData.position += delta; }

    // image settings
    PositionData& getImagePositionData() { return _imagePositionData; }
    PositionData& getImagePositionDataDefault() { return _imagePositionDataDefault; }

    // model settings
    PositionData& getModelPositionData() { return _modelPositionData; }
    PositionData& getModelPositionDataDefault() { return _modelPositionDataDefault; }


    void layerClickToDoOperation(Operation operation, bool relativeJump=true, int slideNum=0, int layerNum=0);
    void layerClickToDoOperation(const std::string& command, Operation operation, bool relativeJump=true, int slideNum=0, int layerNum=0);
    void layerClickEventOperation(const KeyPosition& keyPos, bool relativeJump=true, int slideNum=0, int layerNum=0);

    void addBullet(const std::string& bullet, PositionData& positionData, FontData& fontData);
    
    void addParagraph(const std::string& paragraph, PositionData& positionData, FontData& fontData);
    
    void addImage(const std::string& filename,const PositionData& positionData, const ImageData& imageData);

    void addStereoImagePair(const std::string& filenameLeft, const ImageData& imageDataLeft, const std::string& filenameRight,const ImageData& imageDataRight, const PositionData& positionData);

    void addGraph(const std::string& filename,const std::string& options,const PositionData& positionData, const ImageData& imageData);
    void addVNC(const std::string& filename,const PositionData& positionData, const ImageData& imageData);
    void addBrowser(const std::string& filename,const PositionData& positionData, const ImageData& imageData);
    void addPDF(const std::string& filename,const PositionData& positionData, const ImageData& imageData);
    osg::Image* addInteractiveImage(const std::string& filename,const PositionData& positionData, const ImageData& imageData);

    void addModel(osg::Node* subgraph, const PositionData& positionData, const ModelData& modelData);

    void addModel(const std::string& filename, const PositionData& positionData, const ModelData& modelData);
    
    void addVolume(const std::string& filename, const PositionData& positionData, const VolumeData& volumeData);

    osg::Group* takePresentation() { return _root.release(); }
    
    osg::Group* getPresentation() { return _root.get(); }

    osg::Switch* getPresentationSwitch() { return _presentationSwitch.get(); }

    osg::Switch* getCurrentSlide() { return _slide.get(); }
    
    osg::Group* getCurrentLayer() { return _currentLayer.get(); }

    void setLoopPresentation(bool loop) { _loopPresentation = loop; }
    bool getLoopPresentation() const { return _loopPresentation; }

    void setAutoSteppingActive(bool flag = true) { _autoSteppingActive = flag; }
    bool getAutoSteppingActive() const { return _autoSteppingActive; }

    void setHUDSettings(HUDSettings* hudSettings) { _hudSettings = hudSettings; }
    HUDSettings* getHUDSettings() { return _hudSettings.get(); }
    const HUDSettings* getHUDSettings() const { return _hudSettings.get(); }


protected:

    void findImageStreamsAndAddCallbacks(osg::Node* node);

    osg::Geometry* createTexturedQuadGeometry(const osg::Vec3& pos, const osg::Vec4& rotation, float width,float height, osg::Image* image, bool& usedTextureRectangle);

    osg::Vec3 computePositionInModelCoords(const PositionData& positionData) const;
    void updatePositionFromInModelCoords(const osg::Vec3& vertex, PositionData& positionData) const;
    
    osg::Vec3 convertSlideToModel(const osg::Vec3& position) const;
    osg::Vec3 convertModelToSlide(const osg::Vec3& position) const;

    osg::AnimationPathCallback* getAnimationPathCallback(const PositionData& positionData);
    
    osg::Node* attachMaterialAnimation(osg::Node* model, const PositionData& positionData);
    bool attachTexMat(osg::StateSet* stateset, const ImageData& imageData, float s, float t, bool textureRectangle);
    
    osg::StateSet* createTransformStateSet()
    {
        osg::StateSet* stateset = new osg::StateSet;
        #if !defined(OSG_GLES2_AVAILABLE)
            stateset->setMode(GL_NORMALIZE,osg::StateAttribute::ON);
        #endif
        return stateset;
    }

    osg::Node* decorateSubgraphForPosition(osg::Node* node, PositionData& positionData);

    osg::ref_ptr<osgDB::Options> _options;

    osg::Vec3   _slideOrigin;
    osg::Vec3   _eyeOrigin;
    double      _slideWidth;
    double      _slideHeight;
    double      _slideDistance;
    unsigned int _leftEyeMask;
    unsigned int _rightEyeMask;

    osg::ref_ptr<HUDSettings> _hudSettings;
    
    // title settings
    FontData        _titleFontData;
    FontData        _titleFontDataDefault;

    PositionData    _titlePositionData;
    PositionData    _titlePositionDataDefault;

    // text settings
    FontData        _textFontData;
    FontData        _textFontDataDefault;
    
    PositionData    _textPositionData;
    PositionData    _textPositionDataDefault;

    // image settings
    PositionData    _imagePositionData;
    PositionData    _imagePositionDataDefault;

    // model settings
    PositionData    _modelPositionData;
    PositionData    _modelPositionDataDefault;


    bool        _loopPresentation;
    bool        _autoSteppingActive;
    osg::Vec4   _backgroundColor;
    std::string _presentationName;
    double      _presentationDuration;

    osg::ref_ptr<osg::Group>        _root;
    osg::ref_ptr<osg::Switch>       _presentationSwitch;
    
    osg::ref_ptr<osg::ClearNode>    _slideClearNode;
    osg::ref_ptr<osg::Switch>       _slide;
    std::string                     _slideTitle;
    std::string                     _slideBackgroundImageFileName;
    bool                            _slideBackgroundAsHUD;
    
    osg::ref_ptr<osg::Group>        _previousLayer;
    osg::ref_ptr<osg::Group>        _currentLayer;
    
    osg::ref_ptr<FilePathData>      _filePathData;
    
    std::string findFileAndRecordPath(const std::string& filename);
    
    void recordOptionsFilePath(const osgDB::Options* options);

};

}

#endif