Billboard 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_BILLBOARD
  14. #define OSG_BILLBOARD 1
  15. #include <osg/Matrix>
  16. #include <osg/Geode>
  17. namespace osg {
  18. /** Billboard is a derived form of Geode that orients its osg::Drawable
  19. * children to face the eye point. Typical uses include trees and
  20. * particle explosions.
  21. */
  22. class OSG_EXPORT Billboard : public Geode
  23. {
  24. public:
  25. enum Mode {
  26. POINT_ROT_EYE,
  27. POINT_ROT_WORLD,
  28. AXIAL_ROT
  29. };
  30. Billboard();
  31. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  32. Billboard(const Billboard&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  33. META_Node(osg, Billboard);
  34. /** Set the billboard rotation mode. */
  35. void setMode(Mode mode);
  36. /** Get the billboard rotation mode. */
  37. inline Mode getMode() const { return _mode; }
  38. /** Set the rotation axis for the billboard's child Drawables.
  39. * Only utilized when mode==AXIAL_ROT. */
  40. void setAxis(const Vec3& axis);
  41. /** Get the rotation axis. */
  42. inline const Vec3& getAxis() const { return _axis; }
  43. /** This normal defines child Drawables' front face direction when unrotated. */
  44. void setNormal(const Vec3& normal);
  45. /** Get the front face direction normal. */
  46. inline const Vec3& getNormal() const { return _normal; }
  47. /** Set the specified child Drawable's position. */
  48. inline void setPosition(unsigned int i,const Vec3& pos) { _positionList[i] = pos; }
  49. /** Get the specified child Drawable's position. */
  50. inline const Vec3& getPosition(unsigned int i) const { return _positionList[i]; }
  51. /** Type definition for pivot point position list. */
  52. typedef std::vector<Vec3> PositionList;
  53. /** Set the list of pivot point positions. */
  54. inline void setPositionList(PositionList& pl) { _positionList=pl; }
  55. /** Get the list of pivot point positions. */
  56. inline PositionList& getPositionList() { return _positionList; }
  57. /** Get a const list of pivot point positions. */
  58. inline const PositionList& getPositionList() const { return _positionList; }
  59. /** Add a Drawable with a default position of Vec3(0,0,0).
  60. * Call the base-class Geode::addDrawble() to add the given Drawable
  61. * gset as a child. If Geode::addDrawable() returns true, add the
  62. * default position to the pivot point position list and return true.
  63. * Otherwise, return false. */
  64. virtual bool addDrawable( Drawable *gset );
  65. /** Add a Drawable with a specified position.
  66. * Call the base-class Geode::addDrawble() to add the given Drawable
  67. * gset as a child. If Geode::addDrawable() returns true, add the
  68. * given position pos to the pivot point position list and return true.
  69. * Otherwise, return false. */
  70. virtual bool addDrawable(Drawable *gset,const Vec3& pos);
  71. /** Remove a Drawable and its associated position.
  72. * If gset is a child, remove it, decrement its reference count,
  73. * remove its pivot point position. and return true.
  74. * Otherwise, return false. */
  75. virtual bool removeDrawable( Drawable *gset );
  76. bool computeMatrix(Matrix& modelview, const Vec3& eye_local, const Vec3& pos_local) const;
  77. virtual BoundingSphere computeBound() const;
  78. protected:
  79. virtual ~Billboard();
  80. enum AxisAligned
  81. {
  82. AXIAL_ROT_X_AXIS=AXIAL_ROT+1,
  83. AXIAL_ROT_Y_AXIS,
  84. AXIAL_ROT_Z_AXIS,
  85. POINT_ROT_WORLD_Z_AXIS,
  86. CACHE_DIRTY
  87. };
  88. Mode _mode;
  89. Vec3 _axis;
  90. Vec3 _normal;
  91. Matrix _rotateNormalToZAxis;
  92. PositionList _positionList;
  93. // used internally as cache of which what _axis is aligned to help
  94. // decide which method of rotation to use.
  95. int _cachedMode;
  96. Vec3 _side;
  97. void updateCache();
  98. };
  99. }
  100. #endif