mercator  0.4.0
A terrain generation library for the Worldforge system.
DepthShader.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 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8 
9 #include "iround.h"
10 
11 #include "DepthShader.h"
12 
13 #include "Segment.h"
14 #include "Surface.h"
15 
16 #include <cmath>
17 
18 #include <cassert>
19 
20 namespace Mercator {
21 
22 const std::string DepthShader::key_waterLevel("waterLevel");
23 const std::string DepthShader::key_murkyDepth("murkyDepth");
24 
25 const float DepthShader::default_waterLevel = 0.f;
26 const float DepthShader::default_murkyDepth = -64.f;
27 
28 
29 DepthShader::DepthShader(float waterLevel, float murkyDepth) :
30  m_waterLevel(waterLevel), m_murkyDepth(murkyDepth)
31 {
32 }
33 
35  m_waterLevel(default_waterLevel), m_murkyDepth(default_murkyDepth)
36 {
37  auto I = params.find(key_waterLevel);
38  auto Iend = params.end();
39  if (I != Iend) {
40  m_waterLevel = I->second;
41  }
42  I = params.find(key_murkyDepth);
43  if (I != Iend) {
44  m_murkyDepth = I->second;
45  }
46 }
47 
48 DepthShader::~DepthShader() = default;
49 
50 bool DepthShader::checkIntersect(const Segment & s) const
51 {
52  if (s.getMin() < m_waterLevel) {
53  return true;
54  } else {
55  return false;
56  }
57 }
58 
59 void DepthShader::shade(Surface & s) const
60 {
61  unsigned int channels = s.getChannels();
62  assert(channels > 0);
63  unsigned int colors = channels - 1;
64  ColorT * data = s.getData();
65  const float * height_data = s.getSegment().getPoints();
66  if (height_data == 0) {
67  std::cerr << "WARNING: Mercator: Attempting to shade empty segment."
68  << std::endl << std::flush;
69  return;
70  }
71  unsigned int size = s.getSegment().getSize();
72 
73  unsigned int count = size * size;
74  int j = -1;
75  for (unsigned int i = 0; i < count; ++i) {
76  for (unsigned int k = 0; k < colors; ++k) {
77  data[++j] = colorMax;
78  }
79  float depth = height_data[i];
80  if (depth > m_waterLevel) {
81  data[++j] = colorMin;
82  } else if (depth < m_murkyDepth) {
83  data[++j] = colorMax;
84  } else {
85  data[++j] = colorMax - I_ROUND(colorMax * ((depth - m_murkyDepth)
86  / (m_waterLevel - m_murkyDepth)));
87  }
88  }
89 }
90 
91 } // namespace Mercator
static const float default_murkyDepth
Default depth at which the bottom becomes completely obscured.
Definition: DepthShader.h:34
DataType * getData()
Accessor for a pointer to buffer containing data values.
Definition: Buffer.h:63
Data store for terrain surface data.
Definition: Surface.h:23
static const std::string key_murkyDepth
Key string used when specifying the murky depth parameter.
Definition: DepthShader.h:29
int getSize() const
Accessor for array size of this segment.
Definition: Segment.h:88
std::map< std::string, float > Parameters
STL map of parameter values for a shader constructor.
Definition: Shader.h:59
DepthShader(float waterLevel=default_waterLevel, float murkyDepth=default_murkyDepth)
Constructor.
Definition: DepthShader.cpp:29
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:37
void shade(Surface &) const override
Populate a Surface with data.
Definition: DepthShader.cpp:59
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: Segment.h:191
static const float default_waterLevel
Default level of the surface of the water.
Definition: DepthShader.h:32
const float * getPoints() const
Accessor for buffer containing height points.
Definition: Segment.h:143
static const std::string key_waterLevel
Key string used when specifying the water level parameter.
Definition: DepthShader.h:27
unsigned int getChannels() const
Accessor for the number of data values per height point.
Definition: Buffer.h:58
bool checkIntersect(const Segment &) const override
Check whether this Shader has any effect on the given Segment.
Definition: DepthShader.cpp:50
const Segment & getSegment() const
Accessor for the terrain height segment this surface is associated with.
Definition: Surface.h:37