AutoTransform 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef OSG_AUTOTRANSFORM
  14. #define OSG_AUTOTRANSFORM 1
  15. #include <osg/Group>
  16. #include <osg/Transform>
  17. #include <osg/Quat>
  18. #include <osg/Viewport>
  19. namespace osg {
  20. /** AutoTransform is a derived form of Transform that automatically
  21. * scales or rotates to keep its children aligned with screen coordinates.
  22. */
  23. class OSG_EXPORT AutoTransform : public Transform
  24. {
  25. public :
  26. AutoTransform();
  27. AutoTransform(const AutoTransform& pat,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  28. virtual osg::Object* cloneType() const { return new AutoTransform (); }
  29. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new AutoTransform (*this,copyop); }
  30. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const AutoTransform *>(obj)!=NULL; }
  31. virtual const char* className() const { return "AutoTransform"; }
  32. virtual const char* libraryName() const { return "osg"; }
  33. virtual AutoTransform* asAutoTransform() { return this; }
  34. virtual const AutoTransform* asAutoTransform() const { return this; }
  35. inline void setPosition(const Vec3d& pos) { _position = pos; dirtyBound(); }
  36. inline const Vec3d& getPosition() const { return _position; }
  37. inline void setRotation(const Quat& quat) { _rotation = quat; dirtyBound(); }
  38. inline const Quat& getRotation() const { return _rotation; }
  39. inline void setScale(double scale) { setScale(osg::Vec3(scale,scale,scale)); }
  40. void setScale(const Vec3d& scale);
  41. inline const Vec3d& getScale() const { return _scale; }
  42. void setMinimumScale(double minimumScale) { _minimumScale = minimumScale; }
  43. double getMinimumScale() const { return _minimumScale; }
  44. void setMaximumScale(double maximumScale) { _maximumScale = maximumScale; }
  45. double getMaximumScale() const { return _maximumScale; }
  46. inline void setPivotPoint(const Vec3d& pivot) { _pivotPoint = pivot; dirtyBound(); }
  47. inline const Vec3d& getPivotPoint() const { return _pivotPoint; }
  48. void setAutoUpdateEyeMovementTolerance(float tolerance) { _autoUpdateEyeMovementTolerance = tolerance; }
  49. float getAutoUpdateEyeMovementTolerance() const { return _autoUpdateEyeMovementTolerance; }
  50. enum AutoRotateMode
  51. {
  52. NO_ROTATION,
  53. ROTATE_TO_SCREEN,
  54. ROTATE_TO_CAMERA,
  55. ROTATE_TO_AXIS
  56. };
  57. void setAutoRotateMode(AutoRotateMode mode);
  58. AutoRotateMode getAutoRotateMode() const { return _autoRotateMode; }
  59. /** Set the rotation axis for the AutoTransform's child nodes.
  60. * Only utilized when _autoRotateMode==ROTATE_TO_AXIS. */
  61. void setAxis(const Vec3& axis);
  62. /** Get the rotation axis. */
  63. inline const Vec3& getAxis() const { return _axis; }
  64. /** This normal defines child Nodes' front face direction when unrotated. */
  65. void setNormal(const Vec3& normal);
  66. /** Get the front face direction normal. */
  67. inline const Vec3& getNormal() const { return _normal; }
  68. void setAutoScaleToScreen(bool autoScaleToScreen);
  69. bool getAutoScaleToScreen() const { return _autoScaleToScreen; }
  70. void setAutoScaleTransitionWidthRatio(float ratio) { _autoScaleTransitionWidthRatio = ratio; }
  71. float getAutoScaleTransitionWidthRatio() const { return _autoScaleTransitionWidthRatio; }
  72. virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor* nv) const;
  73. virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor* nv) const;
  74. protected :
  75. virtual ~AutoTransform() {}
  76. Vec3d _position;
  77. Vec3d _pivotPoint;
  78. double _autoUpdateEyeMovementTolerance;
  79. AutoRotateMode _autoRotateMode;
  80. bool _autoScaleToScreen;
  81. mutable Quat _rotation;
  82. mutable Vec3d _scale;
  83. double _minimumScale;
  84. double _maximumScale;
  85. double _autoScaleTransitionWidthRatio;
  86. osg::Matrixd computeMatrix(const osg::NodeVisitor* nv) const;
  87. enum AxisAligned
  88. {
  89. AXIAL_ROT_X_AXIS=ROTATE_TO_AXIS+1,
  90. AXIAL_ROT_Y_AXIS,
  91. AXIAL_ROT_Z_AXIS,
  92. CACHE_DIRTY
  93. };
  94. Vec3 _axis;
  95. Vec3 _normal;
  96. // used internally as cache of which what _axis is aligned to help
  97. // decide which method of rotation to use.
  98. int _cachedMode;
  99. Vec3 _side;
  100. void updateCache();
  101. };
  102. }
  103. #endif