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 "BucketBox.hxx"
39 ReaderWriterSPT::ReaderWriterSPT()
41 supportsExtension("spt", "SimGear paged terrain meta database.");
44 ReaderWriterSPT::~ReaderWriterSPT()
49 ReaderWriterSPT::className() const
51 return "simgear::ReaderWriterSPT";
54 osgDB::ReaderWriter::ReadResult
55 ReaderWriterSPT::readObject(const std::string& fileName, const osgDB::Options* options) const
57 // We get called with different extensions. To make sure search continues,
58 // we need to return FILE_NOT_HANDLED in this case.
59 if (osgDB::getLowerCaseFileExtension(fileName) != "spt")
60 return ReadResult(osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED);
61 if (fileName != "state.spt")
62 return ReadResult(osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND);
64 osg::StateSet* stateSet = new osg::StateSet;
65 stateSet->setAttributeAndModes(new osg::CullFace);
67 std::string imageFileName = options->getPluginStringData("SimGear::FG_WORLD_TEXTURE");
68 if (osg::Image* image = osgDB::readImageFile(imageFileName, options)) {
69 osg::Texture2D* texture = new osg::Texture2D;
70 texture->setImage(image);
71 texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
72 texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP);
73 stateSet->setTextureAttributeAndModes(0, texture);
79 osgDB::ReaderWriter::ReadResult
80 ReaderWriterSPT::readNode(const std::string& fileName, const osgDB::Options* options) const
82 // The file name without path and without the spt extension
83 std::string strippedFileName = osgDB::getStrippedName(fileName);
84 if (strippedFileName == "earth")
85 return createTree(BucketBox(0, -90, 360, 180), options, true);
87 std::stringstream ss(strippedFileName);
91 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
93 BucketBox bucketBoxList[2];
94 unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList);
95 if (bucketBoxListSize == 0)
96 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
98 if (bucketBoxListSize == 1)
99 return createTree(bucketBoxList[0], options, true);
101 assert(bucketBoxListSize == 2);
102 osg::ref_ptr<osg::Group> group = new osg::Group;
103 group->addChild(createTree(bucketBoxList[0], options, true));
104 group->addChild(createTree(bucketBoxList[1], options, true));
105 return group.release();
109 ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const
111 if (bucketBox.getIsBucketSize()) {
112 return createPagedLOD(bucketBox, options);
113 } else if (!topLevel && bucketBox.getStartLevel() == 4) {
114 // We want an other level of indirection for paging
115 return createPagedLOD(bucketBox, options);
117 BucketBox bucketBoxList[100];
118 unsigned numTiles = bucketBox.getSubDivision(bucketBoxList, 100);
123 return createTree(bucketBoxList[0], options, false);
125 osg::ref_ptr<osg::Group> group = new osg::Group;
126 for (unsigned i = 0; i < numTiles; ++i) {
127 osg::Node* node = createTree(bucketBoxList[i], options, false);
130 group->addChild(node);
132 if (!group->getNumChildren())
135 return group.release();
140 ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const
142 osg::PagedLOD* pagedLOD = new osg::PagedLOD;
144 pagedLOD->setCenterMode(osg::PagedLOD::USER_DEFINED_CENTER);
145 SGSpheref sphere = bucketBox.getBoundingSphere();
146 pagedLOD->setCenter(toOsg(sphere.getCenter()));
147 pagedLOD->setRadius(sphere.getRadius());
149 osg::ref_ptr<osgDB::Options> localOptions;
150 localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
151 pagedLOD->setDatabaseOptions(localOptions.get());
154 if (bucketBox.getIsBucketSize())
159 // Add the static sea level textured shell
160 if (osg::Node* tile = createSeaLevelTile(bucketBox, options))
161 pagedLOD->addChild(tile, range, std::numeric_limits<float>::max());
163 // Add the paged file name that creates the subtrees on demand
164 if (bucketBox.getIsBucketSize()) {
165 std::string fileName;
166 fileName = bucketBox.getBucket().gen_index_str() + std::string(".stg");
167 pagedLOD->setFileName(pagedLOD->getNumChildren(), fileName);
169 std::stringstream ss;
170 ss << bucketBox << ".spt";
171 pagedLOD->setFileName(pagedLOD->getNumChildren(), ss.str());
173 pagedLOD->setRange(pagedLOD->getNumChildren(), 0.0, range);
179 ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const
181 if (options->getPluginStringData("SimGear::FG_EARTH") != "ON")
184 osg::Vec3Array* vertices = new osg::Vec3Array;
185 osg::Vec3Array* normals = new osg::Vec3Array;
186 osg::Vec2Array* texCoords = new osg::Vec2Array;
188 unsigned widthLevel = bucketBox.getWidthLevel();
189 unsigned heightLevel = bucketBox.getHeightLevel();
191 unsigned incx = bucketBox.getWidthIncrement(widthLevel + 2);
192 incx = std::min(incx, bucketBox.getSize(0));
193 for (unsigned i = 0; incx != 0;) {
194 unsigned incy = bucketBox.getHeightIncrement(heightLevel + 2);
195 incy = std::min(incy, bucketBox.getSize(1));
196 for (unsigned j = 0; incy != 0;) {
199 unsigned num = bucketBox.getTileTriangles(i, j, incx, incy, v, n, t);
200 for (unsigned k = 0; k < num; ++k) {
201 vertices->push_back(toOsg(v[k]));
202 normals->push_back(toOsg(n[k]));
203 texCoords->push_back(toOsg(t[k]));
206 incy = std::min(incy, bucketBox.getSize(1) - j);
209 incx = std::min(incx, bucketBox.getSize(0) - i);
212 osg::Vec4Array* colors = new osg::Vec4Array;
213 colors->push_back(osg::Vec4(1, 1, 1, 1));
215 osg::Geometry* geometry = new osg::Geometry;
216 geometry->setVertexArray(vertices);
217 geometry->setNormalArray(normals);
218 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
219 geometry->setColorArray(colors);
220 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
221 geometry->setTexCoordArray(0, texCoords);
223 geometry->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES, 0, vertices->size()));
225 osg::Geode* geode = new osg::Geode;
226 geode->addDrawable(geometry);
227 geode->setStateSet(getLowLODStateSet(options));
233 ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const
235 osg::ref_ptr<osgDB::Options> localOptions;
236 localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
237 localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
239 osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get());
240 if (!dynamic_cast<osg::StateSet*>(object.get()))
243 return static_cast<osg::StateSet*>(object.release());
246 } // namespace simgear