| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 OSG_AUTOTRANSFORM |
|---|
| 15 | #define OSG_AUTOTRANSFORM 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Group> |
|---|
| 18 | #include <osg/Transform> |
|---|
| 19 | #include <osg/Quat> |
|---|
| 20 | #include <osg/Viewport> |
|---|
| 21 | |
|---|
| 22 | namespace osg { |
|---|
| 23 | |
|---|
| 24 | /** AutoTransform is a derived form of Transform that automatically |
|---|
| 25 | * scales or rotates to keep its children aligned with screen coordinates. |
|---|
| 26 | */ |
|---|
| 27 | class OSG_EXPORT AutoTransform : public Transform |
|---|
| 28 | { |
|---|
| 29 | public : |
|---|
| 30 | AutoTransform(); |
|---|
| 31 | |
|---|
| 32 | AutoTransform(const AutoTransform& pat,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 33 | |
|---|
| 34 | virtual osg::Object* cloneType() const { return new AutoTransform (); } |
|---|
| 35 | virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new AutoTransform (*this,copyop); } |
|---|
| 36 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const AutoTransform *>(obj)!=NULL; } |
|---|
| 37 | virtual const char* className() const { return "AutoTransform"; } |
|---|
| 38 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 39 | |
|---|
| 40 | virtual void accept(NodeVisitor& nv); |
|---|
| 41 | |
|---|
| 42 | virtual AutoTransform* asAutoTransform() { return this; } |
|---|
| 43 | virtual const AutoTransform* asAutoTransform() const { return this; } |
|---|
| 44 | |
|---|
| 45 | inline void setPosition(const Vec3& pos) { _position = pos; _matrixDirty=true; dirtyBound(); } |
|---|
| 46 | inline const Vec3& getPosition() const { return _position; } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | inline void setRotation(const Quat& quat) { _rotation = quat; _matrixDirty=true; dirtyBound(); } |
|---|
| 50 | inline const Quat& getRotation() const { return _rotation; } |
|---|
| 51 | |
|---|
| 52 | inline void setScale(float scale) { setScale(osg::Vec3(scale,scale,scale)); } |
|---|
| 53 | |
|---|
| 54 | void setScale(const Vec3& scale); |
|---|
| 55 | inline const Vec3& getScale() const { return _scale; } |
|---|
| 56 | |
|---|
| 57 | void setMinimumScale(float minimumScale) { _minimumScale = minimumScale; } |
|---|
| 58 | float getMinimumScale() const { return _minimumScale; } |
|---|
| 59 | |
|---|
| 60 | void setMaximumScale(float maximumScale) { _maximumScale = maximumScale; } |
|---|
| 61 | float getMaximumScale() const { return _maximumScale; } |
|---|
| 62 | |
|---|
| 63 | inline void setPivotPoint(const Vec3& pivot) { _pivotPoint = pivot; _matrixDirty=true; dirtyBound(); } |
|---|
| 64 | inline const Vec3& getPivotPoint() const { return _pivotPoint; } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | void setAutoUpdateEyeMovementTolerance(float tolerance) { _autoUpdateEyeMovementTolerance = tolerance; } |
|---|
| 68 | float getAutoUpdateEyeMovementTolerance() const { return _autoUpdateEyeMovementTolerance; } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | enum AutoRotateMode |
|---|
| 72 | { |
|---|
| 73 | NO_ROTATION, |
|---|
| 74 | ROTATE_TO_SCREEN, |
|---|
| 75 | ROTATE_TO_CAMERA |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | void setAutoRotateMode(AutoRotateMode mode) { _autoRotateMode = mode; _firstTimeToInitEyePoint = true; } |
|---|
| 79 | |
|---|
| 80 | AutoRotateMode getAutoRotateMode() const { return _autoRotateMode; } |
|---|
| 81 | |
|---|
| 82 | void setAutoScaleToScreen(bool autoScaleToScreen) { _autoScaleToScreen = autoScaleToScreen; _matrixDirty=true; } |
|---|
| 83 | |
|---|
| 84 | bool getAutoScaleToScreen() const { return _autoScaleToScreen; } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor* nv) const; |
|---|
| 88 | |
|---|
| 89 | virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor* nv) const; |
|---|
| 90 | |
|---|
| 91 | virtual BoundingSphere computeBound() const; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | protected : |
|---|
| 95 | |
|---|
| 96 | virtual ~AutoTransform() {} |
|---|
| 97 | |
|---|
| 98 | Vec3 _position; |
|---|
| 99 | Vec3 _pivotPoint; |
|---|
| 100 | float _autoUpdateEyeMovementTolerance; |
|---|
| 101 | |
|---|
| 102 | AutoRotateMode _autoRotateMode; |
|---|
| 103 | |
|---|
| 104 | bool _autoScaleToScreen; |
|---|
| 105 | |
|---|
| 106 | mutable Quat _rotation; |
|---|
| 107 | mutable Vec3 _scale; |
|---|
| 108 | mutable bool _firstTimeToInitEyePoint; |
|---|
| 109 | mutable osg::Vec3 _previousEyePoint; |
|---|
| 110 | mutable osg::Vec3 _previousLocalUp; |
|---|
| 111 | mutable Viewport::value_type _previousWidth; |
|---|
| 112 | mutable Viewport::value_type _previousHeight; |
|---|
| 113 | mutable osg::Matrix _previousProjection; |
|---|
| 114 | mutable osg::Vec3 _previousPosition; |
|---|
| 115 | |
|---|
| 116 | float _minimumScale; |
|---|
| 117 | float _maximumScale; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void computeMatrix() const; |
|---|
| 121 | |
|---|
| 122 | mutable bool _matrixDirty; |
|---|
| 123 | mutable osg::Matrix _cachedMatrix; |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | #endif |
|---|