Main Page Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# OgreTerrainPage.cpp

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004 (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright © 2000-2004 The OGRE Team
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023 -----------------------------------------------------------------------------
    00024 */
    00025 #include "OgreTerrainPage.h"
    00026 #include "OgreTerrainRenderable.h"
    00027
    00028 namespace Ogre {
    00029
    00030     //-------------------------------------------------------------------------
    00031     TerrainPage::TerrainPage(unsigned short numTiles)
    00032     {
    00033         tilesPerPage = numTiles;
    00034         // Set up an empty array of TerrainRenderable pointers
    00035         int i, j;
    00036         for ( i = 0; i < tilesPerPage; i++ )
    00037         {
    00038             tiles.push_back( TerrainRow() );
    00039
    00040             for ( j = 0; j < tilesPerPage; j++ )
    00041             {
    00042                 tiles[ i ].push_back( 0 );
    00043             }
    00044         }
    00045
    00046         pageSceneNode = 0;
    00047
    00048     }
    00049     //-------------------------------------------------------------------------
    00050     TerrainPage::~TerrainPage()
    00051     {
    00052         Terrain2D::iterator i, iend;
    00053         iend = tiles.end();
    00054         for (i = tiles.begin(); i != iend; ++i)
    00055         {
    00056             TerrainRow::iterator j, jend;
    00057             jend = i->end();
    00058             for (j = i->begin(); j != jend; ++j)
    00059             {
    00060                 delete *j;
    00061                 *j = 0;
    00062             }
    00063         }
    00064
    00065     }
    00066     //-------------------------------------------------------------------------
    00067     void TerrainPage::linkNeighbours(void)
    00068     {
    00069         //setup the neighbor links.
    00070
    00071         for ( size_t j = 0; j < tilesPerPage; j++ )
    00072         {
    00073             for ( size_t i = 0; i < tilesPerPage; i++ )
    00074             {
    00075                 if ( j != tilesPerPage - 1 )
    00076                 {
    00077                     tiles[ i ][ j ] -> _setNeighbor( TerrainRenderable::SOUTH, tiles[ i ][ j + 1 ] );
    00078                     tiles[ i ][ j + 1 ] -> _setNeighbor( TerrainRenderable::NORTH, tiles[ i ][ j ] );
    00079                 }
    00080
    00081                 if ( i != tilesPerPage - 1 )
    00082                 {
    00083                     tiles[ i ][ j ] -> _setNeighbor( TerrainRenderable::EAST, tiles[ i + 1 ][ j ] );
    00084                     tiles[ i + 1 ][ j ] -> _setNeighbor( TerrainRenderable::WEST, tiles[ i ][ j ] );
    00085                 }
    00086
    00087             }
    00088         }
    00089     }
    00090     //-------------------------------------------------------------------------
    00091     TerrainRenderable * TerrainPage::getTerrainTile( const Vector3 & pt )
    00092     {
    00093         /* Since we don't know if the terrain is square, or has holes, we use a line trace
    00094         to find the containing tile...
    00095         */
    00096
    00097         TerrainRenderable * tile = tiles[ 0 ][ 0 ];
    00098
    00099         while ( tile != 0 )
    00100         {
    00101             AxisAlignedBox b = tile -> getBoundingBox();
    00102             const Vector3 *corners = b.getAllCorners();
    00103
    00104             if ( pt.x < corners[ 0 ].x )
    00105                 tile = tile -> _getNeighbor( TerrainRenderable::WEST );
    00106             else if ( pt.x > corners[ 4 ].x )
    00107                 tile = tile -> _getNeighbor( TerrainRenderable::EAST );
    00108             else if ( pt.z < corners[ 0 ].z )
    00109                 tile = tile -> _getNeighbor( TerrainRenderable::NORTH );
    00110             else if ( pt.z > corners[ 4 ].z )
    00111                 tile = tile -> _getNeighbor( TerrainRenderable::SOUTH );
    00112             else
    00113                 return tile;
    00114         }
    00115
    00116         return 0;
    00117     }
    00118
    00119 }
    00120

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:48 2004