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 (osg::Image* image = osgDB::readImageFile(imageFileName, options)) {
71 osg::Texture2D* texture = new osg::Texture2D;
72 texture->setImage(image);
73 texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
74 texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP);
75 stateSet->setTextureAttributeAndModes(0, texture);
81 osgDB::ReaderWriter::ReadResult
82 ReaderWriterSPT::readNode(const std::string& fileName, const osgDB::Options* options) const
84 // The file name without path and without the spt extension
85 std::string strippedFileName = osgDB::getStrippedName(fileName);
86 if (strippedFileName == "earth")
87 return createTree(BucketBox(0, -90, 360, 180), options, true);
89 std::stringstream ss(strippedFileName);
93 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
95 BucketBox bucketBoxList[2];
96 unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList);
97 if (bucketBoxListSize == 0)
98 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
100 if (bucketBoxListSize == 1)
101 return createTree(bucketBoxList[0], options, true);
103 assert(bucketBoxListSize == 2);
104 osg::ref_ptr<osg::Group> group = new osg::Group;
105 group->addChild(createTree(bucketBoxList[0], options, true));
106 group->addChild(createTree(bucketBoxList[1], options, true));
107 return group.release();
111 ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const
113 if (bucketBox.getIsBucketSize()) {
114 return createPagedLOD(bucketBox, options);
115 } else if (!topLevel && bucketBox.getStartLevel() == 4) {
116 // We want an other level of indirection for paging
117 return createPagedLOD(bucketBox, options);
119 BucketBox bucketBoxList[100];
120 unsigned numTiles = bucketBox.getSubDivision(bucketBoxList, 100);
125 return createTree(bucketBoxList[0], options, false);
127 osg::ref_ptr<osg::Group> group = new osg::Group;
128 for (unsigned i = 0; i < numTiles; ++i) {
129 osg::Node* node = createTree(bucketBoxList[i], options, false);
132 group->addChild(node);
134 if (!group->getNumChildren())
137 return group.release();
142 ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const
144 osg::PagedLOD* pagedLOD = new osg::PagedLOD;
146 pagedLOD->setCenterMode(osg::PagedLOD::USER_DEFINED_CENTER);
147 SGSpheref sphere = bucketBox.getBoundingSphere();
148 pagedLOD->setCenter(toOsg(sphere.getCenter()));
149 pagedLOD->setRadius(sphere.getRadius());
151 osg::ref_ptr<osgDB::Options> localOptions;
152 localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
153 pagedLOD->setDatabaseOptions(localOptions.get());
156 if (bucketBox.getIsBucketSize())
161 // Add the static sea level textured shell
162 if (osg::Node* tile = createSeaLevelTile(bucketBox, options))
163 pagedLOD->addChild(tile, range, std::numeric_limits<float>::max());
165 // Add the paged file name that creates the subtrees on demand
166 if (bucketBox.getIsBucketSize()) {
167 std::string fileName;
168 fileName = bucketBox.getBucket().gen_index_str() + std::string(".stg");
169 pagedLOD->setFileName(pagedLOD->getNumChildren(), fileName);
171 std::stringstream ss;
172 ss << bucketBox << ".spt";
173 pagedLOD->setFileName(pagedLOD->getNumChildren(), ss.str());
175 pagedLOD->setRange(pagedLOD->getNumChildren(), 0.0, range);
181 ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const
183 if (options->getPluginStringData("SimGear::FG_EARTH") != "ON")
186 osg::Vec3Array* vertices = new osg::Vec3Array;
187 osg::Vec3Array* normals = new osg::Vec3Array;
188 osg::Vec2Array* texCoords = new osg::Vec2Array;
190 unsigned widthLevel = bucketBox.getWidthLevel();
191 unsigned heightLevel = bucketBox.getHeightLevel();
193 unsigned incx = bucketBox.getWidthIncrement(widthLevel + 2);
194 incx = std::min(incx, bucketBox.getSize(0));
195 for (unsigned i = 0; incx != 0;) {
196 unsigned incy = bucketBox.getHeightIncrement(heightLevel + 2);
197 incy = std::min(incy, bucketBox.getSize(1));
198 for (unsigned j = 0; incy != 0;) {
201 unsigned num = bucketBox.getTileTriangles(i, j, incx, incy, v, n, t);
202 for (unsigned k = 0; k < num; ++k) {
203 vertices->push_back(toOsg(v[k]));
204 normals->push_back(toOsg(n[k]));
205 texCoords->push_back(toOsg(t[k]));
208 incy = std::min(incy, bucketBox.getSize(1) - j);
211 incx = std::min(incx, bucketBox.getSize(0) - i);
214 osg::Vec4Array* colors = new osg::Vec4Array;
215 colors->push_back(osg::Vec4(1, 1, 1, 1));
217 osg::Geometry* geometry = new osg::Geometry;
218 geometry->setVertexArray(vertices);
219 geometry->setNormalArray(normals);
220 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
221 geometry->setColorArray(colors);
222 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
223 geometry->setTexCoordArray(0, texCoords);
225 geometry->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES, 0, vertices->size()));
227 osg::Geode* geode = new osg::Geode;
228 geode->addDrawable(geometry);
229 geode->setStateSet(getLowLODStateSet(options));
235 ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const
237 osg::ref_ptr<osgDB::Options> localOptions;
238 localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
239 localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
241 osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get());
242 if (!dynamic_cast<osg::StateSet*>(object.get()))
245 return static_cast<osg::StateSet*>(object.release());
248 } // namespace simgear