openscenegraph
Transform
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_TRANSFORM
15#define OSG_TRANSFORM 1
16
17#include <osg/Group>
18#include <osg/Matrix>
19
20#ifndef GL_RESCALE_NORMAL
21#define GL_RESCALE_NORMAL 0x803A
22#endif
23
24#ifndef GL_NORMALIZE
25#define GL_NORMALIZE 0x0BA1
26#endif
27
28namespace osg {
29
30
31
32/** Compute the matrix which transforms objects in local coords to world coords,
33 * by accumulating the Transform local to world matrices along the specified node path.
34*/
35extern OSG_EXPORT Matrix computeLocalToWorld(const NodePath& nodePath, bool ignoreCameras = true);
36
37/** Compute the matrix which transforms objects in world coords to local coords,
38 * by accumulating the Transform world to local matrices along the specified node path.
39*/
40extern OSG_EXPORT Matrix computeWorldToLocal(const NodePath& nodePath, bool ignoreCameras = true);
41
42/** Compute the matrix which transforms objects in local coords to eye coords,
43 * by accumulating the Transform local to world matrices along the specified node path
44 * and multiplying by the supplied initial camera modelview.
45*/
46extern OSG_EXPORT Matrix computeLocalToEye(const Matrix& modelview, const NodePath& nodePath, bool ignoreCameras = true);
47
48/** Compute the matrix which transforms objects in eye coords to local coords,
49 * by accumulating the Transform world to local matrices along the specified node path
50 * and multiplying by the inverse of the supplied initial camera modelview.
51*/
52extern OSG_EXPORT Matrix computeEyeToLocal(const Matrix& modelview, const NodePath& nodePath, bool ignoreCameras = true);
53
54
55/** A Transform is a group node for which all children are transformed by
56 * a 4x4 matrix. It is often used for positioning objects within a scene,
57 * producing trackball functionality or for animation.
58 *
59 * Transform itself does not provide set/get functions, only the interface
60 * for defining what the 4x4 transformation is. Subclasses, such as
61 * MatrixTransform and PositionAttitudeTransform support the use of an
62 * osg::Matrix or a Vec3 and Quat respectively.
63 *
64 * Note: If the transformation matrix scales the subgraph then the normals
65 * of the underlying geometry will need to be renormalized to be unit
66 * vectors once more. This can be done transparently through OpenGL's
67 * use of either GL_NORMALIZE and GL_RESCALE_NORMAL modes. For further
68 * background reading see the glNormalize documentation in the OpenGL
69 * Reference Guide (the blue book). To enable it in the OSG, you simply
70 * need to attach a local osg::StateSet to the osg::Transform, and set
71 * the appropriate mode to ON via
72 * stateset->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
73*/
74class OSG_EXPORT Transform : public Group
75{
76 public :
77
78 Transform();
79
80 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
81 Transform(const Transform&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
82
83 META_Node(osg, Transform);
84
85 virtual Transform* asTransform() { return this; }
86 virtual const Transform* asTransform() const { return this; }
87
88 virtual MatrixTransform* asMatrixTransform() { return 0; }
89 virtual const MatrixTransform* asMatrixTransform() const { return 0; }
90
91 virtual PositionAttitudeTransform* asPositionAttitudeTransform() { return 0; }
92 virtual const PositionAttitudeTransform* asPositionAttitudeTransform() const { return 0; }
93
94 virtual AutoTransform* asAutoTransform() { return 0; }
95 virtual const AutoTransform* asAutoTransform() const { return 0; }
96
97 enum ReferenceFrame
98 {
99 RELATIVE_RF,
100 ABSOLUTE_RF,
101 ABSOLUTE_RF_INHERIT_VIEWPOINT
102 };
103
104 /** Set the transform's ReferenceFrame, either to be relative to its
105 * parent reference frame, or relative to an absolute coordinate
106 * frame. RELATIVE_RF is the default.
107 * Note: Setting the ReferenceFrame to be ABSOLUTE_RF will
108 * also set the CullingActive flag on the transform, and hence all
109 * of its parents, to false, thereby disabling culling of it and
110 * all its parents. This is necessary to prevent inappropriate
111 * culling, but may impact cull times if the absolute transform is
112 * deep in the scene graph. It is therefore recommended to only use
113 * absolute Transforms at the top of the scene, for such things as
114 * heads up displays.
115 * ABSOLUTE_RF_INHERIT_VIEWPOINT is the same as ABSOLUTE_RF except it
116 * adds the ability to use the parents view points position in world coordinates
117 * as its local viewpoint in the new coordinates frame. This is useful for
118 * Render to texture Cameras that wish to use the main views LOD range computation
119 * (which uses the viewpoint rather than the eye point) rather than use the local
120 * eye point defined by the this Transforms' absolute view matrix.
121 */
122 void setReferenceFrame(ReferenceFrame rf);
123
124 ReferenceFrame getReferenceFrame() const { return _referenceFrame; }
125
126 virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
127 {
128 if (_referenceFrame==RELATIVE_RF)
129 {
130 return false;
131 }
132 else // absolute
133 {
134 matrix.makeIdentity();
135 return true;
136 }
137 }
138
139 virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
140 {
141 if (_referenceFrame==RELATIVE_RF)
142 {
143 return false;
144 }
145 else // absolute
146 {
147 matrix.makeIdentity();
148 return true;
149 }
150 }
151
152 /** Overrides Group's computeBound.
153 * There is no need to override in subclasses from osg::Transform
154 * since this computeBound() uses the underlying matrix (calling
155 * computeMatrix if required).
156 */
157 virtual BoundingSphere computeBound() const;
158
159 protected :
160
161 virtual ~Transform();
162
163
164 ReferenceFrame _referenceFrame;
165
166};
167
168}
169
170#endif