1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://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 |
|
---|
31 | namespace 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(size_t pagex, size_t pagez, Real* heightData)
|
---|
64 | {
|
---|
65 | PageSourceListenerList::iterator i, iend;
|
---|
66 | iend = mPageSourceListeners.end();
|
---|
67 | for(i = mPageSourceListeners.begin(); i != iend; ++i)
|
---|
68 | {
|
---|
69 | (*i)->pageConstructed(pagex, pagez, heightData);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | //-------------------------------------------------------------------------
|
---|
73 | TerrainPageSource::TerrainPageSource() : mSceneManager(0), mAsyncLoading(false) {
|
---|
74 | }
|
---|
75 | //-------------------------------------------------------------------------
|
---|
76 | TerrainPage* TerrainPageSource::buildPage(Real* heightData, const MaterialPtr& pMaterial)
|
---|
77 | {
|
---|
78 | String name;
|
---|
79 |
|
---|
80 | // Create a Terrain Page
|
---|
81 | TerrainPage* page = new TerrainPage((mPageSize-1) / (mTileSize-1));
|
---|
82 | // Create a node for all tiles to be attached to
|
---|
83 | // Note we sequentially name since page can be attached at different points
|
---|
84 | // so page x/z is not appropriate
|
---|
85 | static size_t pageIndex = 0;
|
---|
86 | StringUtil::StrStreamType page_str;
|
---|
87 | page_str << pageIndex;
|
---|
88 | name = "page[";
|
---|
89 | name += page_str.str() + "]";
|
---|
90 | page->pageSceneNode = mSceneManager->createSceneNode(name);
|
---|
91 |
|
---|
92 | size_t q = 0;
|
---|
93 | for ( size_t j = 0; j < mPageSize - 1; j += ( mTileSize - 1 ) )
|
---|
94 | {
|
---|
95 | size_t p = 0;
|
---|
96 |
|
---|
97 | for ( size_t i = 0; i < mPageSize - 1; i += ( mTileSize - 1 ) )
|
---|
98 | {
|
---|
99 | StringUtil::StrStreamType new_name_str;
|
---|
100 |
|
---|
101 | // Create scene node for the tile and the TerrainRenderable
|
---|
102 | new_name_str << "tile[" << pageIndex << "][" << (int)p << "," << (int)q << "]";
|
---|
103 | name = new_name_str.str();
|
---|
104 |
|
---|
105 | SceneNode *c = page->pageSceneNode->createChildSceneNode( name );
|
---|
106 | TerrainRenderable *tile = new TerrainRenderable(name);
|
---|
107 | // set queue
|
---|
108 | tile->setRenderQueueGroup(mSceneManager->getWorldGeometryRenderQueue());
|
---|
109 | // Initialise the tile
|
---|
110 | tile->setMaterial(pMaterial);
|
---|
111 | tile->initialise(i, j, heightData);
|
---|
112 | // Attach it to the page
|
---|
113 | page->tiles[ p ][ q ] = tile;
|
---|
114 | // Attach it to the node
|
---|
115 | c ->attachObject( tile );
|
---|
116 | p++;
|
---|
117 | }
|
---|
118 |
|
---|
119 | q++;
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | pageIndex++;
|
---|
124 |
|
---|
125 | // calculate neighbours for page
|
---|
126 | page->linkNeighbours();
|
---|
127 |
|
---|
128 | if(TerrainSceneManager::getOptions().lit)
|
---|
129 | {
|
---|
130 | q = 0;
|
---|
131 | for ( size_t j = 0; j < mPageSize - 1; j += ( mTileSize - 1 ) )
|
---|
132 | {
|
---|
133 | size_t p = 0;
|
---|
134 |
|
---|
135 | for ( size_t i = 0; i < mPageSize - 1; i += ( mTileSize - 1 ) )
|
---|
136 | {
|
---|
137 | page->tiles[ p ][ q ]->_calculateNormals();
|
---|
138 | p++;
|
---|
139 | }
|
---|
140 | q++;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | return page;
|
---|
145 | }
|
---|
146 | //-------------------------------------------------------------------------
|
---|
147 | void TerrainPageSource::firePageConstructed(size_t pagex, size_t pagez, Real* heightData)
|
---|
148 | {
|
---|
149 | TerrainPageSourceListenerManager::getSingleton().firePageConstructed(pagex, pagez, heightData);
|
---|
150 | }
|
---|
151 | //-------------------------------------------------------------------------
|
---|
152 | void TerrainPageSource::addListener(TerrainPageSourceListener* pl)
|
---|
153 | {
|
---|
154 | TerrainPageSourceListenerManager::getSingleton().addListener(pl);
|
---|
155 | }
|
---|
156 | //-------------------------------------------------------------------------
|
---|
157 | void TerrainPageSource::removeListener(TerrainPageSourceListener* pl)
|
---|
158 | {
|
---|
159 | TerrainPageSourceListenerManager::getSingleton().removeListener(pl);
|
---|
160 |
|
---|
161 | }
|
---|
162 | //-------------------------------------------------------------------------
|
---|
163 |
|
---|
164 | }
|
---|