GeometryPool 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 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 OSGTERRAIN_GEOMETRYPOOL
  14. #define OSGTERRAIN_GEOMETRYPOOL 1
  15. #include <osg/Geometry>
  16. #include <osg/MatrixTransform>
  17. #include <osg/Program>
  18. #include <OpenThreads/Mutex>
  19. #include <osgTerrain/TerrainTile>
  20. namespace osgTerrain {
  21. extern OSGTERRAIN_EXPORT const osgTerrain::Locator* computeMasterLocator(const osgTerrain::TerrainTile* tile);
  22. class OSGTERRAIN_EXPORT SharedGeometry : public osg::Drawable
  23. {
  24. public:
  25. SharedGeometry();
  26. SharedGeometry(const SharedGeometry&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  27. META_Node(osgTerrain, SharedGeometry);
  28. void setVertexArray(osg::Array* array) { _vertexArray = array; }
  29. osg::Array* getVertexArray() { return _vertexArray.get(); }
  30. const osg::Array* getVertexArray() const { return _vertexArray.get(); }
  31. void setNormalArray(osg::Array* array) { _normalArray = array; }
  32. osg::Array* getNormalArray() { return _normalArray.get(); }
  33. const osg::Array* getNormalArray() const { return _normalArray.get(); }
  34. void setColorArray(osg::Array* array) { _colorArray = array; }
  35. osg::Array* getColorArray() { return _colorArray.get(); }
  36. const osg::Array* getColorArray() const { return _colorArray.get(); }
  37. void setTexCoordArray(osg::Array* array) { _texcoordArray = array; }
  38. osg::Array* getTexCoordArray() { return _texcoordArray.get(); }
  39. const osg::Array* getTexCoordArray() const { return _texcoordArray.get(); }
  40. void setDrawElements(osg::DrawElements* array) { _drawElements = array; }
  41. osg::DrawElements* getDrawElements() { return _drawElements.get(); }
  42. const osg::DrawElements* getDrawElements() const { return _drawElements.get(); }
  43. typedef std::vector<unsigned int> VertexToHeightFieldMapping;
  44. void setVertexToHeightFieldMapping(const VertexToHeightFieldMapping& vthfm) { _vertexToHeightFieldMapping = vthfm; }
  45. VertexToHeightFieldMapping& getVertexToHeightFieldMapping() { return _vertexToHeightFieldMapping; }
  46. const VertexToHeightFieldMapping& getVertexToHeightFieldMapping() const { return _vertexToHeightFieldMapping; }
  47. osg::VertexArrayState* createVertexArrayStateImplementation(osg::RenderInfo& renderInfo) const;
  48. void compileGLObjects(osg::RenderInfo& renderInfo) const;
  49. void drawImplementation(osg::RenderInfo& renderInfo) const;
  50. void resizeGLObjectBuffers(unsigned int maxSize);
  51. void releaseGLObjects(osg::State* state) const;
  52. virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
  53. virtual void accept(osg::Drawable::AttributeFunctor&);
  54. virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
  55. virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
  56. virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
  57. virtual void accept(osg::PrimitiveFunctor&) const;
  58. virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
  59. virtual void accept(osg::PrimitiveIndexFunctor&) const;
  60. protected:
  61. virtual ~SharedGeometry();
  62. osg::ref_ptr<osg::Array> _vertexArray;
  63. osg::ref_ptr<osg::Array> _normalArray;
  64. osg::ref_ptr<osg::Array> _colorArray;
  65. osg::ref_ptr<osg::Array> _texcoordArray;
  66. osg::ref_ptr<osg::DrawElements> _drawElements;
  67. VertexToHeightFieldMapping _vertexToHeightFieldMapping;
  68. };
  69. class OSGTERRAIN_EXPORT GeometryPool : public osg::Referenced
  70. {
  71. public:
  72. GeometryPool();
  73. struct GeometryKey
  74. {
  75. GeometryKey(): sx(0.0), sy(0.0), y(0.0), nx(0), ny(0) {}
  76. bool operator < (const GeometryKey& rhs) const
  77. {
  78. if (sx<rhs.sx) return true;
  79. if (sx>rhs.sx) return false;
  80. if (sx<rhs.sx) return true;
  81. if (sx>rhs.sx) return false;
  82. if (y<rhs.y) return true;
  83. if (y>rhs.y) return false;
  84. if (nx<rhs.nx) return true;
  85. if (nx>rhs.nx) return false;
  86. return (ny<rhs.ny);
  87. }
  88. double sx;
  89. double sy;
  90. double y;
  91. int nx;
  92. int ny;
  93. };
  94. typedef std::map< GeometryKey, osg::ref_ptr<SharedGeometry> > GeometryMap;
  95. virtual bool createKeyForTile(TerrainTile* tile, GeometryKey& key);
  96. enum LayerType
  97. {
  98. HEIGHTFIELD_LAYER,
  99. COLOR_LAYER,
  100. CONTOUR_LAYER
  101. };
  102. typedef std::vector<LayerType> LayerTypes;
  103. typedef std::map<LayerTypes, osg::ref_ptr<osg::Program> > ProgramMap;
  104. osg::StateSet* getRootStateSetForTerrain(Terrain* terrain);
  105. virtual osg::ref_ptr<osg::Program> getOrCreateProgram(LayerTypes& layerTypes);
  106. virtual osg::ref_ptr<SharedGeometry> getOrCreateGeometry(osgTerrain::TerrainTile* tile);
  107. virtual osg::ref_ptr<osg::MatrixTransform> getTileSubgraph(osgTerrain::TerrainTile* tile);
  108. virtual void applyLayers(osgTerrain::TerrainTile* tile, osg::StateSet* stateset);
  109. protected:
  110. virtual ~GeometryPool();
  111. OpenThreads::Mutex _geometryMapMutex;
  112. GeometryMap _geometryMap;
  113. OpenThreads::Mutex _programMapMutex;
  114. ProgramMap _programMap;
  115. osg::ref_ptr<osg::StateSet> _rootStateSet;
  116. bool _rootStateSetAssigned;
  117. };
  118. class OSGTERRAIN_EXPORT HeightFieldDrawable : public osg::Drawable
  119. {
  120. public:
  121. HeightFieldDrawable();
  122. HeightFieldDrawable(const HeightFieldDrawable&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  123. META_Node(osgTerrain, HeightFieldDrawable);
  124. void setHeightField(osg::HeightField* hf) { _heightField = hf; }
  125. osg::HeightField* getHeightField() { return _heightField.get(); }
  126. const osg::HeightField* getHeightField() const { return _heightField.get(); }
  127. void setGeometry(SharedGeometry* geom) { _geometry = geom; }
  128. SharedGeometry* getGeometry() { return _geometry.get(); }
  129. const SharedGeometry* getGeometry() const { return _geometry.get(); }
  130. void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
  131. osg::Vec3Array* getVertices() { return _vertices.get(); }
  132. const osg::Vec3Array* getVertices() const { return _vertices.get(); }
  133. virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
  134. virtual void compileGLObjects(osg::RenderInfo& renderInfo) const;
  135. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  136. virtual void releaseGLObjects(osg::State* state=0) const;
  137. virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
  138. virtual void accept(osg::Drawable::AttributeFunctor&);
  139. virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
  140. virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
  141. virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
  142. virtual void accept(osg::PrimitiveFunctor&) const;
  143. virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
  144. virtual void accept(osg::PrimitiveIndexFunctor&) const;
  145. protected:
  146. virtual ~HeightFieldDrawable();
  147. osg::ref_ptr<osg::HeightField> _heightField;
  148. osg::ref_ptr<SharedGeometry> _geometry;
  149. osg::ref_ptr<osg::Vec3Array> _vertices;
  150. };
  151. }
  152. #endif