Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
heightmap_loader.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef HEIGHTMAP_LOADER_H
20 #define HEIGHTMAP_LOADER_H
21 
22 #include <functional>
23 #include "../loader.h"
24 
25 namespace smlt {
26 
27 typedef std::function<smlt::Color (const smlt::Vec3&, const smlt::Vec3&, const std::vector<Vec3>&)> HeightmapDiffuseGenerator;
28 
30  uint32_t index[3];
31 };
32 
33 struct TerrainData {
34  Mesh* terrain = nullptr;
35 
36  uint32_t x_size;
37  uint32_t z_size;
38  float max_height;
39  float min_height;
40  float grid_spacing;
41  float one_over_grid_spacing;
42 
43  /* Get the height at the specified coordinate, returns false if
44  * the coordinate falls outside the height grid */
45  optional<float> height_at_xz(const Vec2& xz) const;
46  optional<float> height_at_xz(float x, float z) const {
47  return height_at_xz(Vec2(x, z));
48  }
49 
50  /* Get the triangle indexes at the specified coordinate, returns false if
51  * the coordinate is outside the height grid */
52  optional<TerrainTriangle> triangle_at_xz(const Vec2& xz) const;
53  optional<TerrainTriangle> triangle_at_xz(float x, float z) const {
54  return triangle_at_xz(Vec2(x, z));
55  }
56 };
57 
58 
59 namespace terrain {
60 
61 typedef std::function<void (float height, const Vec3&, float& weight1, float& weight2, float& weight3, float& weight4)> AlphaMapWeightFunc;
62 
63 void recalculate_terrain_normals(smlt::MeshPtr terrain);
64 void smooth_terrain(smlt::MeshPtr terrain, uint32_t iterations=20);
65 AssetID generate_alphamap(smlt::MeshPtr terrain, AlphaMapWeightFunc func);
66 
67 }
68 
70  float min_height = -64.0f;
71  float max_height = 64.0f;
72  float spacing = 2.5f;
73  uint32_t smooth_iterations = 0;
74  bool calculate_normals = true;
75  float texcoord0_repeat = 4.0f;
76 };
77 
78 namespace loaders {
79 
80 class HeightmapLoader : public Loader {
81 public:
82  HeightmapLoader(const Path& filename, std::shared_ptr<std::istream> data):
83  Loader(filename, data) {}
84 
85  /* Hack! This exists for the ability to generate a heightmap from an already
86  * loaded texture. */
87  HeightmapLoader(const TexturePtr texture):
88  Loader("", std::make_shared<std::istringstream>()),
89  texture_(texture) {}
90 
91  void into(Loadable& resource, const LoaderOptions& options = LoaderOptions());
92 
93 private:
94  TexturePtr texture_ = nullptr;
95  void smooth_terrain_iteration(Mesh *mesh, int width, int height);
96 };
97 
99 public:
101  // Add the mesh hint
102  add_hint(LOADER_HINT_MESH);
103  }
104 
105  virtual ~HeightmapLoaderType() {}
106 
107  const char* name() override { return "heightmap_loader"; }
108 
109  bool supports(const Path& filename) const override {
110  return filename.ext() == ".tga" || filename.ext() == ".png";
111  }
112 
113  Loader::ptr loader_for(const Path& filename, std::shared_ptr<std::istream> data) const override {
114  return Loader::ptr(new HeightmapLoader(filename, data));
115  }
116 };
117 
118 }
119 }
120 
121 
122 #endif // HEIGHTMAP_LOADER_H
123 
smlt::Vec3
Definition: vec3.h:25
smlt::loaders::HeightmapLoaderType
Definition: heightmap_loader.h:98
smlt::HeightmapSpecification
Definition: heightmap_loader.h:69
smlt
Definition: animation.cpp:25
smlt::loaders::HeightmapLoader
Definition: heightmap_loader.h:80
smlt::LoaderType
Definition: loader.h:111
smlt::Loader
Definition: loader.h:66
smlt::Color
Definition: color.h:32
smlt::TerrainData
Definition: heightmap_loader.h:33
smlt::Mesh
Definition: mesh.h:127
smlt::optional< float >
smlt::Path
Definition: path.h:7
smlt::Loadable
Definition: loadable.h:26
smlt::TerrainTriangle
Definition: heightmap_loader.h:29
smlt::Vec2
Definition: vec2.h:16