Browse Source

Heightmap handling

Phyks (Lucas Verney) 4 years ago
parent
commit
2668a5d58a
6 changed files with 140 additions and 15 deletions
  1. 0
    9
      README.md
  2. 12
    1
      res/config.json
  3. 14
    4
      src/core/Terrain.cc
  4. 2
    1
      src/core/Terrain.h
  5. 49
    0
      src/framework/Heightmap.cc
  6. 63
    0
      src/framework/Heightmap.h

+ 0
- 9
README.md View File

@@ -22,12 +22,3 @@ The build system is based on a set of CMake files. This builds all the available
22 22
     * `src/core` contains the core code, for the app itself.
23 23
     * `src/framework` contains useful classes and a basic framework for the app.
24 24
     * `src/platform` contains platform-specific code, such as wrappers.
25
-
26
-
27
-## TODO
28
-
29
-* Heightmap handling
30
-* Buffer initialization
31
-* Compute normals
32
-* Config
33
-* Assets loading

+ 12
- 1
res/config.json View File

@@ -24,5 +24,16 @@
24 24
         [0.0, 0.0, 0.0, 0.0],
25 25
         [0.0, 0.0, 0.0, 0.0],
26 26
         [0.0, 0.0, 0.0, 0.0]
27
-    ]
27
+    ],
28
+
29
+    "HEIGHTMAP_METADATA": {
30
+        "X_MIN": 0.0,
31
+        "Y_MIN": 0.0,
32
+        "X_MAX": 0.0,
33
+        "Y_MAX": 0.0,
34
+        "CELLWIDTH": 0.0,
35
+        "CELLHEIGHT": 0.0,
36
+        "N_ROWS": 0,
37
+        "N_COLS": 0
38
+    }
28 39
 }

+ 14
- 4
src/core/Terrain.cc View File

@@ -40,18 +40,28 @@ Terrain::Terrain(
40 40
             static_cast<float>(Config::getValue("TERRAIN_MATERIAL")[3].GetDouble()),
41 41
         })
42 42
     , model_matrix_()
43
-    , terrainData_()
43
+    , heightmap_(
44
+            {
45
+                static_cast<float>(Config::getValue("HEIGHTMAP_METADATA")["X_MIN"].GetDouble()),
46
+                static_cast<float>(Config::getValue("HEIGHTMAP_METADATA")["Y_MIN"].GetDouble()),
47
+                static_cast<float>(Config::getValue("HEIGHTMAP_METADATA")["X_MAX"].GetDouble()),
48
+                static_cast<float>(Config::getValue("HEIGHTMAP_METADATA")["Y_MAX"].GetDouble()),
49
+                static_cast<float>(Config::getValue("HEIGHTMAP_METADATA")["CELLWIDTH"].GetDouble()),
50
+                static_cast<float>(Config::getValue("HEIGHTMAP_METADATA")["CELLHEIGHT"].GetDouble()),
51
+                Config::getValue("HEIGHTMAP_METADATA")["N_ROWS"].GetInt(),
52
+                Config::getValue("HEIGHTMAP_METADATA")["N_COLS"].GetInt()
53
+            },
54
+            "assets/heightmap.arg")
44 55
     , buffer_id_(0)
45 56
     , buffer_length_(0)
46 57
 {
47 58
     // Set the model_matrix_ to identity as a safe default value
48 59
     mat4x4_identity(model_matrix_);
49 60
 
50
-    // TODO[Lucas] Handle terrain data + buffer initialization + compute normal
61
+    // TODO[Lucas] buffer initialization + compute normal
51 62
 
52 63
     // Initialize the buffer
53
-    // TODO[Lucas] buffer_length_ = sizeof(table_data);
54
-    buffer_length_ = 0;
64
+    buffer_length_ = heightmap_.get_metadata().n_rows * heightmap_.get_metadata().n_cols;
55 65
     // The buffer pointer by buffer_id_ contains X, Y, Z components for a
56 66
     // vertex, followed by X, Y, Z component for the normal associated to this
57 67
     // vertex, tightly packed in a 1D array.

+ 2
- 1
src/core/Terrain.h View File

@@ -9,6 +9,7 @@
9 9
 
10 10
 // Local haders
11 11
 #include "platform_gl.h"
12
+#include "Heightmap.h"
12 13
 #include "Program.h"
13 14
 
14 15
 namespace terrain_shader {
@@ -81,7 +82,7 @@ class Terrain {
81 82
         TerrainProgram program_;  /**< Shaders program associated with terrain. */
82 83
         terrain_shader::Material_t material_;  /**< Terrain material. */
83 84
         mat4x4 model_matrix_;  /**< Model matrix to position the terrain. */
84
-        std::vector<float> terrainData_;  /** The actual terrain data. */
85
+        Heightmap heightmap_;  /** The actual terrain data. */
85 86
         GLuint buffer_id_;  /**< GL id of the buffer to store the terrain data. */
86 87
         int buffer_length_;  /**< The length of the buffer to store the terrain data. */
87 88
 };

+ 49
- 0
src/framework/Heightmap.cc View File

@@ -0,0 +1,49 @@
1
+// Associated header
2
+#include "Heightmap.h"
3
+
4
+// System includes
5
+#include <cstdint>
6
+#include <fstream>
7
+
8
+
9
+Heightmap::Heightmap(const heightmap::metadata_t metadata, const char* filename)
10
+: data_()
11
+, metadata_() {
12
+    set_metadata(metadata);
13
+    load_from_file(filename);
14
+}
15
+
16
+
17
+heightmap::metadata_t Heightmap::get_metadata(void) const {
18
+    return metadata_;
19
+}
20
+
21
+
22
+
23
+void Heightmap::set_metadata(const heightmap::metadata_t metadata) {
24
+    metadata_ = metadata;
25
+}
26
+
27
+
28
+void Heightmap::load_from_file(const char* filename) {
29
+    std::ifstream file;
30
+    file.open(filename, std::ios::in | std::ios::binary);
31
+
32
+    data_.resize(metadata_.n_rows);
33
+    for (auto &i : data_) {
34
+        i.resize(metadata_.n_cols);
35
+    }
36
+
37
+    if (file.is_open()) {
38
+        // Loop over the file by increasing cols and increasing rows
39
+        // (from NW corner to SE corner)
40
+        for (int j = 0; j < metadata_.n_cols; ++j) {
41
+            for (int i = 0; i < metadata_.n_rows; ++i) {
42
+                int16_t value;
43
+                file.read(reinterpret_cast<char *>(&value), sizeof(value));
44
+                data_[i][j] =value;
45
+            }
46
+        }
47
+        file.close();
48
+    }
49
+}

+ 63
- 0
src/framework/Heightmap.h View File

@@ -0,0 +1,63 @@
1
+#ifndef SKIMAP_CORE_HEIGHTMAP_H_
2
+#define SKIMAP_CORE_HEIGHTMAP_H_
3
+
4
+// System includes
5
+#include <string>
6
+#include <vector>
7
+
8
+namespace heightmap {
9
+    typedef struct {
10
+        float x_min;
11
+        float y_min;
12
+        float x_max;
13
+        float y_max;
14
+        float cellwidth;
15
+        float cellheight;
16
+        int n_rows;
17
+        int n_cols;
18
+    } metadata_t;
19
+}  // namespace heightmap
20
+
21
+
22
+class Heightmap {
23
+public:
24
+    /**
25
+     * Heightmap constructor.
26
+     *
27
+     * Loads the content of the specified file in the Heightmap.
28
+     *
29
+     * @param metadata  a metadata structure associated with raw data file
30
+     * @param filename  the filename of the file containing the heightmap data
31
+     */
32
+    Heightmap(const heightmap::metadata_t metadata, const char* filename);
33
+
34
+
35
+    /**
36
+     * Get the metadata data member.
37
+     *
38
+     * @return metadata structure.
39
+     */
40
+    heightmap::metadata_t get_metadata(void) const;
41
+
42
+
43
+    /**
44
+     * Set the metadata data member.
45
+     *
46
+     * @param metadata  metadata structure.
47
+     */
48
+    void set_metadata(const heightmap::metadata_t metadata);
49
+
50
+
51
+    /**
52
+     * Loads the content of the specified file in the Heightmap.
53
+     * Heightmap::set_metadata must be called before calling this function.
54
+     *
55
+     * @param filename  the filename of the file containing the heightmap data
56
+     */
57
+    void load_from_file(const char* filename);
58
+private:
59
+    std::vector< std::vector<float> > data_;  /**< The actual height data ((row, col) matrix). */
60
+    heightmap::metadata_t metadata_;  /**< Associated metadata, see heightmap::metadata_t. */
61
+};
62
+
63
+#endif  // SKIMAP_CORE_HEIGHTMAP_H_