1 #ifndef PROCEDURALHEIGHTTEXTURE_HXX
2 #define PROCEDURALHEIGHTTEXTURE_HXX
3
4 #include "Procedural.hxx"
5 #include "Noise.hxx"
6
7 // This class creates height dependent colors, such as green in the valleys and
8 // grey on the peaks. Guess for what ... yes ... mountains :-)
9 class ProceduralHeightTexture : public Procedural
10 {
11 private:
12 Noise *noise;
13
14 Vec3f Origin, // Location of base plane
15 Normal; // Normal of color fade
16 float Length, // Length of color fade
17 Chaos; // Degree of chaos
18 Vec3f HighColor, // High color in shading
19 DeepColor; // Deep color in shading
20
21 // Inspect the height of the terrain
22 float ToDistance(Vec3f point)
23 {
24 return Dot(point - Origin, Normal) / Length;
25 }
26
27 // Maps grey values to colors
28 Vec3f ToColor(float value)
29 {
30 return value * (HighColor - DeepColor) + DeepColor;
31 }
32
33 public:
34 ProceduralHeightTexture(bool useTexture, bool useBumpMap,
35 Vec3f Origin, Vec3f Normal, float Length, float Chaos,
36 Vec3f HighColor = Vec3f(0.42,0.40,0.36), Vec3f DeepColor = Vec3f(0.63,0.56,0.35))
37 : Procedural(useTexture, useBumpMap, true, false),
38 Origin(Origin), Normal(Normal), Length(Length), Chaos(Chaos),
39 HighColor(HighColor), DeepColor(DeepColor)
40 {
41 Normalize(this->Normal);
42
43 noise = new Noise(Vec3f(64));
44 };
45
46 virtual ~ProceduralHeightTexture()
47 {
48 delete noise;
49 };
50
51 // Returns the respective colors
52 virtual Vec3f GetColor (Vec3f coordinate)
53 {
54 // These additional parameters are not needed for shading, but below for bumpmapping
55
56 float height = Chaos * (noise->Turbulence(coordinate, 0.01f) - 1.0) + ToDistance(coordinate);
57
58 if (height < 0.0f)
59 height = 0.0f;
60
61 if (height > 1.0f)
62 height = 1.0f;
63
64 return ToColor(height);
65 };
66
67 // Hills are bumpy enough, aren't they?
68 virtual void BumpMap(Vec3f &normal, Vec3f coordinate)
69 {};
70 };
71
72 #endif
syntax highlighted by Code2HTML, v. 0.9.1