AutoTransform 6.06 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
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library 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
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_AUTOTRANSFORM
#define OSG_AUTOTRANSFORM 1

#include <osg/Group>
#include <osg/Transform>
#include <osg/Quat>
#include <osg/Viewport>

namespace osg {

/** AutoTransform is a derived form of Transform that automatically
  * scales or rotates to keep its children aligned with screen coordinates.
*/
class OSG_EXPORT AutoTransform : public Transform
{
    public :
        AutoTransform();

        AutoTransform(const AutoTransform& pat,const CopyOp& copyop=CopyOp::SHALLOW_COPY);

        virtual osg::Object* cloneType() const { return new AutoTransform (); }
        virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new AutoTransform (*this,copyop); }
        virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const AutoTransform *>(obj)!=NULL; }
        virtual const char* className() const { return "AutoTransform"; }
        virtual const char* libraryName() const { return "osg"; }

        virtual void accept(NodeVisitor& nv);

        virtual AutoTransform* asAutoTransform() { return this; }
        virtual const AutoTransform* asAutoTransform() const { return this; }

        inline void setPosition(const Vec3d& pos) { _position = pos; _matrixDirty=true; dirtyBound(); }
        inline const Vec3d& getPosition() const { return _position; }


        inline void setRotation(const Quat& quat) { _rotation = quat; _matrixDirty=true; dirtyBound(); }
        inline const Quat& getRotation() const { return _rotation; }

        inline void setScale(double scale) { setScale(osg::Vec3(scale,scale,scale)); }

        void setScale(const Vec3d& scale);
        inline const Vec3d& getScale() const { return _scale; }

        void setMinimumScale(double minimumScale) { _minimumScale = minimumScale; }
        double getMinimumScale() const { return _minimumScale; }

        void setMaximumScale(double maximumScale) { _maximumScale = maximumScale; }
        double getMaximumScale() const { return _maximumScale; }

        inline void setPivotPoint(const Vec3d& pivot) { _pivotPoint = pivot; _matrixDirty=true; dirtyBound(); }
        inline const Vec3d& getPivotPoint() const { return _pivotPoint; }


        void setAutoUpdateEyeMovementTolerance(float tolerance) { _autoUpdateEyeMovementTolerance = tolerance; }
        float getAutoUpdateEyeMovementTolerance() const { return _autoUpdateEyeMovementTolerance; }


        enum AutoRotateMode
        {
            NO_ROTATION,
            ROTATE_TO_SCREEN,
            ROTATE_TO_CAMERA,
            ROTATE_TO_AXIS
        };

        void setAutoRotateMode(AutoRotateMode mode);

        AutoRotateMode getAutoRotateMode() const { return _autoRotateMode; }

        /** Set the rotation axis for the AutoTransform's child nodes.
          * Only utilized when _autoRotateMode==ROTATE_TO_AXIS. */
        void setAxis(const Vec3& axis);
        /** Get the rotation axis. */
        inline const Vec3& getAxis() const { return _axis; }

        /** This normal defines child Nodes' front face direction when unrotated. */
        void setNormal(const Vec3& normal);
        /** Get the front face direction normal. */
        inline const Vec3& getNormal() const { return _normal; }

        void setAutoScaleToScreen(bool autoScaleToScreen) { _autoScaleToScreen = autoScaleToScreen; _matrixDirty=true; }

        bool getAutoScaleToScreen() const { return _autoScaleToScreen; }

        void setAutoScaleTransitionWidthRatio(float ratio) { _autoScaleTransitionWidthRatio = ratio; }
        float getAutoScaleTransitionWidthRatio() const { return _autoScaleTransitionWidthRatio; }


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

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

        virtual BoundingSphere computeBound() const;


    protected :

        virtual ~AutoTransform() {}

        Vec3d                           _position;
        Vec3d                           _pivotPoint;
        double                          _autoUpdateEyeMovementTolerance;

        AutoRotateMode                  _autoRotateMode;

        bool                            _autoScaleToScreen;

        mutable Quat                    _rotation;
        mutable Vec3d                   _scale;
        mutable bool                    _firstTimeToInitEyePoint;
        mutable osg::Vec3               _previousEyePoint;
        mutable osg::Vec3               _previousLocalUp;
        mutable Viewport::value_type    _previousWidth;
        mutable Viewport::value_type    _previousHeight;
        mutable osg::Matrixd            _previousProjection;
        mutable osg::Vec3d              _previousPosition;

        double                          _minimumScale;
        double                          _maximumScale;
        double                          _autoScaleTransitionWidthRatio;

        void computeMatrix() const;

        mutable bool                    _matrixDirty;
        mutable osg::Matrixd            _cachedMatrix;

        enum AxisAligned
        {
            AXIAL_ROT_X_AXIS=ROTATE_TO_AXIS+1,
            AXIAL_ROT_Y_AXIS,
            AXIAL_ROT_Z_AXIS,
            CACHE_DIRTY
        };

        Vec3                            _axis;
        Vec3                            _normal;

        // used internally as cache of which what _axis is aligned to help
        // decide which method of rotation to use.
        int                             _cachedMode;
        Vec3                            _side;
        void updateCache();

};

}

#endif