OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GeometryPool.h
Go to the documentation of this file.
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 
14 #ifndef OSGTERRAIN_GEOMETRYPOOL
15 #define OSGTERRAIN_GEOMETRYPOOL 1
16 
17 #include <osg/Geometry>
18 #include <osg/MatrixTransform>
19 #include <osg/Program>
20 
21 #include <OpenThreads/Mutex>
22 
23 #include <osgTerrain/TerrainTile>
24 
25 
26 namespace osgTerrain {
27 
29 
30 
32 {
33  public:
35 
37 
39 
40  void setVertexArray(osg::Array* array) { _vertexArray = array; }
41  osg::Array* getVertexArray() { return _vertexArray.get(); }
42  const osg::Array* getVertexArray() const { return _vertexArray.get(); }
43 
44  void setNormalArray(osg::Array* array) { _normalArray = array; }
45  osg::Array* getNormalArray() { return _normalArray.get(); }
46  const osg::Array* getNormalArray() const { return _normalArray.get(); }
47 
48  void setColorArray(osg::Array* array) { _colorArray = array; }
49  osg::Array* getColorArray() { return _colorArray.get(); }
50  const osg::Array* getColorArray() const { return _colorArray.get(); }
51 
52  void setTexCoordArray(osg::Array* array) { _texcoordArray = array; }
53  osg::Array* getTexCoordArray() { return _texcoordArray.get(); }
54  const osg::Array* getTexCoordArray() const { return _texcoordArray.get(); }
55 
56  void setDrawElements(osg::DrawElements* array) { _drawElements = array; }
57  osg::DrawElements* getDrawElements() { return _drawElements.get(); }
58  const osg::DrawElements* getDrawElements() const { return _drawElements.get(); }
59 
60 
61  typedef std::vector<unsigned int> VertexToHeightFieldMapping;
62 
63  void setVertexToHeightFieldMapping(const VertexToHeightFieldMapping& vthfm) { _vertexToHeightFieldMapping = vthfm; }
64 
65  VertexToHeightFieldMapping& getVertexToHeightFieldMapping() { return _vertexToHeightFieldMapping; }
66  const VertexToHeightFieldMapping& getVertexToHeightFieldMapping() const { return _vertexToHeightFieldMapping; }
67 
68  void compileGLObjects(osg::RenderInfo& renderInfo) const;
69 
70  void drawImplementation(osg::RenderInfo& renderInfo) const;
71 
72  void resizeGLObjectBuffers(unsigned int maxSize);
73  void releaseGLObjects(osg::State* state) const;
74 
75  virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
76  virtual void accept(osg::Drawable::AttributeFunctor&);
77 
78  virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
79  virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
80 
81  virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
82  virtual void accept(osg::PrimitiveFunctor&) const;
83 
84  virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
85  virtual void accept(osg::PrimitiveIndexFunctor&) const;
86 
87 protected:
88 
89  virtual ~SharedGeometry();
90 
96 
97  VertexToHeightFieldMapping _vertexToHeightFieldMapping;
98 };
99 
101 {
102  public:
103  GeometryPool();
104 
105  struct GeometryKey
106  {
107  GeometryKey(): sx(0.0), sy(0.0), y(0.0), nx(0), ny(0) {}
108 
109  bool operator < (const GeometryKey& rhs) const
110  {
111  if (sx<rhs.sx) return true;
112  if (sx>rhs.sx) return false;
113 
114  if (sx<rhs.sx) return true;
115  if (sx>rhs.sx) return false;
116 
117  if (y<rhs.y) return true;
118  if (y>rhs.y) return false;
119 
120  if (nx<rhs.nx) return true;
121  if (nx>rhs.nx) return false;
122 
123  return (ny<rhs.ny);
124  }
125 
126  double sx;
127  double sy;
128  double y;
129 
130  int nx;
131  int ny;
132  };
133 
134  typedef std::map< GeometryKey, osg::ref_ptr<SharedGeometry> > GeometryMap;
135 
136  virtual bool createKeyForTile(TerrainTile* tile, GeometryKey& key);
137 
139  {
142  CONTOUR_LAYER
143  };
144 
145  typedef std::vector<LayerType> LayerTypes;
146  typedef std::map<LayerTypes, osg::ref_ptr<osg::Program> > ProgramMap;
147 
148  osg::StateSet* getRootStateSetForTerrain(Terrain* terrain);
149 
150  virtual osg::ref_ptr<osg::Program> getOrCreateProgram(LayerTypes& layerTypes);
151 
152  virtual osg::ref_ptr<SharedGeometry> getOrCreateGeometry(osgTerrain::TerrainTile* tile);
153 
154  virtual osg::ref_ptr<osg::MatrixTransform> getTileSubgraph(osgTerrain::TerrainTile* tile);
155 
156  virtual void applyLayers(osgTerrain::TerrainTile* tile, osg::StateSet* stateset);
157 
158  protected:
159  virtual ~GeometryPool();
160 
162  GeometryMap _geometryMap;
163 
165  ProgramMap _programMap;
166 
169 };
170 
171 
173 {
174  public:
176 
178 
180 
181  void setHeightField(osg::HeightField* hf) { _heightField = hf; }
182  osg::HeightField* getHeightField() { return _heightField.get(); }
183  const osg::HeightField* getHeightField() const { return _heightField.get(); }
184 
185  void setGeometry(SharedGeometry* geom) { _geometry = geom; }
186  SharedGeometry* getGeometry() { return _geometry.get(); }
187  const SharedGeometry* getGeometry() const { return _geometry.get(); }
188 
189  void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
190  osg::Vec3Array* getVertices() { return _vertices.get(); }
191  const osg::Vec3Array* getVertices() const { return _vertices.get(); }
192 
193  virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
194  virtual void compileGLObjects(osg::RenderInfo& renderInfo) const;
195  virtual void resizeGLObjectBuffers(unsigned int maxSize);
196  virtual void releaseGLObjects(osg::State* state=0) const;
197 
198 
199  virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
200  virtual void accept(osg::Drawable::AttributeFunctor&);
201 
202  virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
203  virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
204 
205  virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
206  virtual void accept(osg::PrimitiveFunctor&) const;
207 
208  virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
209  virtual void accept(osg::PrimitiveIndexFunctor&) const;
210 
211 protected:
212 
213  virtual ~HeightFieldDrawable();
214 
218 };
219 
220 
221 
222 }
223 
224 #endif
osg::ref_ptr< osg::Array > _normalArray
Definition: GeometryPool.h:92
osg::HeightField * getHeightField()
Definition: GeometryPool.h:182
const osg::Array * getNormalArray() const
Definition: GeometryPool.h:46
OpenThreads::Mutex _programMapMutex
Definition: GeometryPool.h:164
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
void setVertices(osg::Vec3Array *vertices)
Definition: GeometryPool.h:189
VertexToHeightFieldMapping _vertexToHeightFieldMapping
Definition: GeometryPool.h:97
osg::ref_ptr< osg::StateSet > _rootStateSet
Definition: GeometryPool.h:167
osg::ref_ptr< osg::Vec3Array > _vertices
Definition: GeometryPool.h:217
void setColorArray(osg::Array *array)
Definition: GeometryPool.h:48
const osg::Vec3Array * getVertices() const
Definition: GeometryPool.h:191
virtual bool supports(const osg::Drawable::AttributeFunctor &) const
Definition: GeometryPool.h:199
virtual bool supports(const osg::Drawable::ConstAttributeFunctor &) const
Definition: GeometryPool.h:202
virtual bool supports(const osg::PrimitiveIndexFunctor &) const
Definition: GeometryPool.h:84
const osg::Array * getColorArray() const
Definition: GeometryPool.h:50
#define META_Node(library, name)
Definition: Node.h:59
osg::ref_ptr< osg::Array > _colorArray
Definition: GeometryPool.h:93
void setVertexArray(osg::Array *array)
Definition: GeometryPool.h:40
VertexToHeightFieldMapping & getVertexToHeightFieldMapping()
Definition: GeometryPool.h:65
virtual bool supports(const osg::PrimitiveFunctor &) const
Definition: GeometryPool.h:81
const VertexToHeightFieldMapping & getVertexToHeightFieldMapping() const
Definition: GeometryPool.h:66
const osg::Array * getTexCoordArray() const
Definition: GeometryPool.h:54
#define OSGTERRAIN_EXPORT
Definition: Export.h:39
osg::ref_ptr< osg::HeightField > _heightField
Definition: GeometryPool.h:215
osg::DrawElements * getDrawElements()
Definition: GeometryPool.h:57
void setTexCoordArray(osg::Array *array)
Definition: GeometryPool.h:52
OpenThreads::Mutex _geometryMapMutex
Definition: GeometryPool.h:161
osg::Array * getTexCoordArray()
Definition: GeometryPool.h:53
osg::Array * getNormalArray()
Definition: GeometryPool.h:45
osg::ref_ptr< osg::Array > _vertexArray
Definition: GeometryPool.h:91
OSGTERRAIN_EXPORT const osgTerrain::Locator * computeMasterLocator(const osgTerrain::TerrainTile *tile)
osg::ref_ptr< osg::Array > _texcoordArray
Definition: GeometryPool.h:94
const osg::DrawElements * getDrawElements() const
Definition: GeometryPool.h:58
void setDrawElements(osg::DrawElements *array)
Definition: GeometryPool.h:56
void setVertexToHeightFieldMapping(const VertexToHeightFieldMapping &vthfm)
Definition: GeometryPool.h:63
virtual bool supports(const osg::Drawable::AttributeFunctor &) const
Definition: GeometryPool.h:75
std::vector< LayerType > LayerTypes
Definition: GeometryPool.h:145
std::map< LayerTypes, osg::ref_ptr< osg::Program > > ProgramMap
Definition: GeometryPool.h:146
virtual bool supports(const osg::Drawable::ConstAttributeFunctor &) const
Definition: GeometryPool.h:78
const SharedGeometry * getGeometry() const
Definition: GeometryPool.h:187
std::map< GeometryKey, osg::ref_ptr< SharedGeometry > > GeometryMap
Definition: GeometryPool.h:134
osg::ref_ptr< SharedGeometry > _geometry
Definition: GeometryPool.h:216
void setNormalArray(osg::Array *array)
Definition: GeometryPool.h:44
virtual bool supports(const osg::PrimitiveIndexFunctor &) const
Definition: GeometryPool.h:208
osg::Vec3Array * getVertices()
Definition: GeometryPool.h:190
SharedGeometry * getGeometry()
Definition: GeometryPool.h:186
const osg::HeightField * getHeightField() const
Definition: GeometryPool.h:183
const osg::Array * getVertexArray() const
Definition: GeometryPool.h:42
osg::Array * getVertexArray()
Definition: GeometryPool.h:41
virtual bool supports(const osg::PrimitiveFunctor &) const
Definition: GeometryPool.h:205
osg::Array * getColorArray()
Definition: GeometryPool.h:49
std::vector< unsigned int > VertexToHeightFieldMapping
Definition: GeometryPool.h:61
void setGeometry(SharedGeometry *geom)
Definition: GeometryPool.h:185
osg::ref_ptr< osg::DrawElements > _drawElements
Definition: GeometryPool.h:95
void setHeightField(osg::HeightField *hf)
Definition: GeometryPool.h:181