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