root/OpenSceneGraph/trunk/src/osgPlugins/txp/TileMapper.cpp @ 13041

Revision 13041, 5.4 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/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14/*    Dec 2010 - TileMapper was fixed and simplified
15    Nick
16*/
17
18#include "TileMapper.h"
19#include "TXPPagedLOD.h"
20
21#include <osg/Material>
22
23using namespace txp;
24
25float TileMapper::getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const
26{
27    if (withLODScale)
28        return (pos-getEyeLocal()).length()*getLODScale();
29    else
30        return (pos-getEyeLocal()).length();
31}
32
33inline TileMapper::value_type distance(const osg::Vec3& coord,const osg::Matrix& matrix)
34{
35
36    return -((TileMapper::value_type)coord[0]*(TileMapper::value_type)matrix(0,2)+
37             (TileMapper::value_type)coord[1]*(TileMapper::value_type)matrix(1,2)+
38             (TileMapper::value_type)coord[2]*(TileMapper::value_type)matrix(2,2)+
39             matrix(3,2));
40}
41
42float TileMapper::getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScale) const
43{
44    const osg::Matrix& matrix = *_modelviewStack.back();
45    float dist = distance(pos,matrix);
46
47    if (withLODScale)
48        return dist*getLODScale();
49    else
50        return dist;
51}
52
53void TileMapper::apply(osg::Node& node)
54{
55    if (node.getName()=="TileContent")
56    {
57        _containsGeode = true;
58        return;
59    }
60
61    if (isCulled(node))
62        return;
63
64    // push the culling mode.
65    pushCurrentMask();
66
67    traverse(node);
68
69    // pop the culling mode.
70    popCurrentMask();
71}
72
73void TileMapper::apply(osg::Group& node)
74{
75    if (node.getName()=="TileContent")
76    {
77        _containsGeode = true;
78        return;
79    }
80
81    if (isCulled(node))
82        return;
83
84    // push the culling mode.
85    pushCurrentMask();
86
87    TileIdentifier* tid = dynamic_cast<TileIdentifier*>(node.getUserData());
88
89    if (tid)
90    {
91        _containsGeode = false;
92    }
93
94    traverse(node);
95
96    if (tid)
97    {
98        if (_containsGeode)
99        {
100            insertTile(*tid);
101
102            _containsGeode = false;
103
104        }
105
106    }
107
108    // pop the culling mode.
109    popCurrentMask();
110}
111
112void TileMapper::apply(osg::Geode&)
113{
114    _containsGeode = true;
115}
116
117void TileMapper::apply(osg::PagedLOD& node)
118{
119    if (isCulled(node))
120        return;
121
122    // push the culling mode.
123    pushCurrentMask();
124
125    TXPPagedLOD* txpPagedLOD = dynamic_cast<TXPPagedLOD*>(&node);
126    if (txpPagedLOD)
127    {
128        _containsGeode = false;
129    }
130
131    traverse(node);
132
133    if (txpPagedLOD)
134    {
135        if (_containsGeode)
136        {
137            insertTile(txpPagedLOD->_tileIdentifier);
138
139            _containsGeode = false;
140        }
141    }
142
143    // pop the culling mode.
144    popCurrentMask();
145}
146
147void TileMapper::insertTile(const TileIdentifier& tid)
148{
149    _tileMap.insert(TileMap::value_type(tid,1));
150}
151
152
153bool TileMapper::isTileNeighbourALowerLODLevel(const TileIdentifier& tid, int dx, int dy) const
154{
155    if (_tileMap.count(TileIdentifier(tid.x+dx,tid.y+dy,tid.lod))!=0)
156    {
157        // we have a neightbour at the same lod level.
158        return false;
159    }
160
161    // find the tiles parents.
162    TileMap::const_iterator itr = _tileMap.find(tid);
163    if (itr==_tileMap.end())
164    {
165        // not found tile in _tileMap, what should we do??
166        // return true as a fallback right now.
167#if 0
168        std::cout << "TileMapper::isTileNeighbourALowerLODLevel() Not found tile in map," << std::endl;
169        std::cout << "    LOD=" << tid.lod << "  X=" << tid.x << "  Y=" << tid.y << std::endl;
170#endif
171        return true;
172    }
173    TileIdentifier parent_tid(tid.x/2,tid.y/2,tid.lod-1);
174
175    bool parentHasNorthNeighour = _tileMap.count(TileIdentifier(parent_tid.x,  parent_tid.y+1,parent_tid.lod))!=0;
176    bool parentHasEastNeighour  = _tileMap.count(TileIdentifier(parent_tid.x+1,parent_tid.y,  parent_tid.lod))!=0;
177    bool parentHasSouthNeighour = _tileMap.count(TileIdentifier(parent_tid.x,  parent_tid.y-1,parent_tid.lod))!=0;
178    bool parentHasWestNeighour  = _tileMap.count(TileIdentifier(parent_tid.x-1,parent_tid.y,  parent_tid.lod))!=0;
179
180
181    // identify whether the tile is a NE/SE/SW/NW tile relative to its parent.
182    osg::Vec3 delta(tid.x%2,tid.y%2,0);
183
184    if (delta.y()>0.0f) // noth side
185    {
186        if (delta.x()>0.0f)
187        {
188            // NE
189            if (dy==1)
190                return parentHasNorthNeighour;
191            else if (dx==1)
192                return parentHasEastNeighour;
193        }
194        else
195        {
196            // NW
197            if (dy==1)
198                return parentHasNorthNeighour;
199            else if (dx==-1)
200                return parentHasWestNeighour;
201        }
202    }
203    else // south side
204    {
205        if (delta.x()>0.0f)
206        {
207            // SE
208            if (dy==-1)
209                return parentHasSouthNeighour;
210            else if (dx==1)
211                return parentHasEastNeighour;
212        }
213        else
214        {
215            // SW
216            if (dy==-1)
217                return parentHasSouthNeighour;
218            else if (dx==-1)
219                return parentHasWestNeighour;
220        }
221    }
222
223    return false;
224}
Note: See TracBrowser for help on using the browser.