~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/terrain/TerrainPage.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Interface: TerrainPage
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2005
 
8
//
 
9
// This program is free software; you can redistribute it and/or modify
 
10
// it under the terms of the GNU General Public License as published by
 
11
// the Free Software Foundation; either version 2 of the License, or
 
12
// (at your option) any later version.
 
13
// 
 
14
// This program is distributed in the hope that it will be useful,
 
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
// GNU General Public License for more details.
 
18
// 
 
19
// You should have received a copy of the GNU General Public License
 
20
// along with this program; if not, write to the Free Software
 
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
 
22
//
 
23
#ifndef EMBEROGRETERRAINPAGE_H
 
24
#define EMBEROGRETERRAINPAGE_H
 
25
 
 
26
#include "../EmberOgrePrerequisites.h"
 
27
 
 
28
#include <Mercator/Terrain.h>
 
29
#include <Mercator/Segment.h>
 
30
#include <Mercator/Surface.h>
 
31
#include <Mercator/TerrainMod.h>
 
32
#include "../Types.h"
 
33
 
 
34
namespace EmberOgre {
 
35
 
 
36
struct PageSegment
 
37
{
 
38
        TerrainPosition pos;
 
39
        Mercator::Segment* segment;
 
40
};
 
41
 
 
42
typedef std::vector<PageSegment> SegmentVector;
 
43
}
 
44
 
 
45
#include "TerrainPageShadow.h"
 
46
 
 
47
namespace Ogre
 
48
{
 
49
        class TerrainOptions;
 
50
}
 
51
 
 
52
namespace EmberOgre {
 
53
namespace Terrain {
 
54
class TerrainShader;
 
55
class TerrainGenerator;
 
56
class TerrainPageSurface;
 
57
class TerrainPage;
 
58
class TerrainPageFoliage;
 
59
class TerrainPageSurfaceLayer;
 
60
class ITerrainPageBridge;
 
61
class terrainModListEntry;
 
62
 
 
63
TYPEDEF_STL_MAP(const Mercator::Shader*, TerrainShader*, ShaderMap);
 
64
 
 
65
 
 
66
 
 
67
/**
 
68
 
 
69
@brief Reprents one terrain page.
 
70
 
 
71
This is a bridge class between one Ogre terrain page instance and one or many Mercator::Segment. Since each Segment is 64x64 meters, but one Ogre page often is much larger, we need to combine many Segments for every single Ogre page.
 
72
 
 
73
Instances of this is created by TerrainGenerator.
 
74
One terrain page is composed of both height data, a material, textures for the material and plant positions for the foliage system. This class handles all of these, some of them directly and some by other classes.
 
75
 
 
76
@author Erik Hjortsberg <erik.hjortsberg@gmail.com>
 
77
*/
 
78
class TerrainPage{
 
79
friend class TerrainPageShadow;
 
80
friend class ITerrainPageShadowTechnique;
 
81
public:
 
82
 
 
83
    TerrainPage(TerrainPosition position, const std::map<const Mercator::Shader*, TerrainShader*> shaderMap, TerrainGenerator* generator);
 
84
 
 
85
    ~TerrainPage();
 
86
        
 
87
        
 
88
        /**
 
89
         *    The number of Mercator::Segments for each axis. I.e. the root of the total number of segments.
 
90
         *    Mainly used to check if it's 1, in which case everything becomes much easier.
 
91
         * @return 
 
92
         */
 
93
        int getNumberOfSegmentsPerAxis() const;
 
94
        
 
95
        
 
96
        /**
 
97
         *    The max height of this page
 
98
         * @return 
 
99
         */
 
100
        float getMaxHeight();
 
101
        
 
102
        /**
 
103
         *    The minimum height of this page
 
104
         * @return 
 
105
         */
 
106
        float getMinHeight();
 
107
        
 
108
        
 
109
        /**
 
110
         * @brief Generates the terrain materials needed.
 
111
         * @param reselectTechnique If true, we'll also see if we need to reselect the technique to use.
 
112
         * @return The new material created.
 
113
         */
 
114
        Ogre::MaterialPtr generateTerrainMaterials(bool reselectTechnique);
 
115
        
 
116
        /**
 
117
         *    Fills the bound height data with height data. If no buffer has been bound nothing will be done.
 
118
         */
 
119
        void updateOgreHeightData(Ogre::Real* heightData);
 
120
        
 
121
        /**
 
122
         *    The total number of vertices used for this page
 
123
         * @return 
 
124
         */
 
125
        long getVerticeCount() const;
 
126
        
 
127
        /**
 
128
         *    The position of the page in Worldforge space
 
129
         * @return 
 
130
         */
 
131
        const TerrainPosition& getWFPosition() const;
 
132
        
 
133
 
 
134
        /**
 
135
         *    The material used for the page
 
136
         * @return 
 
137
         */
 
138
        Ogre::MaterialPtr getMaterial();
 
139
        
 
140
        /**
 
141
         *    creates a new foliage for this page, but does not show it yet
 
142
         */
 
143
        void prepareFoliage();
 
144
        
 
145
        /**
 
146
         *    shows the foliage for this page, if no foliage exists it's created
 
147
         */
 
148
        void showFoliage();
 
149
        
 
150
        
 
151
        /**
 
152
         *    hides the foliage for this page, but does not destroy it
 
153
         */
 
154
        void hideFoliage();
 
155
        
 
156
        
 
157
        /**
 
158
         *    destroys the foliage for this page
 
159
         */
 
160
        void destroyFoliage();
 
161
        
 
162
        /**
 
163
         *    this adds a shader to the page, meaning that it will be used in rendering 
 
164
         * @param shader 
 
165
         */
 
166
        TerrainPageSurfaceLayer* addShader(TerrainShader* shader);
 
167
        
 
168
        /**
 
169
         *    Updates the shader texture for the specific shader
 
170
         * @param shader 
 
171
         */
 
172
        TerrainPageSurfaceLayer* updateShaderTexture(TerrainShader* shader, bool repopulate = true);
 
173
        
 
174
        /**
 
175
         *    Updates all the shader textures of the page.
 
176
         *    You should usually call this after you've made a change to the terrain and already have called populateSurfaces()
 
177
         */
 
178
        void updateAllShaderTextures(bool repopulate = true);
 
179
 
 
180
        void populateSurfaces();
 
181
        
 
182
        void populateSurface(TerrainShader* shader);
 
183
        
 
184
        int getPageSize() const ;
 
185
        
 
186
        void update();
 
187
        
 
188
        void createShadow(const Ogre::Vector3& lightDirection);
 
189
        
 
190
        void updateShadow(const Ogre::Vector3& lightDirection);
 
191
        
 
192
                /**
 
193
         *    The size in pixels of one side of the AlphaTexture. This is in sizes of 64.
 
194
         * @return 
 
195
         */
 
196
        int getAlphaTextureSize() const;
 
197
        
 
198
        SegmentVector& getValidSegments();
 
199
        
 
200
        /**
 
201
         *    Gets the extent of this page in meters, in worldforge space.
 
202
         * @return 
 
203
         */
 
204
        const WFMath::AxisBox<2>& getExtent() const;
 
205
        
 
206
//      const PlantsStore& getPlants() const;
 
207
        
 
208
        TerrainPageSurface* getSurface() const;
 
209
        
 
210
        TerrainPageFoliage* getPageFoliage() const;
 
211
        
 
212
        TerrainPageShadow& getPageShadow();
 
213
        
 
214
        /**
 
215
         *    Gets the segment positioned at the supplied position in local space.
 
216
         * @param pos A Wordforge position in local space, i.e. > 0 && < [width in meters of the page]
 
217
         * @return A pointer to Mercator::Segment or null.
 
218
         */
 
219
        Mercator::Segment* getSegmentAtLocalPosition(const TerrainPosition& pos) const;
 
220
        
 
221
        /**
 
222
         *    Gets the segment positioned at the supplied position in local space and also translates the supplied position into a local position in the returned segment.
 
223
         * @param pos A Wordforge position in local space, i.e. > 0 && < [width in meters of the page]
 
224
         * @param localPositionInSegment The resulting position in the segment space.
 
225
         * @return A pointer to Mercator::Segment or null.
 
226
         */
 
227
        Mercator::Segment* getSegmentAtLocalPosition(const TerrainPosition& pos, TerrainPosition& localPositionInSegment) const;
 
228
 
 
229
        
 
230
        /**
 
231
         *    @brief Binds a bridge instance to this page.
 
232
         * The bridge will be responsible for updating the terrain engine after the Mercator terrain has changed.
 
233
         * This class won't take ownership of the bridge, so it's up to the calling class to make sure that it's properly destroyed, and when so also calling @see unregisterBridge()
 
234
         * @param bridge A vlid bridge instance.
 
235
         */
 
236
        void registerBridge(ITerrainPageBridge* bridge);
 
237
        
 
238
        /**
 
239
         *    @brief Unregisters the current terrain bridge.
 
240
         * Make sure to call this when the bridge is destroyed, so as not to leave any dangling pointers. This won't however delete the bridge.
 
241
         */
 
242
        void unregisterBridge();
 
243
        
 
244
        
 
245
        /**
 
246
         * @brief Gets the normal at the specified local position.
 
247
         * @param localPosition The position, local to the page.
 
248
         * @param normal The normal will be placed here.
 
249
         * @return True if a valid normal could be found at the specified position.
 
250
         */
 
251
        bool getNormal(const TerrainPosition& localPosition, WFMath::Vector<3>& normal) const;
 
252
 
 
253
 
 
254
private:
 
255
 
 
256
        SegmentVector mValidSegments;
 
257
 
 
258
        
 
259
        /**
 
260
        this holds a map of the area, to be used in a map widget etc.
 
261
        */
 
262
        Ogre::TexturePtr mMap;
 
263
 
 
264
 
 
265
        TerrainGenerator* mGenerator;
 
266
        
 
267
        /**
 
268
        Internal position
 
269
        */
 
270
        TerrainPosition mPosition;
 
271
        
 
272
        
 
273
        /**
 
274
         *    gets a segment for the x and y position in the page
 
275
         * @param x 
 
276
         * @param y 
 
277
         * @return 
 
278
         */
 
279
        Mercator::Segment* getSegmentAtLocalIndex(int indexX, int indexY) const;
 
280
        
 
281
 
 
282
 
 
283
        
 
284
        /**
 
285
        * How much to scale the alpha map. This is done to avoid pixelated terrain (a blur filter is applied).
 
286
        This value is taken from the config file.
 
287
        */
 
288
        unsigned int getAlphaMapScale() const;
 
289
 
 
290
        std::auto_ptr<TerrainPageSurface> mTerrainSurface;
 
291
        TerrainPageShadow mShadow;
 
292
        ITerrainPageShadowTechnique* mShadowTechnique;
 
293
        void setupShadowTechnique();
 
294
        
 
295
        /**
 
296
        The extent of this page in meters, in WF space.
 
297
        */
 
298
        const WFMath::AxisBox<2> mExtent;
 
299
        std::auto_ptr<TerrainPageFoliage> mPageFoliage;
 
300
        
 
301
        /**
 
302
        A local copy of the segments for fast lookup. This will also include nonvalid segments.
 
303
        The keys will be the local indices.
 
304
        */
 
305
        Mercator::Terrain::Segmentstore mLocalSegments;
 
306
                
 
307
 
 
308
        /**
 
309
        @brief Bridge to the ogre terrain engine.
 
310
        When the terrain data is changed we need to also update the actual ingame representation that the terrain engine provides. This instance will take care of that.
 
311
        */
 
312
        ITerrainPageBridge* mBridge;
 
313
 
 
314
 
 
315
};
 
316
 
 
317
inline int TerrainPage::getAlphaTextureSize( ) const
 
318
{
 
319
        return (getPageSize() - 1);
 
320
 
 
321
}
 
322
}
 
323
}
 
324
 
 
325
#endif