File: DepthShader.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 (93 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download | duplicates (4)
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
// 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

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

#include "iround.h"

#include <Mercator/DepthShader.h>

#include <Mercator/Segment.h>
#include <Mercator/Surface.h>

#include <cmath>

#include <cassert>

namespace Mercator {

const std::string DepthShader::key_waterLevel("waterLevel");
const std::string DepthShader::key_murkyDepth("murkyDepth");

const float DepthShader::default_waterLevel = 0.f;
const float DepthShader::default_murkyDepth = -64.f;


DepthShader::DepthShader(float waterLevel, float murkyDepth) : 
             m_waterLevel(waterLevel), m_murkyDepth(murkyDepth)
{
}

DepthShader::DepthShader(const Parameters & params) :
             m_waterLevel(default_waterLevel), m_murkyDepth(default_murkyDepth)
{
    Parameters::const_iterator I = params.find(key_waterLevel);
    Parameters::const_iterator Iend = params.end();
    if (I != Iend) {
        m_waterLevel = I->second;
    }
    I = params.find(key_murkyDepth);
    if (I != Iend) {
        m_murkyDepth = I->second;
    }
}

DepthShader::~DepthShader()
{
}

bool DepthShader::checkIntersect(const Segment & s) const
{
    if (s.getMin() < m_waterLevel) {
        return true;
    } else {
        return false;
    }
}

void DepthShader::shade(Surface & s) const
{
    unsigned int channels = s.getChannels();
    assert(channels > 0);
    unsigned int colors = channels - 1;
    ColorT * data = s.getData();
    const float * height_data = s.getSegment().getPoints();
    if (height_data == 0) {
        std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
                  << std::endl << std::flush;
        return;
    }
    unsigned int size = s.getSegment().getSize();

    unsigned int count = size * size;
    int j = -1;
    for (unsigned int i = 0; i < count; ++i) {
        for (unsigned int k = 0; k < colors; ++k) {
            data[++j] = colorMax;
        }
        float depth = height_data[i];
        if (depth > m_waterLevel) {
            data[++j] = colorMin;
        } else if (depth < m_murkyDepth) {
            data[++j] = colorMax;
        } else {
            data[++j] = colorMax - I_ROUND(colorMax * ((depth - m_murkyDepth)
                                              / (m_waterLevel - m_murkyDepth)));
        }
    }
}

} // namespace Mercator