ImpostorSprite 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_ImpostorSprite
  14. #define OSG_ImpostorSprite 1
  15. #include <osg/Vec2>
  16. #include <osg/Geometry>
  17. #include <osg/AlphaFunc>
  18. #include <osg/TexEnv>
  19. #include <osg/Texture2D>
  20. #include <osg/Camera>
  21. #include <osgSim/Export>
  22. namespace osgSim {
  23. class Impostor;
  24. class ImpostorSpriteManager;
  25. /** An ImposterSprite is a textured quad which is rendered in place of
  26. * 3D geometry. The ImposterSprite is generated by rendering the original
  27. * 3D geometry to a texture as an image cache. The ImpostorSprite is
  28. * automatically generated by the osgUtil::CullVisitor so it not
  29. * necessary to deal with it directly.
  30. */
  31. class OSGSIM_EXPORT ImpostorSprite : public osg::Geometry
  32. {
  33. public:
  34. ImpostorSprite();
  35. /** Clone an object of the same type as an ImpostorSprite. */
  36. virtual osg::Object* cloneType() const { return new ImpostorSprite(); }
  37. /** Clone on ImpostorSprite just returns a clone of type,
  38. * since it is not appropriate to share data of an ImpostorSprite.
  39. */
  40. virtual osg::Object* clone(const osg::CopyOp&) const { return new ImpostorSprite(); }
  41. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ImpostorSprite*>(obj)!=NULL; }
  42. virtual const char* libraryName() const { return "osgSim"; }
  43. virtual const char* className() const { return "ImpostorSprite"; }
  44. /** Set the parent, which must be an Impostor.
  45. * Unlike conventional Drawables, ImpostorSprites can only ever have
  46. * one parent.
  47. */
  48. void setParent(Impostor* parent) { _parent = parent; }
  49. /** Get the parent, which is an Impostor. */
  50. Impostor* getParent() { return _parent; }
  51. /** Get the const parent, which is an Impostor. */
  52. const Impostor* getParent() const { return _parent; }
  53. /** Set the eye point for when the ImpostorSprite was snapped. */
  54. inline void setStoredLocalEyePoint(const osg::Vec3& v) { _storedLocalEyePoint=v; }
  55. /** Get the eye point for when the ImpostorSprite was snapped. */
  56. inline const osg::Vec3& getStoredLocalEyePoint() const { return _storedLocalEyePoint; }
  57. /** Set the frame number for when the ImpostorSprite was last used in rendering. */
  58. inline void setLastFrameUsed(unsigned int frameNumber) { _lastFrameUsed = frameNumber; }
  59. /** Get the frame number for when the ImpostorSprite was last used in rendering. */
  60. inline unsigned int getLastFrameUsed() const { return _lastFrameUsed; }
  61. void dirty();
  62. /** Get the coordinates of the corners of the quad.
  63. * Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
  64. */
  65. inline osg::Vec3* getCoords() { return &(_coords->front()); }
  66. /** Get the const coordinates of the corners of the quad. */
  67. inline const osg::Vec3* getCoords() const { return &(_coords->front()); }
  68. /** Get the texture coordinates of the corners of the quad.
  69. * Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
  70. */
  71. inline osg::Vec2* getTexCoords() { return &(_texcoords->front()); }
  72. /** Get the const texture coordinates of the corners of the quad. */
  73. inline const osg::Vec2* getTexCoords() const { return &(_texcoords->front()); }
  74. /** Get the control coordinates of the corners of the quad.
  75. * The control coordinates are the corners of the quad projected
  76. * out onto the front face of bounding box which enclosed the impostor
  77. * geometry when it was pre-rendered into the impostor sprite's texture.
  78. * At the point of creation/or update of the impostor sprite the control
  79. * coords will lie on top of the corners of the quad in screen space - with a pixel error
  80. * of zero. Once the camera moves relative to the impostor sprite the
  81. * control coords will no longer lie on top of the corners of the quad in
  82. * screen space - a pixel error will have accumulated. This pixel error
  83. * can then be used to determine whether the impostor needs to be updated.
  84. * Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
  85. */
  86. inline osg::Vec3* getControlCoords() { return _controlcoords; }
  87. /** Get the const control coordinates of the corners of the quad. */
  88. inline const osg::Vec3* getControlCoords() const { return _controlcoords; }
  89. /** Calculate the pixel error value for passing in the ModelViewProjectionWindow transform,
  90. * which transform local coords into screen space.
  91. */
  92. float calcPixelError(const osg::Matrix& MVPW) const;
  93. void setTexture(osg::Texture2D* tex,int s,int t);
  94. osg::Texture2D* getTexture() { return _texture; }
  95. const osg::Texture2D* getTexture() const { return _texture; }
  96. int s() const { return _s; }
  97. int t() const { return _t; }
  98. /** Set the camera node to use for pre rendering the impostor sprite's texture.*/
  99. void setCamera(osg::Camera* camera) { _camera = camera; }
  100. /** Get the camera node to use for pre rendering the impostor sprite's texture.*/
  101. osg::Camera* getCamera() { return _camera.get(); }
  102. /** Get the const camera node to use for pre rendering the impostor sprite's texture.*/
  103. const osg::Camera* getCamera() const { return _camera.get(); }
  104. protected:
  105. ImpostorSprite(const ImpostorSprite&);
  106. ImpostorSprite& operator = (const ImpostorSprite&) { return *this;}
  107. virtual ~ImpostorSprite();
  108. void init();
  109. Impostor* _parent;
  110. friend class osgSim::ImpostorSpriteManager;
  111. // camera node for doing the pre rendering.
  112. osg::ref_ptr<osg::Camera> _camera;
  113. // support for a double linked list managed by the
  114. // ImposotorSpriteManager.
  115. ImpostorSpriteManager* _ism;
  116. ImpostorSprite* _previous;
  117. ImpostorSprite* _next;
  118. unsigned int _lastFrameUsed;
  119. osg::Vec3 _storedLocalEyePoint;
  120. osg::ref_ptr<osg::Vec3Array> _coords;
  121. osg::ref_ptr<osg::Vec2Array> _texcoords;
  122. osg::Vec3 _controlcoords[4];
  123. osg::Texture2D* _texture;
  124. int _s;
  125. int _t;
  126. };
  127. /** Helper class for managing the reuse of ImpostorSprite resources. */
  128. class OSGSIM_EXPORT ImpostorSpriteManager : public osg::Referenced
  129. {
  130. public:
  131. ImpostorSpriteManager();
  132. bool empty() const { return _first==0; }
  133. ImpostorSprite* first() { return _first; }
  134. ImpostorSprite* last() { return _last; }
  135. void push_back(ImpostorSprite* is);
  136. void remove(ImpostorSprite* is);
  137. ImpostorSprite* createOrReuseImpostorSprite(int s,int t,unsigned int frameNumber);
  138. osg::StateSet* createOrReuseStateSet();
  139. void reset();
  140. protected:
  141. ~ImpostorSpriteManager();
  142. osg::ref_ptr<osg::TexEnv> _texenv;
  143. osg::ref_ptr<osg::AlphaFunc> _alphafunc;
  144. ImpostorSprite* _first;
  145. ImpostorSprite* _last;
  146. typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSetList;
  147. StateSetList _stateSetList;
  148. unsigned int _reuseStateSetIndex;
  149. };
  150. }
  151. #endif