Transform 6.6 KB

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