source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTerrainPageSource.h @ 1273

Revision 1273, 12.4 KB checked in by szydlowski, 18 years ago (diff)

Added the KdTerrainSceneManager?, a subclass of the KdTreeSceneManager? capable of rendering terrain like the TerrainSceneManager? from Ogre.
All the *Kd*Terrain* classes are identical to their octree counterparts, save prefixing all classes and structures with Kd to avoid namespace clashes.
This was necessary, since the TerrainSceneManager? was hard coded in these classes, and all references had to be replaced with the KdTerrainSceneManager?.
Also added a comprehensive README for the demo application.

Line 
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/*
26-----------------------------------------------------------------------------
27This source file is part of the GameTools Project
28http://www.gametools.org
29
30Author: Martin Szydlowski
31-----------------------------------------------------------------------------
32*/
33
34#ifndef __KdTerrainPageSource_H__
35#define __KdTerrainPageSource_H__
36
37#include <OgreSingleton.h>
38#include "OgreKdTerrainPrerequisites.h"
39
40namespace Ogre {
41
42    typedef std::pair<String, String> KdTerrainPageSourceOption;
43    typedef std::vector<KdTerrainPageSourceOption> KdTerrainPageSourceOptionList;
44
45    /** Abstract class which classes can override to receive notifications
46        when a page is ready to be added to the terrain manager.
47    */
48    class _OgreKdTerrainExport KdTerrainPageSourceListener
49    {
50    public:
51        /** Listener method called when a new page is about to be constructed.
52                @param manager The manager in question
53        @param pagex, pagez The index of the page being constructed
54        @param heightData Array of normalised height data (0..1). The size of
55            this buffer will conform to the scene manager page size. The listener
56            may modify the data if it wishes.
57        */
58        virtual void pageConstructed(KdTerrainSceneManager* manager, uint pagex, uint pagez, Real* heightData) = 0;
59    };
60
61        /** Simple manager class to hold onto a list of page source listeners
62            across all sources.
63        */
64        class _OgreKdTerrainExport KdTerrainPageSourceListenerManager :
65                public Singleton<KdTerrainPageSourceListenerManager>
66        {
67        protected:
68        typedef std::vector<KdTerrainPageSourceListener*> KdPageSourceListenerList;
69        KdPageSourceListenerList mPageSourceListeners;
70        public:
71        KdTerrainPageSourceListenerManager() {}
72        ~KdTerrainPageSourceListenerManager() {}
73
74        /** Register a class which will be called back whenever a new page is
75            available.
76        @remarks
77            Since this method is static, it applies to any page source which
78            is in active use; there is no need to register one per source.
79        */
80        void addListener(KdTerrainPageSourceListener* pl);
81        /** Unregister a class which will be called back whenever a new page is
82        available.
83        */
84        void removeListener(KdTerrainPageSourceListener* pl);
85               
86        /// Fire pageContructed events
87        void firePageConstructed(KdTerrainSceneManager* manager, uint pagex, uint pagez, Real* heightData);
88
89       /** Override standard Singleton retrieval.
90        */
91        static KdTerrainPageSourceListenerManager& getSingleton(void);
92        /** Override standard Singleton retrieval.
93        */
94        static KdTerrainPageSourceListenerManager* getSingletonPtr(void);       
95       
96        };
97
98
99    /** Abstract class which describes the interface which a source of terrain
100        pages must implement.
101    @remarks
102        The TerrainSceneManager can accept external classes as providers of
103        terrain data, to allow terrain height data to come from anywhere the
104        user application may choose, and additionally to support on-demand
105        loading an unloading of terrain data. Providers must suclass this class,
106        and implement the abstract methods (details are described within each method)
107    @par
108        The overall sequence of events is this:
109        <ol>
110        <li>TerrainSceneManager is created as usual, and options such as tile
111        size etc are set.</li>
112        <li>CustomTerrainPageSource is registered with TerrainSceneManager by
113        calling registerPageSource(), registering a particular named type of source
114        data with this tile source. <li>
115        <li>TerrainSceneManager::setWorldGeometry is called. Depending on the
116        configuration, this will call one of the page source classes
117        initialise methods, when the scene manager will communicate it's
118        preferred options. It does not have to load anything immediately on this
119        call (especially if the terrain options include paging). It will
120        also set this tile source as the primary.<li>
121        <li>As and when TerrainSceneManager requires more tiles (and this will
122        either be done all up-front, or progressively depending on paging settings)
123        it will call the primary tile source's requestPage() method, with the
124        page it requires. </li>
125        <li>It is then the responsibility of the tile source to prepare
126        TerrainRenderable instances for the page(s) requested, and to attach them
127        to the TerrainSceneManager. Note that preparing the tiles does not
128        involve modifying any shared data so may be done in an alternate thread,
129        if required. Attaching them must be done synchronously though.
130        <li>When paging, the TerrainSceneManager will request tiles in advance,
131        within it's 'buffer zone' so some delay in loading is acceptable. It
132        will also indicate when tiles are no longer required (and will detach
133        them); it is up to the tile source whether that memory is actually freed
134        or held for a while longer.
135        </ol>
136    @note The comments on paging above are in principle, the implementation of
137    paging in this manager is not present yet but the system is designed to
138    extend to it. For now, all tiles are requested up-front.
139    */
140    class _OgreKdTerrainExport KdTerrainPageSource
141    {
142    protected:
143        /// Link back to parent manager
144        KdTerrainSceneManager* mSceneManager;
145        /// Has asynchronous loading been requested?
146        bool mAsyncLoading;
147        /// The expected size of the page in number of vertices
148        uint mPageSize;
149        /// The expected size of a tile in number of vertices
150        uint mTileSize;
151
152        /// Internal method for firing pageContructed events
153        void firePageConstructed(uint pagex, uint pagez, Real* heightData);
154
155        /** Utility method for building a page of tiles based on some source
156        data, wherever that may have come from.
157        @remarks
158            It is expected that this height data is represented in the range
159            [0..1], which will be duly scaled by the TerrainRenderables it
160            creates.
161        */
162        virtual KdTerrainPage* buildPage(Real* heightData, const MaterialPtr& pMaterial);
163
164
165    public:
166        KdTerrainPageSource();
167        virtual ~KdTerrainPageSource() { shutdown(); }
168
169        /** Initialise this tile source based on a series of options as
170            dictated by the scene manager.
171        @param tsm The TerrainSceneManager doing the initialising. This should be
172            allowed NULL, for use by external tools if they want to read data
173            generically without necessarily having a real scene manager involved
174        @param tileSize The number of horizontal (and hence also vertical)
175            vertices in a single tile (which is a TerrainRenderable). This will
176            always be (2^n)+1.
177        @param pageSize The number of horizontal (and hence also vertical)
178            vertices in a single page. This will always be (2^n)+1.
179        @param asyncLoading
180            True if the scene manager would like the tile source to load tiles
181            asynchronously. It does not have to do this, although if it does not
182            when requested, it will likely result in stalls in the terrain rendering.
183        @param optionList
184            A list of name/value pairs describing custom options for this particular
185            page source. The expected convention for option names is
186            "TypeName.OptionName", where TypeName is the type under which this
187            page source has been registered.
188        */
189        virtual void initialise(KdTerrainSceneManager* tsm,
190            uint tileSize, uint pageSize, bool asyncLoading,
191            KdTerrainPageSourceOptionList& optionList)
192        {
193            mSceneManager = tsm;
194            mTileSize = tileSize;
195            mPageSize = pageSize;
196            mAsyncLoading = asyncLoading;
197        }
198        /** Shut down this tile source, freeing all it's memory ready for
199            decommissioning.
200        @remarks
201            This method will normally just be called on destruction; however
202            it may also be called by the TerrainSceneManager if another source
203            is provided for the same type of tile source.
204        */
205        virtual void shutdown(void) {}
206
207        /** Requests a new page of tiles from the source.
208        @remarks
209            The TerrainSceneManager will call this method when it needs new tiles.
210            In response, this class must prepare TerrainRenderable instances for
211            the page requested and attach the entire page when ready using
212            TerrainSceneManager::attachTerrainPage.
213        @par
214            Now, the tile source does not necessarily need to do all that before the
215            return of this method. If it likes, and particularly if asynchronous
216            loading is enabled, it can merely queue this request, and process it
217            either in another thread, or over a series of frames. The key thing
218            is that attaching the new page has to be done synchronously with
219            the main rendering loop in order to avoid concurrency issues;
220            other than that, you are free to load and prepare new tiles in
221            a concurrent fashion if you like.
222        @par
223            Typically the scene manager will request at least one page up-front,
224            with the possibility of requesting more if paging is enabled.
225        @param x The x index of the page requested
226        @param z The z index of the page requested
227        */
228        virtual void requestPage(ushort x, ushort z) = 0;
229        /** This notifies the tile source that the specified page of tiles
230            has been automatically detached.
231        @remarks
232            When paging is enabled, tiles go out of scope and the TerrainSceneManager
233            detaches them automatically, notifying the TerrainPageSource that
234            this has happened. The tile source can choose to either keep these
235            tiles in memory (incase they are requested again) or can delete them
236            if it wishes to free memory. This freeing does not need to be done
237            before the return of this method - like requesting tiles, the
238            freeing of them can be done in another thread or across many frames
239            if required, since the shared data in TerrainSceneManager has already
240            been updated synchronously when the page was detached.
241        @param x The x index of the page expired
242        @param z The z index of the page expired
243        */
244        virtual void expirePage(ushort x, ushort z) = 0;
245       
246        /** Register a class which will be called back whenever a new page is
247            available.
248        @remarks
249            Since this method is static, it applies to any page source which
250            is in active use; there is no need to register one per source.
251        */
252        static void addListener(KdTerrainPageSourceListener* pl);
253        /** Unregister a class which will be called back whenever a new page is
254        available.
255        */
256        static void removeListener(KdTerrainPageSourceListener* pl);
257
258    };
259
260}
261
262#endif
Note: See TracBrowser for help on using the repository browser.