]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/ReaderWriterSPT.cxx
Revert "Use simgear internal stuff for the singleton class."
[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 (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);
76     }
77
78     return stateSet;
79 }
80
81 osgDB::ReaderWriter::ReadResult
82 ReaderWriterSPT::readNode(const std::string& fileName, const osgDB::Options* options) const
83 {
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);
88
89     std::stringstream ss(strippedFileName);
90     BucketBox bucketBox;
91     ss >> bucketBox;
92     if (ss.fail())
93         return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
94
95     BucketBox bucketBoxList[2];
96     unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList);
97     if (bucketBoxListSize == 0)
98         return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
99
100     if (bucketBoxListSize == 1)
101         return createTree(bucketBoxList[0], options, true);
102
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();
108 }
109
110 osg::Node*
111 ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const
112 {
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);
118     } else {
119         BucketBox bucketBoxList[100];
120         unsigned numTiles = bucketBox.getSubDivision(bucketBoxList, 100);
121         if (numTiles == 0)
122             return 0;
123
124         if (numTiles == 1)
125             return createTree(bucketBoxList[0], options, false);
126
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);
130             if (!node)
131                 continue;
132             group->addChild(node);
133         }
134         if (!group->getNumChildren())
135             return 0;
136
137         return group.release();
138     }
139 }
140
141 osg::Node*
142 ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const
143 {
144     osg::PagedLOD* pagedLOD = new osg::PagedLOD;
145
146     pagedLOD->setCenterMode(osg::PagedLOD::USER_DEFINED_CENTER);
147     SGSpheref sphere = bucketBox.getBoundingSphere();
148     pagedLOD->setCenter(toOsg(sphere.getCenter()));
149     pagedLOD->setRadius(sphere.getRadius());
150         
151     osg::ref_ptr<osgDB::Options> localOptions;
152     localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
153     pagedLOD->setDatabaseOptions(localOptions.get());
154         
155     float range;
156     if (bucketBox.getIsBucketSize())
157         range = 200e3;
158     else
159         range = 1e6;
160
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());
164
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);
170     } else {
171         std::stringstream ss;
172         ss << bucketBox << ".spt";
173         pagedLOD->setFileName(pagedLOD->getNumChildren(), ss.str());
174     }
175     pagedLOD->setRange(pagedLOD->getNumChildren(), 0.0, range);
176
177     return pagedLOD;
178 }
179
180 osg::Node*
181 ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const
182 {
183     if (options->getPluginStringData("SimGear::FG_EARTH") != "ON")
184         return 0;
185
186     osg::Vec3Array* vertices = new osg::Vec3Array;
187     osg::Vec3Array* normals = new osg::Vec3Array;
188     osg::Vec2Array* texCoords = new osg::Vec2Array;
189         
190     unsigned widthLevel = bucketBox.getWidthLevel();
191     unsigned heightLevel = bucketBox.getHeightLevel();
192
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;) {
199             SGVec3f v[6], n[6];
200             SGVec2f t[6];
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]));
206             }
207             j += incy;
208             incy = std::min(incy, bucketBox.getSize(1) - j);
209         }
210         i += incx;
211         incx = std::min(incx, bucketBox.getSize(0) - i);
212     }
213         
214     osg::Vec4Array* colors = new osg::Vec4Array;
215     colors->push_back(osg::Vec4(1, 1, 1, 1));
216         
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);
224         
225     geometry->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES, 0, vertices->size()));
226         
227     osg::Geode* geode = new osg::Geode;
228     geode->addDrawable(geometry);
229     geode->setStateSet(getLowLODStateSet(options));
230
231     return geode;
232 }
233
234 osg::StateSet*
235 ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const
236 {
237     osg::ref_ptr<osgDB::Options> localOptions;
238     localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
239     localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
240
241     osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get());
242     if (!dynamic_cast<osg::StateSet*>(object.get()))
243         return 0;
244
245     return static_cast<osg::StateSet*>(object.release());
246 }
247
248 } // namespace simgear
249