File: Terrain.cpp

package info (click to toggle)
mercator 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,008 kB
  • sloc: sh: 10,433; cpp: 4,482; makefile: 115
file content (495 lines) | stat: -rw-r--r-- 16,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2003 Alistair Riddoch, Damien McGinnes

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "iround.h"

#include <Mercator/Terrain.h>

#include <Mercator/Matrix.h>
#include <Mercator/Segment.h>
#include <Mercator/TerrainMod.h>
#include <Mercator/Shader.h>
#include <Mercator/Area.h>
#include <Mercator/Surface.h>

#include <iostream>

#include <cstdio>

namespace Mercator {

const float Terrain::defaultLevel = 8;

/// \brief Construct a new Terrain object with optional options and resolution.
///
/// @param options a bitfield of option flags. Defaults to no flags set.
/// - DEFAULT value provided for no flags set.
/// - SHADED is set if shaders are going to be used on this terrain.
/// @param resolution the spacing between adjacent base points. Defaults to 64.
Terrain::Terrain(unsigned int options, unsigned int resolution) : m_options(options),
                                                                  m_res(resolution)
{

}

/// \brief Desctruct Terrain object, deleting contained objects.
///
/// All Segment objects are deleted, but Shader objects are not yet deleted.
/// Probably need to be fixed.
Terrain::~Terrain()
{
    Segmentstore::const_iterator I = m_segments.begin(); 
    Segmentstore::const_iterator Iend = m_segments.end();
    for (; I != Iend; ++I) {
        Segmentcolumn::const_iterator J = I->second.begin(); 
        Segmentcolumn::const_iterator Jend = I->second.end(); 
        for (; J != Jend; ++J) {
            Segment * s = J->second;
            if (s) delete s;
        }
    }
}

/// \brief Add a new Shader to the list for this terrain.
///
/// As each shader is added, surfaces are added to all existing segments
/// to store the result of the shader.
void Terrain::addShader(const Shader * t, int id)
{
    if (m_shaders.count(id)) {
        std::cerr << "WARNING: duplicate use of shader ID " << id << std::endl;
    }
    
    m_shaders[id] = t;
    
    Segmentstore::const_iterator I = m_segments.begin(); 
    Segmentstore::const_iterator Iend = m_segments.end(); 
    for (; I != Iend; ++I) {
        Segmentcolumn::const_iterator J = I->second.begin(); 
        Segmentcolumn::const_iterator Jend = I->second.end(); 
        for (; J != Jend; ++J) {
            Segment *seg=J->second;
            if (!t->checkIntersect(*seg)) {
                continue;
            }

            Segment::Surfacestore & sss = seg->getSurfaces();
            sss[id] = t->newSurface(*seg);
        }
    }
}

/// \brief remove a Shader from the list for this terrain.
///
/// As each shader is removed, surfaces are removed from existing segments
void Terrain::removeShader(const Shader * t, int id)
{

    m_shaders.erase(m_shaders.find(id));

    // Delete all surfaces for this shader
    Segmentstore::const_iterator I = m_segments.begin();
    Segmentstore::const_iterator Iend = m_segments.end();
    for (; I != Iend; ++I) {
        Segmentcolumn::const_iterator J = I->second.begin();
        Segmentcolumn::const_iterator Jend = I->second.end();
        for (; J != Jend; ++J) {
            Segment *seg=J->second;

            Segment::Surfacestore & sss = seg->getSurfaces();
            Segment::Surfacestore::iterator K = sss.find(id);
            if (K != sss.end()) {
                delete K->second;
                sss.erase(K);
            }
        }
    }
}



/// \brief Add the required Surface objects to a Segment.
///
/// If shading is enabled, each Segment has a set of Surface objects
/// corresponding to the Shader objects available for this terrain.
/// This function creates the necessary Surface objects, and adds them
/// to the list in the Segment object. At this point the Segment is
/// not yet populated with heightfield data, so the Surface cannot
/// be populated. A newly constructed surface does not allocate its
/// buffer memory, so there is very little overhead to creating it.
void Terrain::addSurfaces(Segment & seg)
{
    Segment::Surfacestore & sss = seg.getSurfaces();
    if (!sss.empty()) {
        std::cerr << "WARNING: Adding surfaces to a terrain segment which has surfaces"
                  << std::endl << std::flush;
        sss.clear();
    }
    
    Shaderstore::const_iterator I = m_shaders.begin();
    Shaderstore::const_iterator Iend = m_shaders.end();
    for (; I != Iend; ++I) {
        // shader doesn't touch this segment, skip
        if (!I->second->checkIntersect(seg)) {
            continue;
        }
        
        sss[I->first] = I->second->newSurface(seg);
    }
}

/// \brief Populate the Surface objects associated with a Segment.
///
/// This is called after a Segment has been populated with heightfield
/// data. The actual responsibility for populating the Surface objects
/// is in Segment::populateSurfaces().
void Terrain::shadeSurfaces(Segment & seg)
{
    seg.populateSurfaces();
}

/// \brief Get the height value at a given coordinate x,y.
///
/// This is a convenience function provided to quickly get a height
/// value at a given point. It always succeeds, as if no height data
/// is available it just returns the default value. If a Segment does
/// exist in the right place and is populated, the coords within that
/// Segment are determined, and the heightfield queried. This function
/// does not cause any data to be populated, and does not attempt to
/// do any interpolation to get an accurate height figure. For more
/// accurate results see Terrain::getHeightAndNormal.
float Terrain::get(float x, float y) const
{
    int ix = I_ROUND(floor(x / m_res));
    int iy = I_ROUND(floor(y / m_res));

    Segment * s = getSegment(ix, iy);
    if ((s == 0) || (!s->isValid())) {
        return Terrain::defaultLevel;
    }
    return s->get(I_ROUND(x) - (ix * m_res), I_ROUND(y) - (iy * m_res));
}

/// \brief Get an accurate height and normal vector at a given coordinate
/// x,y.
///
/// This is a more expensive function that Terrain::get() for getting an
/// accurate height value and surface normal at a given point. The main
/// body of functionality is in the Segment::getHeightAndNormal() function
/// called from here.
/// The height and normal are interpolated based on a model where each
/// tile of the heightfield comprises two triangles. If no heightfield data
/// is available for the given location, this function returns false, and
/// no data is returned.
/// @param x coordinate of point to be returned.
/// @param y coordinate of point to be returned.
/// @param h reference to variable which will be used to store the resulting
/// height value.
/// @param n reference to variable which will be used to store the resulting
/// normal value.
/// @return true if heightdata was available, false otherwise.
bool Terrain::getHeightAndNormal(float x, float y, float & h,
                                  WFMath::Vector<3> & n) const
{
    int ix = I_ROUND(floor(x / m_res));
    int iy = I_ROUND(floor(y / m_res));

    Segment * s = getSegment(ix, iy);
    if ((s == 0) || (!s->isValid())) {
        return false;
    }
    s->getHeightAndNormal(x - (ix * m_res), y - (iy * m_res), h, n);
    return true;
}

/// \brief Get the BasePoint at a given base point coordinate.
///
/// Get the BasePoint value for the given coordinate on the base
/// point grid.
/// @param x coordinate on the base point grid.
/// @param y coordinate on the base point grid.
/// @param z reference to varaible which will be used to store the
/// BasePoint data.
/// @return true if a BasePoint is defined at the given coordinate, false
/// otherwise.
bool Terrain::getBasePoint(int x, int y, BasePoint& z) const
{
    Pointstore::const_iterator I = m_basePoints.find(x);
    if (I == m_basePoints.end()) {
        return false;
    }
    Pointcolumn::const_iterator J = I->second.find(y);
    if (J == I->second.end()) {
        return false;
    }
    z = J->second;
    return true;
}

/// \brief Set the BasePoint value at a given base point coordinate.
///
/// Set the BasePoint value for the given coordinate on the base
/// point grid.
/// If inserting this BasePoint completes the set of points required
/// to define one or more Segment objects which were not yet defined,
/// new Segment objects are created. If this replaces a point for one
/// or more Segment objects that were already defined, the contents of
/// those Segment objects are invalidated.
/// @param x coordinate on the base point grid.
/// @param y coordinate on the base point grid.
/// @param z BasePoint value to be used at the given coordinate.
void Terrain::setBasePoint(int x, int y, const BasePoint& z)
{
    m_basePoints[x][y] = z;
    bool pointIsSet[3][3];
    BasePoint existingPoint[3][3];
    for(int i = x - 1, ri = 0; i < x + 2; ++i, ++ri) {
        for(int j = y - 1, rj = 0; j < y + 2; ++j, ++rj) {
            pointIsSet[ri][rj] = getBasePoint(i, j, existingPoint[ri][rj]);
        }
    }
    for(int i = x - 1, ri = 0; i < x + 1; ++i, ++ri) {
        for(int j = y - 1, rj = 0; j < y + 1; ++j, ++rj) {
            Segment * s = getSegment(i, j);
            if (s == 0) { 
                bool complete = pointIsSet[ri][rj] &&
                                pointIsSet[ri + 1][rj + 1] &&
                                pointIsSet[ri + 1][rj] &&
                                pointIsSet[ri][rj + 1];
                if (!complete) {
                    continue;
                }
                s = new Segment(i * m_res, j * m_res, m_res);
                Matrix<2, 2, BasePoint> & cp = s->getControlPoints();
                float min = existingPoint[ri][rj].height();
                float max = existingPoint[ri][rj].height();
                for(unsigned int k = 0; k < 2; ++k) {
                    for(unsigned int l = 0; l < 2; ++l) {
                        cp(k, l) = existingPoint[ri + k][rj + l];
                        min = std::min(cp(k, l).height(), min);
                        max = std::max(cp(k, l).height(), max);
                    }
                }
                s->setMinMax(min, max);
                
                Effectorstore::iterator I = m_effectors.begin();
                Effectorstore::iterator Iend = m_effectors.end();
                for (; I != Iend; ++I) {
                    I->first->addToSegment(*s);
                }

                // apply shaders last, after all other data is in place
                if (isShaded()) {
                    addSurfaces(*s);
                }
                
                m_segments[i][j] = s;
                continue;
            }
            s->setCornerPoint(ri ? 0 : 1, rj ? 0 : 1, z);
        }
    }
}

/// \brief Get the Segment at a given base point coordinate.
///
/// Get the Segment pointer for the given coordinate on the base
/// point grid. The Segment in question may not have been populated
/// with heightfield or surface data.
/// @param x coordinate on the base point grid.
/// @param y coordinate on the base point grid.
/// @return a valid pointer if a Segment is defined at the given coordinate,
/// zero otherwise.
Segment * Terrain::getSegment(int x, int y) const
{
    Segmentstore::const_iterator I = m_segments.find(x);
    if (I == m_segments.end()) {
        return 0;
    }
    Segmentcolumn::const_iterator J = I->second.find(y);
    if (J == I->second.end()) {
        return 0;
    }
    return J->second;
}

/// \brief Add an effector to the terrain
void Terrain::addEffector(const Effector * eff)
{
    //work out which segments are overlapped by this effector
    //note that the bbox is expanded by one grid unit because
    //segments share edges. this ensures a mod along an edge
    //will affect both segments.

    m_effectors.insert(Effectorstore::value_type(eff, eff->bbox()));

    int lx=I_ROUND(floor((eff->bbox().lowCorner()[0] - 1) / m_res));
    int ly=I_ROUND(floor((eff->bbox().lowCorner()[1] - 1) / m_res));
    int hx=I_ROUND(ceil((eff->bbox().highCorner()[0] + 1) / m_res));
    int hy=I_ROUND(ceil((eff->bbox().highCorner()[1] + 1) / m_res));

    for (int i=lx;i<hx;++i) {
        for (int j=ly;j<hy;++j) {
            Segment *s=getSegment(i,j);
            if (s) {
                eff->addToSegment(*s);
            }
        } // of y loop
    } // of x loop
}

Terrain::Rect Terrain::updateEffector(const Effector * eff)
{
    Effectorstore::iterator I = m_effectors.find(eff);

    if (I == m_effectors.end()) {
        return Rect();
    }

    const Rect old_box = I->second;


    std::set<Segment*> removed, added, updated;

    int lx=I_ROUND(floor((old_box.lowCorner()[0] - 1) / m_res));
    int ly=I_ROUND(floor((old_box.lowCorner()[1] - 1) / m_res));
    int hx=I_ROUND(ceil((old_box.highCorner()[0] + 1) / m_res));
    int hy=I_ROUND(ceil((old_box.highCorner()[1] + 1) / m_res));

    for (int i=lx;i<hx;++i) {
        for (int j=ly;j<hy;++j) {
            Segment *s=getSegment(i,j);
            if (!s) {
                continue;
            }

            removed.insert(s);

        } // of y loop
    } // of x loop

    lx=I_ROUND(floor((eff->bbox().lowCorner()[0] - 1) / m_res));
    ly=I_ROUND(floor((eff->bbox().lowCorner()[1] - 1) / m_res));
    hx=I_ROUND(ceil((eff->bbox().highCorner()[0] + 1) / m_res));
    hy=I_ROUND(ceil((eff->bbox().highCorner()[1] + 1) / m_res));

    for (int i=lx;i<hx;++i) {
        for (int j=ly;j<hy;++j) {
            Segment *s=getSegment(i,j);
            if (!s) {
                continue;
            }

            std::set<Segment*>::iterator J = removed.find(s);
            if (J == removed.end()) {
                added.insert(s);
            } else {
                updated.insert(s);
                removed.erase(J);
            }
        } // of y loop
    } // of x loop

    std::set<Segment*>::iterator J = removed.begin();
    std::set<Segment*>::iterator Jend = removed.end();
    for (; J != Jend; ++J) {
        eff->removeFromSegment(**J);
    }

    J = added.begin();
    Jend = added.end();
    for (; J != Jend; ++J) {
        eff->addToSegment(**J);
    }

    J = updated.begin();
    Jend = updated.end();
    for (; J != Jend; ++J) {
        eff->updateToSegment(**J);
    }

    I->second = eff->bbox();
    return old_box;
}

/// \brief Remove an effector from the terrain
void Terrain::removeEffector(const Effector * eff)
{
    m_effectors.erase(eff);

    const Rect & eff_box = eff->bbox();

    int lx=I_ROUND(floor((eff_box.lowCorner()[0] - 1) / m_res));
    int ly=I_ROUND(floor((eff_box.lowCorner()[1] - 1) / m_res));
    int hx=I_ROUND(ceil((eff_box.highCorner()[0] + 1) / m_res));
    int hy=I_ROUND(ceil((eff_box.highCorner()[1] + 1) / m_res));

    for (int i=lx;i<hx;++i) {
        for (int j=ly;j<hy;++j) {
            Segment *s=getSegment(i,j);
            if (s) {
                eff->removeFromSegment(*s);
            }
        } // of y loop
    } // of x loop
}

/// \brief Add a modifier to the terrain.
///
/// Add a new TerrainMod object to the terrain, which defines a modification
/// to the terrain heightfield or surface data. The segments are responsible
/// for storing the TerrainMod objects, so the apropriate Segment objects
/// are found and the TerrainMode is passed to each in turn.
/// @param t reference to the TerrainMod object to be applied.
void Terrain::addMod(const TerrainMod * mod)
{
    addEffector(mod);
}

Terrain::Rect Terrain::updateMod(const TerrainMod * mod)
{
    return updateEffector(mod);
}

void Terrain::removeMod(const TerrainMod * mod)
{
    removeEffector(mod);
}

/// \brief Add an area modifier to the terrain.
///
/// Add a new Area object to the terrain, which defines a modification
/// to the surface.
void Terrain::addArea(const Area * area)
{
    int layer = area->getLayer();

    Shaderstore::const_iterator I = m_shaders.find(layer);
    if (I != m_shaders.end()) {
        area->setShader(I->second);
    }
    
    addEffector(area);
}

/// \brief Apply changes to an area modifier to the terrain.
Terrain::Rect Terrain::updateArea(const Area * area)
{
    return updateEffector(area);
}

/// \brief Remove an area modifier from the terrain.
///
/// Remove an existing Area object from the terrain, and mark all the
/// affected terrain surfaces as invalid.
void Terrain::removeArea(const Area * area)
{
    removeEffector(area);
}

} // namespace Mercator