root/OpenSceneGraph/trunk/src/osgVolume/VolumeTile.cpp @ 13041

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

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 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#include <osgVolume/VolumeTile>
15#include <osgVolume/Volume>
16
17
18using namespace osg;
19using namespace osgVolume;
20
21/////////////////////////////////////////////////////////////////////////////////
22//
23// TileID
24//
25TileID::TileID():
26    level(-1),
27    x(-1),
28    y(-1),
29    z(-1)
30{
31}
32
33TileID::TileID(int in_level, int in_x, int in_y, int in_z):
34    level(in_level),
35    x(in_x),
36    y(in_y),
37    z(in_z)
38{
39}
40
41
42/////////////////////////////////////////////////////////////////////////////////
43//
44// VolumeTile
45//
46VolumeTile::VolumeTile():
47    _volume(0),
48    _dirty(false),
49    _hasBeenTraversal(false)
50{
51    setThreadSafeRefUnref(true);
52
53    setNumChildrenRequiringUpdateTraversal(1);
54}
55
56VolumeTile::VolumeTile(const VolumeTile& volumeTile,const osg::CopyOp& copyop):
57    Group(volumeTile,copyop),
58    _volume(0),
59    _dirty(false),
60    _hasBeenTraversal(false),
61    _layer(volumeTile._layer)
62{
63    setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
64
65    if (volumeTile.getVolumeTechnique())
66    {
67        setVolumeTechnique(osg::clone(volumeTile.getVolumeTechnique()));
68    }
69}
70
71VolumeTile::~VolumeTile()
72{
73    if (_volume) setVolume(0);
74}
75
76void VolumeTile::setVolume(Volume* volume)
77{
78    if (_volume == volume) return;
79
80    if (_volume) _volume->unregisterVolumeTile(this);
81
82    _volume = volume;
83
84    if (_volume) _volume->registerVolumeTile(this);
85}
86
87void VolumeTile::setTileID(const TileID& tileID)
88{
89    if (_tileID == tileID) return;
90
91    if (_volume) _volume->unregisterVolumeTile(this);
92
93    _tileID = tileID;
94
95    if (_volume) _volume->registerVolumeTile(this);
96}
97
98
99void VolumeTile::traverse(osg::NodeVisitor& nv)
100{
101    if (!_hasBeenTraversal)
102    {
103        if (!_volume)
104        {
105            osg::NodePath& nodePath = nv.getNodePath();
106            if (!nodePath.empty())
107            {
108                for(osg::NodePath::reverse_iterator itr = nodePath.rbegin();
109                    itr != nodePath.rend() && !_volume;
110                    ++itr)
111                {
112                    osgVolume::Volume* volume = dynamic_cast<Volume*>(*itr);
113                    if (volume)
114                    {
115                        OSG_INFO<<"Assigning volume system "<<volume<<std::endl;
116                        setVolume(volume);
117                    }
118                }
119            }
120        }
121
122        _hasBeenTraversal = true;
123    }
124
125    if (nv.getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR &&
126        _layer->requiresUpdateTraversal())
127    {
128        _layer->update(nv);
129    }
130
131    if (_volumeTechnique.valid())
132    {
133        _volumeTechnique->traverse(nv);
134    }
135    else
136    {
137        osg::Group::traverse(nv);
138    }
139}
140
141void VolumeTile::init()
142{
143    if (_volumeTechnique.valid() && getDirty())
144    {
145        _volumeTechnique->init();
146
147        setDirty(false);
148    }
149}
150
151void VolumeTile::setLayer(Layer* layer)
152{
153    _layer = layer;
154}
155
156void VolumeTile::setVolumeTechnique(VolumeTechnique* volumeTechnique)
157{
158    if (_volumeTechnique == volumeTechnique) return;
159
160    int dirtyDelta = _dirty ? -1 : 0;
161
162    if (_volumeTechnique.valid())
163    {
164        _volumeTechnique->_volumeTile = 0;
165    }
166
167    _volumeTechnique = volumeTechnique;
168
169    if (_volumeTechnique.valid())
170    {
171        _volumeTechnique->_volumeTile = this;
172        ++dirtyDelta;
173    }
174
175    if (dirtyDelta>0) setDirty(true);
176    else if (dirtyDelta<0) setDirty(false);
177}
178
179void VolumeTile::setDirty(bool dirty)
180{
181    if (_dirty==dirty) return;
182
183    _dirty = dirty;
184
185    if (_dirty)
186    {
187        setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
188    }
189    else if (getNumChildrenRequiringUpdateTraversal()>0)
190    {
191        setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()-1);
192    }
193}
194
195osg::BoundingSphere VolumeTile::computeBound() const
196{
197    const Locator* masterLocator = getLocator();
198    if (_layer.valid() && !masterLocator)
199    {
200        masterLocator = _layer->getLocator();
201    }
202
203    if (masterLocator)
204    {
205        osg::Vec3d left, right;
206        masterLocator->computeLocalBounds(left, right);
207
208        return osg::BoundingSphere((left+right)*0.5, (right-left).length()*0.5);
209    }
210    else if (_layer.valid())
211    {
212        // we have a layer but no Locator defined so will assume a Identity Locator
213        return osg::BoundingSphere( osg::Vec3(0.5,0.5,0.5), 0.867);
214    }
215    else
216    {
217        return osg::BoundingSphere();
218    }
219}
Note: See TracBrowser for help on using the browser.