source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTerrainPage.cpp @ 1273

Revision 1273, 4.5 KB checked in by szydlowski, 18 years ago (diff)

Added the KdTerrainSceneManager?, a subclass of the KdTreeSceneManager? capable of rendering terrain like the TerrainSceneManager? from Ogre.
All the *Kd*Terrain* classes are identical to their octree counterparts, save prefixing all classes and structures with Kd to avoid namespace clashes.
This was necessary, since the TerrainSceneManager? was hard coded in these classes, and all references had to be replaced with the KdTerrainSceneManager?.
Also added a comprehensive README for the demo application.

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#include "OgreKdTerrainPage.h"
26#include "OgreKdTerrainRenderable.h"
27
28namespace Ogre {
29
30    //-------------------------------------------------------------------------
31    KdTerrainPage::KdTerrainPage(unsigned short numTiles)
32    {
33        tilesPerPage = numTiles;
34        // Set up an empty array of TerrainRenderable pointers
35        int i, j;
36        for ( i = 0; i < tilesPerPage; i++ )
37        {
38            tiles.push_back( KdTerrainRow() );
39
40            for ( j = 0; j < tilesPerPage; j++ )
41            {
42                tiles[ i ].push_back( 0 );
43            }
44        }
45
46        pageSceneNode = 0;
47
48    }
49    //-------------------------------------------------------------------------
50    KdTerrainPage::~KdTerrainPage()
51    {
52        KdTerrain2D::iterator i, iend;
53        iend = tiles.end();
54        for (i = tiles.begin(); i != iend; ++i)
55        {
56            KdTerrainRow::iterator j, jend;
57            jend = i->end();
58            for (j = i->begin(); j != jend; ++j)
59            {
60                delete *j;
61                *j = 0;
62            }
63        }
64
65    }
66    //-------------------------------------------------------------------------
67    void KdTerrainPage::linkNeighbours(void)
68    {
69        //setup the neighbor links.
70
71        for ( size_t j = 0; j < tilesPerPage; j++ )
72        {
73            for ( size_t i = 0; i < tilesPerPage; i++ )
74            {
75                if ( j != tilesPerPage - 1 )
76                {
77                    tiles[ i ][ j ] -> _setNeighbor( KdTerrainRenderable::SOUTH, tiles[ i ][ j + 1 ] );
78                    tiles[ i ][ j + 1 ] -> _setNeighbor( KdTerrainRenderable::NORTH, tiles[ i ][ j ] );
79                }
80
81                if ( i != tilesPerPage - 1 )
82                {
83                    tiles[ i ][ j ] -> _setNeighbor( KdTerrainRenderable::EAST, tiles[ i + 1 ][ j ] );
84                    tiles[ i + 1 ][ j ] -> _setNeighbor( KdTerrainRenderable::WEST, tiles[ i ][ j ] );
85                }
86
87            }
88        }
89    }
90    //-------------------------------------------------------------------------
91    KdTerrainRenderable * KdTerrainPage::getTerrainTile( const Vector3 & pt )
92    {
93        /* Since we don't know if the terrain is square, or has holes, we use a line trace
94        to find the containing tile...
95        */
96
97        KdTerrainRenderable * tile = tiles[ 0 ][ 0 ];
98
99        while ( tile != 0 )
100        {
101            AxisAlignedBox b = tile -> getBoundingBox();
102            const Vector3 *corners = b.getAllCorners();
103
104            if ( pt.x < corners[ 0 ].x )
105                tile = tile -> _getNeighbor( KdTerrainRenderable::WEST );
106            else if ( pt.x > corners[ 4 ].x )
107                tile = tile -> _getNeighbor( KdTerrainRenderable::EAST );
108            else if ( pt.z < corners[ 0 ].z )
109                tile = tile -> _getNeighbor( KdTerrainRenderable::NORTH );
110            else if ( pt.z > corners[ 4 ].z )
111                tile = tile -> _getNeighbor( KdTerrainRenderable::SOUTH );
112            else
113                return tile;
114        }
115
116        return 0;
117    }
118        //-------------------------------------------------------------------------
119        void KdTerrainPage::setRenderQueue(uint8 qid)
120        {
121                for ( size_t j = 0; j < tilesPerPage; j++ )
122                {
123                        for ( size_t i = 0; i < tilesPerPage; i++ )
124                        {
125                                if ( j != tilesPerPage - 1 )
126                                {
127                                        tiles[ i ][ j ]->setRenderQueueGroup(qid);
128                                }
129                        }
130                }
131        }
132
133}
134
Note: See TracBrowser for help on using the repository browser.