]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/ReaderWriterSPT.cxx
Make tsync part of libSimGearCore when building shared libraries
[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 "BucketBox.hxx"
36
37 namespace simgear {
38
39 ReaderWriterSPT::ReaderWriterSPT()
40 {
41     supportsExtension("spt", "SimGear paged terrain meta database.");
42 }
43
44 ReaderWriterSPT::~ReaderWriterSPT()
45 {
46 }
47
48 const char*
49 ReaderWriterSPT::className() const
50 {
51     return "simgear::ReaderWriterSPT";
52 }
53
54 osgDB::ReaderWriter::ReadResult
55 ReaderWriterSPT::readObject(const std::string& fileName, const osgDB::Options* options) const
56 {
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);
63
64     osg::StateSet* stateSet = new osg::StateSet;
65     stateSet->setAttributeAndModes(new osg::CullFace);
66
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);
74     }
75
76     return stateSet;
77 }
78
79 osgDB::ReaderWriter::ReadResult
80 ReaderWriterSPT::readNode(const std::string& fileName, const osgDB::Options* options) const
81 {
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);
86
87     std::stringstream ss(strippedFileName);
88     BucketBox bucketBox;
89     ss >> bucketBox;
90     if (ss.fail())
91         return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
92
93     BucketBox bucketBoxList[2];
94     unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList);
95     if (bucketBoxListSize == 0)
96         return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
97
98     if (bucketBoxListSize == 1)
99         return createTree(bucketBoxList[0], options, true);
100
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();
106 }
107
108 osg::Node*
109 ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const
110 {
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);
116     } else {
117         BucketBox bucketBoxList[100];
118         unsigned numTiles = bucketBox.getSubDivision(bucketBoxList, 100);
119         if (numTiles == 0)
120             return 0;
121
122         if (numTiles == 1)
123             return createTree(bucketBoxList[0], options, false);
124
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);
128             if (!node)
129                 continue;
130             group->addChild(node);
131         }
132         if (!group->getNumChildren())
133             return 0;
134
135         return group.release();
136     }
137 }
138
139 osg::Node*
140 ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const
141 {
142     osg::PagedLOD* pagedLOD = new osg::PagedLOD;
143
144     pagedLOD->setCenterMode(osg::PagedLOD::USER_DEFINED_CENTER);
145     SGSpheref sphere = bucketBox.getBoundingSphere();
146     pagedLOD->setCenter(toOsg(sphere.getCenter()));
147     pagedLOD->setRadius(sphere.getRadius());
148         
149     osg::ref_ptr<osgDB::Options> localOptions;
150     localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
151     pagedLOD->setDatabaseOptions(localOptions.get());
152         
153     float range;
154     if (bucketBox.getIsBucketSize())
155         range = 200e3;
156     else
157         range = 1e6;
158
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());
162
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);
168     } else {
169         std::stringstream ss;
170         ss << bucketBox << ".spt";
171         pagedLOD->setFileName(pagedLOD->getNumChildren(), ss.str());
172     }
173     pagedLOD->setRange(pagedLOD->getNumChildren(), 0.0, range);
174
175     return pagedLOD;
176 }
177
178 osg::Node*
179 ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const
180 {
181     if (options->getPluginStringData("SimGear::FG_EARTH") != "ON")
182         return 0;
183
184     osg::Vec3Array* vertices = new osg::Vec3Array;
185     osg::Vec3Array* normals = new osg::Vec3Array;
186     osg::Vec2Array* texCoords = new osg::Vec2Array;
187         
188     unsigned widthLevel = bucketBox.getWidthLevel();
189     unsigned heightLevel = bucketBox.getHeightLevel();
190
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;) {
197             SGVec3f v[6], n[6];
198             SGVec2f t[6];
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]));
204             }
205             j += incy;
206             incy = std::min(incy, bucketBox.getSize(1) - j);
207         }
208         i += incx;
209         incx = std::min(incx, bucketBox.getSize(0) - i);
210     }
211         
212     osg::Vec4Array* colors = new osg::Vec4Array;
213     colors->push_back(osg::Vec4(1, 1, 1, 1));
214         
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);
222         
223     geometry->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES, 0, vertices->size()));
224         
225     osg::Geode* geode = new osg::Geode;
226     geode->addDrawable(geometry);
227     geode->setStateSet(getLowLODStateSet(options));
228
229     return geode;
230 }
231
232 osg::StateSet*
233 ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const
234 {
235     osg::ref_ptr<osgDB::Options> localOptions;
236     localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
237     localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
238
239     osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get());
240     if (!dynamic_cast<osg::StateSet*>(object.get()))
241         return 0;
242
243     return static_cast<osg::StateSet*>(object.release());
244 }
245
246 } // namespace simgear
247