| 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 | |
|---|
| 28 | namespace 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 | */ |
|---|
| 35 | extern 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 | */ |
|---|
| 40 | extern 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 | */ |
|---|
| 46 | extern 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 | */ |
|---|
| 52 | extern 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 osg::Vec3/osg::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 | */ |
|---|
| 74 | class 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 | enum ReferenceFrame |
|---|
| 95 | { |
|---|
| 96 | RELATIVE_RF, |
|---|
| 97 | ABSOLUTE_RF, |
|---|
| 98 | ABSOLUTE_RF_INHERIT_VIEWPOINT |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | /** Set the transform's ReferenceFrame, either to be relative to its |
|---|
| 102 | * parent reference frame, or relative to an absolute coordinate |
|---|
| 103 | * frame. RELATIVE_RF is the default. |
|---|
| 104 | * Note: Setting the ReferenceFrame to be ABSOLUTE_RF will |
|---|
| 105 | * also set the CullingActive flag on the transform, and hence all |
|---|
| 106 | * of its parents, to false, thereby disabling culling of it and |
|---|
| 107 | * all its parents. This is necessary to prevent inappropriate |
|---|
| 108 | * culling, but may impact cull times if the absolute transform is |
|---|
| 109 | * deep in the scene graph. It is therefore recommended to only use |
|---|
| 110 | * absolute Transforms at the top of the scene, for such things as |
|---|
| 111 | * heads up displays. |
|---|
| 112 | * ABSOLUTE_RF_INHERIT_VIEWPOINT is the same as ABSOLUTE_RF except it |
|---|
| 113 | * adds the ability to use the parents view points position in world coordinates |
|---|
| 114 | * as its local viewpoint in the new coordinates frame. This is useful for |
|---|
| 115 | * Render to texture Cameras that wish to use the main views LOD range computation |
|---|
| 116 | * (which uses the viewpoint rather than the eye point) rather than use the local |
|---|
| 117 | * eye point defined by the this Transforms' absolute view matrix. |
|---|
| 118 | */ |
|---|
| 119 | void setReferenceFrame(ReferenceFrame rf); |
|---|
| 120 | |
|---|
| 121 | ReferenceFrame getReferenceFrame() const { return _referenceFrame; } |
|---|
| 122 | |
|---|
| 123 | virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const |
|---|
| 124 | { |
|---|
| 125 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 126 | { |
|---|
| 127 | return false; |
|---|
| 128 | } |
|---|
| 129 | else // absolute |
|---|
| 130 | { |
|---|
| 131 | matrix.makeIdentity(); |
|---|
| 132 | return true; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const |
|---|
| 137 | { |
|---|
| 138 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 139 | { |
|---|
| 140 | return false; |
|---|
| 141 | } |
|---|
| 142 | else // absolute |
|---|
| 143 | { |
|---|
| 144 | matrix.makeIdentity(); |
|---|
| 145 | return true; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** Overrides Group's computeBound. |
|---|
| 150 | * There is no need to override in subclasses from osg::Transform |
|---|
| 151 | * since this computeBound() uses the underlying matrix (calling |
|---|
| 152 | * computeMatrix if required). |
|---|
| 153 | */ |
|---|
| 154 | virtual BoundingSphere computeBound() const; |
|---|
| 155 | |
|---|
| 156 | protected : |
|---|
| 157 | |
|---|
| 158 | virtual ~Transform(); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | ReferenceFrame _referenceFrame; |
|---|
| 162 | |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | #endif |
|---|