openscenegraph
GeometryPool
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
26namespace osgTerrain {
27
28extern OSGTERRAIN_EXPORT const osgTerrain::Locator* computeMasterLocator(const osgTerrain::TerrainTile* tile);
29
30
31class OSGTERRAIN_EXPORT SharedGeometry : public osg::Drawable
32{
33 public:
34 SharedGeometry();
35
36 SharedGeometry(const SharedGeometry&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
37
38 META_Node(osgTerrain, SharedGeometry);
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
69 osg::VertexArrayState* createVertexArrayStateImplementation(osg::RenderInfo& renderInfo) const;
70
71 void compileGLObjects(osg::RenderInfo& renderInfo) const;
72
73 void drawImplementation(osg::RenderInfo& renderInfo) const;
74
75 void resizeGLObjectBuffers(unsigned int maxSize);
76 void releaseGLObjects(osg::State* state) const;
77
78 virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
79 virtual void accept(osg::Drawable::AttributeFunctor&);
80
81 virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
82 virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
83
84 virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
85 virtual void accept(osg::PrimitiveFunctor&) const;
86
87 virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
88 virtual void accept(osg::PrimitiveIndexFunctor&) const;
89
90protected:
91
92 virtual ~SharedGeometry();
93
94 osg::ref_ptr<osg::Array> _vertexArray;
95 osg::ref_ptr<osg::Array> _normalArray;
96 osg::ref_ptr<osg::Array> _colorArray;
97 osg::ref_ptr<osg::Array> _texcoordArray;
98 osg::ref_ptr<osg::DrawElements> _drawElements;
99
100 VertexToHeightFieldMapping _vertexToHeightFieldMapping;
101};
102
103class OSGTERRAIN_EXPORT GeometryPool : public osg::Referenced
104{
105 public:
106 GeometryPool();
107
108 struct GeometryKey
109 {
110 GeometryKey(): sx(0.0), sy(0.0), y(0.0), nx(0), ny(0) {}
111
112 bool operator < (const GeometryKey& rhs) const
113 {
114 if (sx<rhs.sx) return true;
115 if (sx>rhs.sx) return false;
116
117 if (sx<rhs.sx) return true;
118 if (sx>rhs.sx) return false;
119
120 if (y<rhs.y) return true;
121 if (y>rhs.y) return false;
122
123 if (nx<rhs.nx) return true;
124 if (nx>rhs.nx) return false;
125
126 return (ny<rhs.ny);
127 }
128
129 double sx;
130 double sy;
131 double y;
132
133 int nx;
134 int ny;
135 };
136
137 typedef std::map< GeometryKey, osg::ref_ptr<SharedGeometry> > GeometryMap;
138
139 virtual bool createKeyForTile(TerrainTile* tile, GeometryKey& key);
140
141 enum LayerType
142 {
143 HEIGHTFIELD_LAYER,
144 COLOR_LAYER,
145 CONTOUR_LAYER
146 };
147
148 typedef std::vector<LayerType> LayerTypes;
149 typedef std::map<LayerTypes, osg::ref_ptr<osg::Program> > ProgramMap;
150
151 osg::StateSet* getRootStateSetForTerrain(Terrain* terrain);
152
153 virtual osg::ref_ptr<osg::Program> getOrCreateProgram(LayerTypes& layerTypes);
154
155 virtual osg::ref_ptr<SharedGeometry> getOrCreateGeometry(osgTerrain::TerrainTile* tile);
156
157 virtual osg::ref_ptr<osg::MatrixTransform> getTileSubgraph(osgTerrain::TerrainTile* tile);
158
159 virtual void applyLayers(osgTerrain::TerrainTile* tile, osg::StateSet* stateset);
160
161 protected:
162 virtual ~GeometryPool();
163
164 OpenThreads::Mutex _geometryMapMutex;
165 GeometryMap _geometryMap;
166
167 OpenThreads::Mutex _programMapMutex;
168 ProgramMap _programMap;
169
170 osg::ref_ptr<osg::StateSet> _rootStateSet;
171 bool _rootStateSetAssigned;
172};
173
174
175class OSGTERRAIN_EXPORT HeightFieldDrawable : public osg::Drawable
176{
177 public:
178 HeightFieldDrawable();
179
180 HeightFieldDrawable(const HeightFieldDrawable&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
181
182 META_Node(osgTerrain, HeightFieldDrawable);
183
184 void setHeightField(osg::HeightField* hf) { _heightField = hf; }
185 osg::HeightField* getHeightField() { return _heightField.get(); }
186 const osg::HeightField* getHeightField() const { return _heightField.get(); }
187
188 void setGeometry(SharedGeometry* geom) { _geometry = geom; }
189 SharedGeometry* getGeometry() { return _geometry.get(); }
190 const SharedGeometry* getGeometry() const { return _geometry.get(); }
191
192 void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
193 osg::Vec3Array* getVertices() { return _vertices.get(); }
194 const osg::Vec3Array* getVertices() const { return _vertices.get(); }
195
196 virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
197 virtual void compileGLObjects(osg::RenderInfo& renderInfo) const;
198 virtual void resizeGLObjectBuffers(unsigned int maxSize);
199 virtual void releaseGLObjects(osg::State* state=0) const;
200
201
202 virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
203 virtual void accept(osg::Drawable::AttributeFunctor&);
204
205 virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
206 virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
207
208 virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
209 virtual void accept(osg::PrimitiveFunctor&) const;
210
211 virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
212 virtual void accept(osg::PrimitiveIndexFunctor&) const;
213
214protected:
215
216 virtual ~HeightFieldDrawable();
217
218 osg::ref_ptr<osg::HeightField> _heightField;
219 osg::ref_ptr<SharedGeometry> _geometry;
220 osg::ref_ptr<osg::Vec3Array> _vertices;
221};
222
223
224
225}
226
227#endif