source: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp @ 112

Revision 112, 31.7 KB checked in by mattausch, 19 years ago (diff)
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/***************************************************************************
26octreescenemanager.cpp  -  description
27-------------------
28begin                : Fri Sep 27 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 <OgreOctreeSceneManager.h>
37#include <OgreOctreeSceneQuery.h>
38#include <OgreOctreeNode.h>
39#include <OgreOctreeCamera.h>
40#include <OgreRenderSystem.h>
41#include <windows.h>
42
43extern "C"
44{
45    void findNodesInBox( Ogre::SceneManager *sm,
46                         const Ogre::AxisAlignedBox &box,
47                         std::list < Ogre::SceneNode * > &list,
48                         Ogre::SceneNode *exclude )
49    {
50        static_cast<Ogre::OctreeSceneManager*>( sm ) -> findNodesIn( box, list, exclude );
51    }
52    void findNodesInSphere( Ogre::SceneManager *sm,
53                            const Ogre::Sphere &sphere,
54                            std::list < Ogre::SceneNode * > &list,
55                            Ogre::SceneNode *exclude )
56    {
57        static_cast<Ogre::OctreeSceneManager*>( sm ) -> findNodesIn( sphere, list, exclude );
58    }
59}
60
61namespace Ogre
62{
63enum Intersection
64{
65    OUTSIDE=0,
66    INSIDE=1,
67    INTERSECT=2
68};
69int OctreeSceneManager::intersect_call = 0;
70
71Intersection intersect( const Ray &one, const AxisAlignedBox &two )
72{
73    OctreeSceneManager::intersect_call++;
74    // Null box?
75    if (two.isNull()) return OUTSIDE;
76
77    bool inside = true;
78    const Vector3* pCorners = two.getAllCorners();
79    Vector3 origin = one.getOrigin();
80    Vector3 dir = one.getDirection();
81
82    Vector3 maxT(-1, -1, -1);
83
84    int i = 0;
85    for(i=0; i<3; i++ )
86    {
87        if( origin[i] < pCorners[0][i] )
88        {
89            inside = false;
90            if( dir[i] > 0 )
91            {
92                maxT[i] = (pCorners[0][i] - origin[i])/ dir[i];
93            }
94        }
95        else if( origin[i] > pCorners[4][i] )
96        {
97            inside = false;
98            if( dir[i] < 0 )
99            {
100                maxT[i] = (pCorners[4][i] - origin[i]) / dir[i];
101            }
102        }
103    }
104
105    if( inside )
106    {
107        return INTERSECT;
108    }
109    int whichPlane = 0;
110    if( maxT[1] > maxT[whichPlane])
111        whichPlane = 1;
112    if( maxT[2] > maxT[whichPlane])
113        whichPlane = 2;
114
115    if( ((int)maxT[whichPlane]) & 0x80000000 )
116    {
117        return OUTSIDE;
118    }
119    for(i=0; i<3; i++ )
120    {
121        if( i!= whichPlane )
122        {
123            float f = origin[i] + maxT[whichPlane] * dir[i];
124            if ( f < (pCorners[0][i] - 0.00001f) ||
125                    f > (pCorners[4][i] +0.00001f ) )
126            {
127                return OUTSIDE;
128            }
129        }
130    }
131
132    return INTERSECT;
133
134}
135
136
137/** Checks how the second box intersects with the first.
138*/
139Intersection intersect( const PlaneBoundedVolume &one, const AxisAlignedBox &two )
140{
141    OctreeSceneManager::intersect_call++;
142    // Null box?
143    if (two.isNull()) return OUTSIDE;
144
145    // Get corners of the box
146    const Vector3* pCorners = two.getAllCorners();
147
148    // For each plane, see if all points are on the negative side
149    // If so, object is not visible.
150    // If one or more are, it's partial.
151    // If all aren't, full
152    int corners[ 8 ] = {0, 4, 3, 5, 2, 6, 1, 7};
153    bool all_inside = true;
154    PlaneList::const_iterator i, iend;
155    iend = one.planes.end();
156    for (i = one.planes.begin(); i != iend; ++i)
157    {
158        const Plane& plane = *i;
159        bool all_outside = true;
160
161        float distance = 0;
162
163        for ( int corner = 0; corner < 8; ++corner )
164        {
165            distance = plane.getDistance( pCorners[ corners[ corner ] ] );
166            all_outside = all_outside && ( distance < 0 );
167            all_inside = all_inside && ( distance >= 0 );
168
169            if ( !all_outside && !all_inside )
170                break;
171        }
172
173        if ( all_outside )
174            return OUTSIDE;
175    }
176
177    if ( all_inside )
178        return INSIDE;
179    else
180        return INTERSECT;
181
182}
183
184
185/** Checks how the second box intersects with the first.
186*/
187Intersection intersect( const AxisAlignedBox &one, const AxisAlignedBox &two )
188{
189    OctreeSceneManager::intersect_call++;
190    // Null box?
191    if (one.isNull() || two.isNull()) return OUTSIDE;
192
193    const Vector3 * outside = one.getAllCorners();
194    const Vector3 *inside = two.getAllCorners();
195
196    if ( inside[ 4 ].x < outside[ 0 ].x ||
197            inside[ 4 ].y < outside[ 0 ].y ||
198            inside[ 4 ].z < outside[ 0 ].z ||
199            inside[ 0 ].x > outside[ 4 ].x ||
200            inside[ 0 ].y > outside[ 4 ].y ||
201            inside[ 0 ].z > outside[ 4 ].z )
202    {
203        return OUTSIDE;
204    }
205
206    bool full = ( inside[ 0 ].x > outside[ 0 ].x &&
207                  inside[ 0 ].y > outside[ 0 ].y &&
208                  inside[ 0 ].z > outside[ 0 ].z &&
209                  inside[ 4 ].x < outside[ 4 ].x &&
210                  inside[ 4 ].y < outside[ 4 ].y &&
211                  inside[ 4 ].z < outside[ 4 ].z );
212
213    if ( full )
214        return INSIDE;
215    else
216        return INTERSECT;
217
218}
219
220/** Checks how the box intersects with the sphere.
221*/
222Intersection intersect( const Sphere &one, const AxisAlignedBox &two )
223{
224    OctreeSceneManager::intersect_call++;
225    // Null box?
226    if (two.isNull()) return OUTSIDE;
227
228    float sradius = one.getRadius();
229
230    sradius *= sradius;
231
232    Vector3 scenter = one.getCenter();
233
234    const Vector3 *corners = two.getAllCorners();
235
236    float s, d = 0;
237
238    Vector3 mndistance = ( corners[ 0 ] - scenter );
239    Vector3 mxdistance = ( corners[ 4 ] - scenter );
240
241    if ( mndistance.squaredLength() < sradius &&
242            mxdistance.squaredLength() < sradius )
243    {
244        return INSIDE;
245    }
246
247    //find the square of the distance
248    //from the sphere to the box
249    for ( int i = 0 ; i < 3 ; i++ )
250    {
251        if ( scenter[ i ] < corners[ 0 ][ i ] )
252        {
253            s = scenter[ i ] - corners[ 0 ][ i ];
254            d += s * s;
255        }
256
257        else if ( scenter[ i ] > corners[ 4 ][ i ] )
258        {
259            s = scenter[ i ] - corners[ 4 ][ i ];
260            d += s * s;
261        }
262
263    }
264
265    bool partial = ( d <= sradius );
266
267    if ( !partial )
268    {
269        return OUTSIDE;
270    }
271
272    else
273    {
274        return INTERSECT;
275    }
276
277
278}
279
280unsigned long white = 0xFFFFFFFF;
281
282unsigned short OctreeSceneManager::mIndexes[ 24 ] = {0, 1, 1, 2, 2, 3, 3, 0,       //back
283        0, 6, 6, 5, 5, 1,             //left
284        3, 7, 7, 4, 4, 2,             //right
285        6, 7, 5, 4 };          //front
286unsigned long OctreeSceneManager::mColors[ 8 ] = {white, white, white, white, white, white, white, white };
287
288
289OctreeSceneManager::OctreeSceneManager( ) : SceneManager()
290{
291    AxisAlignedBox b( -10000, -10000, -10000, 10000, 10000, 10000 );
292    int depth = 8;
293    mOctree = 0;
294    init( b, depth );
295}
296
297OctreeSceneManager::OctreeSceneManager( AxisAlignedBox &box, int max_depth ) : SceneManager()
298{
299    mOctree = 0;
300    init( box, max_depth );
301}
302
303void OctreeSceneManager::init( AxisAlignedBox &box, int depth )
304{
305    delete mSceneRoot; //get rid of old root.
306
307    // -- Changes by Steve
308    // Don't do it this way, it will add it to the mSceneNodes which we don't want
309    //mSceneRoot = createSceneNode( "SceneRoot" );
310    mSceneRoot = new OctreeNode( this, "SceneRoot" );
311        mSceneRoot->_notifyRootNode();
312    // -- End changes by Steve
313
314    if ( mOctree != 0 )
315        delete mOctree;
316
317    mOctree = new Octree( 0 );
318#ifdef GTP_VISIBILITY_MODIFIED_OGRE
319        mNumOctreeNodes = 1;
320#endif // GTP_VISIBILITY_MODIFIED_OGRE
321    mMaxDepth = depth;
322    mBox = box;
323
324    mOctree -> mBox = box;
325
326    Vector3 min = box.getMinimum();
327
328    Vector3 max = box.getMaximum();
329
330    mOctree -> mHalfSize = ( max - min ) / 2;
331
332
333    mShowBoxes = false;
334
335    mCullCamera = false;
336
337    mNumObjects = 0;
338
339    Vector3 v( 1.5, 1.5, 1.5 );
340
341    mScaleFactor.setScale( v );
342
343
344
345    // setDisplaySceneNodes( true );
346    // setShowBoxes( true );
347
348    //
349    //setUseCullCamera( true );
350    //mSceneRoot isn't put into the octree since it has no volume.
351
352}
353
354OctreeSceneManager::~OctreeSceneManager()
355{
356    // -- Changed by Steve
357    // Don't do this here, SceneManager will do it
358    /*
359    if( mSceneRoot )
360    delete mSceneRoot;
361    */
362    // --End Changes by Steve
363
364    if ( mOctree )
365        delete mOctree;
366}
367
368Camera * OctreeSceneManager::createCamera( const String &name )
369{
370    Camera * c = new OctreeCamera( name, this );
371    mCameras.insert( CameraList::value_type( name, c ) );
372    return c;
373}
374
375void OctreeSceneManager::destroySceneNode( const String &name )
376{
377    OctreeNode * on = static_cast < OctreeNode* > ( getSceneNode( name ) );
378
379    if ( on != 0 )
380        _removeOctreeNode( on );
381
382    SceneManager::destroySceneNode( name );
383}
384
385bool OctreeSceneManager::getOptionValues( const String & key, StringVector  &refValueList )
386{
387    return SceneManager::getOptionValues( key, refValueList );
388}
389
390bool OctreeSceneManager::getOptionKeys( StringVector & refKeys )
391{
392    SceneManager::getOptionKeys( refKeys );
393    refKeys.push_back( "CullCamera" );
394    refKeys.push_back( "Size" );
395    refKeys.push_back( "ShowOctree" );
396    refKeys.push_back( "Depth" );
397
398    return true;
399}
400
401
402void OctreeSceneManager::_updateOctreeNode( OctreeNode * onode )
403{
404    AxisAlignedBox box = onode -> _getWorldAABB();
405
406    if ( box.isNull() )
407        return ;
408
409
410    if ( onode -> getOctant() == 0 )
411    {
412        //if outside the octree, force into the root node.
413        if ( ! onode -> _isIn( mOctree -> mBox ) )
414            mOctree->_addNode( onode );
415        else
416            _addOctreeNode( onode, mOctree );
417        return ;
418    }
419
420    if ( ! onode -> _isIn( onode -> getOctant() -> mBox ) )
421    {
422        _removeOctreeNode( onode );
423
424        //if outside the octree, force into the root node.
425        if ( ! onode -> _isIn( mOctree -> mBox ) )
426            mOctree->_addNode( onode );
427        else
428            _addOctreeNode( onode, mOctree );
429    }
430}
431
432/** Only removes the node from the octree.  It leaves the octree, even if it's empty.
433*/
434void OctreeSceneManager::_removeOctreeNode( OctreeNode * n )
435{
436    Octree * oct = n -> getOctant();
437
438    if ( oct )
439    {
440        oct -> _removeNode( n );
441    }
442
443    n->setOctant(0);
444}
445
446
447void OctreeSceneManager::_addOctreeNode( OctreeNode * n, Octree *octant, int depth )
448{
449
450    AxisAlignedBox bx = n -> _getWorldAABB();
451
452
453    //if the octree is twice as big as the scene node,
454    //we will add it to a child.
455    if ( ( depth < mMaxDepth ) && octant -> _isTwiceSize( bx ) )
456    {
457        int x, y, z;
458        octant -> _getChildIndexes( bx, &x, &y, &z );
459
460        if ( octant -> mChildren[ x ][ y ][ z ] == 0 )
461        {
462            octant -> mChildren[ x ][ y ][ z ] = new Octree( octant );
463#ifdef GTP_VISIBILITY_MODIFIED_OGRE
464        mNumOctreeNodes ++;
465#endif // GTP_VISIBILITY_MODIFIED_OGRE
466
467            const Vector3 *corners = octant -> mBox.getAllCorners();
468            Vector3 min, max;
469
470            if ( x == 0 )
471            {
472                min.x = corners[ 0 ].x;
473                max.x = ( corners[ 0 ].x + corners[ 4 ].x ) / 2;
474            }
475
476            else
477            {
478                min.x = ( corners[ 0 ].x + corners[ 4 ].x ) / 2;
479                max.x = corners[ 4 ].x;
480            }
481
482            if ( y == 0 )
483            {
484                min.y = corners[ 0 ].y;
485                max.y = ( corners[ 0 ].y + corners[ 4 ].y ) / 2;
486            }
487
488            else
489            {
490                min.y = ( corners[ 0 ].y + corners[ 4 ].y ) / 2;
491                max.y = corners[ 4 ].y;
492            }
493
494            if ( z == 0 )
495            {
496                min.z = corners[ 0 ].z;
497                max.z = ( corners[ 0 ].z + corners[ 4 ].z ) / 2;
498            }
499
500            else
501            {
502                min.z = ( corners[ 0 ].z + corners[ 4 ].z ) / 2;
503                max.z = corners[ 4 ].z;
504            }
505
506            octant -> mChildren[ x ][ y ][ z ] -> mBox.setExtents( min, max );
507            octant -> mChildren[ x ][ y ][ z ] -> mHalfSize = ( max - min ) / 2;
508        }
509
510                //matt: remove later
511                //OutputDebugString("adding octree node\n");
512               
513
514        _addOctreeNode( n, octant -> mChildren[ x ][ y ][ z ], ++depth );
515
516    }
517
518    else
519    {
520        octant -> _addNode( n );
521    }
522}
523
524
525SceneNode * OctreeSceneManager::createSceneNode( void )
526{
527    OctreeNode * on = new OctreeNode( this );
528    mSceneNodes[ on->getName() ] = on;
529    return on;
530}
531
532SceneNode * OctreeSceneManager::createSceneNode( const String &name )
533{
534    OctreeNode * on = new OctreeNode( this, name );
535    mSceneNodes[ on->getName() ] = on;
536    return on;
537}
538
539void OctreeSceneManager::_updateSceneGraph( Camera * cam )
540{
541    SceneManager::_updateSceneGraph( cam );
542}
543
544void OctreeSceneManager::_alertVisibleObjects( void )
545{
546    NodeList::iterator it = mVisible.begin();
547
548    while ( it != mVisible.end() )
549    {
550        OctreeNode * node = *it;
551
552        ++it;
553    }
554}
555
556void OctreeSceneManager::_findVisibleObjects( Camera * cam, bool onlyShadowCasters )
557{
558
559    getRenderQueue()->clear();
560    mBoxes.clear();
561    mVisible.clear();
562
563    if ( mCullCamera )
564    {
565        Camera * c = getCamera( "CullCamera" );
566
567        if ( c != 0 )
568            cam = getCamera( "CullCamera" );
569    }
570
571    mNumObjects = 0;
572
573    //walk the octree, adding all visible Octreenodes nodes to the render queue.
574    walkOctree( static_cast < OctreeCamera * > ( cam ), getRenderQueue(), mOctree, false, onlyShadowCasters );
575
576
577    // Show the octree boxes & cull camera if required
578    if ( mShowBoxes || mCullCamera )
579    {
580
581
582
583        if ( mShowBoxes )
584        {
585            for ( BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it )
586            {
587                getRenderQueue()->addRenderable(*it);
588            }
589        }
590
591        if ( mCullCamera )
592        {
593            OctreeCamera * c = static_cast<OctreeCamera*>(getCamera( "CullCamera" ));
594
595            if ( c != 0 )
596            {
597                getRenderQueue()->addRenderable(c);
598            }
599        }
600
601    }
602
603
604
605}
606
607void OctreeSceneManager::walkOctree( OctreeCamera *camera, RenderQueue *queue,
608                                     Octree *octant, bool foundvisible, bool onlyShadowCasters )
609{
610
611    //return immediately if nothing is in the node.
612    if ( octant -> numNodes() == 0 )
613        return ;
614
615    OctreeCamera::Visibility v = OctreeCamera::NONE;
616
617    if ( foundvisible )
618    {
619        v = OctreeCamera::FULL;
620    }
621
622    else if ( octant == mOctree )
623    {
624        v = OctreeCamera::PARTIAL;
625    }
626
627    else
628    {
629        AxisAlignedBox box;
630        octant -> _getCullBounds( &box );
631        v = camera -> getVisibility( box );
632    }
633
634
635    // if the octant is visible, or if it's the root node...
636    if ( v != OctreeCamera::NONE )
637    {
638
639        //Add stuff to be rendered;
640        NodeList::iterator it = octant -> mNodes.begin();
641
642        if ( mShowBoxes )
643        {
644            mBoxes.push_back( octant->getWireBoundingBox() );
645        }
646
647        bool vis = true;
648
649        while ( it != octant -> mNodes.end() )
650        {
651            OctreeNode * sn = *it;
652
653            // if this octree is partially visible, manually cull all
654            // scene nodes attached directly to this level.
655
656            if ( v == OctreeCamera::PARTIAL )
657                vis = camera -> isVisible( sn -> _getWorldAABB() );
658
659            if ( vis )
660            {
661
662                mNumObjects++;
663                sn -> _addToRenderQueue(camera, queue, onlyShadowCasters );
664
665                mVisible.push_back( sn );
666
667                if ( mDisplayNodes )
668                    queue -> addRenderable( sn );
669
670                // check if the scene manager or this node wants the bounding box shown.
671                if (sn->getShowBoundingBox() || mShowBoundingBoxes)
672                    sn->_addBoundingBoxToQueue(queue);
673            }
674
675            ++it;
676        }
677
678        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
679            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 0 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
680
681        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
682            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 0 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
683
684        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
685            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 1 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
686
687        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
688            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 1 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
689
690        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
691            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 0 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
692
693        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
694            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 0 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
695
696        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
697            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 1 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
698
699        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
700            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 1 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
701
702    }
703
704}
705
706// --- non template versions
707void _findNodes( const AxisAlignedBox &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
708{
709
710        if ( !full )
711        {
712                AxisAlignedBox obox;
713                octant -> _getCullBounds( &obox );
714
715                Intersection isect = intersect( t, obox );
716
717                if ( isect == OUTSIDE )
718                        return ;
719
720                full = ( isect == INSIDE );
721        }
722
723
724        NodeList::iterator it = octant -> mNodes.begin();
725
726        while ( it != octant -> mNodes.end() )
727        {
728                OctreeNode * on = ( *it );
729
730                if ( on != exclude )
731                {
732                        if ( full )
733                        {
734                                list.push_back( on );
735                        }
736
737                        else
738                        {
739                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
740
741                                if ( nsect != OUTSIDE )
742                                {
743                                        list.push_back( on );
744                                }
745                        }
746
747                }
748
749                ++it;
750        }
751
752
753
754        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
755                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
756
757        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
758                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
759
760        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
761                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
762
763        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
764                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
765
766        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
767                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
768
769        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
770                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
771
772        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
773                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
774
775        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
776                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
777
778}
779
780void _findNodes( const Sphere &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
781{
782
783        if ( !full )
784        {
785                AxisAlignedBox obox;
786                octant -> _getCullBounds( &obox );
787
788                Intersection isect = intersect( t, obox );
789
790                if ( isect == OUTSIDE )
791                        return ;
792
793                full = ( isect == INSIDE );
794        }
795
796
797        NodeList::iterator it = octant -> mNodes.begin();
798
799        while ( it != octant -> mNodes.end() )
800        {
801                OctreeNode * on = ( *it );
802
803                if ( on != exclude )
804                {
805                        if ( full )
806                        {
807                                list.push_back( on );
808                        }
809
810                        else
811                        {
812                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
813
814                                if ( nsect != OUTSIDE )
815                                {
816                                        list.push_back( on );
817                                }
818                        }
819
820                }
821
822                ++it;
823        }
824
825
826
827        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
828                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
829
830        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
831                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
832
833        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
834                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
835
836        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
837                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
838
839        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
840                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
841
842        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
843                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
844
845        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
846                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
847
848        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
849                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
850
851}
852
853
854void _findNodes( const PlaneBoundedVolume &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
855{
856
857        if ( !full )
858        {
859                AxisAlignedBox obox;
860                octant -> _getCullBounds( &obox );
861
862                Intersection isect = intersect( t, obox );
863
864                if ( isect == OUTSIDE )
865                        return ;
866
867                full = ( isect == INSIDE );
868        }
869
870
871        NodeList::iterator it = octant -> mNodes.begin();
872
873        while ( it != octant -> mNodes.end() )
874        {
875                OctreeNode * on = ( *it );
876
877                if ( on != exclude )
878                {
879                        if ( full )
880                        {
881                                list.push_back( on );
882                        }
883
884                        else
885                        {
886                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
887
888                                if ( nsect != OUTSIDE )
889                                {
890                                        list.push_back( on );
891                                }
892                        }
893
894                }
895
896                ++it;
897        }
898
899
900
901        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
902                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
903
904        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
905                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
906
907        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
908                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
909
910        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
911                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
912
913        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
914                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
915
916        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
917                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
918
919        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
920                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
921
922        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
923                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
924
925}
926
927void _findNodes( const Ray &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
928{
929
930        if ( !full )
931        {
932                AxisAlignedBox obox;
933                octant -> _getCullBounds( &obox );
934
935                Intersection isect = intersect( t, obox );
936
937                if ( isect == OUTSIDE )
938                        return ;
939
940                full = ( isect == INSIDE );
941        }
942
943
944        NodeList::iterator it = octant -> mNodes.begin();
945
946        while ( it != octant -> mNodes.end() )
947        {
948                OctreeNode * on = ( *it );
949
950                if ( on != exclude )
951                {
952                        if ( full )
953                        {
954                                list.push_back( on );
955                        }
956
957                        else
958                        {
959                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
960
961                                if ( nsect != OUTSIDE )
962                                {
963                                        list.push_back( on );
964                                }
965                        }
966
967                }
968
969                ++it;
970        }
971
972
973
974        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
975                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
976
977        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
978                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
979
980        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
981                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
982
983        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
984                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
985
986        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
987                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
988
989        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
990                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
991
992        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
993                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
994
995        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
996                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
997
998}
999
1000void OctreeSceneManager::findNodesIn( const AxisAlignedBox &box, std::list < SceneNode * > &list, SceneNode *exclude )
1001{
1002    _findNodes( box, list, exclude, false, mOctree );
1003}
1004
1005void OctreeSceneManager::findNodesIn( const Sphere &sphere, std::list < SceneNode * > &list, SceneNode *exclude )
1006{
1007    _findNodes( sphere, list, exclude, false, mOctree );
1008}
1009
1010void OctreeSceneManager::findNodesIn( const PlaneBoundedVolume &volume, std::list < SceneNode * > &list, SceneNode *exclude )
1011{
1012    _findNodes( volume, list, exclude, false, mOctree );
1013}
1014
1015void OctreeSceneManager::findNodesIn( const Ray &r, std::list < SceneNode * > &list, SceneNode *exclude )
1016{
1017    _findNodes( r, list, exclude, false, mOctree );
1018}
1019
1020void OctreeSceneManager::resize( const AxisAlignedBox &box )
1021{
1022    std::list < SceneNode * > nodes;
1023    std::list < SceneNode * > ::iterator it;
1024
1025    _findNodes( mOctree->mBox, nodes, 0, true, mOctree );
1026
1027    delete mOctree;
1028
1029    mOctree = new Octree( 0 );
1030#ifdef GTP_VISIBILITY_MODIFIED_OGRE
1031        mNumOctreeNodes = 1;
1032#endif // GTP_VISIBILITY_MODIFIED_OGRE
1033    mOctree->mBox = box;
1034
1035    it = nodes.begin();
1036
1037    while ( it != nodes.end() )
1038    {
1039        OctreeNode * on = static_cast < OctreeNode * > ( *it );
1040        on -> setOctant( 0 );
1041        _updateOctreeNode( on );
1042        ++it;
1043    }
1044
1045}
1046
1047bool OctreeSceneManager::setOption( const String & key, const void * val )
1048{
1049    if ( key == "Size" )
1050    {
1051        resize( * static_cast < const AxisAlignedBox * > ( val ) );
1052        return true;
1053    }
1054
1055    else if ( key == "Depth" )
1056    {
1057        mMaxDepth = * static_cast < const int * > ( val );
1058        resize( mOctree->mBox );
1059        return true;
1060    }
1061
1062    else if ( key == "ShowOctree" )
1063    {
1064        mShowBoxes = * static_cast < const bool * > ( val );
1065        return true;
1066    }
1067
1068    else if ( key == "CullCamera" )
1069    {
1070        mCullCamera = * static_cast < const bool * > ( val );
1071        return true;
1072    }
1073
1074    return SceneManager::setOption( key, val );
1075
1076
1077}
1078
1079bool OctreeSceneManager::getOption( const String & key, void *val )
1080{
1081    if ( key == "Size" )
1082    {
1083        AxisAlignedBox * b = static_cast < AxisAlignedBox * > ( val );
1084        b -> setExtents( mOctree->mBox.getMinimum(), mOctree->mBox.getMaximum() );
1085        return true;
1086    }
1087
1088    else if ( key == "Depth" )
1089    {
1090        * static_cast < int * > ( val ) = mMaxDepth;
1091        return true;
1092    }
1093
1094    else if ( key == "ShowOctree" )
1095    {
1096
1097        * static_cast < bool * > ( val ) = mShowBoxes;
1098        return true;
1099    }
1100
1101    else if ( key == "CullCamera" )
1102    {
1103        * static_cast < bool * > ( val ) = mCullCamera;
1104        return true;
1105    }
1106
1107    return SceneManager::getOption( key, val );
1108
1109}
1110
1111void OctreeSceneManager::clearScene(void)
1112{
1113    SceneManager::clearScene();
1114    init(mBox, mMaxDepth);
1115
1116}
1117
1118//---------------------------------------------------------------------
1119AxisAlignedBoxSceneQuery*
1120OctreeSceneManager::createAABBQuery(const AxisAlignedBox& box, unsigned long mask)
1121{
1122    OctreeAxisAlignedBoxSceneQuery* q = new OctreeAxisAlignedBoxSceneQuery(this);
1123    q->setBox(box);
1124    q->setQueryMask(mask);
1125    return q;
1126}
1127//---------------------------------------------------------------------
1128SphereSceneQuery*
1129OctreeSceneManager::createSphereQuery(const Sphere& sphere, unsigned long mask)
1130{
1131    OctreeSphereSceneQuery* q = new OctreeSphereSceneQuery(this);
1132    q->setSphere(sphere);
1133    q->setQueryMask(mask);
1134    return q;
1135}
1136//---------------------------------------------------------------------
1137PlaneBoundedVolumeListSceneQuery*
1138OctreeSceneManager::createPlaneBoundedVolumeQuery(const PlaneBoundedVolumeList& volumes,
1139        unsigned long mask)
1140{
1141    OctreePlaneBoundedVolumeListSceneQuery* q = new OctreePlaneBoundedVolumeListSceneQuery(this);
1142    q->setVolumes(volumes);
1143    q->setQueryMask(mask);
1144    return q;
1145}
1146
1147//---------------------------------------------------------------------
1148RaySceneQuery*
1149OctreeSceneManager::createRayQuery(const Ray& ray, unsigned long mask)
1150{
1151    OctreeRaySceneQuery* q = new OctreeRaySceneQuery(this);
1152    q->setRay(ray);
1153    q->setQueryMask(mask);
1154    return q;
1155}
1156//---------------------------------------------------------------------
1157IntersectionSceneQuery*
1158OctreeSceneManager::createIntersectionQuery(unsigned long mask)
1159{
1160
1161    // Octree implementation performs WORSE for < 500 objects
1162    // TODO: optimise it so it's better in all cases
1163    //OctreeIntersectionSceneQuery* q = new OctreeIntersectionSceneQuery(this);
1164    DefaultIntersectionSceneQuery* q = new DefaultIntersectionSceneQuery(this);
1165    q->setQueryMask(mask);
1166    return q;
1167}
1168#ifdef GTP_VISIBILITY_MODIFIED_OGRE
1169//-----------------------------------------------------------------------       
1170BoxList *OctreeSceneManager::getBoxes()
1171{
1172        return &mBoxes;
1173}
1174//-----------------------------------------------------------------------
1175void OctreeSceneManager::_renderOctant(Camera *cam, Octree *octant, bool onlyShadowCasters)
1176{
1177        //Add stuff to be rendered;
1178    NodeList::iterator it = octant->mNodes.begin();
1179         
1180    while(it != octant->mNodes.end())
1181    {
1182                OctreeNode *sn = *it;
1183
1184        ++mNumObjects;
1185
1186                // check bounding box visibility of scene nodes
1187                if (cam->isVisible(sn->_getWorldAABB()))
1188                {
1189                        sn->_addToRenderQueue(cam, getRenderQueue(), onlyShadowCasters);
1190                        //node->_findVisibleObjects(cam, getRenderQueue(), false, mDisplayNodes, false);
1191                        mVisible.push_back(sn);
1192
1193                        if (mDisplayNodes)
1194                        {
1195                                getRenderQueue()->addRenderable(sn);
1196                        }
1197
1198                        // check if the scene manager or this node wants the bounding box shown.
1199                        if (sn->getShowBoundingBox() || mShowBoundingBoxes)
1200                        {
1201                                sn->_addBoundingBoxToQueue(getRenderQueue());
1202                        }
1203                }
1204        ++it;
1205        }
1206
1207        if (mShowBoxes)
1208        {
1209                getRenderQueue()->addRenderable(octant->getWireBoundingBox());
1210                mBoxes.push_back(octant->getWireBoundingBox());
1211        }
1212
1213        SceneManager::_renderVisibleObjects();
1214
1215        // delete all rendered objects from the render queue
1216        _deleteRenderedQueueGroups();
1217}
1218#endif // GTP_VISIBILITY_MODIFIED_OGRE
1219}
Note: See TracBrowser for help on using the repository browser.