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-2006 Torus Knot Software Ltd |
---|
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 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgreTerrainPageSource.h" |
---|
30 | #include "OgreTerrainPage.h" |
---|
31 | #include "OgreTerrainRenderable.h" |
---|
32 | #include "OgreSceneNode.h" |
---|
33 | #include "OgreTerrainSceneManager.h" |
---|
34 | |
---|
35 | namespace Ogre { |
---|
36 | |
---|
37 | //----------------------------------------------------------------------- |
---|
38 | template<> TerrainPageSourceListenerManager* Singleton<TerrainPageSourceListenerManager>::ms_Singleton = 0; |
---|
39 | TerrainPageSourceListenerManager* TerrainPageSourceListenerManager::getSingletonPtr(void) |
---|
40 | { |
---|
41 | return ms_Singleton; |
---|
42 | } |
---|
43 | TerrainPageSourceListenerManager& TerrainPageSourceListenerManager::getSingleton(void) |
---|
44 | { |
---|
45 | assert( ms_Singleton ); return ( *ms_Singleton ); |
---|
46 | } |
---|
47 | //------------------------------------------------------------------------- |
---|
48 | void TerrainPageSourceListenerManager::addListener(TerrainPageSourceListener* pl) |
---|
49 | { |
---|
50 | mPageSourceListeners.push_back(pl); |
---|
51 | } |
---|
52 | //------------------------------------------------------------------------- |
---|
53 | void TerrainPageSourceListenerManager::removeListener(TerrainPageSourceListener* pl) |
---|
54 | { |
---|
55 | PageSourceListenerList::iterator i, iend; |
---|
56 | iend = mPageSourceListeners.end(); |
---|
57 | for(i = mPageSourceListeners.begin(); i != iend; ++i) |
---|
58 | { |
---|
59 | if (*i == pl) |
---|
60 | { |
---|
61 | mPageSourceListeners.erase(i); |
---|
62 | break; |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | //------------------------------------------------------------------------- |
---|
67 | void TerrainPageSourceListenerManager::firePageConstructed( |
---|
68 | TerrainSceneManager* sm, size_t pagex, size_t pagez, Real* heightData) |
---|
69 | { |
---|
70 | PageSourceListenerList::iterator i, iend; |
---|
71 | iend = mPageSourceListeners.end(); |
---|
72 | for(i = mPageSourceListeners.begin(); i != iend; ++i) |
---|
73 | { |
---|
74 | (*i)->pageConstructed(sm, pagex, pagez, heightData); |
---|
75 | } |
---|
76 | } |
---|
77 | //------------------------------------------------------------------------- |
---|
78 | TerrainPageSource::TerrainPageSource() : mSceneManager(0), mAsyncLoading(false) { |
---|
79 | } |
---|
80 | //------------------------------------------------------------------------- |
---|
81 | TerrainPage* TerrainPageSource::buildPage(Real* heightData, const MaterialPtr& pMaterial) |
---|
82 | { |
---|
83 | String name; |
---|
84 | |
---|
85 | // Create a Terrain Page |
---|
86 | TerrainPage* page = new TerrainPage((mPageSize-1) / (mTileSize-1)); |
---|
87 | // Create a node for all tiles to be attached to |
---|
88 | // Note we sequentially name since page can be attached at different points |
---|
89 | // so page x/z is not appropriate |
---|
90 | StringUtil::StrStreamType page_str; |
---|
91 | size_t pageIndex = mSceneManager->_getPageCount(); |
---|
92 | page_str << pageIndex; |
---|
93 | name = "page["; |
---|
94 | name += page_str.str() + "]"; |
---|
95 | if (mSceneManager->hasSceneNode(name)) |
---|
96 | { |
---|
97 | page->pageSceneNode = mSceneManager->getSceneNode(name); |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | page->pageSceneNode = mSceneManager->createSceneNode(name); |
---|
102 | } |
---|
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; |
---|
118 | if (mSceneManager->hasSceneNode(name)) |
---|
119 | { |
---|
120 | c = mSceneManager->getSceneNode( name ); |
---|
121 | if (c->getParentSceneNode() != page->pageSceneNode) |
---|
122 | page->pageSceneNode->addChild(c); |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | c = page->pageSceneNode->createChildSceneNode( name ); |
---|
127 | } |
---|
128 | |
---|
129 | TerrainRenderable *tile = new TerrainRenderable(name, mSceneManager); |
---|
130 | // set queue |
---|
131 | tile->setRenderQueueGroup(mSceneManager->getWorldGeometryRenderQueue()); |
---|
132 | // Initialise the tile |
---|
133 | tile->setMaterial(pMaterial); |
---|
134 | tile->initialise(i, j, heightData); |
---|
135 | // Attach it to the page |
---|
136 | page->tiles[ p ][ q ] = tile; |
---|
137 | // Attach it to the node |
---|
138 | c ->attachObject( tile ); |
---|
139 | p++; |
---|
140 | } |
---|
141 | |
---|
142 | q++; |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | pageIndex++; |
---|
147 | |
---|
148 | // calculate neighbours for page |
---|
149 | page->linkNeighbours(); |
---|
150 | |
---|
151 | if(mSceneManager->getOptions().lit) |
---|
152 | { |
---|
153 | q = 0; |
---|
154 | for ( size_t j = 0; j < mPageSize - 1; j += ( mTileSize - 1 ) ) |
---|
155 | { |
---|
156 | size_t p = 0; |
---|
157 | |
---|
158 | for ( size_t i = 0; i < mPageSize - 1; i += ( mTileSize - 1 ) ) |
---|
159 | { |
---|
160 | page->tiles[ p ][ q ]->_calculateNormals(); |
---|
161 | p++; |
---|
162 | } |
---|
163 | q++; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | return page; |
---|
168 | } |
---|
169 | //------------------------------------------------------------------------- |
---|
170 | void TerrainPageSource::firePageConstructed(size_t pagex, size_t pagez, Real* heightData) |
---|
171 | { |
---|
172 | TerrainPageSourceListenerManager::getSingleton().firePageConstructed( |
---|
173 | mSceneManager, pagex, pagez, heightData); |
---|
174 | } |
---|
175 | //------------------------------------------------------------------------- |
---|
176 | void TerrainPageSource::addListener(TerrainPageSourceListener* pl) |
---|
177 | { |
---|
178 | TerrainPageSourceListenerManager::getSingleton().addListener(pl); |
---|
179 | } |
---|
180 | //------------------------------------------------------------------------- |
---|
181 | void TerrainPageSource::removeListener(TerrainPageSourceListener* pl) |
---|
182 | { |
---|
183 | TerrainPageSourceListenerManager::getSingleton().removeListener(pl); |
---|
184 | |
---|
185 | } |
---|
186 | //------------------------------------------------------------------------- |
---|
187 | |
---|
188 | } |
---|