root/OpenSceneGraph/trunk/src/osgPlugins/txp/TXPParser.h @ 11122

Revision 11122, 13.1 kB (checked in by robert, 4 years ago)

From Ryan Kawicki, "I guess I missed these during my testing, but if the database pager has outstanding requests while the application is shutting down, the archive can become invalidated through unsafe calls to ReaderWriterTXP::getArchive. I've made this function return a ref_ptr and change other locations to as needed to conform to the change. I've tested this and no more crashes.

Following files from revision 11057 have been attached."

  • 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
35#ifndef __TXPPARSER_H_
36#define __TXPPARSER_H_
37
38#include <osg/Referenced>
39#include <osg/Texture2D>
40#include <osg/StateSet>
41#include <osg/Node>
42
43#include <stack>
44
45#include "trpage_read.h"
46
47#include "TXPArchive.h"
48
49namespace txp
50{
51
52// Gets local texture via the image helper
53osg::Texture2D* getLocalTexture(trpgrImageHelper& image_helper, const trpgTexture* tex);
54osg::Texture2D* getTemplateTexture(trpgrImageHelper& image_helper, trpgLocalMaterial* locmat, const trpgTexture* tex, int index=0);
55
56
57
58// This is group that will has geode node
59// It is better since all the geometry children will be
60// added into one Geode node as drawables, then having one
61// geode node per child
62// Means, instad of having
63// Group
64//    +-----------
65//    |            |
66//    Geode        Geode
67//    |            |
68//    Drawable    Drawable
69// we will have
70// Group
71//    |
72//    Geode
73//    +-----------
74//    |            |
75//    Drawable    Drawable
76
77class GeodeGroup : public osg::Group
78{
79public:
80
81    GeodeGroup() : osg::Group(), _geode(NULL)
82    {}
83   
84    GeodeGroup(const GeodeGroup& gg,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
85    osg::Group(gg, copyop), _geode(gg._geode)
86    {}
87   
88    META_Node(txp, GeodeGroup);
89   
90    osg::Geode* getGeode()
91    {
92        if (_geode == 0)
93        {   
94            _geode = new osg::Geode();   
95            addChild(_geode);
96        }
97       
98        return _geode;
99    }
100protected:
101    osg::Geode*    _geode;
102};
103
104
105class TXPArchive;
106class childRefRead;
107struct DeferredLightAttribute;
108
109class TXPParser : public trpgSceneParser, public osg::Referenced
110{
111public:
112    TXPParser();
113   
114    // Sets the archive to be parsed
115    inline void setArchive(TXPArchive* archive)
116    {
117        _archive = archive;
118    }
119
120    // Gets the archive
121    inline TXPArchive* getArchive()
122    {
123        return _archive.get();
124    }
125
126    // Scene parser
127    osg::Group *parseScene(
128        trpgReadBuffer &buf,
129        std::map<int,osg::ref_ptr<osg::StateSet> > &materials,
130        std::map<int,osg::ref_ptr<osg::Node> > &models,
131        double realMinRange, double realMaxRange, double usedMaxRange);
132
133    // Returns the current Top Group
134    inline osg::Group* getCurrTop()
135    {
136        return _currentTop ? _currentTop : _root.get();
137    }
138   
139    // Sets the group as potentional tile group
140    inline void setPotentionalTileGroup(osg::Group* grp)
141    {
142        _tileGroups[grp] = 1;
143    }
144   
145    // Return the current material list (passed in to ParseScene())
146    inline std::map<int,osg::ref_ptr<osg::StateSet> >* getMaterials()
147    {
148        return _materialMap;
149    }
150
151    // Ensure material is loaded
152    inline void loadMaterial( int ix )
153    {
154        _archive->loadMaterial( ix );
155    }
156   
157    // New to TerraPage 2.0 - local materials
158    std::vector<osg::ref_ptr<osg::StateSet> >* getLocalMaterials()
159    {
160        return &_localMaterials;
161    }
162   
163    // Load local materials
164    void loadLocalMaterials();
165   
166    // Return the current model list
167    std::map<int,osg::ref_ptr<osg::Node> >* getModels()
168    {
169        return _models;
170    }
171   
172    // Request a model to be load
173    bool requestModel(int ix);
174   
175    // Return a reference to the tile header (after a tile has been read)
176    inline trpgTileHeader *getTileHeaderRef()
177    {
178        return &_tileHeader;
179    }
180   
181   
182    // Returns true if we are under billboard subgraph
183    inline const bool underBillboardSubgraph() const
184    {
185        return _underBillboardSubgraph;
186    }
187   
188    // Sets if we are under billboard subgraph
189    inline void setUnderBillboardSubgraph(bool b)
190    {
191        _underBillboardSubgraph = b;
192    }
193   
194    // TXP Billboard info
195    struct TXPBillboardInfo
196    {
197        int type;
198        int mode;
199        trpg3dPoint center;
200        trpg3dPoint axis;
201    };
202   
203    // Sets info for the last billboard parsed
204    inline void setLastBillboardInfo(TXPBillboardInfo& info)
205    {
206        _lastBillboardInfo = info;
207    }
208   
209    // Gets info for the last billboard parsed
210    inline void getLastBillboardInfo(TXPBillboardInfo& info)
211    {
212        info = _lastBillboardInfo;
213    }
214   
215    // Gets light attrib
216    DeferredLightAttribute& getLightAttribute(int ix);
217   
218    // Returns if we are under layer subgraph
219    inline const bool underLayerSubgraph() const
220    {
221        return _underLayerSubgraph;
222    }
223   
224    // Sets if we are under layer subgraph
225    inline void setUnderLayerSubgraph(bool b)
226    {
227        _underLayerSubgraph = b;
228    }
229   
230    // Set the current layer geode
231    inline void setLayerGeode(osg::Geode* layer)
232    {
233        _layerGeode = layer;
234    }
235   
236    // Returns the current layer geode
237    inline osg::Geode* getLayerGeode() const
238    {
239        return _layerGeode;
240    }
241   
242    inline unsigned int getNumLayerLavels() const { return _numLayerLevels; }
243   
244    // default value to use when setting up textures.
245    void setMaxAnisotropy(float anisotropy)
246    {
247        _defaultMaxAnisotropy = anisotropy;
248    }
249    float getMaxAnisotropy() const
250    {
251        return _defaultMaxAnisotropy;
252    }
253
254    // We need to change the LOD ranges within tiles since
255    // we have done it for the PagedLODs representing tiles
256    inline double checkAndGetMinRange(double range)
257    {
258        if ((range-_realMinRange) < 0.0001)
259            return 0.0;
260        else
261            return range;
262    }
263    inline double checkAndGetMaxRange(double range)
264    {
265        if ((range-_realMaxRange) < 0.0001)
266            return _usedMaxRange;
267        else
268            return range;
269    }
270
271    // gets tile center, from the top lod node
272    inline const osg::Vec3 getTileCenter() const
273    {
274        return _tileCenter;
275    }
276
277    inline void setCurrentNode( osg::Node* node )
278    {
279        _currentNode = node;
280    }
281
282   // After parsing this will return the number of trpgChildRef node found.
283   unsigned int GetNbChildrenRef() const;
284 
285   // This will return the trpgChildRef node pointer associated with the index.
286   // Will return 0 if index is out of bound
287   const trpgChildRef* GetChildRef(unsigned int idx) const;
288 
289
290   
291protected:
292
293    virtual ~TXPParser();
294   
295    // Removes any empty groups and LODs
296    void removeEmptyGroups();
297   
298    // Called on start children
299    bool StartChildren(void *);
300   
301    // Called on end children
302    bool EndChildren(void *);
303   
304    // THE archive
305    osg::ref_ptr< TXPArchive > _archive;
306   
307    // Current parent
308    osg::Group* _currentTop;
309
310    // Current node
311    osg::Node* _currentNode;
312   
313    // The root of the tile
314    osg::ref_ptr<osg::Group> _root;
315   
316    // Parents list
317    std::stack<osg::Group*>    _parents;
318   
319    // Potentional Tile groups
320    std::map<osg::Group*,int>    _tileGroups;
321   
322    // Replace the tile lod to regular group
323    void replaceTileLod(osg::Group*);
324   
325    // Materials
326    typedef std::map<int,osg::ref_ptr<osg::StateSet> >* MaterialMapType;
327    MaterialMapType _materialMap;
328   
329    // Local materials
330    std::vector<osg::ref_ptr<osg::StateSet> >    _localMaterials;
331   
332    // Model list
333    typedef std::map<int,osg::ref_ptr<osg::Node> > OSGModelsMapType;
334    OSGModelsMapType*      _models;
335
336    // Tile header
337    trpgTileHeader    _tileHeader;
338   
339    // true if we are under billboard subgraph
340    bool _underBillboardSubgraph;
341   
342    // Number of levels below the billboard node
343    int _numBillboardLevels;
344   
345    // Last billboard we parsed
346    TXPBillboardInfo _lastBillboardInfo;
347   
348    // true if we are under layer subgraph
349    bool _underLayerSubgraph;
350   
351    // Numbers of levels below layer subgraph
352    int _numLayerLevels;
353   
354    // Our Layer Geode
355    osg::Geode*    _layerGeode;
356   
357    // default value to use when setting up textures.
358    float _defaultMaxAnisotropy;
359   
360    // LOD ranges to be used when parsing tiles
361    double _realMinRange;
362    double _realMaxRange;
363    double _usedMaxRange;
364
365    // tile center
366    osg::Vec3 _tileCenter;
367
368    // TEMP
369    osg::Geode* createBoundingBox(int x,int y, int lod);
370
371    private:
372
373       childRefRead *_childRefCB;
374
375   
376};
377
378
379//! callback functions for various scene graph elements
380//----------------------------------------------------------------------------
381class geomRead : public trpgr_Callback
382{
383public:
384
385    geomRead(TXPParser *in_parse) : _parse(in_parse)
386    {}
387    void *Parse(trpgToken tok,trpgReadBuffer &buf);
388protected:
389    TXPParser *_parse;
390};
391
392
393//----------------------------------------------------------------------------
394class groupRead : public trpgr_Callback
395{
396public:
397    groupRead(TXPParser *in_parse) : _parse(in_parse)
398    {}
399    void *Parse(trpgToken tok,trpgReadBuffer &buf);
400protected:
401    TXPParser *_parse;
402};
403
404//----------------------------------------------------------------------------
405class attachRead : public trpgr_Callback
406{
407public:
408    attachRead(TXPParser *in_parse) : _parse(in_parse)
409    {}
410    void *Parse(trpgToken tok,trpgReadBuffer &buf);
411protected:
412    TXPParser *_parse;
413
414};
415//----------------------------------------------------------------------------
416class childRefRead : public trpgr_Callback
417{
418public:
419   typedef std::vector<trpgChildRef> ChildRefList;
420
421    childRefRead(TXPParser *in_parse) : _parse(in_parse)
422    {}
423    void Reset();
424    void *Parse(trpgToken tok,trpgReadBuffer &buf);
425
426   // After parsing this will return the number of trpgChildRef node found.
427   unsigned int GetNbChildrenRef() const
428   {
429      return childRefList.size();
430   }
431   // This will return the trpgChildRef node associated with the index.
432   // this will return 0 if idx is out of bound
433   const trpgChildRef* GetChildRef(unsigned int idx) const
434   {
435      if(idx >= childRefList.size())
436         return 0;
437      else
438         return &childRefList[idx];
439   }
440
441protected:
442    TXPParser *_parse;
443
444 
445
446private:
447
448   ChildRefList childRefList;
449
450
451};
452
453//----------------------------------------------------------------------------
454class lodRead : public trpgr_Callback
455{
456public:
457    lodRead(TXPParser *in_parse) : _parse(in_parse)
458    {}
459    void *Parse(trpgToken tok,trpgReadBuffer &buf);
460protected:
461    TXPParser *_parse;
462};
463
464//----------------------------------------------------------------------------
465class tileHeaderRead : public trpgr_Callback
466{
467public:
468    tileHeaderRead(TXPParser *in_parse) : _parse(in_parse)
469    {}
470    void *Parse(trpgToken tok,trpgReadBuffer &buf);
471protected:
472    TXPParser *_parse;
473};
474
475//----------------------------------------------------------------------------
476
477class modelRefRead : public trpgr_Callback
478{
479public:
480    modelRefRead(TXPParser *in_parse) : _parse(in_parse)
481    {}
482    void *Parse(trpgToken tok,trpgReadBuffer &buf);
483protected:
484    TXPParser *_parse;
485};
486
487//----------------------------------------------------------------------------
488class billboardRead : public trpgr_Callback
489{
490public:
491    billboardRead(TXPParser *in_parse) : _parse(in_parse)
492    {}
493    void *Parse(trpgToken tok,trpgReadBuffer &buf);
494protected:
495    TXPParser *_parse;
496};
497
498//----------------------------------------------------------------------------
499class lightRead: public trpgr_Callback
500{
501public:
502    lightRead(TXPParser *in_parse) : _parse(in_parse)
503    {}
504    void *Parse(trpgToken tok,trpgReadBuffer &buf);
505protected:
506    TXPParser *_parse;
507};
508
509//----------------------------------------------------------------------------
510class layerRead: public trpgr_Callback
511{
512public:
513    layerRead(TXPParser *in_parse)  : _parse(in_parse)
514    {}
515    void *Parse(trpgToken tok,trpgReadBuffer &buf);
516protected:
517    TXPParser *_parse;
518};
519
520//----------------------------------------------------------------------------
521class labelRead: public trpgr_Callback
522{
523public:
524    labelRead(TXPParser *in_parse)  : _parse(in_parse)
525    {}
526    void *Parse(trpgToken tok,trpgReadBuffer &buf);
527protected:
528    TXPParser *_parse;
529};
530
531} // namespace
532
533#endif // __TXPPARSER_H_
534
Note: See TracBrowser for help on using the browser.