1 // ReaderWriterSPT.cxx -- Provide a paged database for flightgear scenery.
3 // Copyright (C) 2010 - 2011 Mathias Froehlich
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.
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.
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.
21 # include <simgear_config.h>
24 #include "ReaderWriterSPT.hxx"
28 #include <osg/CullFace>
29 #include <osg/PagedLOD>
30 #include <osg/Texture2D>
32 #include <osgDB/FileNameUtils>
33 #include <osgDB/ReadFile>
35 #include <simgear/scene/util/OsgMath.hxx>
37 #include "BucketBox.hxx"
41 ReaderWriterSPT::ReaderWriterSPT()
43 supportsExtension("spt", "SimGear paged terrain meta database.");
46 ReaderWriterSPT::~ReaderWriterSPT()
51 ReaderWriterSPT::className() const
53 return "simgear::ReaderWriterSPT";
56 osgDB::ReaderWriter::ReadResult
57 ReaderWriterSPT::readObject(const std::string& fileName, const osgDB::Options* options) const
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);
66 osg::StateSet* stateSet = new osg::StateSet;
67 stateSet->setAttributeAndModes(new osg::CullFace);
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");
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);
87 osgDB::ReaderWriter::ReadResult
88 ReaderWriterSPT::readNode(const std::string& fileName, const osgDB::Options* options) const
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);
95 std::stringstream ss(strippedFileName);
99 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
101 BucketBox bucketBoxList[2];
102 unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList);
103 if (bucketBoxListSize == 0)
104 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
106 if (bucketBoxListSize == 1)
107 return createTree(bucketBoxList[0], options, true);
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();
117 ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const
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);
125 BucketBox bucketBoxList[100];
126 unsigned numTiles = bucketBox.getSubDivision(bucketBoxList, 100);
131 return createTree(bucketBoxList[0], options, false);
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);
138 group->addChild(node);
140 if (!group->getNumChildren())
143 return group.release();
148 ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const
150 osg::PagedLOD* pagedLOD = new osg::PagedLOD;
152 pagedLOD->setCenterMode(osg::PagedLOD::USER_DEFINED_CENTER);
153 SGSpheref sphere = bucketBox.getBoundingSphere();
154 pagedLOD->setCenter(toOsg(sphere.getCenter()));
155 pagedLOD->setRadius(sphere.getRadius());
157 osg::ref_ptr<osgDB::Options> localOptions;
158 localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
159 pagedLOD->setDatabaseOptions(localOptions.get());
162 if (bucketBox.getIsBucketSize())
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());
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);
177 std::stringstream ss;
178 ss << bucketBox << ".spt";
179 pagedLOD->setFileName(pagedLOD->getNumChildren(), ss.str());
181 pagedLOD->setRange(pagedLOD->getNumChildren(), 0.0, range);
187 ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const
189 if (options->getPluginStringData("SimGear::FG_EARTH") != "ON")
192 osg::Vec3Array* vertices = new osg::Vec3Array;
193 osg::Vec3Array* normals = new osg::Vec3Array;
194 osg::Vec2Array* texCoords = new osg::Vec2Array;
196 unsigned widthLevel = bucketBox.getWidthLevel();
197 unsigned heightLevel = bucketBox.getHeightLevel();
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;) {
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]));
214 incy = std::min(incy, bucketBox.getSize(1) - j);
217 incx = std::min(incx, bucketBox.getSize(0) - i);
220 osg::Vec4Array* colors = new osg::Vec4Array;
221 colors->push_back(osg::Vec4(1, 1, 1, 1));
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);
231 geometry->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES, 0, vertices->size()));
233 osg::Geode* geode = new osg::Geode;
234 geode->addDrawable(geometry);
235 geode->setStateSet(getLowLODStateSet(options));
241 ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const
243 osg::ref_ptr<osgDB::Options> localOptions;
244 localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
245 localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
247 osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get());
248 if (!dynamic_cast<osg::StateSet*>(object.get()))
251 return static_cast<osg::StateSet*>(object.release());
254 } // namespace simgear