openscenegraph
AutoTransform
Go to the documentation of this file.
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
22namespace 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*/
27class 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 AutoTransform* asAutoTransform() { return this; }
41 virtual const AutoTransform* asAutoTransform() const { return this; }
42
43 inline void setPosition(const Vec3d& pos) { _position = pos; dirtyBound(); }
44 inline const Vec3d& getPosition() const { return _position; }
45
46
47 inline void setRotation(const Quat& quat) { _rotation = quat; dirtyBound(); }
48 inline const Quat& getRotation() const { return _rotation; }
49
50 inline void setScale(double scale) { setScale(osg::Vec3(scale,scale,scale)); }
51
52 void setScale(const Vec3d& scale);
53 inline const Vec3d& getScale() const { return _scale; }
54
55 void setMinimumScale(double minimumScale) { _minimumScale = minimumScale; }
56 double getMinimumScale() const { return _minimumScale; }
57
58 void setMaximumScale(double maximumScale) { _maximumScale = maximumScale; }
59 double getMaximumScale() const { return _maximumScale; }
60
61 inline void setPivotPoint(const Vec3d& pivot) { _pivotPoint = pivot; dirtyBound(); }
62 inline const Vec3d& getPivotPoint() const { return _pivotPoint; }
63
64
65 void setAutoUpdateEyeMovementTolerance(float tolerance) { _autoUpdateEyeMovementTolerance = tolerance; }
66 float getAutoUpdateEyeMovementTolerance() const { return _autoUpdateEyeMovementTolerance; }
67
68
69 enum AutoRotateMode
70 {
71 NO_ROTATION,
72 ROTATE_TO_SCREEN,
73 ROTATE_TO_CAMERA,
74 ROTATE_TO_AXIS
75 };
76
77 void setAutoRotateMode(AutoRotateMode mode);
78
79 AutoRotateMode getAutoRotateMode() const { return _autoRotateMode; }
80
81 /** Set the rotation axis for the AutoTransform's child nodes.
82 * Only utilized when _autoRotateMode==ROTATE_TO_AXIS. */
83 void setAxis(const Vec3& axis);
84 /** Get the rotation axis. */
85 inline const Vec3& getAxis() const { return _axis; }
86
87 /** This normal defines child Nodes' front face direction when unrotated. */
88 void setNormal(const Vec3& normal);
89 /** Get the front face direction normal. */
90 inline const Vec3& getNormal() const { return _normal; }
91
92 void setAutoScaleToScreen(bool autoScaleToScreen);
93 bool getAutoScaleToScreen() const { return _autoScaleToScreen; }
94
95 void setAutoScaleTransitionWidthRatio(float ratio) { _autoScaleTransitionWidthRatio = ratio; }
96 float getAutoScaleTransitionWidthRatio() const { return _autoScaleTransitionWidthRatio; }
97
98
99 virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor* nv) const;
100
101 virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor* nv) const;
102
103 protected :
104
105 virtual ~AutoTransform() {}
106
107 Vec3d _position;
108 Vec3d _pivotPoint;
109 double _autoUpdateEyeMovementTolerance;
110
111 AutoRotateMode _autoRotateMode;
112
113 bool _autoScaleToScreen;
114
115 mutable Quat _rotation;
116 mutable Vec3d _scale;
117
118 double _minimumScale;
119 double _maximumScale;
120 double _autoScaleTransitionWidthRatio;
121
122 osg::Matrixd computeMatrix(const osg::NodeVisitor* nv) const;
123
124 enum AxisAligned
125 {
126 AXIAL_ROT_X_AXIS=ROTATE_TO_AXIS+1,
127 AXIAL_ROT_Y_AXIS,
128 AXIAL_ROT_Z_AXIS,
129 CACHE_DIRTY
130 };
131
132 Vec3 _axis;
133 Vec3 _normal;
134
135 // used internally as cache of which what _axis is aligned to help
136 // decide which method of rotation to use.
137 int _cachedMode;
138 Vec3 _side;
139 void updateCache();
140
141};
142
143}
144
145#endif