source: OGRE/trunk/ogre_changes/Ogre1.2/PlugIns/OctreeSceneManager/src/OgreOctree.cpp @ 2128

Revision 2128, 6.8 KB checked in by mattausch, 17 years ago (diff)

removed bug with aabb assertion

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25/***************************************************************************
26octree.cpp  -  description
27-------------------
28begin                : Mon Sep 30 2002
29copyright            : (C) 2002 by Jon Anderson
30email                : janders@users.sf.net
31
32Enhancements 2003 - 2004 (C) The OGRE Team
33
34***************************************************************************/
35
36#include <OgreOctree.h>
37#include <OgreOctreeNode.h>
38
39namespace Ogre
40{
41
42/** Returns true is the box will fit in a child.
43*/
44bool Octree::_isTwiceSize( AxisAlignedBox &box )
45{
46    const Vector3 * pts1 = mBox.getAllCorners();
47    const Vector3 * pts2 = box.getAllCorners();
48
49    return ( ( pts2[ 4 ].x -pts2[ 0 ].x ) <= ( pts1[ 4 ].x - pts1[ 0 ].x ) / 2 ) &&
50           ( ( pts2[ 4 ].y - pts2[ 0 ].y ) <= ( pts1[ 4 ].y - pts1[ 0 ].y ) / 2 ) &&
51           ( ( pts2[ 4 ].z - pts2[ 0 ].z ) <= ( pts1[ 4 ].z - pts1[ 0 ].z ) / 2 ) ;
52
53}
54
55/** It's assumed the the given box has already been proven to fit into
56* a child.  Since it's a loose octree, only the centers need to be
57* compared to find the appropriate node.
58*/
59void Octree::_getChildIndexes( AxisAlignedBox &box, int *x, int *y, int *z )
60{
61    Vector3 max = mBox.getMaximum();
62    Vector3 min = box.getMinimum();
63
64    Vector3 center = mBox.getMaximum().midPoint( mBox.getMinimum() );
65
66    Vector3 ncenter = box.getMaximum().midPoint( box.getMinimum() );
67
68    if ( ncenter.x > center.x )
69        * x = 1;
70    else
71        *x = 0;
72
73    if ( ncenter.y > center.y )
74        * y = 1;
75    else
76        *y = 0;
77
78    if ( ncenter.z > center.z )
79        * z = 1;
80    else
81        *z = 0;
82
83}
84
85Octree::Octree( Octree * parent )
86    : mWireBoundingBox(0),
87      mHalfSize( 0, 0, 0 )
88#ifdef GTP_VISIBILITY_MODIFIED_OGRE
89        , mLastVisited(0), mVisible(false), mLastRendered(-1)
90#endif //GTP_VISIBILITY_MODIFIED_OGRE
91{
92    //initialize all children to null.
93    for ( int i = 0; i < 2; i++ )
94    {
95        for ( int j = 0; j < 2; j++ )
96        {
97            for ( int k = 0; k < 2; k++ )
98            {
99                mChildren[ i ][ j ][ k ] = 0;
100            }
101        }
102    }
103
104    mParent = parent;
105
106    mNumNodes = 0;
107}
108
109Octree::~Octree()
110{
111    //initialize all children to null.
112    for ( int i = 0; i < 2; i++ )
113    {
114        for ( int j = 0; j < 2; j++ )
115        {
116            for ( int k = 0; k < 2; k++ )
117            {
118                if ( mChildren[ i ][ j ][ k ] != 0 )
119                    delete mChildren[ i ][ j ][ k ];
120            }
121        }
122    }
123
124    if(mWireBoundingBox)
125        delete mWireBoundingBox;
126
127    mParent = 0;
128}
129
130void Octree::_addNode( OctreeNode * n )
131{
132    mNodes.push_back( n );
133    n -> setOctant( this );
134
135    //update total counts.
136    _ref();
137
138#ifdef GTP_VISIBILITY_MODIFIED_OGRE
139        _updateBounds();
140#endif
141}
142
143void Octree::_removeNode( OctreeNode * n )
144{
145    mNodes.erase( std::find( mNodes.begin(), mNodes.end(), n ) );
146    n -> setOctant( 0 );
147
148    //update total counts.
149    _unref();
150
151#ifdef GTP_VISIBILITY_MODIFIED_OGRE
152        _updateBounds();
153#endif
154}
155
156void Octree::_getCullBounds( AxisAlignedBox *b )
157{
158    const Vector3 * corners = mBox.getAllCorners();
159    b -> setExtents( corners[ 0 ] - mHalfSize, corners[ 4 ] + mHalfSize );
160}
161
162WireBoundingBox* Octree::getWireBoundingBox()
163{
164    // Create a WireBoundingBox if needed
165    if(mWireBoundingBox == 0)
166        mWireBoundingBox = new WireBoundingBox();
167
168#ifdef GTP_VISIBILITY_MODIFIED_OGRE
169        mWireBoundingBox->setupBoundingBox(mWorldAABB);
170#else
171        mWireBoundingBox->setupBoundingBox(mBox);
172#endif 
173
174    return mWireBoundingBox;
175}
176
177#ifdef GTP_VISIBILITY_MODIFIED_OGRE
178//-----------------------------------------------------------------------       
179int Octree::lastVisited()
180{
181        return mLastVisited;
182}
183//-----------------------------------------------------------------------
184void Octree::setLastVisited(int frameid)
185{
186        mLastVisited = frameid;
187}
188//-----------------------------------------------------------------------       
189int Octree::lastRendered()
190{
191        return mLastRendered;
192}
193//-----------------------------------------------------------------------
194void Octree::setLastRendered(int frameid)
195{
196        mLastRendered = frameid;
197}
198//-----------------------------------------------------------------------       
199void Octree::setOctreeVisible(bool visible)
200{
201        mVisible = visible;
202}
203//-----------------------------------------------------------------------       
204bool Octree::isOctreeVisible()
205{
206        return mVisible;
207}
208//-----------------------------------------------------------------------       
209Octree *Octree::getParent()
210{
211        return mParent;
212}
213//-----------------------------------------------------------------------       
214AxisAlignedBox Octree::_getWorldAABB(void) const
215{
216        return mWorldAABB;
217}
218//-----------------------------------------------------------------------
219void Octree::_updateBounds()
220{
221        // Reset bounds first
222    mWorldAABB.setNull();
223 
224    // Update bounds from own attached objects
225        NodeList::iterator it, it_end;
226        it_end = mNodes.end();
227
228    for (it = mNodes.begin(); it != it_end; ++it)
229    {
230        // Merge world bounds of each object
231                mWorldAABB.merge((*it)->_getWorldAABB());
232    }
233
234    // Merge with children
235        for (int i = 0; i < 2; ++i)
236    {
237        for (int j = 0; j < 2; ++j)
238        {
239            for (int k = 0; k < 2; ++k)
240            {
241                if (mChildren[i][j][k])
242                                {
243                                        mWorldAABB.merge(mChildren[i][j][k]->_getWorldAABB());
244                                }
245            }
246        }
247        }
248
249        // HACK: clamp to bounds
250        AxisAlignedBox box;
251        _getCullBounds(&box);
252        mWorldAABB = mWorldAABB.intersection(box);
253       
254        // recursively update parent bounds
255        if (mParent)
256        {
257                mParent->_updateBounds();
258        }
259 }
260
261#endif //GTP_VISIBILITY_MODIFIED_OGRE
262}
Note: See TracBrowser for help on using the repository browser.