[5] | 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-2006 Torus Knot Software Ltd |
---|
| 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 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | /*************************************************************************** |
---|
| 30 | octreenode.cpp - description |
---|
| 31 | ------------------- |
---|
| 32 | begin : Fri Sep 27 2002 |
---|
| 33 | copyright : (C) 2002 by Jon Anderson |
---|
| 34 | email : janders@users.sf.net |
---|
| 35 | |
---|
| 36 | Enhancements 2003 - 2004 (C) The OGRE Team |
---|
| 37 | |
---|
| 38 | ***************************************************************************/ |
---|
| 39 | |
---|
| 40 | #include <OgreRoot.h> |
---|
| 41 | |
---|
| 42 | #include <OgreOctreeNode.h> |
---|
| 43 | #include <OgreOctreeSceneManager.h> |
---|
| 44 | |
---|
| 45 | namespace Ogre |
---|
| 46 | { |
---|
| 47 | unsigned long green = 0xFFFFFFFF; |
---|
| 48 | |
---|
| 49 | unsigned short OctreeNode::mIndexes[ 24 ] = {0, 1, 1, 2, 2, 3, 3, 0, //back |
---|
| 50 | 0, 6, 6, 5, 5, 1, //left |
---|
| 51 | 3, 7, 7, 4, 4, 2, //right |
---|
| 52 | 6, 7, 5, 4 }; //front |
---|
| 53 | unsigned long OctreeNode::mColors[ 8 ] = {green, green, green, green, green, green, green, green }; |
---|
| 54 | |
---|
| 55 | OctreeNode::OctreeNode( SceneManager* creator ) : SceneNode( creator ) |
---|
| 56 | { |
---|
| 57 | mOctant = 0; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | OctreeNode::OctreeNode( SceneManager* creator, const String& name ) : SceneNode( creator, name ) |
---|
| 61 | { |
---|
| 62 | mOctant = 0; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | OctreeNode::~OctreeNode() |
---|
| 66 | {} |
---|
| 67 | void OctreeNode::_removeNodeAndChildren( ) |
---|
| 68 | { |
---|
| 69 | static_cast< OctreeSceneManager * > ( mCreator ) -> _removeOctreeNode( this ); |
---|
| 70 | //remove all the children nodes as well from the octree. |
---|
| 71 | ChildNodeMap::iterator it = mChildren.begin(); |
---|
| 72 | while( it != mChildren.end() ) |
---|
| 73 | { |
---|
| 74 | static_cast<OctreeNode *>( it->second ) -> _removeNodeAndChildren(); |
---|
| 75 | ++it; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | Node * OctreeNode::removeChild( unsigned short index ) |
---|
| 79 | { |
---|
| 80 | OctreeNode *on = static_cast<OctreeNode* >( SceneNode::removeChild( index ) ); |
---|
| 81 | on -> _removeNodeAndChildren(); |
---|
| 82 | return on; |
---|
| 83 | } |
---|
| 84 | Node * OctreeNode::removeChild( Node* child ) |
---|
| 85 | { |
---|
| 86 | OctreeNode *on = static_cast<OctreeNode* >( SceneNode::removeChild( child ) ); |
---|
| 87 | on -> _removeNodeAndChildren(); |
---|
| 88 | return on; |
---|
| 89 | } |
---|
| 90 | void OctreeNode::removeAllChildren() |
---|
| 91 | { |
---|
| 92 | ChildNodeMap::iterator i, iend; |
---|
| 93 | iend = mChildren.end(); |
---|
| 94 | for (i = mChildren.begin(); i != iend; ++i) |
---|
| 95 | { |
---|
| 96 | OctreeNode* on = static_cast<OctreeNode*>(i->second); |
---|
| 97 | on->setParent(0); |
---|
| 98 | on->_removeNodeAndChildren(); |
---|
| 99 | } |
---|
| 100 | mChildren.clear(); |
---|
| 101 | mChildrenToUpdate.clear(); |
---|
| 102 | |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | Node * OctreeNode::removeChild( const String & name ) |
---|
| 106 | { |
---|
| 107 | OctreeNode *on = static_cast< OctreeNode * >( SceneNode::removeChild( name ) ); |
---|
| 108 | on -> _removeNodeAndChildren( ); |
---|
| 109 | return on; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | //same as SceneNode, only it doesn't care about children... |
---|
| 113 | void OctreeNode::_updateBounds( void ) |
---|
| 114 | { |
---|
| 115 | mWorldAABB.setNull(); |
---|
| 116 | mLocalAABB.setNull(); |
---|
| 117 | |
---|
| 118 | // Update bounds from own attached objects |
---|
| 119 | ObjectMap::iterator i = mObjectsByName.begin(); |
---|
| 120 | AxisAlignedBox bx; |
---|
| 121 | |
---|
| 122 | while ( i != mObjectsByName.end() ) |
---|
| 123 | { |
---|
| 124 | |
---|
| 125 | // Get local bounds of object |
---|
| 126 | bx = i->second ->getBoundingBox(); |
---|
| 127 | |
---|
| 128 | mLocalAABB.merge( bx ); |
---|
| 129 | |
---|
| 130 | mWorldAABB.merge( i->second ->getWorldBoundingBox(true) ); |
---|
| 131 | ++i; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | //update the OctreeSceneManager that things might have moved. |
---|
| 136 | // if it hasn't been added to the octree, add it, and if has moved |
---|
| 137 | // enough to leave it's current node, we'll update it. |
---|
| 138 | if ( ! mWorldAABB.isNull() ) |
---|
| 139 | { |
---|
| 140 | static_cast < OctreeSceneManager * > ( mCreator ) -> _updateOctreeNode( this ); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** Since we are loose, only check the center. |
---|
| 146 | */ |
---|
| 147 | bool OctreeNode::_isIn( AxisAlignedBox &box ) |
---|
| 148 | { |
---|
| 149 | // Always fail if not in the scene graph or box is null |
---|
| 150 | if (!mIsInSceneGraph || box.isNull()) return false; |
---|
| 151 | |
---|
| 152 | // Always succeed if AABB is infinite |
---|
| 153 | if (box.isInfinite()) |
---|
| 154 | return true; |
---|
| 155 | |
---|
| 156 | Vector3 center = mWorldAABB.getMaximum().midPoint( mWorldAABB.getMinimum() ); |
---|
| 157 | |
---|
| 158 | Vector3 bmin = box.getMinimum(); |
---|
| 159 | Vector3 bmax = box.getMaximum(); |
---|
| 160 | |
---|
| 161 | bool centre = ( bmax > center && bmin < center ); |
---|
| 162 | if (!centre) |
---|
| 163 | return false; |
---|
| 164 | |
---|
| 165 | // Even if covering the centre line, need to make sure this BB is not large |
---|
| 166 | // enough to require being moved up into parent. When added, bboxes would |
---|
| 167 | // end up in parent due to cascade but when updating need to deal with |
---|
| 168 | // bbox growing too large for this child |
---|
| 169 | Vector3 octreeSize = bmax - bmin; |
---|
| 170 | Vector3 nodeSize = mWorldAABB.getMaximum() - mWorldAABB.getMinimum(); |
---|
| 171 | return nodeSize < octreeSize; |
---|
| 172 | |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** Addes the attached objects of this OctreeScene node into the queue. */ |
---|
| 176 | void OctreeNode::_addToRenderQueue( Camera* cam, RenderQueue *queue, |
---|
| 177 | bool onlyShadowCasters, VisibleObjectsBoundsInfo* visibleBounds ) |
---|
| 178 | { |
---|
| 179 | ObjectMap::iterator mit = mObjectsByName.begin(); |
---|
| 180 | |
---|
| 181 | while ( mit != mObjectsByName.end() ) |
---|
| 182 | { |
---|
| 183 | MovableObject * mo = mit->second; |
---|
| 184 | |
---|
| 185 | mo->_notifyCurrentCamera(cam); |
---|
| 186 | if ( mo->isVisible() && |
---|
| 187 | (!onlyShadowCasters || mo->getCastShadows())) |
---|
| 188 | { |
---|
| 189 | mo -> _updateRenderQueue( queue ); |
---|
| 190 | |
---|
| 191 | if (visibleBounds) |
---|
| 192 | { |
---|
| 193 | visibleBounds->merge(mo->getWorldBoundingBox(true), |
---|
| 194 | mo->getWorldBoundingSphere(true), cam); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | ++mit; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | void OctreeNode::getRenderOperation( RenderOperation& rend ) |
---|
| 205 | { |
---|
| 206 | |
---|
| 207 | /* TODO |
---|
| 208 | rend.useIndexes = true; |
---|
| 209 | rend.numTextureCoordSets = 0; // no textures |
---|
| 210 | rend.vertexOptions = LegacyRenderOperation::VO_DIFFUSE_COLOURS; |
---|
| 211 | rend.operationType = LegacyRenderOperation::OT_LINE_LIST; |
---|
| 212 | rend.numVertices = 8; |
---|
| 213 | rend.numIndexes = 24; |
---|
| 214 | |
---|
| 215 | rend.pVertices = mCorners; |
---|
| 216 | rend.pIndexes = mIndexes; |
---|
| 217 | rend.pDiffuseColour = mColors; |
---|
| 218 | |
---|
| 219 | const Vector3 * corners = _getLocalAABB().getAllCorners(); |
---|
| 220 | |
---|
| 221 | int index = 0; |
---|
| 222 | |
---|
| 223 | for ( int i = 0; i < 8; i++ ) |
---|
| 224 | { |
---|
| 225 | rend.pVertices[ index ] = corners[ i ].x; |
---|
| 226 | index++; |
---|
| 227 | rend.pVertices[ index ] = corners[ i ].y; |
---|
| 228 | index++; |
---|
| 229 | rend.pVertices[ index ] = corners[ i ].z; |
---|
| 230 | index++; |
---|
| 231 | } |
---|
| 232 | */ |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | } |
---|
| 236 | } |
---|