root/OpenSceneGraph/trunk/src/osgPlugins/txp/TXPArchive.h @ 13041

Revision 13041, 9.0 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/***************************************************************************
2 * December 2003
3 *
4 * This TerraPage loader was re-written in a fashion to use PagedLOD
5 * to manage paging entirely, also includes a version of Terrex's smart mesh
6 * adapted to work with PagedLOD. The essential code by Boris Bralo is still present,
7 * slight modified.
8 * nick at terrex dot com
9 *
10 * Ported to PagedLOD technology by Trajce Nikolov (Nick) & Robert Osfield
11 *****************************************************************************/
12
13/***************************************************************************
14 * OpenSceneGraph loader for Terrapage format database
15 * by Boris Bralo 2002
16 *
17 * based on/modifed  sgl (Scene Graph Library) loader by Bryan Walsh
18 *
19 * This loader is based on/modified from Terrain Experts Performer Loader,
20 * and was ported to SGL by Bryan Walsh / bryanw at earthlink dot net
21 *
22 * That loader is redistributed under the terms listed on Terrain Experts
23 * website (www.terrex.com/www/pages/technology/technologypage.htm)
24 *
25 * "TerraPage is provided as an Open Source format for use by anyone...
26 * We supply the TerraPage C++ source code free of charge.  Anyone
27 * can use it and redistribute it as needed (including our competitors).
28 * We do, however, ask that you keep the TERREX copyrights intact."
29 *
30 * Copyright Terrain Experts Inc. 1999.
31 * All Rights Reserved.
32 *
33 *****************************************************************************/
34#ifndef __TXPARCHIVE_H_
35#define __TXPARCHIVE_H_
36
37#include "trpage_sys.h"
38#include "trpage_read.h"
39#include "trpage_geom.h"
40
41#include <osg/Referenced>
42#include <osg/BoundingBox>
43#include <osg/Texture2D>
44#include <osg/StateSet>
45#include <osg/Node>
46#include <osg/PagedLOD>
47#include <osg/Array>
48#include <osgSim/LightPointNode>
49#include <osgText/Font>
50
51#include <OpenThreads/Mutex>
52
53namespace txp
54{
55    // this one handles different placement of light direction in osg and terrapage
56    struct DeferredLightAttribute
57    {
58        // light point at (0,0,0) looking in (0,0,0) direction
59        osg::ref_ptr<osgSim::LightPointNode> lightPoint;
60        osg::ref_ptr<osg::StateSet> fallback;
61        osg::Vec3 attitude;
62    };
63
64    class TXPParser;
65    class TXPArchive : public trpgr_Archive, public osg::Referenced
66    {
67    public:
68        // Constructor
69        TXPArchive();
70
71        // Opens the archive file and reads the header
72        bool openFile(const std::string& archiveName);
73        // Load the materials from the archve
74        bool loadMaterials();
75        bool loadMaterial(int ix);
76        bool loadTexture(int i);
77
78        // Load the models from the archive
79        bool loadModels();
80        bool loadModel(int ix);
81
82        // Load the light attribs from the archive
83        bool loadLightAttributes();
84
85        // Load the text styles from the archive
86        bool loadTextStyles();
87        inline std::map<int, osg::ref_ptr<osgText::Font> >& getStyles()
88        {
89            return _fonts;
90        }
91        inline std::map<int, osg::Vec4 >& getTextColors()
92        {
93            return _fcolors;
94        }
95
96        // Add light attrib
97        void addLightAttribute(osgSim::LightPointNode* lpn, osg::StateSet* fallback , const osg::Vec3& attitude,int handle);
98
99        int getNumLightAttributes()
100        {
101            return _lights.size();
102        }
103
104        // Get light attrib
105        inline DeferredLightAttribute& getLightAttribute(unsigned int i)
106        {
107            return _lights[i];
108        };
109
110        // Gets some informations for a given tile
111        struct TileInfo
112        {
113            osg::Vec3           center;
114            double              minRange;
115            double              maxRange;
116            double              lod0Range;
117            float               radius;
118            osg::Vec3           size;
119            osg::BoundingBox    bbox;
120        };
121        struct TileLocationInfo
122        {
123            TileLocationInfo() : x( -1 ), y( -1 ), lod( -1 )
124            {}
125            TileLocationInfo(int gx, int gy, int glod, const trpgwAppAddress& gaddr, float gzmin = 0.0f, float gzmax = 0.0f):
126                    x( gx ), y( gy ), lod( glod ), addr( gaddr ), zmin( gzmin ), zmax( gzmax )
127            {}
128            int x, y, lod;
129            trpgwAppAddress addr;
130            float zmin, zmax;
131        };
132
133        bool getTileInfo(int x, int y, int lod, TileInfo& info);
134        bool getTileInfo(const TileLocationInfo& loc, TileInfo& info);
135
136        // Set/Get the archive id
137        inline void setId(int id)
138        {
139            _id = id;
140        }
141        inline const int& getId() const
142        {
143            return _id;
144        }
145
146        // Returns the number of LODs for this archive
147        inline const int& getNumLODs() const
148        {
149            return _numLODs;
150        }
151
152        // Returns the extents of the archive
153        // FIXME - Needs to change for databases that aren't flat-earth
154        void getExtents(osg::BoundingBox& extents);
155    //     {
156    //         extents.set(_swExtents.x,_swExtents.y,0.0f,_neExtents.x,_neExtents.y,0.0f);
157    //     }
158
159        // Returns the origin of the archive
160        inline void getOrigin(double& x, double& y)
161        {
162            x=_swExtents.x;
163            y=_swExtents.y;
164        }
165
166        // Returns global texture
167        inline osg::Texture2D* getGlobalTexture(int id)
168        {
169            return GetTexMapEntry(id).get();
170        }
171
172        // Returns scenegraph representing the Tile.
173        // For version 2.1 and over this function can only be call
174        // with lod = 0, since the archive tile table will contain
175        // only tiles with lod = 0
176        osg::Group* getTileContent(
177                int x,
178                int y,
179                int lod,
180                double realMinRange,
181                double realMaxRange,
182                double usedMaxRange,
183                osg::Vec3& tileCenter,
184                std::vector<TileLocationInfo>& childInfoList);
185
186        //  To be used for Version 2.1 with lod > 0
187        osg::Group* getTileContent(
188                const TileLocationInfo& loc,
189                double realMinRange,
190                double realMaxRange,
191                double usedMaxRange,
192                osg::Vec3& tileCenter,
193                std::vector<TileLocationInfo>& childInfoList);
194
195        // Get the number of tiles for given LOD
196        bool getLODSize(int lod, int& x, int& y);
197
198        void GetVersion(int& majorVer, int& minorVer) const
199        {
200            majorVer = _majorVersion;
201            minorVer = _minorVersion;
202        }
203
204        //////////////////////////////////////////////////////////////////
205        // This section brought to you by A. Danklefsen and the team @
206        // Alion Science And Technology 2/12/07
207        //
208        // This will allow you to have smc / fid / swc / stp values and
209        // places them on the userdata of the state set. this way your own
210        // terrain loader / parser can know these values
211        void SetUserDataToMaterialAttributes(osg::StateSet& osg_state_set, const trpgMaterial& mat)
212        {
213            if(!_loadMaterialsToStateSet)
214                return;
215
216            int attr_values = 0;
217            osg::ref_ptr<osg::IntArray> ourValueArray = new osg::IntArray();
218            for(int attrIter = 0 ; attrIter < 4; ++attrIter)
219            {
220                mat.GetAttr(attrIter, attr_values);
221                ourValueArray->push_back(attr_values);
222            }
223            osg_state_set.setUserData(ourValueArray.get());
224        }
225
226        void SetMaterialAttributesToStateSetVar(bool value) {_loadMaterialsToStateSet = value;}
227
228    protected:
229
230        // Destructor
231        virtual ~TXPArchive();
232
233        // Id of the archive
234        int _id;
235
236        // Number of the LODs
237        int _numLODs;
238
239        // Archive extents
240        trpg2dPoint _swExtents;
241        trpg2dPoint _neExtents;
242
243        // Terra Page Parser
244        osg::ref_ptr<TXPParser>    _parser;
245
246        // Textures
247        typedef std::map<int,osg::ref_ptr<osg::Texture2D> > OSGTexMapType;
248        OSGTexMapType _texmap;
249
250        void SetTexMap(int key,osg::ref_ptr<osg::Texture2D> ref);
251        osg::ref_ptr<osg::Texture2D> GetTexMapEntry(int key);
252
253
254        // States
255        typedef std::map<int,osg::ref_ptr<osg::StateSet> > OSGStatesMapType;
256        OSGStatesMapType _statesMap;
257
258        void SetStatesMap(int key,osg::ref_ptr<osg::StateSet> ref);
259        osg::ref_ptr<osg::StateSet> GetStatesMapEntry(int key);
260
261        // Models
262        typedef std::map<int,osg::ref_ptr<osg::Node> > OSGModelsMapType;
263        OSGModelsMapType      _models;
264
265        // Light attributes vector
266        std::map<int, DeferredLightAttribute>           _lights;
267
268        // Text styles / Fonts
269        std::map<int, osg::ref_ptr<osgText::Font> >    _fonts;
270
271        // Text colors
272        std::map<int, osg::Vec4 >            _fcolors;
273
274        //
275        OpenThreads::Mutex  _mutex;
276
277        // Cache those: TerraPage version
278        int _majorVersion, _minorVersion;
279
280        bool _isMaster;
281
282        bool _loadMaterialsToStateSet;
283
284    };
285
286} // namespace
287
288#endif // __TXPARCHIVE_H_
289
Note: See TracBrowser for help on using the browser.