mercator  0.4.0
A terrain generation library for the Worldforge system.
ThresholdShader.cpp
1 // This file may be redistributed and modified only under the terms of
2 // the GNU General Public License (See COPYING for details).
3 // Copyright (C) 2003 Alistair Riddoch
4 
5 #include "ThresholdShader.h"
6 
7 #include "Segment.h"
8 #include "Surface.h"
9 
10 #include <cassert>
11 
12 namespace Mercator {
13 
14 const std::string HighShader::key_threshold("threshold");
15 
16 const float HighShader::default_threshold = 1.f;
17 
18 HighShader::HighShader(float threshold) : m_threshold(threshold)
19 {
20 }
21 
22 HighShader::HighShader(const Parameters & params) : m_threshold(default_threshold)
23 {
24  auto I = params.find(key_threshold);
25  auto Iend = params.end();
26  if (I != Iend) {
27  m_threshold = I->second;
28  }
29 }
30 
31 HighShader::~HighShader() = default;
32 
33 bool HighShader::checkIntersect(const Segment & s) const
34 {
35  if (s.getMax() > m_threshold) {
36  return true;
37  } else {
38  return false;
39  }
40 }
41 
42 void HighShader::shade(Surface & s) const
43 {
44  unsigned int channels = s.getChannels();
45  assert(channels > 0);
46  unsigned int colors = channels - 1;
47  ColorT * data = s.getData();
48  const float * height_data = s.getSegment().getPoints();
49  if (height_data == 0) {
50  std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
51  << std::endl << std::flush;
52  return;
53  }
54  unsigned int size = s.getSegment().getSize();
55 
56  unsigned int count = size * size;
57  int j = -1;
58  for (unsigned int i = 0; i < count; ++i) {
59  for (unsigned int k = 0; k < colors; ++k) {
60  data[++j] = colorMax;
61  }
62  data[++j] = ((height_data[i] > m_threshold) ? colorMax : colorMin);
63  }
64 }
65 
66 const std::string LowShader::key_threshold("threshold");
67 
68 const float LowShader::default_threshold = -1.f;
69 
70 LowShader::LowShader(float threshold) : m_threshold(threshold)
71 {
72 }
73 
74 LowShader::LowShader(const Parameters & params) : m_threshold(default_threshold)
75 {
76  auto I = params.find(key_threshold);
77  auto Iend = params.end();
78  if (I != Iend) {
79  m_threshold = I->second;
80  }
81 }
82 
83 LowShader::~LowShader() = default;
84 
85 bool LowShader::checkIntersect(const Segment & s) const
86 {
87  if (s.getMin() < m_threshold) {
88  return true;
89  } else {
90  return false;
91  }
92 }
93 
94 void LowShader::shade(Surface & s) const
95 {
96  unsigned int channels = s.getChannels();
97  assert(channels > 0);
98  unsigned int colors = channels - 1;
99  ColorT * data = s.getData();
100  const float * height_data = s.getSegment().getPoints();
101  if (height_data == 0) {
102  std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
103  << std::endl << std::flush;
104  return;
105  }
106  unsigned int size = s.getSegment().getSize();
107 
108  unsigned int count = size * size;
109  int j = -1;
110  for (unsigned int i = 0; i < count; ++i) {
111  for (unsigned int k = 0; k < colors; ++k) {
112  data[++j] = colorMax;
113  }
114  data[++j] = ((height_data[i] < m_threshold) ? colorMax : colorMin);
115  }
116 }
117 
118 const std::string BandShader::key_lowThreshold("lowThreshold");
119 const std::string BandShader::key_highThreshold("highThreshold");
120 
121 const float BandShader::default_lowThreshold = -1.f;
122 const float BandShader::default_highThreshold = 1.f;
123 
124 BandShader::BandShader(float low_threshold, float high_threshold) :
125  m_lowThreshold(low_threshold), m_highThreshold(high_threshold)
126 {
127 }
128 
130  m_lowThreshold(default_lowThreshold), m_highThreshold(default_highThreshold)
131 {
132  auto I = params.find(key_lowThreshold);
133  auto Iend = params.end();
134  if (I != Iend) {
135  m_lowThreshold = I->second;
136  }
137  I = params.find(key_highThreshold);
138  if (I != Iend) {
139  m_highThreshold = I->second;
140  }
141 }
142 
143 BandShader::~BandShader() = default;
144 
145 bool BandShader::checkIntersect(const Segment & s) const
146 {
147  if ((s.getMin() < m_highThreshold) &&
148  (s.getMax() > m_lowThreshold)) {
149  return true;
150  } else {
151  return false;
152  }
153 }
154 
155 void BandShader::shade(Surface & s) const
156 {
157  unsigned int channels = s.getChannels();
158  assert(channels > 0);
159  unsigned int colors = channels - 1;
160  ColorT * data = s.getData();
161  const float * height_data = s.getSegment().getPoints();
162  if (height_data == nullptr) {
163  std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
164  << std::endl << std::flush;
165  return;
166  }
167  unsigned int size = s.getSegment().getSize();
168 
169  unsigned int count = size * size;
170  int j = -1;
171  for (unsigned int i = 0; i < count; ++i) {
172  for (unsigned int k = 0; k < colors; ++k) {
173  data[++j] = colorMax;
174  }
175  data[++j] = (((height_data[i] > m_lowThreshold) &&
176  (height_data[i] < m_highThreshold)) ? colorMax : colorMin);
177  }
178 }
179 
180 } // namespace Mercator
HighShader(float threshold=default_threshold)
Constructor.
static const std::string key_lowThreshold
Key string used when specifying the low threshold parameter.
DataType * getData()
Accessor for a pointer to buffer containing data values.
Definition: Buffer.h:63
LowShader(float threshold=default_threshold)
Constructor.
bool checkIntersect(const Segment &) const override
Check whether this Shader has any effect on the given Segment.
Data store for terrain surface data.
Definition: Surface.h:23
float threshold() const
Accessor for level above which the shader renders.
BandShader(float low_threshold=default_lowThreshold, float high_threshold=default_highThreshold)
Constructor.
void shade(Surface &) const override
Populate a Surface with data.
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: Segment.h:191
std::map< std::string, float > Parameters
STL map of parameter values for a shader constructor.
Definition: Shader.h:59
int getSize() const
Accessor for array size of this segment.
Definition: Segment.h:88
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:37
static const float default_lowThreshold
Default level above which the shader renders.
static const float default_threshold
Default level below which the shader renders.
unsigned int getChannels() const
Accessor for the number of data values per height point.
Definition: Buffer.h:58
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: Segment.h:189
const Segment & getSegment() const
Accessor for the terrain height segment this surface is associated with.
Definition: Surface.h:37
static const float default_threshold
Default level above which the shader renders.
static const std::string key_highThreshold
Key string used when specifying the high threshold parameter.
void shade(Surface &) const override
Populate a Surface with data.
const float * getPoints() const
Accessor for buffer containing height points.
Definition: Segment.h:143
void shade(Surface &) const override
Populate a Surface with data.
static const float default_highThreshold
Default level below which the shader renders.
static const std::string key_threshold
Key string used when specifying the threshold parameter.
bool checkIntersect(const Segment &) const override
Check whether this Shader has any effect on the given Segment.
static const std::string key_threshold
Key string used when specifying the threshold parameter.
bool checkIntersect(const Segment &) const override
Check whether this Shader has any effect on the given Segment.