Browse Source

VBO buffer filling with terrain data

Phyks (Lucas Verney) 4 years ago
parent
commit
ee508c399e
3 changed files with 71 additions and 8 deletions
  1. 31
    6
      src/core/Terrain.cc
  2. 23
    1
      src/framework/Heightmap.cc
  3. 17
    1
      src/framework/Heightmap.h

+ 31
- 6
src/core/Terrain.cc View File

@@ -58,15 +58,40 @@ Terrain::Terrain(
58 58
     // Set the model_matrix_ to identity as a safe default value
59 59
     mat4x4_identity(model_matrix_);
60 60
 
61
-    // TODO[Lucas] buffer initialization + compute normal
62
-
63 61
     // Initialize the buffer
64
-    buffer_length_ = heightmap_.get_metadata().n_rows * heightmap_.get_metadata().n_cols;
62
+    const int n_vertices  = heightmap_.get_metadata().n_rows * heightmap_.get_metadata().n_cols;
63
+    buffer_length_ = 2 * n_vertices * 3 * sizeof(float);  // Two arrays of n_vertices, with 3 float components
64
+    float* buffer_data = new float[buffer_length_];
65
+
66
+    // Get height data
67
+    float* heights_data = new float[3 * n_vertices];  // 3 components for each vertice
68
+    heightmap_.get_as_buffer(buffer_data);
69
+
70
+    // TODO[Lucas] Compute normals
71
+    float* normals_data = new float[3 * n_vertices];  // 3 components for each vertice
72
+
73
+    // Merge the two arrays
74
+    for (int i = 0; i < 3 * n_vertices; i += 3) {
75
+        buffer_data[2 * i] = heights_data[i];
76
+        buffer_data[(2 * i) + 1] = heights_data[i + 1];
77
+        buffer_data[(2 * i) + 2] = heights_data[i + 2];
78
+        buffer_data[(2 * i) + 3] = normals_data[i];
79
+        buffer_data[(2 * i) + 4] = normals_data[i + 1];
80
+        buffer_data[(2 * i) + 5] = normals_data[i + 2];
81
+    }
82
+
65 83
     // The buffer pointer by buffer_id_ contains X, Y, Z components for a
66 84
     // vertex, followed by X, Y, Z component for the normal associated to this
67
-    // vertex, tightly packed in a 1D array.
68
-    GLvoid* table_data = NULL;
69
-    buffer_id_ = create_vbo(buffer_length_, table_data, GL_STATIC_DRAW);
85
+    // vertex, tightly packed in a 1D array. Vertices are ordered from NW
86
+    // corner to SE corner.
87
+    buffer_id_ = create_vbo(
88
+            buffer_length_,
89
+            static_cast<GLvoid*>(buffer_data),
90
+            GL_STATIC_DRAW);
91
+
92
+    delete[] normals_data;
93
+    delete[] heights_data;
94
+    delete[] buffer_data;
70 95
 }
71 96
 
72 97
 

+ 23
- 1
src/framework/Heightmap.cc View File

@@ -19,6 +19,27 @@ heightmap::metadata_t Heightmap::get_metadata(void) const {
19 19
 }
20 20
 
21 21
 
22
+void Heightmap::get_as_buffer(float* buffer) const {
23
+    // Store the current position in variables
24
+    int x = metadata_.x_min;
25
+    int y = metadata_.y_max;
26
+
27
+    // Loop from NW corner to SE corner
28
+    for (int j = 0; j < metadata_.n_cols; ++j) {
29
+        for (int i = 0; i < metadata_.n_rows; ++i) {
30
+            buffer[(j * (3 * metadata_.n_rows)) + (3 * i)] = x;
31
+            buffer[(j * (3 * metadata_.n_rows)) + (3 * i) + 1] = y;
32
+            buffer[(j * (3 * metadata_.n_rows)) + (3 * i) + 2] = data_[i][j];
33
+
34
+            // Update y value
35
+            y -= metadata_.cellheight;
36
+        }
37
+        // Update x value and reset y value
38
+        x -= metadata_.cellwidth;
39
+        y = metadata_.y_max;
40
+    }
41
+}
42
+
22 43
 
23 44
 void Heightmap::set_metadata(const heightmap::metadata_t metadata) {
24 45
     metadata_ = metadata;
@@ -36,7 +57,8 @@ void Heightmap::load_from_file(const char* filename) {
36 57
 
37 58
     if (file.is_open()) {
38 59
         // Loop over the file by increasing cols and increasing rows
39
-        // (from NW corner to SE corner)
60
+        // (from NW corner to SE corner). NW corner corresponds to (0, 0)
61
+        // point.
40 62
         for (int j = 0; j < metadata_.n_cols; ++j) {
41 63
             for (int i = 0; i < metadata_.n_rows; ++i) {
42 64
                 int16_t value;

+ 17
- 1
src/framework/Heightmap.h View File

@@ -1,3 +1,7 @@
1
+/**
2
+ * Heightmaps are stored in ARG format, with metadata in the global JSON config
3
+ * file. See http://geotrellis.io/documentation/0.9.0/geotrellis/io/arg/.
4
+ */
1 5
 #ifndef SKIMAP_CORE_HEIGHTMAP_H_
2 6
 #define SKIMAP_CORE_HEIGHTMAP_H_
3 7
 
@@ -24,7 +28,7 @@ public:
24 28
     /**
25 29
      * Heightmap constructor.
26 30
      *
27
-     * Loads the content of the specified file in the Heightmap.
31
+     * Loads the content of the specified ARG file in the Heightmap.
28 32
      *
29 33
      * @param metadata  a metadata structure associated with raw data file
30 34
      * @param filename  the filename of the file containing the heightmap data
@@ -32,6 +36,18 @@ public:
32 36
     Heightmap(const heightmap::metadata_t metadata, const char* filename);
33 37
 
34 38
 
39
+    /**
40
+     * Get the heights data as a 1D array containing (X, Y, Z) components
41
+     * consecutively for each vertice, ready to be stored in a VBO buffer.
42
+     * Vertices are ordered from NW corner to SE corner.
43
+     *
44
+     * @param[out] buffer  is the 1D array initialized in the caller, able to
45
+     *                     store all the components:
46
+     *                     (3 * n_rows * n_cols) values.
47
+     */
48
+    void get_as_buffer(float* buffer) const;
49
+
50
+
35 51
     /**
36 52
      * Get the metadata data member.
37 53
      *