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

Revision 115, 31.7 KB checked in by mattausch, 19 years ago (diff)

added depth pass algorithm + delayed transparent object rendering (so depth ordering is right)

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
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        _addOctreeNode( n, octant -> mChildren[ x ][ y ][ z ], ++depth );
511
512    }
513
514    else
515    {
516        octant -> _addNode( n );
517    }
518}
519
520
521SceneNode * OctreeSceneManager::createSceneNode( void )
522{
523    OctreeNode * on = new OctreeNode( this );
524    mSceneNodes[ on->getName() ] = on;
525    return on;
526}
527
528SceneNode * OctreeSceneManager::createSceneNode( const String &name )
529{
530    OctreeNode * on = new OctreeNode( this, name );
531    mSceneNodes[ on->getName() ] = on;
532    return on;
533}
534
535void OctreeSceneManager::_updateSceneGraph( Camera * cam )
536{
537    SceneManager::_updateSceneGraph( cam );
538}
539
540void OctreeSceneManager::_alertVisibleObjects( void )
541{
542    NodeList::iterator it = mVisible.begin();
543
544    while ( it != mVisible.end() )
545    {
546        OctreeNode * node = *it;
547
548        ++it;
549    }
550}
551
552void OctreeSceneManager::_findVisibleObjects( Camera * cam, bool onlyShadowCasters )
553{
554
555    getRenderQueue()->clear();
556    mBoxes.clear();
557    mVisible.clear();
558
559    if ( mCullCamera )
560    {
561        Camera * c = getCamera( "CullCamera" );
562
563        if ( c != 0 )
564            cam = getCamera( "CullCamera" );
565    }
566
567    mNumObjects = 0;
568
569    //walk the octree, adding all visible Octreenodes nodes to the render queue.
570    walkOctree( static_cast < OctreeCamera * > ( cam ), getRenderQueue(), mOctree, false, onlyShadowCasters );
571
572
573    // Show the octree boxes & cull camera if required
574    if ( mShowBoxes || mCullCamera )
575    {
576
577
578
579        if ( mShowBoxes )
580        {
581            for ( BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it )
582            {
583                getRenderQueue()->addRenderable(*it);
584            }
585        }
586
587        if ( mCullCamera )
588        {
589            OctreeCamera * c = static_cast<OctreeCamera*>(getCamera( "CullCamera" ));
590
591            if ( c != 0 )
592            {
593                getRenderQueue()->addRenderable(c);
594            }
595        }
596
597    }
598
599
600
601}
602
603void OctreeSceneManager::walkOctree( OctreeCamera *camera, RenderQueue *queue,
604                                     Octree *octant, bool foundvisible, bool onlyShadowCasters )
605{
606
607    //return immediately if nothing is in the node.
608    if ( octant -> numNodes() == 0 )
609        return ;
610
611    OctreeCamera::Visibility v = OctreeCamera::NONE;
612
613    if ( foundvisible )
614    {
615        v = OctreeCamera::FULL;
616    }
617
618    else if ( octant == mOctree )
619    {
620        v = OctreeCamera::PARTIAL;
621    }
622
623    else
624    {
625        AxisAlignedBox box;
626        octant -> _getCullBounds( &box );
627        v = camera -> getVisibility( box );
628    }
629
630
631    // if the octant is visible, or if it's the root node...
632    if ( v != OctreeCamera::NONE )
633    {
634
635        //Add stuff to be rendered;
636        NodeList::iterator it = octant -> mNodes.begin();
637
638        if ( mShowBoxes )
639        {
640            mBoxes.push_back( octant->getWireBoundingBox() );
641        }
642
643        bool vis = true;
644
645        while ( it != octant -> mNodes.end() )
646        {
647            OctreeNode * sn = *it;
648
649            // if this octree is partially visible, manually cull all
650            // scene nodes attached directly to this level.
651
652            if ( v == OctreeCamera::PARTIAL )
653                vis = camera -> isVisible( sn -> _getWorldAABB() );
654
655            if ( vis )
656            {
657
658                mNumObjects++;
659                sn -> _addToRenderQueue(camera, queue, onlyShadowCasters );
660
661                mVisible.push_back( sn );
662
663                if ( mDisplayNodes )
664                    queue -> addRenderable( sn );
665
666                // check if the scene manager or this node wants the bounding box shown.
667                if (sn->getShowBoundingBox() || mShowBoundingBoxes)
668                    sn->_addBoundingBoxToQueue(queue);
669            }
670
671            ++it;
672        }
673
674        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
675            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 0 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
676
677        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
678            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 0 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
679
680        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
681            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 1 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
682
683        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
684            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 1 ][ 0 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
685
686        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
687            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 0 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
688
689        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
690            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 0 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
691
692        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
693            walkOctree( camera, queue, octant -> mChildren[ 0 ][ 1 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
694
695        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
696            walkOctree( camera, queue, octant -> mChildren[ 1 ][ 1 ][ 1 ], ( v == OctreeCamera::FULL ), onlyShadowCasters );
697
698    }
699
700}
701
702// --- non template versions
703void _findNodes( const AxisAlignedBox &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
704{
705
706        if ( !full )
707        {
708                AxisAlignedBox obox;
709                octant -> _getCullBounds( &obox );
710
711                Intersection isect = intersect( t, obox );
712
713                if ( isect == OUTSIDE )
714                        return ;
715
716                full = ( isect == INSIDE );
717        }
718
719
720        NodeList::iterator it = octant -> mNodes.begin();
721
722        while ( it != octant -> mNodes.end() )
723        {
724                OctreeNode * on = ( *it );
725
726                if ( on != exclude )
727                {
728                        if ( full )
729                        {
730                                list.push_back( on );
731                        }
732
733                        else
734                        {
735                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
736
737                                if ( nsect != OUTSIDE )
738                                {
739                                        list.push_back( on );
740                                }
741                        }
742
743                }
744
745                ++it;
746        }
747
748
749
750        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
751                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
752
753        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
754                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
755
756        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
757                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
758
759        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
760                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
761
762        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
763                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
764
765        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
766                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
767
768        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
769                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
770
771        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
772                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
773
774}
775
776void _findNodes( const Sphere &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
777{
778
779        if ( !full )
780        {
781                AxisAlignedBox obox;
782                octant -> _getCullBounds( &obox );
783
784                Intersection isect = intersect( t, obox );
785
786                if ( isect == OUTSIDE )
787                        return ;
788
789                full = ( isect == INSIDE );
790        }
791
792
793        NodeList::iterator it = octant -> mNodes.begin();
794
795        while ( it != octant -> mNodes.end() )
796        {
797                OctreeNode * on = ( *it );
798
799                if ( on != exclude )
800                {
801                        if ( full )
802                        {
803                                list.push_back( on );
804                        }
805
806                        else
807                        {
808                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
809
810                                if ( nsect != OUTSIDE )
811                                {
812                                        list.push_back( on );
813                                }
814                        }
815
816                }
817
818                ++it;
819        }
820
821
822
823        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
824                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
825
826        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
827                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
828
829        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
830                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
831
832        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
833                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
834
835        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
836                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
837
838        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
839                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
840
841        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
842                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
843
844        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
845                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
846
847}
848
849
850void _findNodes( const PlaneBoundedVolume &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
851{
852
853        if ( !full )
854        {
855                AxisAlignedBox obox;
856                octant -> _getCullBounds( &obox );
857
858                Intersection isect = intersect( t, obox );
859
860                if ( isect == OUTSIDE )
861                        return ;
862
863                full = ( isect == INSIDE );
864        }
865
866
867        NodeList::iterator it = octant -> mNodes.begin();
868
869        while ( it != octant -> mNodes.end() )
870        {
871                OctreeNode * on = ( *it );
872
873                if ( on != exclude )
874                {
875                        if ( full )
876                        {
877                                list.push_back( on );
878                        }
879
880                        else
881                        {
882                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
883
884                                if ( nsect != OUTSIDE )
885                                {
886                                        list.push_back( on );
887                                }
888                        }
889
890                }
891
892                ++it;
893        }
894
895
896
897        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
898                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
899
900        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
901                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
902
903        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
904                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
905
906        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
907                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
908
909        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
910                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
911
912        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
913                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
914
915        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
916                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
917
918        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
919                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
920
921}
922
923void _findNodes( const Ray &t, std::list < SceneNode * > &list, SceneNode *exclude, bool full, Octree *octant )
924{
925
926        if ( !full )
927        {
928                AxisAlignedBox obox;
929                octant -> _getCullBounds( &obox );
930
931                Intersection isect = intersect( t, obox );
932
933                if ( isect == OUTSIDE )
934                        return ;
935
936                full = ( isect == INSIDE );
937        }
938
939
940        NodeList::iterator it = octant -> mNodes.begin();
941
942        while ( it != octant -> mNodes.end() )
943        {
944                OctreeNode * on = ( *it );
945
946                if ( on != exclude )
947                {
948                        if ( full )
949                        {
950                                list.push_back( on );
951                        }
952
953                        else
954                        {
955                                Intersection nsect = intersect( t, on -> _getWorldAABB() );
956
957                                if ( nsect != OUTSIDE )
958                                {
959                                        list.push_back( on );
960                                }
961                        }
962
963                }
964
965                ++it;
966        }
967
968
969
970        if ( octant -> mChildren[ 0 ][ 0 ][ 0 ] != 0 )
971                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 0 ] );
972
973        if ( octant -> mChildren[ 1 ][ 0 ][ 0 ] != 0 )
974                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 0 ] );
975
976        if ( octant -> mChildren[ 0 ][ 1 ][ 0 ] != 0 )
977                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 0 ] );
978
979        if ( octant -> mChildren[ 1 ][ 1 ][ 0 ] != 0 )
980                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 0 ] );
981
982        if ( octant -> mChildren[ 0 ][ 0 ][ 1 ] != 0 )
983                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 0 ][ 1 ] );
984
985        if ( octant -> mChildren[ 1 ][ 0 ][ 1 ] != 0 )
986                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 0 ][ 1 ] );
987
988        if ( octant -> mChildren[ 0 ][ 1 ][ 1 ] != 0 )
989                _findNodes( t, list, exclude, full, octant -> mChildren[ 0 ][ 1 ][ 1 ] );
990
991        if ( octant -> mChildren[ 1 ][ 1 ][ 1 ] != 0 )
992                _findNodes( t, list, exclude, full, octant -> mChildren[ 1 ][ 1 ][ 1 ] );
993
994}
995
996void OctreeSceneManager::findNodesIn( const AxisAlignedBox &box, std::list < SceneNode * > &list, SceneNode *exclude )
997{
998    _findNodes( box, list, exclude, false, mOctree );
999}
1000
1001void OctreeSceneManager::findNodesIn( const Sphere &sphere, std::list < SceneNode * > &list, SceneNode *exclude )
1002{
1003    _findNodes( sphere, list, exclude, false, mOctree );
1004}
1005
1006void OctreeSceneManager::findNodesIn( const PlaneBoundedVolume &volume, std::list < SceneNode * > &list, SceneNode *exclude )
1007{
1008    _findNodes( volume, list, exclude, false, mOctree );
1009}
1010
1011void OctreeSceneManager::findNodesIn( const Ray &r, std::list < SceneNode * > &list, SceneNode *exclude )
1012{
1013    _findNodes( r, list, exclude, false, mOctree );
1014}
1015
1016void OctreeSceneManager::resize( const AxisAlignedBox &box )
1017{
1018    std::list < SceneNode * > nodes;
1019    std::list < SceneNode * > ::iterator it;
1020
1021    _findNodes( mOctree->mBox, nodes, 0, true, mOctree );
1022
1023    delete mOctree;
1024
1025    mOctree = new Octree( 0 );
1026#ifdef GTP_VISIBILITY_MODIFIED_OGRE
1027        mNumOctreeNodes = 1;
1028#endif // GTP_VISIBILITY_MODIFIED_OGRE
1029    mOctree->mBox = box;
1030
1031    it = nodes.begin();
1032
1033    while ( it != nodes.end() )
1034    {
1035        OctreeNode * on = static_cast < OctreeNode * > ( *it );
1036        on -> setOctant( 0 );
1037        _updateOctreeNode( on );
1038        ++it;
1039    }
1040
1041}
1042
1043bool OctreeSceneManager::setOption( const String & key, const void * val )
1044{
1045    if ( key == "Size" )
1046    {
1047        resize( * static_cast < const AxisAlignedBox * > ( val ) );
1048        return true;
1049    }
1050
1051    else if ( key == "Depth" )
1052    {
1053        mMaxDepth = * static_cast < const int * > ( val );
1054        resize( mOctree->mBox );
1055        return true;
1056    }
1057
1058    else if ( key == "ShowOctree" )
1059    {
1060        mShowBoxes = * static_cast < const bool * > ( val );
1061        return true;
1062    }
1063
1064    else if ( key == "CullCamera" )
1065    {
1066        mCullCamera = * static_cast < const bool * > ( val );
1067        return true;
1068    }
1069
1070    return SceneManager::setOption( key, val );
1071
1072
1073}
1074
1075bool OctreeSceneManager::getOption( const String & key, void *val )
1076{
1077    if ( key == "Size" )
1078    {
1079        AxisAlignedBox * b = static_cast < AxisAlignedBox * > ( val );
1080        b -> setExtents( mOctree->mBox.getMinimum(), mOctree->mBox.getMaximum() );
1081        return true;
1082    }
1083
1084    else if ( key == "Depth" )
1085    {
1086        * static_cast < int * > ( val ) = mMaxDepth;
1087        return true;
1088    }
1089
1090    else if ( key == "ShowOctree" )
1091    {
1092
1093        * static_cast < bool * > ( val ) = mShowBoxes;
1094        return true;
1095    }
1096
1097    else if ( key == "CullCamera" )
1098    {
1099        * static_cast < bool * > ( val ) = mCullCamera;
1100        return true;
1101    }
1102
1103    return SceneManager::getOption( key, val );
1104
1105}
1106
1107void OctreeSceneManager::clearScene(void)
1108{
1109    SceneManager::clearScene();
1110    init(mBox, mMaxDepth);
1111
1112}
1113
1114//---------------------------------------------------------------------
1115AxisAlignedBoxSceneQuery*
1116OctreeSceneManager::createAABBQuery(const AxisAlignedBox& box, unsigned long mask)
1117{
1118    OctreeAxisAlignedBoxSceneQuery* q = new OctreeAxisAlignedBoxSceneQuery(this);
1119    q->setBox(box);
1120    q->setQueryMask(mask);
1121    return q;
1122}
1123//---------------------------------------------------------------------
1124SphereSceneQuery*
1125OctreeSceneManager::createSphereQuery(const Sphere& sphere, unsigned long mask)
1126{
1127    OctreeSphereSceneQuery* q = new OctreeSphereSceneQuery(this);
1128    q->setSphere(sphere);
1129    q->setQueryMask(mask);
1130    return q;
1131}
1132//---------------------------------------------------------------------
1133PlaneBoundedVolumeListSceneQuery*
1134OctreeSceneManager::createPlaneBoundedVolumeQuery(const PlaneBoundedVolumeList& volumes,
1135        unsigned long mask)
1136{
1137    OctreePlaneBoundedVolumeListSceneQuery* q = new OctreePlaneBoundedVolumeListSceneQuery(this);
1138    q->setVolumes(volumes);
1139    q->setQueryMask(mask);
1140    return q;
1141}
1142
1143//---------------------------------------------------------------------
1144RaySceneQuery*
1145OctreeSceneManager::createRayQuery(const Ray& ray, unsigned long mask)
1146{
1147    OctreeRaySceneQuery* q = new OctreeRaySceneQuery(this);
1148    q->setRay(ray);
1149    q->setQueryMask(mask);
1150    return q;
1151}
1152//---------------------------------------------------------------------
1153IntersectionSceneQuery*
1154OctreeSceneManager::createIntersectionQuery(unsigned long mask)
1155{
1156
1157    // Octree implementation performs WORSE for < 500 objects
1158    // TODO: optimise it so it's better in all cases
1159    //OctreeIntersectionSceneQuery* q = new OctreeIntersectionSceneQuery(this);
1160    DefaultIntersectionSceneQuery* q = new DefaultIntersectionSceneQuery(this);
1161    q->setQueryMask(mask);
1162    return q;
1163}
1164#ifdef GTP_VISIBILITY_MODIFIED_OGRE
1165//-----------------------------------------------------------------------       
1166BoxList *OctreeSceneManager::getBoxes()
1167{
1168        return &mBoxes;
1169}
1170//-----------------------------------------------------------------------
1171void OctreeSceneManager::_renderOctant(Camera *cam, Octree *octant, bool onlyShadowCasters,
1172                                                                           bool leaveTransparentsInQueue)//, bool useZPassQueue)
1173{
1174        //Add stuff to be rendered;
1175    NodeList::iterator it = octant->mNodes.begin();
1176         
1177    while(it != octant->mNodes.end())
1178    {
1179                OctreeNode *sn = *it;
1180
1181        ++mNumObjects;
1182
1183                // check bounding box visibility of scene nodes
1184                if (cam->isVisible(sn->_getWorldAABB()))
1185                {
1186                        sn->_addToRenderQueue(cam, getRenderQueue(), onlyShadowCasters);
1187                        mVisible.push_back(sn);
1188
1189                        if (mDisplayNodes)
1190                        {
1191                                getRenderQueue()->addRenderable(sn);
1192                        }
1193
1194                        // check if the scene manager or this node wants the bounding box shown.
1195                        if (sn->getShowBoundingBox() || mShowBoundingBoxes)
1196                        {
1197                                sn->_addBoundingBoxToQueue(getRenderQueue());
1198                        }
1199                }
1200        ++it;
1201        }
1202
1203        if (mShowBoxes)
1204        {
1205                octant->getWireBoundingBox()->setMaterial("BaseWhiteNoLighting");
1206
1207                getRenderQueue()->addRenderable(octant->getWireBoundingBox());
1208                mBoxes.push_back(octant->getWireBoundingBox());
1209        }
1210
1211        //-- the actual rendering
1212        SceneManager::_renderVisibleObjects();
1213        // delete all rendered objects from the render queue
1214        _deleteRenderedQueueGroups(leaveTransparentsInQueue);
1215}
1216#endif // GTP_VISIBILITY_MODIFIED_OGRE
1217}
Note: See TracBrowser for help on using the repository browser.