[921] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | /***************************************************************************
|
---|
| 26 | octree.cpp - description
|
---|
| 27 | -------------------
|
---|
| 28 | begin : Mon Sep 30 2002
|
---|
| 29 | copyright : (C) 2002 by Jon Anderson
|
---|
| 30 | email : janders@users.sf.net
|
---|
| 31 |
|
---|
| 32 | Enhancements 2003 - 2004 (C) The OGRE Team
|
---|
| 33 |
|
---|
| 34 | ***************************************************************************/
|
---|
| 35 |
|
---|
| 36 | #include <OgreOctree.h>
|
---|
| 37 | #include <OgreOctreeNode.h>
|
---|
| 38 |
|
---|
| 39 | namespace Ogre
|
---|
| 40 | {
|
---|
| 41 |
|
---|
| 42 | /** Returns true is the box will fit in a child.
|
---|
| 43 | */
|
---|
| 44 | bool 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 | */
|
---|
| 59 | void 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 |
|
---|
| 85 | Octree::Octree( Octree * parent )
|
---|
| 86 | : mWireBoundingBox(0),
|
---|
| 87 | mHalfSize( 0, 0, 0 )
|
---|
| 88 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
[2308] | 89 | , mLastVisited(0), mVisible(false), mLastRendered(-1)
|
---|
| 90 | , mFullyVisible(false)
|
---|
[2454] | 91 | , mAssumedVisible(0)
|
---|
[921] | 92 | #endif //GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 93 | {
|
---|
| 94 | //initialize all children to null.
|
---|
| 95 | for ( int i = 0; i < 2; i++ )
|
---|
| 96 | {
|
---|
| 97 | for ( int j = 0; j < 2; j++ )
|
---|
| 98 | {
|
---|
| 99 | for ( int k = 0; k < 2; k++ )
|
---|
| 100 | {
|
---|
| 101 | mChildren[ i ][ j ][ k ] = 0;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | mParent = parent;
|
---|
| 107 | mNumNodes = 0;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | Octree::~Octree()
|
---|
| 111 | {
|
---|
| 112 | //initialize all children to null.
|
---|
| 113 | for ( int i = 0; i < 2; i++ )
|
---|
| 114 | {
|
---|
| 115 | for ( int j = 0; j < 2; j++ )
|
---|
| 116 | {
|
---|
| 117 | for ( int k = 0; k < 2; k++ )
|
---|
| 118 | {
|
---|
| 119 | if ( mChildren[ i ][ j ][ k ] != 0 )
|
---|
| 120 | delete mChildren[ i ][ j ][ k ];
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | if(mWireBoundingBox)
|
---|
| 126 | delete mWireBoundingBox;
|
---|
| 127 |
|
---|
| 128 | mParent = 0;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void Octree::_addNode( OctreeNode * n )
|
---|
| 132 | {
|
---|
| 133 | mNodes.push_back( n );
|
---|
| 134 | n -> setOctant( this );
|
---|
| 135 |
|
---|
| 136 | //update total counts.
|
---|
| 137 | _ref();
|
---|
| 138 |
|
---|
| 139 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 140 | _updateBounds();
|
---|
| 141 | #endif
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | void Octree::_removeNode( OctreeNode * n )
|
---|
| 145 | {
|
---|
| 146 | mNodes.erase( std::find( mNodes.begin(), mNodes.end(), n ) );
|
---|
| 147 | n -> setOctant( 0 );
|
---|
| 148 |
|
---|
| 149 | //update total counts.
|
---|
| 150 | _unref();
|
---|
| 151 |
|
---|
| 152 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 153 | _updateBounds();
|
---|
| 154 | #endif
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | void Octree::_getCullBounds( AxisAlignedBox *b )
|
---|
| 158 | {
|
---|
| 159 | const Vector3 * corners = mBox.getAllCorners();
|
---|
| 160 | b -> setExtents( corners[ 0 ] - mHalfSize, corners[ 4 ] + mHalfSize );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | WireBoundingBox* Octree::getWireBoundingBox()
|
---|
| 164 | {
|
---|
| 165 | // Create a WireBoundingBox if needed
|
---|
| 166 | if(mWireBoundingBox == 0)
|
---|
| 167 | mWireBoundingBox = new WireBoundingBox();
|
---|
| 168 |
|
---|
| 169 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 170 | mWireBoundingBox->setupBoundingBox(mWorldAABB);
|
---|
| 171 | #else
|
---|
| 172 | mWireBoundingBox->setupBoundingBox(mBox);
|
---|
| 173 | #endif
|
---|
| 174 |
|
---|
| 175 | return mWireBoundingBox;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 179 | //-----------------------------------------------------------------------
|
---|
| 180 | int Octree::lastVisited()
|
---|
| 181 | {
|
---|
| 182 | return mLastVisited;
|
---|
| 183 | }
|
---|
| 184 | //-----------------------------------------------------------------------
|
---|
| 185 | void Octree::setLastVisited(int frameid)
|
---|
| 186 | {
|
---|
| 187 | mLastVisited = frameid;
|
---|
| 188 | }
|
---|
| 189 | //-----------------------------------------------------------------------
|
---|
| 190 | int Octree::lastRendered()
|
---|
| 191 | {
|
---|
| 192 | return mLastRendered;
|
---|
| 193 | }
|
---|
| 194 | //-----------------------------------------------------------------------
|
---|
| 195 | void Octree::setLastRendered(int frameid)
|
---|
| 196 | {
|
---|
| 197 | mLastRendered = frameid;
|
---|
| 198 | }
|
---|
| 199 | //-----------------------------------------------------------------------
|
---|
| 200 | void Octree::setOctreeVisible(bool visible)
|
---|
| 201 | {
|
---|
| 202 | mVisible = visible;
|
---|
| 203 | }
|
---|
| 204 | //-----------------------------------------------------------------------
|
---|
| 205 | bool Octree::isOctreeVisible()
|
---|
| 206 | {
|
---|
| 207 | return mVisible;
|
---|
| 208 | }
|
---|
| 209 | //-----------------------------------------------------------------------
|
---|
[2257] | 210 | void Octree::setOctreeFullyVisible(bool visible)
|
---|
| 211 | {
|
---|
| 212 | mFullyVisible = visible;
|
---|
| 213 | }
|
---|
| 214 | //-----------------------------------------------------------------------
|
---|
| 215 | bool Octree::isOctreeFullyVisible()
|
---|
| 216 | {
|
---|
[2308] | 217 | // all childrens are visible
|
---|
| 218 | #if 0
|
---|
| 219 | return mNumChildren == mNumVisibleChildren;
|
---|
| 220 | #else
|
---|
[2257] | 221 | return mFullyVisible;
|
---|
[2308] | 222 | #endif
|
---|
[2257] | 223 | }
|
---|
[2308] | 224 | //-----------------------------------------------------------------------
|
---|
[2454] | 225 | void Octree::setAssumedVisible(int assumedVisible)
|
---|
[2308] | 226 | {
|
---|
[2454] | 227 | mAssumedVisible = assumedVisible;
|
---|
[2308] | 228 | }
|
---|
| 229 | //-----------------------------------------------------------------------
|
---|
[2454] | 230 | int Octree::getAssumedVisible()
|
---|
[2308] | 231 | {
|
---|
[2454] | 232 | return mAssumedVisible;
|
---|
[2308] | 233 | }
|
---|
| 234 | //-----------------------------------------------------------------------
|
---|
[2454] | 235 | void Octree::decAssumedVisible()
|
---|
[2308] | 236 | {
|
---|
[2454] | 237 | -- mAssumedVisible;
|
---|
[2308] | 238 | }
|
---|
| 239 | //-----------------------------------------------------------------------
|
---|
[921] | 240 | Octree *Octree::getParent()
|
---|
| 241 | {
|
---|
| 242 | return mParent;
|
---|
| 243 | }
|
---|
| 244 | //-----------------------------------------------------------------------
|
---|
| 245 | AxisAlignedBox Octree::_getWorldAABB(void) const
|
---|
| 246 | {
|
---|
| 247 | return mWorldAABB;
|
---|
| 248 | }
|
---|
| 249 | //-----------------------------------------------------------------------
|
---|
| 250 | void Octree::_updateBounds()
|
---|
| 251 | {
|
---|
| 252 | // Reset bounds first
|
---|
| 253 | mWorldAABB.setNull();
|
---|
| 254 |
|
---|
| 255 | // Update bounds from own attached objects
|
---|
| 256 | NodeList::iterator it, it_end;
|
---|
| 257 | it_end = mNodes.end();
|
---|
| 258 |
|
---|
| 259 | for (it = mNodes.begin(); it != it_end; ++it)
|
---|
| 260 | {
|
---|
| 261 | // Merge world bounds of each object
|
---|
| 262 | mWorldAABB.merge((*it)->_getWorldAABB());
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | // Merge with children
|
---|
| 266 | for (int i = 0; i < 2; ++i)
|
---|
| 267 | {
|
---|
| 268 | for (int j = 0; j < 2; ++j)
|
---|
| 269 | {
|
---|
| 270 | for (int k = 0; k < 2; ++k)
|
---|
| 271 | {
|
---|
| 272 | if (mChildren[i][j][k])
|
---|
| 273 | {
|
---|
| 274 | mWorldAABB.merge(mChildren[i][j][k]->_getWorldAABB());
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
[2128] | 279 |
|
---|
[921] | 280 | // HACK: clamp to bounds
|
---|
| 281 | AxisAlignedBox box;
|
---|
| 282 | _getCullBounds(&box);
|
---|
| 283 | mWorldAABB = mWorldAABB.intersection(box);
|
---|
| 284 |
|
---|
| 285 | // recursively update parent bounds
|
---|
| 286 | if (mParent)
|
---|
| 287 | {
|
---|
| 288 | mParent->_updateBounds();
|
---|
| 289 | }
|
---|
[2308] | 290 | }
|
---|
[921] | 291 |
|
---|
[2308] | 292 |
|
---|
[921] | 293 | #endif //GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 294 | }
|
---|