source: OGRE/trunk/ogrenew/PlugIns/OctreeSceneManager/src/OgreTerrainPageSource.cpp @ 692

Revision 692, 6.2 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

RevLine 
[692]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 "OgreTerrainPageSource.h"
26#include "OgreTerrainPage.h"
27#include "OgreTerrainRenderable.h"
28#include "OgreSceneNode.h"
29#include "OgreTerrainSceneManager.h"
30
31namespace Ogre {
32
33    //-----------------------------------------------------------------------
34    template<> TerrainPageSourceListenerManager* Singleton<TerrainPageSourceListenerManager>::ms_Singleton = 0;
35    TerrainPageSourceListenerManager* TerrainPageSourceListenerManager::getSingletonPtr(void)
36    {
37        return ms_Singleton;
38    }
39    TerrainPageSourceListenerManager& TerrainPageSourceListenerManager::getSingleton(void)
40    { 
41        assert( ms_Singleton );  return ( *ms_Singleton ); 
42    }
43    //-------------------------------------------------------------------------
44        void TerrainPageSourceListenerManager::addListener(TerrainPageSourceListener* pl)
45        {
46        mPageSourceListeners.push_back(pl);
47        }
48    //-------------------------------------------------------------------------
49        void TerrainPageSourceListenerManager::removeListener(TerrainPageSourceListener* pl)
50        {
51        PageSourceListenerList::iterator i, iend;
52        iend = mPageSourceListeners.end();
53        for(i = mPageSourceListeners.begin(); i != iend; ++i)
54        {
55            if (*i == pl)
56            {
57                mPageSourceListeners.erase(i);
58                break;
59            }
60        }
61        }
62    //-------------------------------------------------------------------------
63        void TerrainPageSourceListenerManager::firePageConstructed(
64                TerrainSceneManager* sm, size_t pagex, size_t pagez, Real* heightData)
65        {
66        PageSourceListenerList::iterator i, iend;
67        iend = mPageSourceListeners.end();
68        for(i = mPageSourceListeners.begin(); i != iend; ++i)
69        {
70            (*i)->pageConstructed(sm, pagex, pagez, heightData);
71        }
72        }
73        //-------------------------------------------------------------------------
74        TerrainPageSource::TerrainPageSource() : mSceneManager(0), mAsyncLoading(false) {
75        }
76        //-------------------------------------------------------------------------
77        TerrainPage* TerrainPageSource::buildPage(Real* heightData, const MaterialPtr& pMaterial)
78    {
79        String name;
80
81        // Create a Terrain Page
82        TerrainPage* page = new TerrainPage((mPageSize-1) / (mTileSize-1));
83        // Create a node for all tiles to be attached to
84        // Note we sequentially name since page can be attached at different points
85        // so page x/z is not appropriate
86                StringUtil::StrStreamType page_str;
87                size_t pageIndex = mSceneManager->_getPageCount();
88                page_str << pageIndex;
89        name = "page[";
90        name += page_str.str() + "]";
91        page->pageSceneNode = mSceneManager->createSceneNode(name);
92       
93        size_t q = 0;
94        for ( size_t j = 0; j < mPageSize - 1; j += ( mTileSize - 1 ) )
95        {
96            size_t p = 0;
97
98            for ( size_t i = 0; i < mPageSize - 1; i += ( mTileSize - 1 ) )
99            {
100                                StringUtil::StrStreamType new_name_str;
101                               
102                // Create scene node for the tile and the TerrainRenderable
103                new_name_str << "tile[" << pageIndex << "][" << (int)p << "," << (int)q << "]";
104                                name = new_name_str.str();
105
106                SceneNode *c = page->pageSceneNode->createChildSceneNode( name );
107                TerrainRenderable *tile = new TerrainRenderable(name, mSceneManager);
108                                // set queue
109                                tile->setRenderQueueGroup(mSceneManager->getWorldGeometryRenderQueue());
110                // Initialise the tile
111                tile->setMaterial(pMaterial);
112                tile->initialise(i, j, heightData);
113                // Attach it to the page
114                page->tiles[ p ][ q ] = tile;
115                // Attach it to the node
116                c ->attachObject( tile );
117                p++;
118            }
119
120            q++;
121
122        }
123
124        pageIndex++;
125
126        // calculate neighbours for page
127        page->linkNeighbours();
128
129                if(mSceneManager->getOptions().lit)
130                {
131                        q = 0;
132                        for ( size_t j = 0; j < mPageSize - 1; j += ( mTileSize - 1 ) )
133                        {
134                                size_t p = 0;
135
136                                for ( size_t i = 0; i < mPageSize - 1; i += ( mTileSize - 1 ) )
137                                {
138                                        page->tiles[ p ][ q ]->_calculateNormals();
139                                        p++;
140                                }
141                                q++;
142                        }
143                }
144
145        return page;
146    }
147    //-------------------------------------------------------------------------
148    void TerrainPageSource::firePageConstructed(size_t pagex, size_t pagez, Real* heightData)
149    {
150                TerrainPageSourceListenerManager::getSingleton().firePageConstructed(
151                        mSceneManager, pagex, pagez, heightData);
152    }
153    //-------------------------------------------------------------------------
154    void TerrainPageSource::addListener(TerrainPageSourceListener* pl)
155    {
156                TerrainPageSourceListenerManager::getSingleton().addListener(pl);
157    }
158    //-------------------------------------------------------------------------
159    void TerrainPageSource::removeListener(TerrainPageSourceListener* pl)
160    {
161                TerrainPageSourceListenerManager::getSingleton().removeListener(pl);
162
163    }
164    //-------------------------------------------------------------------------
165
166}
Note: See TracBrowser for help on using the repository browser.