1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
15#define OSG_TRANSFORM 1
20#ifndef GL_RESCALE_NORMAL
21#define GL_RESCALE_NORMAL 0x803A
25#define GL_NORMALIZE 0x0BA1
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.
35extern OSG_EXPORT Matrix computeLocalToWorld(const NodePath& nodePath, bool ignoreCameras = true);
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.
40extern OSG_EXPORT Matrix computeWorldToLocal(const NodePath& nodePath, bool ignoreCameras = true);
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.
46extern OSG_EXPORT Matrix computeLocalToEye(const Matrix& modelview, const NodePath& nodePath, bool ignoreCameras = true);
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.
52extern OSG_EXPORT Matrix computeEyeToLocal(const Matrix& modelview, const NodePath& nodePath, bool ignoreCameras = true);
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.
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.
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);
74class OSG_EXPORT Transform : public Group
80 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
81 Transform(const Transform&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
83 META_Node(osg, Transform);
85 virtual Transform* asTransform() { return this; }
86 virtual const Transform* asTransform() const { return this; }
88 virtual MatrixTransform* asMatrixTransform() { return 0; }
89 virtual const MatrixTransform* asMatrixTransform() const { return 0; }
91 virtual PositionAttitudeTransform* asPositionAttitudeTransform() { return 0; }
92 virtual const PositionAttitudeTransform* asPositionAttitudeTransform() const { return 0; }
94 virtual AutoTransform* asAutoTransform() { return 0; }
95 virtual const AutoTransform* asAutoTransform() const { return 0; }
101 ABSOLUTE_RF_INHERIT_VIEWPOINT
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
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.
122 void setReferenceFrame(ReferenceFrame rf);
124 ReferenceFrame getReferenceFrame() const { return _referenceFrame; }
126 virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
128 if (_referenceFrame==RELATIVE_RF)
134 matrix.makeIdentity();
139 virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
141 if (_referenceFrame==RELATIVE_RF)
147 matrix.makeIdentity();
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).
157 virtual BoundingSphere computeBound() const;
161 virtual ~Transform();
164 ReferenceFrame _referenceFrame;