1
/***************************************************************************
2
OgrePagingLandScapePageManager.h - description
4
begin : Sat May 01 2004
5
copyright : (C) 2003-2006 by Jose A. Milan and Tuan Kuranes
6
email : spoke2@supercable.es && tuan.kuranes@free.fr
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU Lesser General Public License as *
13
* published by the Free Software Foundation; either version 2 of the *
14
* License, or (at your option) any later version. *
16
***************************************************************************/
18
#ifndef PAGINGLandScapePAGEMANAGER_H
19
#define PAGINGLandScapePAGEMANAGER_H
21
#include "OgrePagingLandScapePrerequisites.h"
22
#include "OgreSingleton.h"
23
#include "OgreFrameListener.h"
24
#include "OgrePagingLandScapeQueue.h"
26
#include "OgrePagingLandScapePage.h"
30
class _OgrePagingLandScapeExport PagingLandScapePageManager : public FrameListener
35
PagingLandScapePageManager(PagingLandScapeSceneManager * scnMgr);
37
~PagingLandScapePageManager(void);
39
/// upon new landscape
41
/// empty before loading a landscape or final deletion
43
/// reset paging but keep page pool in memory
46
/// used to load first page earlier than first updatePaging() call
47
/// say like just after scene manager setWorldGeom()
48
void LoadFirstPage(PagingLandScapeCamera* cam);
49
/// Make sure page under camera is loaded,
50
/// that neighbor pages are preLoaded, preLoading
51
/// and process queues accordingly
52
void updatePaging(PagingLandScapeCamera* cam);
54
// recursively call LOD update on all page and tiles
55
void _updateLod(void);
57
// load everything around camera just now.
58
void loadNow(PagingLandScapeCamera *cam);
59
// Make sure page gets into loaded page list when loaded from outside
60
// ie "LoadNow" getOption
61
void addLoadedPage(PagingLandScapePage *p);
63
/// if page is already instantiated get page at this pos,
64
/// otherwise allocate one, if alwaysReturn is set true.
65
PagingLandScapePage* getPage(const unsigned int i , const unsigned int j,
66
const bool alwaysReturn = true);
67
/// Instantiate a new page from pool.
68
PagingLandScapePage* getNewPage(const unsigned int i , const unsigned int j);
69
/// Return a page to the pool.
70
void releasePage (PagingLandScapePage*p );
72
/// get Tile at absolute position in space, return answer even when position is outside landscape
73
/// when alwaysAnswer is true
74
PagingLandScapeTile* getTile(const Real posx, const Real posz, bool alwaysAnswer);
75
/// get Tile at absolute position but unscaled by landscape scale in space,
76
/// return answer even when position is outside landscape
77
/// when alwaysAnswer is true
78
PagingLandScapeTile* getTileUnscaled(const Real posx, const Real posz, bool alwaysAnswer);
79
// get Tile at relative position (in the page containing the tile page coordinate system) in space,
80
/// return answer even when position is outside landscape
81
/// when alwaysAnswer is true
82
PagingLandScapeTile* getTile(const Real posx, const Real posz,
83
const unsigned int pagex, const unsigned int pagez,
85
// get Tile at relative position but unscaled by landscape scale (in the page containing the tile page coordinate system) in space,
86
/// return answer even when position is outside landscape
87
/// when alwaysAnswer is true
88
PagingLandScapeTile* getTileUnscaled(const Real posx, const Real posz,
89
const unsigned int pagex, const unsigned int pagez,
91
// get Tile at relative position but unscaled by landscape scale (in the page containing the tile page coordinate system) in space,
92
/// return answer even when position is outside landscape
93
/// when alwaysAnswer is true
94
PagingLandScapeTile* getTilePage (unsigned int &posx, unsigned int &posz, const unsigned int pagex, const unsigned int pagez);
96
void getGlobalToPage(Real& x, Real& z) const;
97
/** Get the Page indices from a position
98
@param posx the world position vector.
99
@param posz the world position vector.
100
@param x result placed in reference to the x index of the page
101
@param z result placed in reference to the z index of the page
103
inline bool getPageIndices(const Real posx, const Real posz, unsigned int& x, unsigned int& z, bool alwaysAnswer) const
107
getNearestPageIndicesUnscaled(posx * mOptions->invScale.x, posz* mOptions->invScale.z, x, z);
112
return getRealPageIndicesUnscaled(posx * mOptions->invScale.x, posz* mOptions->invScale.z, x, z);
115
/** Get the Page indices from a position, returning page only if position is in.
116
@param posx the world position vector but unscaled.
117
@param posz the world position vector but unscaled.
118
@param x result placed in reference to the x index of the page
119
@param z result placed in reference to the z index of the page
121
inline bool getRealPageIndicesUnscaled(const Real posx, const Real posz,
122
unsigned int& x, unsigned int& z)
125
const Real lx = ((posx + mOptions->maxUnScaledX) * mOptions->invPageSizeMinusOne);
126
const Real lz = ((posz + mOptions->maxUnScaledZ) * mOptions->invPageSizeMinusOne);
128
// make sure indices are not negative or outside range of number of pages
129
if (lx >= mOptions->world_width || lx < static_cast <Real> (0.0) ||
130
lz >= mOptions->world_height || lz < static_cast <Real> (0.0) )
136
x = static_cast< unsigned int > (lx);
137
z = static_cast< unsigned int > (lz);
141
/** Get the Page indices from a position, always returning a page.
142
@param posx the world position vector but unscaled.
143
@param posz the world position vector but unscaled.
144
@param x result placed in reference to the x index of the page
145
@param z result placed in reference to the z index of the page
147
void getNearestPageIndicesUnscaled(const Real posx, const Real posz, unsigned int& x, unsigned int& z) const;
149
/** Get the Tile indices from a position
150
@param posx the world position vector.
151
@param posz the world position vector.
152
@param pagex the world position page number.
153
@param pagez the world position page number.
154
@param x result placed in reference to the x index of the page
155
@param z result placed in reference to the z index of the page
157
bool getTileIndices(const Real posx, const Real posz, const unsigned int pagex, const unsigned int pagez, unsigned int& x, unsigned int& z, bool alwaysAnswer) const;
159
/** Get the Tile indices from a position, returning tile only if position is in.
160
@param posx the world position vector but unscaled.
161
@param posz the world position vector but unscaled.
162
@param x result placed in reference to the x index of the page
163
@param z result placed in reference to the z index of the page
165
inline bool getRealTileIndicesUnscaled(const Real posx, const Real posz,
166
const unsigned int pagex, const unsigned int pagez,
167
unsigned int& x, unsigned int& z) const
169
// adjust x and z to be local to page
170
const int pSize = mOptions->PageSize - 1;
171
const int tilex = static_cast< int >((posx - ((pagex * pSize) - mOptions->maxUnScaledX)) * mOptions->invTileSizeMinusOne);
172
const int tilez = static_cast< int >((posz - ((pagez * pSize) - mOptions->maxUnScaledZ)) * mOptions->invTileSizeMinusOne);
175
const int tilesPerPage = static_cast< int >(mOptions->NumTiles);
176
//const int tilesPerPage = static_cast< int >(mOptions->NumTiles(pSize * inv_tSize) - 1);
178
if (tilex > tilesPerPage || tilex < 0 ||
179
tilez > tilesPerPage || tilez < 0)
185
x = static_cast< unsigned int >(tilex);
186
z = static_cast< unsigned int >(tilez);
191
/** Get the Tile indices from a position, returning tile only if position is in.
192
@param posx the world position vector but unscaled.
193
@param posz the world position vector but unscaled.
194
@param x result placed in reference to the x index of the page
195
@param z result placed in reference to the z index of the page
197
void getNearestTileIndicesUnscaled(const Real posx, const Real posz, const unsigned int pagex, const unsigned int pagez, unsigned int& x, unsigned int& z) const;
199
void setTerrainReady(bool isready)
201
mTerrainReady = isready;
205
void removeFromQueues(PagingLandScapePage* p);
207
bool frameStarted(const FrameEvent& evt);
208
bool frameEnded(const FrameEvent& evt);
210
void setWorldGeometryRenderQueue(uint8 qid);
211
RenderQueueGroupID getRenderQueueGroupID(void)
213
return mRenderQueueGroupID;
216
void setMapMaterial(void);
217
void WorldDimensionChange(void);
222
unsigned int getCurrentCameraPageX(void) const;
224
unsigned int getCurrentCameraPageZ(void) const;
226
unsigned int getCurrentCameraTileX(void) const;
228
unsigned int getCurrentCameraTileZ(void) const;
230
int getPagePreloadQueueSize(void) const;
232
int getPageLoadQueueSize(void) const;
234
int getPageTextureloadQueueSize(void) const;
236
int getLoadedPageSize(void) const;
238
int getPreLoadedPageSize(void) const;
240
int getTextureLoadedPageSize(void) const;
242
int getUnloadedPagesSize(void) const;
244
RenderQueueGroupID getPageRenderQueue(){return mRenderQueueGroupID;};
246
PagingLandScapeOptions* getOptions(){return mOptions;}
247
PagingLandScapeSceneManager* getSceneManager(){return mSceneManager;}
249
bool isEnabled ()const {return mEnabled;}
250
void setEnabled (const bool enabled){mEnabled = enabled;}
254
PagingLandScapeOptions* mOptions;
255
PagingLandScapeSceneManager *mSceneManager;
259
void processUnloadQueues();
260
void processLoadQueues();
261
void updateLoadedPages();
262
void queuePageNeighbors ();
264
void makePageLoadedNow(PagingLandScapePage* p);
265
PagingLandScapePage* find_nearest(const Vector3& pos, const unsigned int x, const unsigned int z, PagingLandScapePageList& mQueue) const;
267
PagingLandScapeData2DManager* mData2d;
268
PagingLandScapeTextureManager* mTexture;
269
PagingLandScapeRenderableManager* mRenderablesMgr;
271
/** LandScape pages for the terrain.
273
//PagingLandScapePages mPages;
275
PagingLandScapeQueue <PagingLandScapePage> mPageLoadQueue;
276
PagingLandScapeQueue <PagingLandScapePage> mPagePreloadQueue;
277
PagingLandScapeQueue <PagingLandScapePage> mPageTextureloadQueue;
279
PagingLandScapePageList mLoadedPages;
280
PagingLandScapePageList mPreLoadedPages;
281
PagingLandScapePageList mTextureLoadedPages;
283
PagingLandScapePageList mActivePages;
284
PagingLandScapePageList mFreePages;
285
PagingLandScapePageArray mPagePool;
288
unsigned int mHeight;
290
int mNextQueueFrameCount;
295
PagingLandScapeCamera* mCurrentcam;
299
unsigned int mRenderableLoadInterval;
300
unsigned int mPageLoadInterval;
302
RenderQueueGroupID mRenderQueueGroupID;
304
//if not queued to be removed from frame listener
305
//or SM is in paused State