Coverage Report

Created: 2026-01-09 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/Components/Terrain/include/OgreTerrain.h
Line
Count
Source
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-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
29
#ifndef __Ogre_Terrain_H__
30
#define __Ogre_Terrain_H__
31
32
#include "OgreTerrainPrerequisites.h"
33
#include "OgreCommon.h"
34
#include "OgreVector.h"
35
#include "OgreAxisAlignedBox.h"
36
#include "OgreSceneManager.h"
37
#include "OgreTerrainMaterialGenerator.h"
38
#include "OgreTerrainLayerBlendMap.h"
39
#include "OgreWorkQueue.h"
40
#include "OgreTerrainLodManager.h"
41
#include <future>
42
43
namespace Ogre
44
{
45
    /** \addtogroup Optional
46
    *  @{
47
    */
48
    /** \defgroup Terrain Terrain
49
    *   Editable %Terrain System with LOD @cite de2000fast, serialization and \ref Paging support
50
    *  @{
51
    */
52
53
    /** The main containing class for a chunk of terrain.
54
    @par
55
        Terrain can be edited and stored.
56
    The data format for this in a file is:<br/>
57
    <b>TerrainData (Identifier 'TERR')</b>\n
58
    [Version 1]
59
    <table>
60
    <tr>
61
        <td><b>Name</b></td>
62
        <td><b>Type</b></td>
63
        <td><b>Description</b></td>
64
    </tr>
65
    <tr>
66
        <td>Terrain orientation</td>
67
        <td>uint8</td>
68
        <td>The orientation of the terrain; XZ = 0, XY = 1, YZ = 2</td>
69
    </tr>
70
    <tr>
71
        <td>Terrain size</td>
72
        <td>uint16</td>
73
        <td>The number of vertices along one side of the terrain</td>
74
    </tr>
75
    <tr>
76
        <td>Terrain world size</td>
77
        <td>Real</td>
78
        <td>The world size of one side of the terrain</td>
79
    </tr>
80
    <tr>
81
        <td>Max batch size</td>
82
        <td>uint16</td>
83
        <td>The maximum batch size in vertices along one side</td>
84
    </tr>
85
    <tr>
86
        <td>Min batch size</td>
87
        <td>uint16</td>
88
        <td>The minimum batch size in vertices along one side</td>
89
    </tr>
90
    <tr>
91
        <td>Position</td>
92
        <td>Vector3</td>
93
        <td>The location of the centre of the terrain</td>
94
    </tr>
95
    <tr>
96
        <td>Height data</td>
97
        <td>float[size*size]</td>
98
        <td>List of floating point heights</td>
99
    </tr>
100
    <tr>
101
        <td>LayerDeclaration</td>
102
        <td>LayerDeclaration*</td>
103
        <td>The layer declaration for this terrain (see below)</td>
104
    </tr>
105
    <tr>
106
        <td>Layer count</td>
107
        <td>uint8</td>
108
        <td>The number of layers in this terrain</td>
109
    </tr>
110
    <tr>
111
        <td>LayerInstance list</td>
112
        <td>LayerInstance*</td>
113
        <td>A number of LayerInstance definitions based on layer count (see below)</td>
114
    </tr>
115
    <tr>
116
        <td>Layer blend map size</td>
117
        <td>uint16</td>
118
        <td>The size of the layer blend maps as stored in this file</td>
119
    </tr>
120
    <tr>
121
        <td>Packed blend texture data</td>
122
        <td>uint8*</td>
123
        <td>layerCount-1 sets of blend texture data interleaved as RGBA</td>
124
    </tr>
125
    <tr>
126
        <td>Optional derived map data</td>
127
        <td>TerrainDerivedMap list</td>
128
        <td>0 or more sets of map data derived from the original terrain</td>
129
    </tr>
130
    <tr>
131
        <td>Delta data</td>
132
        <td>float[size*size]</td>
133
        <td>At each vertex, delta information for the LOD at which this vertex disappears</td>
134
    </tr>
135
    <tr>
136
        <td>Quadtree delta data</td>
137
        <td>float[quadtrees*lods]</td>
138
        <td>At each quadtree node, for each lod a record of the max delta value in the region</td>
139
    </tr>
140
    </table>
141
    <b>TerrainLayerDeclaration (Identifier 'TDCL')</b>\n
142
    [Version 1]
143
    <table>
144
    <tr>
145
        <td><b>Name</b></td>
146
        <td><b>Type</b></td>
147
        <td><b>Description</b></td>
148
    </tr>
149
    <tr>
150
        <td>TerrainLayerSampler Count</td>
151
        <td>uint8</td>
152
        <td>Number of samplers in this declaration</td>
153
    </tr>
154
    <tr>
155
        <td>TerrainLayerSampler List</td>
156
        <td>TerrainLayerSampler*</td>
157
        <td>List of TerrainLayerSampler structures</td>
158
    </tr>
159
    <tr>
160
        <td>Sampler Element Count</td>
161
        <td>uint8</td>
162
        <td>Number of sampler elements in this declaration</td>
163
    </tr>
164
    <tr>
165
        <td>TerrainLayerSamplerElement List</td>
166
        <td>TerrainLayerSamplerElement*</td>
167
        <td>List of TerrainLayerSamplerElement structures</td>
168
    </tr>
169
    </table>
170
    <b>TerrainLayerSampler (Identifier 'TSAM')</b>\n
171
    [Version 1]
172
    <table>
173
    <tr>
174
        <td><b>Name</b></td>
175
        <td><b>Type</b></td>
176
        <td><b>Description</b></td>
177
    </tr>
178
    <tr>
179
        <td>Alias</td>
180
        <td>String</td>
181
        <td>Alias name of this sampler</td>
182
    </tr>
183
    <tr>
184
        <td>Format</td>
185
        <td>uint8</td>
186
        <td>Desired pixel format</td>
187
    </tr>
188
    </table>
189
    <b>TerrainLayerSamplerElement (Identifier 'TSEL')</b>\n
190
    [Version 1]
191
    <table>
192
    <tr>
193
        <td><b>Name</b></td>
194
        <td><b>Type</b></td>
195
        <td><b>Description</b></td>
196
    </tr>
197
    <tr>
198
        <td>Source</td>
199
        <td>uint8</td>
200
        <td>Sampler source index</td>
201
    </tr>
202
    <tr>
203
        <td>Semantic</td>
204
        <td>uint8</td>
205
        <td>Semantic interpretation of this element</td>
206
    </tr>
207
    <tr>
208
        <td>Element start</td>
209
        <td>uint8</td>
210
        <td>Start of this element in the sampler</td>
211
    </tr>
212
    <tr>
213
        <td>Element count</td>
214
        <td>uint8</td>
215
        <td>Number of elements in the sampler used by this entry</td>
216
    </tr>
217
    </table>
218
    <b>LayerInstance (Identifier 'TLIN')</b>\n
219
    [Version 1]
220
    <table>
221
    <tr>
222
        <td><b>Name</b></td>
223
        <td><b>Type</b></td>
224
        <td><b>Description</b></td>
225
    </tr>
226
    <tr>
227
        <td>World size</td>
228
        <td>Real</td>
229
        <td>The world size of this layer (determines UV scaling)</td>
230
    </tr>
231
    <tr>
232
        <td>Texture list</td>
233
        <td>String*</td>
234
        <td>List of texture names corresponding to the number of samplers in the layer declaration</td>
235
    </tr>
236
    </table>
237
    <b>TerrainDerivedData (Identifier 'TDDA')</b>\n
238
    [Version 1]
239
    <table>
240
    <tr>
241
        <td><b>Name</b></td>
242
        <td><b>Type</b></td>
243
        <td><b>Description</b></td>
244
    </tr>
245
    <tr>
246
        <td>Derived data type name</td>
247
        <td>String</td>
248
        <td>Name of the derived data type ('normalmap', 'lightmap', 'colourmap', 'compositemap')</td>
249
    </tr>
250
    <tr>
251
        <td>Size</td>
252
        <td>uint16</td>
253
        <td>Size of the data along one edge</td>
254
    </tr>
255
    <tr>
256
        <td>Data</td>
257
        <td>varies based on type</td>
258
        <td>The data</td>
259
    </tr>
260
    </table>
261
    */
262
    class _OgreTerrainExport Terrain : public SceneManager::Listener
263
    {
264
    public:
265
        friend class TerrainLodManager;
266
267
        /** Constructor.
268
        @param sm The SceneManager to use.
269
        */
270
        Terrain(SceneManager* sm);
271
        virtual ~Terrain();
272
273
        static const uint32 TERRAIN_CHUNK_ID;
274
        static const uint16 TERRAIN_CHUNK_VERSION;
275
        static const uint16 TERRAIN_MAX_BATCH_SIZE;
276
277
        static const uint32 TERRAINLAYERDECLARATION_CHUNK_ID;
278
        static const uint16 TERRAINLAYERDECLARATION_CHUNK_VERSION;
279
        static const uint32 TERRAINLAYERSAMPLER_CHUNK_ID;
280
        static const uint16 TERRAINLAYERSAMPLER_CHUNK_VERSION;
281
        static const uint32 TERRAINLAYERSAMPLERELEMENT_CHUNK_ID;
282
        static const uint16 TERRAINLAYERSAMPLERELEMENT_CHUNK_VERSION;
283
        static const uint32 TERRAINLAYERINSTANCE_CHUNK_ID;
284
        static const uint16 TERRAINLAYERINSTANCE_CHUNK_VERSION;
285
        static const uint32 TERRAINDERIVEDDATA_CHUNK_ID;
286
        static const uint16 TERRAINDERIVEDDATA_CHUNK_VERSION;
287
        static const uint32 TERRAINGENERALINFO_CHUNK_ID;
288
        static const uint16 TERRAINGENERALINFO_CHUNK_VERSION;
289
290
        static const uint32 LOD_MORPH_CUSTOM_PARAM;
291
292
        typedef std::vector<Real> RealVector;
293
294
        /** An instance of a layer, with specific texture names
295
        */
296
        struct _OgreTerrainExport LayerInstance
297
        {
298
            /// The world size of the texture to be applied in this layer
299
            Real worldSize;
300
            /// List of texture names to import; must match with TerrainLayerDeclaration
301
            StringVector textureNames;
302
303
            LayerInstance()
304
0
                : worldSize(100) {}
305
        };
306
        typedef std::vector<LayerInstance> LayerInstanceList;
307
308
        /// The alignment of the terrain
309
        enum Alignment
310
        {
311
            /// Terrain is in the X/Z plane
312
            ALIGN_X_Z = 0, 
313
            /// Terrain is in the X/Y plane
314
            ALIGN_X_Y = 1, 
315
            /// Terrain is in the Y/Z plane
316
            ALIGN_Y_Z = 2
317
        };
318
319
        /** Structure encapsulating import data that you may use to bootstrap 
320
            the terrain without loading from a native data stream. 
321
        */
322
        struct ImportData
323
        {
324
            /// The alignment of the terrain
325
            Alignment terrainAlign;
326
            /// Terrain size (along one edge) in vertices; must be 2^n+1
327
            uint16 terrainSize;
328
            /** Maximum batch size (along one edge) in vertices; must be 2^n+1 and <= 65
329
330
                The terrain will be divided into hierarchical tiles, and this is the maximum
331
                size of one tile in vertices (at any LOD).
332
            */
333
            uint16 maxBatchSize;
334
            /** Minimum batch size (along one edge) in vertices; must be 2^n+1.
335
336
            The terrain will be divided into tiles, and this is the minimum
337
            size of one tile in vertices (at any LOD). Adjacent tiles will be
338
            collected together into one batch to drop LOD levels once they are individually at this minimum,
339
            so setting this value higher means greater batching at the expense
340
            of making adjacent tiles use a common LOD.
341
            Once the entire terrain is collected together into one batch this 
342
            effectively sets the minimum LOD.
343
            */
344
            uint16 minBatchSize;
345
346
            /** Position of the terrain.
347
348
                Represents the position of the centre of the terrain. 
349
            */
350
            Vector3 pos;
351
352
            /** The world size of the terrain. */
353
            Real worldSize;
354
355
            /** Optional heightmap providing the initial heights for the terrain. 
356
357
                If supplied, should ideally be terrainSize * terrainSize, but if
358
                it isn't it will be resized.
359
            */
360
            Image* inputImage;
361
362
            /** Optional list of terrainSize * terrainSize floats defining the terrain. 
363
                The list of floats wil be interpreted such that the first row
364
                in the array equates to the bottom row of vertices. 
365
            */
366
            float* inputFloat;
367
368
            /** If neither inputImage or inputFloat are supplied, the constant
369
                height at which the initial terrain should be created (flat). 
370
            */
371
            float constantHeight;
372
373
            /** Whether this structure should 'own' the input data (inputImage and
374
                inputFloat), and therefore delete it on destruction. 
375
                The default is false so you have to manage your own memory. If you
376
                set it to true, then you must have allocated the memory through
377
                OGRE_NEW (for Image) and OGRE_ALLOC_T (for inputFloat), the latter
378
                with the category MEMCATEGORY_GEOMETRY.
379
            */
380
            bool deleteInputData;
381
382
            /// How to scale the input values provided (if any)
383
            Real inputScale;
384
            /// How to bias the input values provided (if any)
385
            Real inputBias;
386
387
            /** Definition of the contents of each layer (required).
388
            Most likely,  you will pull a declaration from a TerrainMaterialGenerator
389
            of your choice.
390
            */
391
            TerrainLayerDeclaration layerDeclaration;
392
            /** List of layer structures, one for each layer required.
393
                Can be empty or underfilled if required, list will be padded with
394
                blank textures.
395
            */
396
            LayerInstanceList layerList;
397
398
            ImportData() 
399
0
                : terrainAlign(ALIGN_X_Z)
400
0
                , terrainSize(1025)
401
0
                , maxBatchSize(65)
402
0
                , minBatchSize(17)
403
0
                , pos(Vector3::ZERO)
404
0
                , worldSize(1000)
405
0
                , inputImage(0)
406
0
                , inputFloat(0)
407
0
                , constantHeight(0)
408
0
                , deleteInputData(false)
409
0
                , inputScale(1.0)
410
0
                , inputBias(0.0)
411
0
            {
412
413
0
            }
414
415
            ImportData(const ImportData& rhs)
416
                : terrainAlign(ALIGN_X_Z)
417
                , terrainSize(1025)
418
                , maxBatchSize(65)
419
                , minBatchSize(17)
420
                , pos(Vector3::ZERO)
421
                , worldSize(1000)
422
                , inputImage(0)
423
                , inputFloat(0)
424
                , constantHeight(0)
425
                , deleteInputData(false)
426
                , inputScale(1.0)
427
                , inputBias(0.0)
428
0
            {
429
0
                *this = rhs;
430
0
            }
431
432
            ImportData& operator=(const ImportData& rhs)
433
0
            {
434
                // basic copy
435
0
                terrainAlign = rhs.terrainAlign;
436
0
                terrainSize = rhs.terrainSize;
437
0
                maxBatchSize = rhs.maxBatchSize;
438
0
                minBatchSize = rhs.minBatchSize;
439
0
                pos = rhs.pos;
440
0
                worldSize = rhs.worldSize;
441
0
                constantHeight = rhs.constantHeight;
442
0
                deleteInputData = rhs.deleteInputData;
443
0
                inputScale = rhs.inputScale;
444
0
                inputBias = rhs.inputBias;
445
0
                layerDeclaration = rhs.layerDeclaration;
446
0
                layerList = rhs.layerList;
447
448
                // By-value copies in ownership cases
449
0
                if (rhs.deleteInputData)
450
0
                {
451
0
                    if (rhs.inputImage)
452
0
                        inputImage = OGRE_NEW Image(*rhs.inputImage);
453
0
                    else
454
0
                        inputImage = 0;
455
456
0
                    if (rhs.inputFloat)
457
0
                    {
458
0
                        inputFloat = OGRE_ALLOC_T(float, terrainSize*terrainSize, MEMCATEGORY_GEOMETRY);
459
0
                        memcpy(inputFloat, rhs.inputFloat, sizeof(float) * terrainSize*terrainSize);
460
0
                    }
461
0
                    else
462
0
                        inputFloat = 0;
463
0
                }
464
0
                else
465
0
                {
466
                    // re-use pointers
467
0
                    inputImage = rhs.inputImage;
468
0
                    inputFloat = rhs.inputFloat;
469
0
                }
470
0
                return *this;
471
0
            }
472
473
            /// Delete any input data if this struct is set to do so
474
            void destroy()
475
0
            {
476
0
                if (deleteInputData)
477
0
                {
478
0
                    OGRE_DELETE inputImage;
479
0
                    OGRE_FREE(inputFloat, MEMCATEGORY_GEOMETRY);
480
0
                    inputImage = 0;
481
0
                    inputFloat = 0;
482
0
                }
483
484
0
            }
485
486
            ~ImportData()
487
0
            {
488
0
                destroy();
489
0
            }
490
491
        };
492
493
        /// Neighbour index enumeration - indexed anticlockwise from East like angles
494
        enum NeighbourIndex
495
        {
496
            NEIGHBOUR_EAST = 0, 
497
            NEIGHBOUR_NORTHEAST = 1, 
498
            NEIGHBOUR_NORTH = 2, 
499
            NEIGHBOUR_NORTHWEST = 3, 
500
            NEIGHBOUR_WEST = 4, 
501
            NEIGHBOUR_SOUTHWEST = 5, 
502
            NEIGHBOUR_SOUTH = 6, 
503
            NEIGHBOUR_SOUTHEAST = 7,
504
505
            NEIGHBOUR_COUNT = 8
506
        };
507
508
0
        SceneManager* getSceneManager() const { return mSceneMgr; }
509
510
        /// Enumeration of relative spaces that you might want to use to address the terrain
511
        enum Space
512
        {
513
            /// Simple global world space, axes and positions are all in world space
514
            WORLD_SPACE = 0, 
515
            /// As world space, but positions are relative to the terrain world position
516
            LOCAL_SPACE = 1, 
517
            /** x & y are parametric values on the terrain from 0 to 1, with the
518
            origin at the bottom left. z is the world space height at that point.
519
            */
520
            TERRAIN_SPACE = 2,
521
            /** x & y are integer points on the terrain from 0 to size-1, with the
522
            origin at the bottom left. z is the world space height at that point.
523
            */
524
            POINT_SPACE = 3
525
        };
526
527
        /** Interface used to by the Terrain instance to allocate GPU buffers.
528
        @remarks This class exists to make it easier to re-use buffers between
529
            multiple instances of terrain.
530
        */
531
        class _OgreTerrainExport GpuBufferAllocator : public TerrainAlloc
532
        {
533
        public:
534
0
            GpuBufferAllocator() {}
535
0
            virtual ~GpuBufferAllocator() {}
536
537
            /** Allocate (or reuse) vertex buffers for a terrain LOD.
538
            @param forTerrain
539
            @param numVertices The total number of vertices
540
            @param destPos Pointer to a vertex buffer for positions, to be bound
541
            @param destDelta Pointer to a vertex buffer for deltas, to be bound
542
            */
543
            virtual void allocateVertexBuffers(Terrain* forTerrain, size_t numVertices, HardwareVertexBufferSharedPtr& destPos, HardwareVertexBufferSharedPtr& destDelta) = 0;
544
            /** Free (or return to the pool) vertex buffers for terrain. 
545
            */
546
            virtual void freeVertexBuffers(const HardwareVertexBufferSharedPtr& posbuf, const HardwareVertexBufferSharedPtr& deltabuf) = 0;
547
548
            /** Get a shared index buffer for a given number of settings.
549
550
                Since all index structures are the same at the same LOD level and
551
                relative position, we can share index buffers. Therefore the 
552
                buffer returned from this method does not need to be 'freed' like
553
                the vertex buffers since it is never owned.
554
            @param batchSize The batch size along one edge
555
            @param vdatasize The size of the referenced vertex data along one edge
556
            @param vertexIncrement The number of vertices to increment for each new indexed row / column
557
            @param xoffset The x offset from the start of vdatasize, at that resolution
558
            @param yoffset The y offset from the start of vdatasize, at that resolution
559
            @param numSkirtRowsCols Number of rows and columns of skirts
560
            @param skirtRowColSkip The number of rows / cols to skip in between skirts
561
            */
562
            virtual HardwareIndexBufferSharedPtr getSharedIndexBuffer(uint16 batchSize, 
563
                uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols, 
564
                uint16 skirtRowColSkip) = 0;
565
566
            /// Free any buffers we're holding
567
            virtual void freeAllBuffers() = 0;
568
569
        };
570
        /// Standard implementation of a buffer allocator which re-uses buffers
571
        class _OgreTerrainExport DefaultGpuBufferAllocator : public GpuBufferAllocator
572
        {
573
        public:
574
            DefaultGpuBufferAllocator();
575
            virtual ~DefaultGpuBufferAllocator();
576
            void allocateVertexBuffers(Terrain* forTerrain, size_t numVertices, HardwareVertexBufferSharedPtr& destPos, HardwareVertexBufferSharedPtr& destDelta) override;
577
            void freeVertexBuffers(const HardwareVertexBufferSharedPtr& posbuf, const HardwareVertexBufferSharedPtr& deltabuf) override;
578
            HardwareIndexBufferSharedPtr getSharedIndexBuffer(uint16 batchSize, 
579
                uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols, 
580
                uint16 skirtRowColSkip) override;
581
            void freeAllBuffers() override;
582
583
            /** 'Warm start' the allocator based on needing x instances of 
584
                terrain with the given configuration.
585
            */
586
            void warmStart(size_t numInstances, uint16 terrainSize, uint16 maxBatchSize, 
587
                uint16 minBatchSize);
588
589
        private:
590
            typedef std::list<HardwareVertexBufferSharedPtr> VBufList;
591
            VBufList mFreePosBufList;
592
            VBufList mFreeDeltaBufList;
593
            typedef std::map<uint32, HardwareIndexBufferSharedPtr> IBufMap;
594
            IBufMap mSharedIBufMap;
595
596
            uint32 hashIndexBuffer(uint16 batchSize, 
597
                uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols, 
598
                uint16 skirtRowColSkip);
599
            HardwareVertexBufferSharedPtr getVertexBuffer(VBufList& list, size_t vertexSize, size_t numVertices);
600
601
        };
602
603
        /** Tell this instance to use the given GpuBufferAllocator. 
604
605
            May only be called when the terrain is not loaded.
606
        */
607
        void setGpuBufferAllocator(GpuBufferAllocator* alloc);
608
609
        /// Get the current buffer allocator
610
        GpuBufferAllocator* getGpuBufferAllocator();
611
612
        /// Utility method to get the number of indexes required to render a given batch
613
        static size_t _getNumIndexesForBatchSize(uint16 batchSize);
614
        /** Utility method to populate a (locked) index buffer.
615
        @param pIndexes Pointer to an index buffer to populate
616
        @param batchSize The number of vertices down one side of the batch
617
        @param vdatasize The number of vertices down one side of the vertex data being referenced
618
        @param vertexIncrement The number of vertices to increment for each new indexed row / column
619
        @param xoffset The x offset from the start of the vertex data being referenced
620
        @param yoffset The y offset from the start of the vertex data being referenced
621
        @param numSkirtRowsCols Number of rows and columns of skirts
622
        @param skirtRowColSkip The number of rows / cols to skip in between skirts
623
624
        */
625
        static void _populateIndexBuffer(uint16* pIndexes, uint16 batchSize, 
626
            uint16 vdatasize, uint16 vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols,
627
            uint16 skirtRowColSkip);
628
629
        /** Utility method to calculate the skirt index for a given original vertex index. */
630
        static uint16 _calcSkirtVertexIndex(uint16 mainIndex, uint16 vdatasize, bool isCol, 
631
            uint16 numSkirtRowsCols, uint16 skirtRowColSkip);
632
633
        /** Convert a position from one space to another with respect to this terrain.
634
        @param inSpace The space that inPos is expressed as
635
        @param inPos The incoming position
636
        @param outSpace The space which outPos should be expressed as
637
        @param outPos The output position to be populated
638
        */
639
        void convertPosition(Space inSpace, const Vector3& inPos, Space outSpace, Vector3& outPos) const;
640
        /** Convert a position from one space to another with respect to this terrain.
641
        @param inSpace The space that inPos is expressed as
642
        @param inPos The incoming position
643
        @param outSpace The space which outPos should be expressed as
644
        @return The output position 
645
        */
646
        Vector3 convertPosition(Space inSpace, const Vector3& inPos, Space outSpace) const;
647
        /** Convert a direction from one space to another with respect to this terrain.
648
        @param inSpace The space that inDir is expressed as
649
        @param inDir The incoming direction
650
        @param outSpace The space which outDir should be expressed as
651
        @param outDir The output direction to be populated
652
        */
653
        void convertDirection(Space inSpace, const Vector3& inDir, Space outSpace, Vector3& outDir) const;
654
        /** Convert a direction from one space to another with respect to this terrain.
655
        @param inSpace The space that inDir is expressed as
656
        @param inDir The incoming direction
657
        @param outSpace The space which outDir should be expressed as
658
        @return The output direction 
659
        */
660
        Vector3 convertDirection(Space inSpace, const Vector3& inDir, Space outSpace) const;
661
662
        /** Set the resource group to use when loading / saving. 
663
        @param resGroup Resource group name - you can set this to blank to use
664
            the default in TerrainGlobalOptions.
665
        */
666
0
        void setResourceGroup(const String& resGroup) { mResourceGroup = resGroup; }
667
668
        /** Get the resource group to use when loading / saving. 
669
            If this is blank, the default in TerrainGlobalOptions will be used.
670
        */
671
0
        const String& getResourceGroup() const { return mResourceGroup; }
672
673
        /** Get the final resource group to use when loading / saving. 
674
        */
675
        const String& _getDerivedResourceGroup() const;
676
677
        /** Save terrain data in native form to a standalone file
678
        @param filename The name of the file to save to. If this is a filename with
679
            no path elements, then it is saved in the first writeable location
680
            available in the resource group you have chosen to use for this
681
            terrain. If the filename includes path specifiers then it is saved
682
            directly instead (but note that it may not be reloadable via the
683
            resource system if the location is not on the path). 
684
        */
685
        void save(const String& filename);
686
        /** Save terrain data in native form to a serializing stream.
687
688
            If you want complete control over where the terrain data goes, use
689
            this form.
690
        */
691
        void save(StreamSerialiser& stream);
692
693
        /** Prepare the terrain from a standalone file.
694
        @note
695
        This is safe to do in a background thread as it creates no GPU resources.
696
        It reads data from a native terrain data chunk. For more advanced uses, 
697
        such as loading from a shared file, use the StreamSerialiser form.
698
        */
699
        bool prepare(const String& filename);
700
        /** Prepare terrain data from saved data.
701
702
            This is safe to do in a background thread as it creates no GPU resources.
703
            It reads data from a native terrain data chunk. 
704
        @return true if the preparation was successful
705
        */
706
        bool prepare(DataStreamPtr& stream);
707
        /** Prepare terrain data from saved data.
708
709
            This is safe to do in a background thread as it creates no GPU resources.
710
            It reads data from a native terrain data chunk. 
711
        @return true if the preparation was successful
712
        */
713
        bool prepare(StreamSerialiser& stream);
714
715
        /** Prepare the terrain from some import data rather than loading from 
716
            native data. 
717
718
            This method may be called in a background thread.
719
        */
720
        bool prepare(const ImportData& importData);
721
722
        /** Prepare and load the terrain in one simple call from a standalone file.
723
        @note
724
            This method must be called from the primary render thread. To load data
725
            in a background thread, use the prepare() method.
726
        */
727
        void load(const String& filename);
728
729
        /** Prepare and load the terrain in one simple call from a stream.
730
        @note
731
        This method must be called from the primary render thread. To load data
732
        in a background thread, use the prepare() method.
733
        */
734
        void load(StreamSerialiser& stream);
735
736
        /** Load the terrain based on the data already populated via prepare methods. 
737
738
            This method must be called in the main render thread. 
739
        @param lodLevel Load the specified LOD level
740
        @param synchronous Load type
741
        */
742
        void load(int lodLevel = 0, bool synchronous = true);
743
744
        /** Return whether the terrain is loaded. 
745
746
            Should only be called from the render thread really, since this is
747
            where the loaded state changes.
748
        */
749
0
        bool isLoaded() const { return mIsLoaded; }
750
751
        /** Returns whether this terrain has been modified since it was first loaded / defined. 
752
753
            This flag is reset on save().
754
        */
755
0
        bool isModified() const { return mModified; }
756
757
758
        /** Returns whether terrain heights have been modified since the terrain was first loaded / defined. 
759
760
        This flag is reset on save().
761
        */
762
0
        bool isHeightDataModified() const { return mHeightDataModified; }
763
764
765
        /** Unload the terrain and free GPU resources. 
766
767
            This method must be called in the main render thread.
768
        */
769
        void unload();
770
771
        /** Free CPU resources created during prepare methods.
772
773
            This is safe to do in a background thread after calling unload().
774
        */
775
        void unprepare();
776
777
778
        /** Get a pointer to all the height data for this terrain.
779
780
            The height data is in world coordinates, relative to the position 
781
            of the terrain.
782
        @par
783
            This pointer is not const, so you can update the height data if you
784
            wish. However, changes will not be propagated until you call 
785
            Terrain::dirty or Terrain::dirtyRect.
786
        */
787
        float* getHeightData() const;
788
789
        /** Get a pointer to the height data for a given point. 
790
        */
791
        float* getHeightData(uint32 x, uint32 y) const;
792
793
        /** Get the height data for a given terrain point. 
794
        @param x, y Discrete coordinates in terrain vertices, values from 0 to size-1,
795
            left/right bottom/top
796
        */
797
        float getHeightAtPoint(uint32 x, uint32 y) const;
798
799
        /** Set the height data for a given terrain point. 
800
        @note this doesn't take effect until you call update()
801
        @param x, y Discrete coordinates in terrain vertices, values from 0 to size-1,
802
        left/right bottom/top
803
        @param h The new height
804
        */
805
        void setHeightAtPoint(uint32 x, uint32 y, float h);
806
807
        /** Get the height data for a given terrain position. 
808
        @param x, y Position in terrain space, values from 0 to 1 left/right bottom/top
809
        */
810
        float getHeightAtTerrainPosition(Real x, Real y) const;
811
812
        /** Get the height data for a given world position (projecting the point
813
            down on to the terrain). 
814
        @param x, y,z Position in world space. Positions will be clamped to the edge
815
            of the terrain
816
        */
817
        float getHeightAtWorldPosition(Real x, Real y, Real z) const;
818
        /// @overload
819
        float getHeightAtWorldPosition(const Vector3& pos) const;
820
821
        /** Get a pointer to all the delta data for this terrain.
822
823
            The delta data is a measure at a given vertex of by how much vertically
824
            a vertex will have to move to reach the point at which it will be
825
            removed in the next lower LOD.
826
        */
827
        const float* getDeltaData() const;
828
829
        /** Get a pointer to the delta data for a given point. 
830
        */
831
        const float* getDeltaData(uint32 x, uint32 y) const;
832
833
        /** Get a Vector3 of the world-space point on the terrain, aligned as per
834
            options.
835
        @note This point is relative to Terrain::getPosition
836
        */
837
        void getPoint(uint32 x, uint32 y, Vector3* outpos) const;
838
839
        /** Get a Vector3 of the world-space point on the terrain, aligned as per
840
        options. Cascades into neighbours if out of bounds.
841
        @note This point is relative to Terrain::getPosition - neighbours are
842
            adjusted to be relative to this tile
843
        */
844
        void getPointFromSelfOrNeighbour(int32 x, int32 y, Vector3* outpos) const;
845
846
        /** Get a Vector3 of the world-space point on the terrain, supplying the
847
            height data manually (can be more optimal). 
848
        @note This point is relative to Terrain::getPosition
849
        */
850
        void getPoint(uint32 x, uint32 y, float height, Vector3* outpos) const;
851
        /** Get a transform which converts Vector4(xindex, yindex, height, 1) into 
852
            an object-space position including scalings and alignment.
853
        */
854
        Affine3 getPointTransform() const;
855
856
        /** Translate a vector from world space to local terrain space based on the alignment options.
857
        @param inVec The vector in basis space, where x/y represents the 
858
        terrain plane and z represents the up vector
859
        @param[out] outVec
860
        */
861
        void getTerrainVector(const Vector3& inVec, Vector3* outVec) const;
862
        /// @overload
863
        void getTerrainVector(Real x, Real y, Real z, Vector3* outVec) const;
864
865
        /** Translate a vector from world space to local terrain space based on a specified alignment.
866
        @param inVec The vector in basis space, where x/y represents the 
867
        terrain plane and z represents the up vector
868
        @param[out] outVec
869
        @param align The alignment of the terrain
870
        */
871
        void getTerrainVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec) const;
872
        /// @overload
873
        void getTerrainVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec) const;
874
875
        /** Translate a vector into world space based on the alignment options.
876
        @param inVec The vector in basis space, where x/y represents the 
877
        terrain plane and z represents the up vector
878
        @param[out] outVec
879
        */
880
        void getVector(const Vector3& inVec, Vector3* outVec) const;
881
        /// @overload
882
        void getVector(Real x, Real y, Real z, Vector3* outVec) const;
883
884
        /** Translate a vector into world space based on a specified alignment.
885
        @param inVec The vector in basis space, where x/y represents the 
886
        terrain plane and z represents the up vector
887
        @param[out] outVec
888
        @param align The alignment of the terrain
889
        */
890
        void getVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec) const;
891
        /// @overload
892
        void getVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec) const;
893
894
895
        /** Convert a position from terrain basis space to world space. 
896
        @param TSpos Terrain space position, where (0,0) is the bottom-left of the
897
            terrain, and (1,1) is the top-right. The Z coordinate is in absolute
898
            height units.
899
        @note This position is relative to Terrain::getPosition
900
        @param outWSpos World space output position (setup according to current alignment). 
901
        */
902
        void getPosition(const Vector3& TSpos, Vector3* outWSpos) const;
903
        /// @overload
904
        void getPosition(Real x, Real y, Real z, Vector3* outWSpos) const;
905
906
        /** Convert a position from world space to terrain basis space. 
907
        @param WSpos World space position (setup according to current alignment). 
908
        @param outTSpos Terrain space output position, where (0,0) is the bottom-left of the
909
        terrain, and (1,1) is the top-right. The Z coordinate is in absolute
910
        height units.
911
        */
912
        void getTerrainPosition(const Vector3& WSpos, Vector3* outTSpos) const;
913
        /// @overload
914
        void getTerrainPosition(Real x, Real y, Real z, Vector3* outTSpos) const;
915
        /** Convert a position from terrain basis space to world space based on a specified alignment. 
916
        @param TSpos Terrain space position, where (0,0) is the bottom-left of the
917
            terrain, and (1,1) is the top-right. The Z coordinate is in absolute
918
            height units.
919
        @param outWSpos World space output position (setup according to alignment). 
920
        @param align The alignment of the terrain
921
        */
922
        void getPositionAlign(const Vector3& TSpos, Alignment align, Vector3* outWSpos) const;
923
        /// @overload
924
        void getPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outWSpos) const;
925
926
        /** Convert a position from world space to terrain basis space based on a specified alignment. 
927
        @param WSpos World space position (setup according to alignment). 
928
        @param outTSpos Terrain space output position, where (0,0) is the bottom-left of the
929
        terrain, and (1,1) is the top-right. The Z coordinate is in absolute
930
        height units.
931
        @param align The alignment of the terrain
932
        */
933
        void getTerrainPositionAlign(const Vector3& WSpos, Alignment align, Vector3* outTSpos) const;
934
        /// @overload
935
        void getTerrainPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outTSpos) const;
936
937
938
        /// Get the alignment of the terrain
939
        Alignment getAlignment() const;
940
        /// Get the size of the terrain in vertices along one side
941
        uint16 getSize() const;
942
        /** Set the size of terrain in vertices along one side. 
943
        @note The existing height data will be bilinear filtered to fill the new size
944
        @param newSize the new size of the terrain
945
        */
946
        void setSize(uint16 newSize);
947
        /// Get the maximum size in vertices along one side of a batch 
948
        uint16 getMaxBatchSize() const;
949
        /// Get the minimum size in vertices along one side of a batch 
950
        uint16 getMinBatchSize() const;
951
        /// Get the size of the terrain in world units
952
        Real getWorldSize() const;
953
        /** Set the world size of terrain. 
954
        @param newWorldSize the new world size of the terrain
955
        */
956
        void setWorldSize(Real newWorldSize);
957
958
        /// @name Layers
959
        /// @{
960
        /** Get the number of layers in this terrain. */
961
0
        uint8 getLayerCount() const { return static_cast<uint8>(mLayers.size()); }
962
963
        /** Get the declaration which describes the layers in this terrain. */
964
0
        const TerrainLayerDeclaration& getLayerDeclaration() const { return mLayerDecl; }
965
966
        /** Add a new layer to this terrain.
967
        @param worldSize The size of the texture in this layer in world units. Default
968
        to zero to use the default
969
        @param textureNames A list of textures to assign to the samplers in this
970
            layer. Leave blank to provide these later. 
971
        */
972
        void addLayer(Real worldSize = 0, const StringVector* textureNames = 0);
973
974
        /** Add a new layer to this terrain at a specific index.
975
        @param index The index at which to insert this layer (existing layers are shifted forwards)
976
        @param worldSize The size of the texture in this layer in world units. Default
977
        to zero to use the default
978
        @param textureNames A list of textures to assign to the samplers in this
979
            layer. Leave blank to provide these later. 
980
        */
981
        void addLayer(uint8 index, Real worldSize = 0, const StringVector* textureNames = 0);
982
983
        /** Remove a layer from the terrain.
984
        */
985
        void removeLayer(uint8 index);
986
987
        /** Replace an existing terrain layer, optionally preserving all other layer blend maps
988
        @param index The 0 based index of the terrain layer to replace
989
        @param keepBlends True to keep using the existing blend maps.  False to reset the blend map for the layer.
990
        Irrelevant if index == 0
991
        @param worldSize The size of the texture in this layer in world units. Default
992
        to zero to use the default
993
        @param textureNames A list of textures to assign to the samplers in this
994
            layer. Leave blank to provide these later. 
995
        */
996
        void replaceLayer(uint8 index, bool keepBlends, Real worldSize = 0, const StringVector* textureNames = 0);
997
998
        /** Get the maximum number of layers supported with the current options. 
999
        @note When you change the options requested, this value can change. 
1000
        */
1001
        uint8 getMaxLayers() const;
1002
1003
        /** How large an area in world space the texture in a terrain layer covers
1004
        before repeating. 
1005
        @param index The layer index.
1006
        */
1007
        Real getLayerWorldSize(uint8 index) const;
1008
        /** How large an area in world space the texture in a terrain layer covers
1009
        before repeating. 
1010
        @param index The layer index.
1011
        @param size The world size of the texture before repeating
1012
        */
1013
        void setLayerWorldSize(uint8 index, Real size);
1014
1015
        /** Get the layer UV multiplier. 
1016
1017
            This is derived from the texture world size. The base UVs in the 
1018
            terrain vary from 0 to 1 and this multiplier is used (in a fixed-function 
1019
            texture coord scaling or a shader parameter) to translate it to the
1020
            final value.
1021
        @param index The layer index.
1022
        */
1023
        Real getLayerUVMultiplier(uint8 index) const;
1024
1025
        /** Get the name of the texture bound to a given index within a given layer.
1026
        See the LayerDeclaration for a list of sampelrs within a layer.
1027
        @param layerIndex The layer index.
1028
        @param samplerIndex The sampler index within a layer
1029
        */
1030
        const String& getLayerTextureName(uint8 layerIndex, uint8 samplerIndex) const;
1031
        /** Set the name of the texture bound to a given index within a given layer.
1032
        See the LayerDeclaration for a list of sampelrs within a layer.
1033
        @param layerIndex The layer index.
1034
        @param samplerIndex The sampler index within a layer
1035
        @param textureName The name of the texture to use
1036
        */
1037
        void setLayerTextureName(uint8 layerIndex, uint8 samplerIndex, const String& textureName);
1038
1039
        /** Get the requested size of the blend maps used to blend between layers
1040
            for this terrain. 
1041
            Note that where hardware limits this, the actual blend maps may be lower
1042
            resolution. This option is derived from TerrainGlobalOptions when the
1043
            terrain is created.
1044
        */
1045
0
        uint16 getLayerBlendMapSize() const { return mLayerBlendMapSize; }
1046
        /// @}
1047
1048
        /** Get the requested size of lightmap for this terrain. 
1049
        Note that where hardware limits this, the actual lightmap may be lower
1050
        resolution. This option is derived from TerrainGlobalOptions when the
1051
        terrain is created.
1052
        */
1053
0
        uint16 getLightmapSize() const { return mLightmapSize; }
1054
1055
        /// Get access to the lightmap, if enabled (as requested by the material generator)
1056
0
        const TexturePtr& getLightmap() const { return mLightmap; }
1057
1058
        /** Get the requested size of composite map for this terrain. 
1059
        Note that where hardware limits this, the actual texture may be lower
1060
        resolution. This option is derived from TerrainGlobalOptions when the
1061
        terrain is created.
1062
        */
1063
0
        uint16 getCompositeMapSize() const { return mCompositeMapSize; }
1064
1065
        /// Get access to the composite map, if enabled (as requested by the material generator)
1066
0
        const TexturePtr& getCompositeMap() const { return mCompositeMap; }
1067
1068
        /// Get the world position of the terrain centre
1069
0
        const Vector3& getPosition() const { return mPos; }
1070
        /// Set the position of the terrain centre in world coordinates
1071
        void setPosition(const Vector3& pos);
1072
        /// Get the root scene node for the terrain (internal use only)
1073
        SceneNode* _getRootSceneNode() const;
1074
        /** Mark the entire terrain as dirty. 
1075
        By marking a section of the terrain as dirty, you are stating that you have
1076
        changed the height data within this rectangle. This rectangle will be merged with
1077
        any existing outstanding changes. To finalise the changes, you must 
1078
        call update(), updateGeometry(), or updateDerivedData().
1079
        */
1080
        void dirty();
1081
1082
        /** Mark a region of the terrain as dirty. 
1083
        By marking a section of the terrain as dirty, you are stating that you have
1084
        changed the height data within this rectangle. This rectangle will be merged with
1085
        any existing outstanding changes. To finalise the changes, you must 
1086
        call update(), updateGeometry(), or updateDerivedData().
1087
        @param rect A rectangle expressed in vertices describing the dirty region;
1088
            left < right, top < bottom, left & top are inclusive, right & bottom exclusive
1089
        */
1090
        void dirtyRect(const Rect& rect);
1091
1092
        /** Mark a region of the terrain composite map as dirty. 
1093
1094
            You don't usually need to call this directly, it is inferred from 
1095
            changing the other data on the terrain.
1096
        */
1097
        void _dirtyCompositeMapRect(const Rect& rect);
1098
1099
        /** Mark a region of the lightmap as dirty.
1100
1101
            You only need to call this if you need to tell the terrain to update
1102
            the lightmap data for some reason other than the terrain geometry
1103
            has changed. Changing terrain geometry automatically dirties the
1104
            correct lightmap areas.
1105
        @note
1106
            The lightmap won't actually be updated until update() or updateDerivedData()
1107
            is called.
1108
        */
1109
        void dirtyLightmapRect(const Rect& rect);
1110
1111
        /** Mark a the entire lightmap as dirty.
1112
1113
            You only need to call this if you need to tell the terrain to update
1114
            the lightmap data for some reason other than the terrain geometry
1115
            has changed. Changing terrain geometry automatically dirties the
1116
            correct lightmap areas.
1117
        @note
1118
            The lightmap won't actually be updated until update() or updateDerivedData()
1119
            is called.
1120
        */
1121
        void dirtyLightmap();
1122
1123
        /** Trigger the update process for the terrain.
1124
1125
            Updating the terrain will process any dirty sections of the terrain.
1126
            This may affect many things:
1127
            <ol><li>The terrain geometry</li>
1128
            <li>The terrain error metrics which determine LOD transitions</li>
1129
            <li>The terrain normal map, if present</li>
1130
            <li>The terrain lighting map, if present</li>
1131
            <li>The terrain composite map, if present</li>
1132
            </ol>
1133
            If threading is enabled, only item 1 (the geometry) will be updated
1134
            synchronously, ie will be fully up to date when this method returns.
1135
            The other elements are more expensive to compute, and will be queued
1136
            for processing in a background thread, in the order shown above. As these
1137
            updates complete, the effects will be shown.
1138
1139
            You can also separate the timing of updating the geometry, LOD and the lighting
1140
            information if you want, by calling updateGeometry() and
1141
            updateDerivedData() separately.
1142
            @param synchronous If true, all updates will happen immediately and not
1143
            in a separate thread.
1144
        */
1145
        void update(bool synchronous = false);
1146
1147
        /** Performs an update on the terrain geometry based on the dirty region.
1148
1149
            Terrain geometry will be updated when this method returns.
1150
        */
1151
        void updateGeometry();
1152
        /** Performs an update on the terrain geometry based on the dirty region.
1153
1154
            Terrain geometry will be updated when this method returns, and no
1155
            neighbours will be notified.
1156
        */
1157
        void updateGeometryWithoutNotifyNeighbours();
1158
1159
        // Used as a type mask for updateDerivedData
1160
        static const uint8 DERIVED_DATA_DELTAS;
1161
        static const uint8 DERIVED_DATA_NORMALS;
1162
        static const uint8 DERIVED_DATA_LIGHTMAP;
1163
        static const uint8 DERIVED_DATA_ALL;
1164
1165
        /** Updates derived data for the terrain (LOD, lighting) to reflect changed height data, in a separate
1166
        thread if threading is enabled (OGRE_THREAD_SUPPORT). 
1167
        If threading is enabled, on return from this method the derived
1168
        data will not necessarily be updated immediately, the calculation 
1169
        may be done in the background. Only one update will run in the background
1170
        at once. This derived data can typically survive being out of sync for a 
1171
        few frames which is why it is not done synchronously
1172
        @param synchronous If true, the update will happen immediately and not
1173
            in a separate thread.
1174
        @param typeMask Mask indicating the types of data we should generate
1175
        */
1176
        void updateDerivedData(bool synchronous = false, uint8 typeMask = 0xFF);
1177
1178
        /** Performs an update on the terrain composite map based on its dirty region.
1179
1180
            Rather than calling this directly, call updateDerivedData, which will
1181
            also call it after the other derived data has been updated (there is
1182
            no point updating the composite map until lighting has been updated).
1183
            However the blend maps may call this directly when only the blending 
1184
            information has been updated.
1185
        */
1186
        void updateCompositeMap();
1187
1188
        /** Performs an update on the terrain composite map based on its dirty region, 
1189
            but only at a maximum frequency. 
1190
1191
        Rather than calling this directly, call updateDerivedData, which will
1192
        also call it after the other derived data has been updated (there is
1193
        no point updating the composite map until lighting has been updated).
1194
        However the blend maps may call this directly when only the blending 
1195
        information has been updated.
1196
        @note
1197
        This method will log the request for an update, but won't do it just yet 
1198
        unless there are no further requests in the next 'delay' seconds. This means
1199
        you can call it all the time but only pick up changes in quiet times.
1200
        */
1201
        void updateCompositeMapWithDelay(Real delay = 2);
1202
1203
1204
        /** The default size of 'skirts' used to hide terrain cracks
1205
            (default 10, set for new Terrain using TerrainGlobalOptions)
1206
        */
1207
0
        Real getSkirtSize() const { return mSkirtSize; }
1208
1209
        /// Get the total number of LOD levels in the terrain
1210
0
        uint16 getNumLodLevels() const { return mNumLodLevels; }
1211
1212
        /// Get the number of LOD levels in a leaf of the terrain quadtree
1213
0
        uint16 getNumLodLevelsPerLeaf() const { return mNumLodLevelsPerLeafNode; }
1214
1215
        /** Calculate (or recalculate) the delta values of heights between a vertex
1216
            in its recorded position, and the place it will end up in the LOD
1217
            in which it is removed. 
1218
        @param rect Rectangle describing the area in which heights have altered 
1219
        @return A Rectangle describing the area which was updated (may be wider
1220
            than the input rectangle)
1221
        */
1222
        Rect calculateHeightDeltas(const Rect& rect);
1223
1224
        /** Finalise the height deltas. 
1225
        Calculated height deltas are kept in a separate calculation field to make
1226
        them safe to perform in a background thread. This call promotes those
1227
        calculations to the runtime values, and must be called in the main thread.
1228
        @param rect Rectangle describing the area to finalise 
1229
        @param cpuData When updating vertex data, update the CPU copy (background)
1230
        */
1231
        void finaliseHeightDeltas(const Rect& rect, bool cpuData);
1232
1233
        /** Calculate (or recalculate) the normals on the terrain
1234
        @param rect Rectangle describing the area of heights that were changed
1235
        @param outFinalRect Output rectangle describing the area updated
1236
        @return Pointer to a PixelBox full of normals (caller responsible for deletion)
1237
        */
1238
        PixelBox* calculateNormals(const Rect& rect, Rect& outFinalRect);
1239
1240
        /** Finalise the normals. 
1241
        Calculated normals are kept in a separate calculation area to make
1242
        them safe to perform in a background thread. This call promotes those
1243
        calculations to the runtime values, and must be called in the main thread.
1244
        @param rect Rectangle describing the area to finalise 
1245
        @param normalsBox Pointer to a PixelBox full of normals
1246
        */
1247
        void finaliseNormals(const Rect& rect, PixelBox* normalsBox);
1248
1249
        /** Calculate (or recalculate) the terrain lightmap
1250
        @param rect Rectangle describing the area of heights that were changed
1251
        @param extraTargetRect Rectangle describing a target area of the terrain that
1252
            needs to be calculated additionally (e.g. from a neighbour)
1253
        @param outFinalRect Output rectangle describing the area updated in the lightmap
1254
        @return Pointer to a PixelBox full of lighting data (caller responsible for deletion)
1255
        */
1256
        PixelBox* calculateLightmap(const Rect& rect, const Rect& extraTargetRect, Rect& outFinalRect);
1257
1258
        /** Finalise the lightmap. 
1259
        Calculating lightmaps is kept in a separate calculation area to make
1260
        it safe to perform in a background thread. This call promotes those
1261
        calculations to the runtime values, and must be called in the main thread.
1262
        @param rect Rectangle describing the area to finalise 
1263
        @param lightmapBox Pointer to a PixelBox full of normals
1264
        */
1265
        void finaliseLightmap(const Rect& rect, PixelBox* lightmapBox);
1266
1267
        /** Gets the resolution of the entire terrain (down one edge) at a 
1268
            given LOD level. 
1269
        */
1270
        uint16 getResolutionAtLod(uint16 lodLevel) const;
1271
1272
        /** Test for intersection of a given ray with the terrain. If the ray hits
1273
         the terrain, the point of intersection is returned.
1274
         @param ray The ray to test for intersection
1275
         @param cascadeToNeighbours Whether the ray will be projected onto neighbours if
1276
            no intersection is found
1277
         @param distanceLimit The distance from the ray origin at which we will stop looking,
1278
            0 indicates no limit
1279
         @return A pair which contains whether the ray hit the terrain and, if so, where.
1280
         @remarks This can be called from any thread as long as no parallel write to
1281
         the heightmap data occurs.
1282
         */
1283
        std::pair<bool, Vector3> rayIntersects(const Ray& ray, 
1284
            bool cascadeToNeighbours = false, Real distanceLimit = 0); //const;
1285
        
1286
        /// Get the AABB (local coords) of the entire terrain
1287
        const AxisAlignedBox& getAABB() const;
1288
        /// Get the AABB (world coords) of the entire terrain
1289
        AxisAlignedBox getWorldAABB() const;
1290
        /// Get the minimum height of the terrain
1291
        Real getMinHeight() const;
1292
        /// Get the maximum height of the terrain
1293
        Real getMaxHeight() const;
1294
        /// Get the bounding radius of the entire terrain
1295
        Real getBoundingRadius() const;
1296
1297
        /// Get the material being used for the terrain
1298
        const MaterialPtr& getMaterial() const;
1299
        /// Internal getting of material 
1300
0
        const MaterialPtr& _getMaterial() const { return mMaterial; }
1301
        /// Get the material being used for the terrain composite map
1302
        const MaterialPtr& getCompositeMapMaterial() const;
1303
        /// Internal getting of material  for the terrain composite map
1304
0
        const MaterialPtr& _getCompositeMapMaterial() const { return mCompositeMapMaterial; }
1305
1306
        /// Get the name of the material being used for the terrain
1307
0
        const String& getMaterialName() const { return mMaterialName; }
1308
1309
        /// Overridden from SceneManager::Listener
1310
        void preFindVisibleObjects(SceneManager* source, 
1311
            SceneManager::IlluminationRenderStage irs, Viewport* v) override;
1312
        /// Overridden from SceneManager::Listener
1313
        void sceneManagerDestroyed(SceneManager* source) override;
1314
1315
        /// Get the render queue group that this terrain will be rendered into
1316
0
        uint8 getRenderQueueGroup(void) const { return mRenderQueueGroup; }
1317
        /** Set the render queue group that this terrain will be rendered into.
1318
        @remarks The default is specified in TerrainGlobalOptions
1319
        */
1320
0
        void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; }
1321
1322
        /// Get the visibility flags for this terrain.
1323
0
        uint32 getVisibilityFlags(void) const { return mVisibilityFlags; }
1324
        /** Set the visibility flags for this terrain.
1325
        @remarks The default is specified in TerrainGlobalOptions
1326
        */
1327
0
        void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; }
1328
1329
        /// Get the query flags for this terrain.
1330
0
        uint32 getQueryFlags(void) const { return mQueryFlags; }
1331
        /** Set the query flags for this terrain.
1332
        @remarks The default is specified in TerrainGlobalOptions
1333
        */
1334
0
        void setQueryFlags(uint32 flags) { mQueryFlags = flags; }
1335
        
1336
        /** As setQueryFlags, except the flags passed as parameters are appended to the existing flags on this object. */
1337
0
        void addQueryFlags(uint32 flags) { mQueryFlags |= flags; }
1338
        
1339
        /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */
1340
0
        void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; }
1341
        
1342
1343
        /** Retrieve the layer blending map for a given layer, which may
1344
            be used to edit the blending information for that layer.
1345
        @note
1346
            You can only do this after the terrain has been loaded. You may 
1347
            edit the content of the blend layer in another thread, but you
1348
            may only upload it in the main render thread.
1349
        @param layerIndex The layer index, which should be 1 or higher (since 
1350
            the bottom layer has no blending).
1351
        @return Pointer to the TerrainLayerBlendMap requested. The caller must
1352
            not delete this instance, use freeTemporaryResources if you want
1353
            to save the memory after completing your editing.
1354
        */
1355
        TerrainLayerBlendMap* getLayerBlendMap(uint8 layerIndex);
1356
1357
        /** Get the index of the blend texture that a given layer uses.
1358
        @param layerIndex The layer index, must be >= 1 and less than the number
1359
            of layers
1360
        @return The index of the shared blend texture
1361
        */
1362
        uint8 getBlendTextureIndex(uint8 layerIndex) const;
1363
1364
        /// @deprecated use getBlendTextures()
1365
        OGRE_DEPRECATED uint8 getBlendTextureCount() const;
1366
        /// Get the number of blend textures needed for a given number of layers
1367
0
        static uint8 getBlendTextureCount(uint8 numLayers) { return ((numLayers - 2) / 4) + 1; }
1368
1369
1370
        /** Get the packed blend textures.
1371
        @note These are indexed by the blend texture index, not the layer index
1372
            (multiple layers will share a blend texture)
1373
        */
1374
0
        const std::vector<TexturePtr>& getBlendTextures() const { return mBlendTextureList; }
1375
1376
        /// @deprecated use getBlendTextures()
1377
        OGRE_DEPRECATED const String& getBlendTextureName(uint8 textureIndex) const;
1378
1379
        /** Set whether a global colour map is enabled. 
1380
1381
            A global colour map can add variation to your terrain and reduce the 
1382
            perceived tiling effect you might get in areas of continuous lighting
1383
            and the same texture. 
1384
            The global colour map is only used when the material generator chooses
1385
            to use it.
1386
        @note You must only call this from the main render thread
1387
        @param enabled Whether the global colour map is enabled or not
1388
        @param size The resolution of the colour map. A value of zero means 'no change'
1389
            and the default is set in TerrainGlobalOptions.
1390
        */
1391
        void setGlobalColourMapEnabled(bool enabled, uint16 size = 0);
1392
        /// Get whether a global colour map is enabled on this terrain
1393
0
        bool getGlobalColourMapEnabled() const { return mGlobalColourMapEnabled; }
1394
        /// Get the size of the global colour map (if used)
1395
0
        uint16 getGlobalColourMapSize() const { return mGlobalColourMapSize; }
1396
        /// Get access to the global colour map, if enabled
1397
0
        const TexturePtr& getGlobalColourMap() const { return mColourMap; }
1398
1399
        /** Widen a rectangular area of terrain to take into account an extrusion vector.
1400
        @param vec A vector in world space
1401
        @param inRect Input rectangle
1402
        @param outRect Output rectangle
1403
        */
1404
        void widenRectByVector(const Vector3& vec, const Rect& inRect, Rect& outRect);
1405
1406
        /** Widen a rectangular area of terrain to take into account an extrusion vector, 
1407
            but specify the min / max heights to extrude manually.
1408
        @param vec A vector in world space
1409
        @param inRect Input rectangle
1410
        @param minHeight, maxHeight The extents of the height to extrude
1411
        @param outRect Output rectangle
1412
        */
1413
        void widenRectByVector(const Vector3& vec, const Rect& inRect, 
1414
            Real minHeight, Real maxHeight, Rect& outRect);
1415
1416
        /** Free as many resources as possible for optimal run-time memory use.
1417
1418
            This class keeps some temporary storage around in order to make
1419
            certain actions (such as editing) possible more quickly. Calling this
1420
            method will cause as many of those resources as possible to be
1421
            freed. You might want to do this for example when you are finished
1422
            editing a particular terrain and want to have optimal runtime
1423
            efficiency.
1424
        */
1425
        void freeTemporaryResources();
1426
1427
        /** Get a blend texture with a given index.
1428
        @param index The blend texture index (note: not layer index; derive
1429
        the texture index from getLayerBlendTextureIndex)
1430
        */
1431
        const TexturePtr& getLayerBlendTexture(uint8 index) const;
1432
1433
        /** Get the texture index and colour channel of the blend information for 
1434
            a given layer. 
1435
        @param layerIndex The index of the layer (1 or higher, layer 0 has no blend data)
1436
        @return A pair in which the first value is the texture index, and the 
1437
            second value is the colour channel (RGBA)
1438
        */
1439
        std::pair<uint8,uint8> getLayerBlendTextureIndex(uint8 layerIndex) const;
1440
1441
        /** @name Internal implementation options for the terrain material
1442
1443
        The TerrainMaterialGenerator should call this methods to specify the
1444
        options it would like to use when creating a material. Not all the data
1445
        is guaranteed to be up to date on return from this method - for example some
1446
        maps may be generated in the background. However, on return from this method
1447
        all the features that are requested will be referenceable by materials, the
1448
        data may just take a few frames to be fully populated.
1449
        */
1450
        /// @{
1451
        /** Request vertex morphing information.
1452
        @param morph Whether LOD morphing information is required to be calculated
1453
        */
1454
0
        void _setMorphRequired(bool morph) { mLodMorphRequired = morph; }
1455
        /// Get whether LOD morphing is needed
1456
0
        bool _getMorphRequired() const { return mLodMorphRequired; }
1457
1458
        /** Request a terrain-wide normal map.
1459
        @param normalMap Whether a terrain-wide normal map is requested. This is usually
1460
            mutually exclusive with the lightmap option.
1461
        */
1462
        void _setNormalMapRequired(bool normalMap);
1463
1464
        /** Request a terrain-wide light map.
1465
        @param lightMap Whether a terrain-wide lightmap including precalculated 
1466
            lighting is required (light direction in TerrainGlobalOptions)
1467
        @param shadowsOnly If true, the lightmap contains only shadows, 
1468
            no directional lighting intensity
1469
        */
1470
        void _setLightMapRequired(bool lightMap, bool shadowsOnly = false);
1471
1472
        /** Request a terrain-wide composite map.
1473
1474
        A composite map is a texture with all of the blending and lighting baked in, such that
1475
        at distance this texture can be used as an approximation of the multi-layer
1476
        blended material. It is actually up to the material generator to render this
1477
        composite map, because obviously precisely what it looks like depends on what
1478
        the main material looks like. For this reason, the composite map is one piece
1479
        of derived terrain data that is always calculated in the render thread, and
1480
        usually on the GPU. It is expected that if this option is requested,
1481
        the material generator will use it to construct distant LOD techniques.
1482
        @param compositeMap Whether a terrain-wide composite map is needed.
1483
        */
1484
        void _setCompositeMapRequired(bool compositeMap);
1485
        /// @}
1486
1487
        /// Whether we're using vertex compression or not
1488
        bool _getUseVertexCompression() const; 
1489
1490
        /// Utility method, get the first LOD Level at which this vertex is no longer included
1491
        uint16 getLODLevelWhenVertexEliminated(long x, long y) const;
1492
        /// Utility method, get the first LOD Level at which this vertex is no longer included
1493
        uint16 getLODLevelWhenVertexEliminated(long rowOrColulmn) const;
1494
1495
1496
        /// Get the top level of the quad tree which is used to divide up the terrain
1497
0
        TerrainQuadTreeNode* getQuadTree() { return mQuadTree; }
1498
1499
        /// Get the (global) normal map texture
1500
0
        TexturePtr getTerrainNormalMap() const { return mTerrainNormalMap; }
1501
1502
        /** Retrieve the terrain's neighbour, or null if not present.
1503
1504
            Terrains only know about their neighbours if they are notified via
1505
            setNeighbour. This information is not saved with the terrain since every
1506
            tile must be able to be independent.
1507
        @param index The index of the neighbour
1508
        */
1509
        Terrain* getNeighbour(NeighbourIndex index) const;
1510
1511
        /** Set a terrain's neighbour, or null to detach one. 
1512
1513
            This information is not saved with the terrain since every
1514
            tile must be able to be independent. However if modifications are
1515
            made to a tile which can affect its neighbours, while connected the
1516
            changes will be propagated. 
1517
        @param index The index of the neighbour
1518
        @param neighbour The terrain instance to become the neighbour, or null to reset.
1519
        @param recalculate If true, this terrain instance will recalculate elements
1520
            that could be affected by the connection of this tile (e.g. matching 
1521
            heights, calcaulting normals, calculating shadows crossing the boundary). 
1522
            If false, this terrain's state is assumed to be up to date already 
1523
            (e.g. was calculated with this tile present before and the state saved). 
1524
        @param notifyOther Whether the neighbour should also be notified (recommended
1525
            to leave this at the default so relationships are up to date before
1526
            background updates are triggered)
1527
        */
1528
        void setNeighbour(NeighbourIndex index, Terrain* neighbour, bool recalculate = false, bool notifyOther = true);
1529
1530
        /** Get the opposite neighbour relationship (useful for finding the 
1531
            neighbour index from the perspective of the tile the other side of the
1532
            boundary).
1533
        */
1534
        static NeighbourIndex getOppositeNeighbour(NeighbourIndex index);
1535
1536
        /** Get the neighbour enum for a given offset in a grid (signed).
1537
        */
1538
        static NeighbourIndex getNeighbourIndex(long offsetx, long offsety);
1539
1540
        /** Tell this instance to notify all neighbours that will be affected
1541
            by a height change that has taken place. 
1542
1543
            This method will determine which neighbours need notification and call
1544
            their neighbourModified method. It is called automatically by 
1545
            updateGeometry().
1546
        */
1547
        void notifyNeighbours();
1548
1549
        /** Notify that a neighbour has just finished updating and that this
1550
            change affects this tile. 
1551
        @param index The neighbour index (from this tile's perspective)
1552
        @param edgerect The area at the edge of this tile that needs height / normal
1553
            recalculation (may be null)
1554
        @param shadowrect The area on this tile where shadows need recalculating (may be null)
1555
        */
1556
        void neighbourModified(NeighbourIndex index, const Rect& edgerect, const Rect& shadowrect);
1557
1558
        /** Utility method to pick a neighbour based on a ray. 
1559
        @param ray The ray in world space
1560
        @param distanceLimit Limit beyond which we want to ignore neighbours (0 for infinite)
1561
        @return The first neighbour along this ray, or null
1562
        */
1563
        Terrain* raySelectNeighbour(const Ray& ray, Real distanceLimit = 0);
1564
1565
        /** Dump textures to files.
1566
1567
            This is a debugging method.
1568
        */
1569
        void _dumpTextures(const String& prefix, const String& suffix);
1570
1571
        /** Query whether a derived data update is in progress or not. */
1572
0
        bool isDerivedDataUpdateInProgress() const { return mDerivedDataUpdateInProgress; }
1573
1574
1575
        /// Utility method to convert axes from world space to terrain space (xy terrain, z up)
1576
        static void convertWorldToTerrainAxes(Alignment align, const Vector3& worldVec, Vector3* terrainVec);
1577
        /// Utility method to convert axes from terrain space (xy terrain, z up) tp world space
1578
        static void convertTerrainToWorldAxes(Alignment align, const Vector3& terrainVec, Vector3* worldVec);
1579
1580
        /// Utility method to write a layer declaration to a stream
1581
        static void writeLayerDeclaration(const TerrainLayerDeclaration& decl, StreamSerialiser& ser);
1582
        /// Utility method to read a layer declaration from a stream
1583
        static bool readLayerDeclaration(StreamSerialiser& ser, TerrainLayerDeclaration& targetdecl);
1584
        /// Utility method to write a layer instance list to a stream
1585
        static void writeLayerInstanceList(const Terrain::LayerInstanceList& lst, StreamSerialiser& ser);
1586
        /// Utility method to read a layer instance list from a stream
1587
        static bool readLayerInstanceList(StreamSerialiser& ser, size_t numSamplers, Terrain::LayerInstanceList& targetlst);
1588
1589
        // This mutex is write-locked by neighbours if they are in the process of deleting themselves.
1590
        // It should be read-locked whenever using neighbours in calculations which are possibly running in a
1591
        // background thread.
1592
        OGRE_RW_MUTEX(mNeighbourMutex);
1593
1594
        void waitForDerivedProcesses();
1595
    private:
1596
        void generateMaterial();
1597
1598
        /** Gets the data size at a given LOD level.
1599
        */
1600
        uint getGeoDataSizeAtLod(uint16 lodLevel) const;
1601
        /** Get the real lod level
1602
         @param lodLevel LOD level which can be negative.
1603
         @note After mapping, [-mNumLodLevels, -1] equals to [0,mNumLodLevels-1]
1604
         So you can reference the lowest LOD with -1
1605
         */
1606
        inline int getPositiveLodLevel( int lodLevel ) const
1607
0
        {
1608
0
            return (lodLevel>=0) ? lodLevel : mNumLodLevels+lodLevel;
1609
0
        }
1610
        void freeLodData();
1611
1612
        void freeCPUResources();
1613
        void freeGPUResources();
1614
        void determineLodLevels();
1615
        void distributeVertexData();
1616
        void updateBaseScale();
1617
        void createGPUBlendTextures();
1618
        void createLayerBlendMaps();
1619
        void createOrDestroyGPUNormalMap();
1620
        void createOrDestroyGPUColourMap();
1621
        void createOrDestroyGPULightmap();
1622
        void createOrDestroyGPUCompositeMap();
1623
1624
        void convertSpace(Space inSpace, const Vector3& inVec, Space outSpace, Vector3& outVec, bool translation) const;
1625
        Vector3 convertWorldToTerrainAxes(const Vector3& inVec) const;
1626
        Vector3 convertTerrainToWorldAxes(const Vector3& inVec) const;
1627
        /** Get a Vector3 of the world-space point on the terrain, aligned Y-up always.
1628
        @note This point is relative to Terrain::getPosition
1629
        */
1630
        void getPointAlign(uint32 x, uint32 y, Alignment align, Vector3* outpos) const;
1631
        /** Get a Vector3 of the world-space point on the terrain, supplying the
1632
        height data manually (can be more optimal). 
1633
        @note This point is relative to Terrain::getPosition
1634
        */
1635
        void getPointAlign(uint32 x, uint32 y, float height, Alignment align, Vector3* outpos) const;
1636
        void calculateCurrentLod(Viewport* vp);
1637
1638
        /// Delete blend maps for all layers >= lowIndex
1639
        void deleteBlendMaps(uint8 lowIndex);
1640
        /// Shift/slide all GPU blend texture channels > index up one slot.  Blend data may shift into the next texture
1641
        void shiftUpGPUBlendChannels(uint8 index);
1642
        /// Shift/slide all GPU blend texture channels > index down one slot.  Blend data may shift into the previous texture
1643
        void shiftDownGPUBlendChannels(uint8 index);
1644
        /// Copy a GPU blend channel from one source to another.  Source and Dest are not required to be in the same texture
1645
        void copyBlendTextureChannel(uint8 srcIndex, uint8 srcChannel, uint8 destIndex, uint8 destChannel );
1646
        /// Reset a blend channel back to full black
1647
        void clearGPUBlendChannel(uint8 index, uint channel);
1648
1649
        void copyGlobalOptions();
1650
        void checkLayers(bool includeGPUResources);
1651
        void checkDeclaration();
1652
        void deriveUVMultipliers();
1653
1654
        void updateDerivedDataImpl(const Rect& rect, const Rect& lightmapExtraRect, bool synchronous, uint8 typeMask);
1655
1656
        void getEdgeRect(NeighbourIndex index, int32 range, Rect* outRect) const;
1657
        // get the equivalent of the passed in edge rectangle in neighbour
1658
        void getNeighbourEdgeRect(NeighbourIndex index, const Rect& inRect, Rect* outRect) const;
1659
        // get the equivalent of the passed in edge point in neighbour
1660
        void getNeighbourPoint(NeighbourIndex index, uint32 x, uint32 y, uint32 *outx, uint32 *outy) const;
1661
        // overflow a point into a neighbour index and point
1662
        void getNeighbourPointOverflow(int32 x, int32 y, NeighbourIndex *outindex, uint32 *outx, uint32 *outy) const;
1663
1664
        /// Removes this terrain instance from neighbouring terrain's list of neighbours.
1665
        void removeFromNeighbours();
1666
1667
        SceneManager* mSceneMgr;
1668
        SceneNode* mRootNode;
1669
        String mResourceGroup;
1670
        bool mIsLoaded;
1671
        bool mModified;
1672
        bool mHeightDataModified;
1673
        
1674
        /// The height data (world coords relative to mPos)
1675
        float* mHeightData;
1676
        /// The delta information defining how a vertex moves before it is removed at a lower LOD
1677
        float* mDeltaData;
1678
        Alignment mAlign;
1679
        Real mWorldSize;
1680
        uint16 mSize;
1681
        uint16 mMaxBatchSize;
1682
        uint16 mMinBatchSize;
1683
        Vector3 mPos;
1684
        TerrainQuadTreeNode* mQuadTree;
1685
        uint16 mNumLodLevels;
1686
        uint16 mNumLodLevelsPerLeafNode;
1687
        uint16 mTreeDepth;
1688
        /// Base position in world space, relative to mPos
1689
        Real mBase;
1690
        /// Relationship between one point on the terrain and world size
1691
        Real mScale;
1692
        TerrainLayerDeclaration mLayerDecl;
1693
        LayerInstanceList mLayers;
1694
        RealVector mLayerUVMultiplier;
1695
1696
        Real mSkirtSize;
1697
        uint8 mRenderQueueGroup;
1698
        uint32 mVisibilityFlags;
1699
        uint32 mQueryFlags;
1700
1701
        Rect mDirtyGeometryRect;
1702
        Rect mDirtyDerivedDataRect;
1703
        Rect mDirtyGeometryRectForNeighbours;
1704
        Rect mDirtyLightmapFromNeighboursRect;
1705
        bool mDerivedDataUpdateInProgress;
1706
        /// If another update is requested while one is already running
1707
        uint8 mDerivedUpdatePendingMask;
1708
1709
        bool mGenerateMaterialInProgress;
1710
        /// Don't release Height/DeltaData when preparing
1711
        mutable bool mPrepareInProgress;
1712
        /// A data holder for communicating with the background derived data update
1713
        struct DerivedDataRequest
1714
        {
1715
            Terrain* terrain;
1716
            // types requested
1717
            uint8 typeMask;
1718
            Rect dirtyRect;
1719
            Rect lightmapExtraDirtyRect;
1720
        };
1721
1722
        /// A data holder for communicating with the background derived data update
1723
        struct DerivedDataResponse
1724
        {
1725
            Terrain* terrain;
1726
            /// Remaining types not yet processed
1727
            uint8 remainingTypeMask;
1728
            /// The area of deltas that was updated
1729
            Rect deltaUpdateRect;
1730
            /// The area of normals that was updated
1731
            Rect normalUpdateRect;
1732
            /// The area of lightmap that was updated
1733
            Rect lightmapUpdateRect;
1734
            /// All CPU-side data, independent of textures; to be blitted in main thread
1735
            PixelBox* normalMapBox;
1736
            PixelBox* lightMapBox;
1737
        };
1738
1739
        DerivedDataResponse handleRequest(DerivedDataRequest& req);
1740
        void handleResponse(const DerivedDataResponse& res, const DerivedDataRequest& req);
1741
1742
        String mMaterialName;
1743
        MaterialPtr mMaterial;
1744
        TerrainMaterialGeneratorPtr mMaterialGenerator;
1745
        unsigned long long int mMaterialGenerationCount;
1746
        bool mMaterialDirty;
1747
        mutable bool mMaterialParamsDirty;
1748
1749
        uint16 mLayerBlendMapSize;
1750
        uint16 mLayerBlendMapSizeActual;
1751
        typedef std::vector<TexturePtr> TexturePtrList;
1752
        TexturePtrList mBlendTextureList;
1753
        TerrainLayerBlendMapList mLayerBlendMapList;
1754
1755
        uint16 mGlobalColourMapSize;
1756
        bool mGlobalColourMapEnabled;
1757
        TexturePtr mColourMap;
1758
1759
        uint16 mLightmapSize;
1760
        uint16 mLightmapSizeActual;
1761
        TexturePtr mLightmap;
1762
1763
        uint16 mCompositeMapSize;
1764
        uint16 mCompositeMapSizeActual;
1765
        TexturePtr mCompositeMap;
1766
        Rect mCompositeMapDirtyRect;
1767
        unsigned long mCompositeMapUpdateCountdown;
1768
        unsigned long mLastMillis;
1769
        /// True if the updates included lightmap changes (widen)
1770
        bool mCompositeMapDirtyRectLightmapUpdate;
1771
        mutable MaterialPtr mCompositeMapMaterial;
1772
1773
        /// staging images read in prepere
1774
        typedef std::vector<Image> ImageList;
1775
        ImageList mCpuBlendMapStorage;
1776
        Image mCpuColourMap;
1777
        Image mCpuLightmap;
1778
        Image mCpuCompositeMap;
1779
        Image mCpuTerrainNormalMap;
1780
1781
        static NameGenerator msBlendTextureGenerator;
1782
1783
        bool mLodMorphRequired;
1784
        bool mNormalMapRequired;
1785
        bool mLightMapRequired;
1786
        bool mLightMapShadowsOnly;
1787
        bool mCompositeMapRequired;
1788
        /// Texture storing normals for the whole terrain
1789
        TexturePtr mTerrainNormalMap;
1790
1791
        const Camera* mLastLODCamera;
1792
        unsigned long mLastLODFrame;
1793
        int mLastViewportHeight;
1794
1795
        Terrain* mNeighbours[NEIGHBOUR_COUNT];
1796
1797
        GpuBufferAllocator* mCustomGpuBufferAllocator;
1798
        DefaultGpuBufferAllocator mDefaultGpuBufferAllocator;
1799
1800
        size_t getPositionBufVertexSize() const;
1801
        size_t getDeltaBufVertexSize() const;
1802
1803
        TerrainLodManager* mLodManager;
1804
1805
        std::future<void> mDerivedDataFuture;
1806
1807
    public:
1808
        /** Increase Terrain's LOD level by 1
1809
          @param synchronous Run synchronously
1810
          */
1811
        void increaseLodLevel(bool synchronous = false);
1812
        /** Removes highest LOD level loaded
1813
          @remarks If there is LOD level load in progress it's load is canceled instead of removal of already loaded one.
1814
          */
1815
        void decreaseLodLevel();
1816
1817
0
        int getHighestLodPrepared() const { return (mLodManager) ? mLodManager->getHighestLodPrepared() : -1; };
1818
0
        int getHighestLodLoaded() const { return (mLodManager) ? mLodManager->getHighestLodLoaded() : -1; };
1819
0
        int getTargetLodLevel() const { return (mLodManager) ? mLodManager->getTargetLodLevel() : -1; };
1820
    private:
1821
        /// Test a single quad of the terrain for ray intersection.
1822
        OGRE_FORCE_INLINE std::pair<bool, Vector3> checkQuadIntersection(int x, int y, const Ray& ray) const;
1823
    };
1824
1825
1826
    /** Options class which just stores default options for the terrain.
1827
1828
        None of these options are stored with the terrain when saved. They are
1829
        options that you can use to modify the behaviour of the terrain when it
1830
        is loaded or created. 
1831
    @note
1832
        You should construct a single instance of this class per application and
1833
        do so before you start working with any other terrain classes.
1834
    */
1835
    class _OgreTerrainExport TerrainGlobalOptions : public TerrainAlloc, public Singleton<TerrainGlobalOptions>
1836
    {
1837
    protected:
1838
1839
        Real mSkirtSize;
1840
        Vector3 mLightMapDir;
1841
        bool mCastsShadows;
1842
        Real mMaxPixelError;
1843
        uint8 mRenderQueueGroup;
1844
        uint32 mVisibilityFlags;
1845
        uint32 mQueryFlags;
1846
        bool mUseRayBoxDistanceCalculation;
1847
        TerrainMaterialGeneratorPtr mDefaultMaterialGenerator;
1848
        uint16 mLayerBlendMapSize;
1849
        Real mDefaultLayerTextureWorldSize;
1850
        uint16 mDefaultGlobalColourMapSize;
1851
        uint16 mLightmapSize;
1852
        uint16 mCompositeMapSize;
1853
        ColourValue mCompositeMapAmbient;
1854
        ColourValue mCompositeMapDiffuse;
1855
        Real mCompositeMapDistance;
1856
        String mResourceGroup;
1857
        bool mUseVertexCompressionWhenAvailable;
1858
1859
    public:
1860
        TerrainGlobalOptions();
1861
        ~TerrainGlobalOptions();
1862
1863
1864
        /** The default size of 'skirts' used to hide terrain cracks
1865
        (default 10)
1866
        */
1867
0
        Real getSkirtSize() const { return mSkirtSize; }
1868
        /** method - the default size of 'skirts' used to hide terrain cracks
1869
        (default 10)
1870
1871
        @note Changing this value only applies to Terrain instances loaded / reloaded afterwards.
1872
        */
1873
0
        void setSkirtSize(Real skirtSz) { mSkirtSize = skirtSz; }
1874
        /// Get the shadow map light direction to use (world space)
1875
0
        const Vector3& getLightMapDirection() const { return mLightMapDir; }
1876
        /** Set the shadow map light direction to use (world space). */
1877
0
        void setLightMapDirection(const Vector3& v) { mLightMapDir = v; }
1878
        /// Get the composite map ambient light to use 
1879
0
        const ColourValue& getCompositeMapAmbient() const { return mCompositeMapAmbient; }
1880
        /// Set the composite map ambient light to use 
1881
0
        void setCompositeMapAmbient(const ColourValue& c) { mCompositeMapAmbient = c; }
1882
        /// Get the composite map iffuse light to use 
1883
0
        const ColourValue& getCompositeMapDiffuse() const { return mCompositeMapDiffuse; }
1884
        /// Set the composite map diffuse light to use 
1885
0
        void setCompositeMapDiffuse(const ColourValue& c) { mCompositeMapDiffuse = c; }
1886
        /// Get the distance at which to start using a composite map if present
1887
0
        Real getCompositeMapDistance() const { return mCompositeMapDistance; }
1888
        /// Set the distance at which to start using a composite map if present
1889
0
        void setCompositeMapDistance(Real c) { mCompositeMapDistance = c; }
1890
1891
1892
        /** Whether the terrain will be able to cast shadows (texture shadows
1893
        only are supported, and you must be using depth shadow maps).
1894
        */
1895
0
        bool getCastsDynamicShadows() const { return mCastsShadows; }
1896
1897
        /** Whether the terrain will be able to cast shadows (texture shadows
1898
        only are supported, and you must be using depth shadow maps).
1899
        This value can be set dynamically, and affects all existing terrains.
1900
        It defaults to false. 
1901
        */
1902
0
        void setCastsDynamicShadows(bool s) { mCastsShadows = s; }
1903
1904
        /** Get the maximum screen pixel error that should be allowed when rendering. */
1905
0
        Real getMaxPixelError() const { return mMaxPixelError; }
1906
1907
        /** Set the maximum screen pixel error that should  be allowed when rendering. 
1908
        @note
1909
            This value can be varied dynamically and affects all existing terrains.
1910
            It will be weighted by the LOD bias on viewports. 
1911
        */
1912
0
        void setMaxPixelError(Real pixerr) { mMaxPixelError = pixerr; }
1913
1914
        /// Get the render queue group that this terrain will be rendered into
1915
0
        uint8 getRenderQueueGroup(void) const { return mRenderQueueGroup; }
1916
        /** Set the render queue group that terrains will be rendered into.
1917
        @remarks This applies to newly created terrains, after which they will
1918
            maintain their own queue group settings
1919
        */
1920
0
        void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; }
1921
1922
        /// Get the visbility flags that terrains will be rendered with
1923
0
        uint32 getVisibilityFlags(void) const { return mVisibilityFlags; }
1924
        /** Set the visbility flags that terrains will be rendered with
1925
        @remarks This applies to newly created terrains, after which they will
1926
        maintain their own settings
1927
        */
1928
0
        void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; }
1929
1930
        /** Set the default query flags for terrains.
1931
        @remarks This applies to newly created terrains, after which they will
1932
        maintain their own settings
1933
        */
1934
0
        void  setQueryFlags(uint32 flags) { mQueryFlags = flags; }
1935
        /** Get the default query flags for terrains.
1936
        */
1937
0
        uint32 getQueryFlags(void) const { return mQueryFlags; }
1938
1939
        /** As setQueryFlags, except the flags passed as parameters are appended to the existing flags on this object. */
1940
0
        void addQueryFlags(uint32 flags) { mQueryFlags |= flags; }
1941
1942
        /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */
1943
0
        void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; }
1944
1945
        /** Returns whether or not to use an accurate calculation of camera distance
1946
            from a terrain tile (ray / AABB intersection) or whether to use the
1947
            simpler distance from the tile centre. 
1948
        */
1949
0
        bool getUseRayBoxDistanceCalculation() const { return mUseRayBoxDistanceCalculation; }
1950
1951
        /** Sets whether to use an accurate ray / box intersection to determine
1952
            distance from a terrain tile, or whether to use the simple distance
1953
            from the tile centre.
1954
            Using ray/box intersection will result in higher detail terrain because 
1955
            the LOD calculation is more conservative, assuming the 'worst case scenario' 
1956
            of a large height difference at the edge of a tile. This is guaranteed to give you at least
1957
            the max pixel error or better, but will often give you more detail than
1958
            you need. Not using the ray/box method is cheaper but will only use
1959
            the max pixel error as a guide, the actual error will vary above and
1960
            below that. The default is not to use the ray/box approach.
1961
        */
1962
0
        void setUseRayBoxDistanceCalculation(bool rb) { mUseRayBoxDistanceCalculation = rb; }
1963
1964
        /** Get the default material generator.
1965
        */
1966
        TerrainMaterialGeneratorPtr getDefaultMaterialGenerator();
1967
1968
        /** Set the default material generator.
1969
        */
1970
        void setDefaultMaterialGenerator(const TerrainMaterialGeneratorPtr& gen);
1971
1972
        /** Get the default size of the blend maps for a new terrain. 
1973
        */
1974
0
        uint16 getLayerBlendMapSize() const { return mLayerBlendMapSize; }
1975
1976
        /** Sets the default size of blend maps for a new terrain.
1977
        This is the resolution of each blending layer for a new terrain. 
1978
        Once created, this information will be stored with the terrain. 
1979
        */
1980
0
        void setLayerBlendMapSize(uint16 sz) { mLayerBlendMapSize = sz;}
1981
1982
        /** Get the default world size for a layer 'splat' texture to cover. 
1983
        */
1984
0
        Real getDefaultLayerTextureWorldSize() const { return mDefaultLayerTextureWorldSize; }
1985
1986
        /** Set the default world size for a layer 'splat' texture to cover. 
1987
        */
1988
0
        void setDefaultLayerTextureWorldSize(Real sz) { mDefaultLayerTextureWorldSize = sz; }
1989
1990
        /** Get the default size of the terrain global colour map for a new terrain. 
1991
        */
1992
0
        uint16 getDefaultGlobalColourMapSize() const { return mDefaultGlobalColourMapSize; }
1993
1994
        /** Set the default size of the terrain global colour map for a new terrain. 
1995
        Once created, this information will be stored with the terrain. 
1996
        */
1997
0
        void setDefaultGlobalColourMapSize(uint16 sz) { mDefaultGlobalColourMapSize = sz;}
1998
1999
2000
        /** Get the default size of the lightmaps for a new terrain. 
2001
        */
2002
0
        uint16 getLightMapSize() const { return mLightmapSize; }
2003
2004
        /** Sets the default size of lightmaps for a new terrain.
2005
        */
2006
0
        void setLightMapSize(uint16 sz) { mLightmapSize = sz;}
2007
2008
        /** Get the default size of the composite maps for a new terrain. 
2009
        */
2010
0
        uint16 getCompositeMapSize() const { return mCompositeMapSize; }
2011
2012
        /** Sets the default size of composite maps for a new terrain.
2013
        */
2014
0
        void setCompositeMapSize(uint16 sz) { mCompositeMapSize = sz;}
2015
2016
        /** Set the default resource group to use to load / save terrains.
2017
        */
2018
0
        void setDefaultResourceGroup(const String& grp) { mResourceGroup = grp; }
2019
2020
        /** Get the default resource group to use to load / save terrains.
2021
        */
2022
0
        const String& getDefaultResourceGroup() const { return mResourceGroup; }
2023
        
2024
        /** Get whether to allow vertex compression to be used when the material
2025
            generator states that it supports it.
2026
        */
2027
0
        bool getUseVertexCompressionWhenAvailable() const { return mUseVertexCompressionWhenAvailable; }
2028
2029
        /** Set whether to allow vertex compression to be used when the material
2030
         generator states that it supports it.
2031
         @note You should only call this before creating any terrain instances. 
2032
         The default is true, so if a material generator supports compressed vertices, 
2033
         and so does the hardware (this basically means shader support), they will be used).
2034
         However you can disable this in an emergency if required.
2035
         */
2036
0
        void setUseVertexCompressionWhenAvailable(bool enable) { mUseVertexCompressionWhenAvailable = enable; }
2037
2038
        /// @copydoc Singleton::getSingleton()
2039
        static TerrainGlobalOptions& getSingleton(void);
2040
        /// @copydoc Singleton::getSingleton()
2041
        static TerrainGlobalOptions* getSingletonPtr(void);
2042
2043
2044
    };
2045
2046
2047
    /** @} */
2048
    /** @} */
2049
}
2050
2051
2052
2053
2054
#endif