]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/ReaderWriterSPT.cxx
Random buildings - initial commit.
[simgear.git] / simgear / scene / tgdb / ReaderWriterSPT.cxx
1 // ReaderWriterSPT.cxx -- Provide a paged database for flightgear scenery.
2 //
3 // Copyright (C) 2010 - 2011  Mathias Froehlich
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19
20 #ifdef HAVE_CONFIG_H
21 #  include <simgear_config.h>
22 #endif
23
24 #include "ReaderWriterSPT.hxx"
25
26 #include <cassert>
27
28 #include <osg/CullFace>
29 #include <osg/PagedLOD>
30 #include <osg/Texture2D>
31
32 #include <osgDB/FileNameUtils>
33 #include <osgDB/ReadFile>
34
35 #include <simgear/scene/util/OsgMath.hxx>
36
37 #include "BucketBox.hxx"
38
39 namespace simgear {
40
41 ReaderWriterSPT::ReaderWriterSPT()
42 {
43     supportsExtension("spt", "SimGear paged terrain meta database.");
44 }
45
46 ReaderWriterSPT::~ReaderWriterSPT()
47 {
48 }
49
50 const char*
51 ReaderWriterSPT::className() const
52 {
53     return "simgear::ReaderWriterSPT";
54 }
55
56 osgDB::ReaderWriter::ReadResult
57 ReaderWriterSPT::readObject(const std::string& fileName, const osgDB::Options* options) const
58 {
59     // We get called with different extensions. To make sure search continues,
60     // we need to return FILE_NOT_HANDLED in this case.
61     if (osgDB::getLowerCaseFileExtension(fileName) != "spt")
62         return ReadResult(osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED);
63     if (fileName != "state.spt")
64         return ReadResult(osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND);
65
66     osg::StateSet* stateSet = new osg::StateSet;
67     stateSet->setAttributeAndModes(new osg::CullFace);
68
69     std::string imageFileName = options->getPluginStringData("SimGear::FG_WORLD_TEXTURE");
70     if (imageFileName.empty()) {
71         imageFileName = options->getPluginStringData("SimGear::FG_ROOT");
72         imageFileName = osgDB::concatPaths(imageFileName, "Textures");
73         imageFileName = osgDB::concatPaths(imageFileName, "Globe");
74         imageFileName = osgDB::concatPaths(imageFileName, "world.topo.bathy.200407.3x4096x2048.png");
75     }
76     if (osg::Image* image = osgDB::readImageFile(imageFileName, options)) {
77         osg::Texture2D* texture = new osg::Texture2D;
78         texture->setImage(image);
79         texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
80         texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP);
81         stateSet->setTextureAttributeAndModes(0, texture);
82     }
83
84     return stateSet;
85 }
86
87 osgDB::ReaderWriter::ReadResult
88 ReaderWriterSPT::readNode(const std::string& fileName, const osgDB::Options* options) const
89 {
90     // The file name without path and without the spt extension
91     std::string strippedFileName = osgDB::getStrippedName(fileName);
92     if (strippedFileName == "earth")
93         return createTree(BucketBox(0, -90, 360, 180), options, true);
94
95     std::stringstream ss(strippedFileName);
96     BucketBox bucketBox;
97     ss >> bucketBox;
98     if (ss.fail())
99         return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
100
101     BucketBox bucketBoxList[2];
102     unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList);
103     if (bucketBoxListSize == 0)
104         return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
105
106     if (bucketBoxListSize == 1)
107         return createTree(bucketBoxList[0], options, true);
108
109     assert(bucketBoxListSize == 2);
110     osg::ref_ptr<osg::Group> group = new osg::Group;
111     group->addChild(createTree(bucketBoxList[0], options, true));
112     group->addChild(createTree(bucketBoxList[1], options, true));
113     return group.release();
114 }
115
116 osg::Node*
117 ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const
118 {
119     if (bucketBox.getIsBucketSize()) {
120         return createPagedLOD(bucketBox, options);
121     } else if (!topLevel && bucketBox.getStartLevel() == 4) {
122         // We want an other level of indirection for paging
123         return createPagedLOD(bucketBox, options);
124     } else {
125         BucketBox bucketBoxList[100];
126         unsigned numTiles = bucketBox.getSubDivision(bucketBoxList, 100);
127         if (numTiles == 0)
128             return 0;
129
130         if (numTiles == 1)
131             return createTree(bucketBoxList[0], options, false);
132
133         osg::ref_ptr<osg::Group> group = new osg::Group;
134         for (unsigned i = 0; i < numTiles; ++i) {
135             osg::Node* node = createTree(bucketBoxList[i], options, false);
136             if (!node)
137                 continue;
138             group->addChild(node);
139         }
140         if (!group->getNumChildren())
141             return 0;
142
143         return group.release();
144     }
145 }
146
147 osg::Node*
148 ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const
149 {
150     osg::PagedLOD* pagedLOD = new osg::PagedLOD;
151
152     pagedLOD->setCenterMode(osg::PagedLOD::USER_DEFINED_CENTER);
153     SGSpheref sphere = bucketBox.getBoundingSphere();
154     pagedLOD->setCenter(toOsg(sphere.getCenter()));
155     pagedLOD->setRadius(sphere.getRadius());
156         
157     osg::ref_ptr<osgDB::Options> localOptions;
158     localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
159     pagedLOD->setDatabaseOptions(localOptions.get());
160         
161     float range;
162     if (bucketBox.getIsBucketSize())
163         range = 200e3;
164     else
165         range = 1e6;
166
167     // Add the static sea level textured shell
168     if (osg::Node* tile = createSeaLevelTile(bucketBox, options))
169         pagedLOD->addChild(tile, range, std::numeric_limits<float>::max());
170
171     // Add the paged file name that creates the subtrees on demand
172     if (bucketBox.getIsBucketSize()) {
173         std::string fileName;
174         fileName = bucketBox.getBucket().gen_index_str() + std::string(".stg");
175         pagedLOD->setFileName(pagedLOD->getNumChildren(), fileName);
176     } else {
177         std::stringstream ss;
178         ss << bucketBox << ".spt";
179         pagedLOD->setFileName(pagedLOD->getNumChildren(), ss.str());
180     }
181     pagedLOD->setRange(pagedLOD->getNumChildren(), 0.0, range);
182
183     return pagedLOD;
184 }
185
186 osg::Node*
187 ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const
188 {
189     if (options->getPluginStringData("SimGear::FG_EARTH") != "ON")
190         return 0;
191
192     osg::Vec3Array* vertices = new osg::Vec3Array;
193     osg::Vec3Array* normals = new osg::Vec3Array;
194     osg::Vec2Array* texCoords = new osg::Vec2Array;
195         
196     unsigned widthLevel = bucketBox.getWidthLevel();
197     unsigned heightLevel = bucketBox.getHeightLevel();
198
199     unsigned incx = bucketBox.getWidthIncrement(widthLevel + 2);
200     incx = std::min(incx, bucketBox.getSize(0));
201     for (unsigned i = 0; incx != 0;) {
202         unsigned incy = bucketBox.getHeightIncrement(heightLevel + 2);
203         incy = std::min(incy, bucketBox.getSize(1));
204         for (unsigned j = 0; incy != 0;) {
205             SGVec3f v[6], n[6];
206             SGVec2f t[6];
207             unsigned num = bucketBox.getTileTriangles(i, j, incx, incy, v, n, t);
208             for (unsigned k = 0; k < num; ++k) {
209                 vertices->push_back(toOsg(v[k]));
210                 normals->push_back(toOsg(n[k]));
211                 texCoords->push_back(toOsg(t[k]));
212             }
213             j += incy;
214             incy = std::min(incy, bucketBox.getSize(1) - j);
215         }
216         i += incx;
217         incx = std::min(incx, bucketBox.getSize(0) - i);
218     }
219         
220     osg::Vec4Array* colors = new osg::Vec4Array;
221     colors->push_back(osg::Vec4(1, 1, 1, 1));
222         
223     osg::Geometry* geometry = new osg::Geometry;
224     geometry->setVertexArray(vertices);
225     geometry->setNormalArray(normals);
226     geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
227     geometry->setColorArray(colors);
228     geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
229     geometry->setTexCoordArray(0, texCoords);
230         
231     geometry->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES, 0, vertices->size()));
232         
233     osg::Geode* geode = new osg::Geode;
234     geode->addDrawable(geometry);
235     geode->setStateSet(getLowLODStateSet(options));
236
237     return geode;
238 }
239
240 osg::StateSet*
241 ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const
242 {
243     osg::ref_ptr<osgDB::Options> localOptions;
244     localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
245     localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
246
247     osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get());
248     if (!dynamic_cast<osg::StateSet*>(object.get()))
249         return 0;
250
251     return static_cast<osg::StateSet*>(object.release());
252 }
253
254 } // namespace simgear
255