Skip to content

Commit cfca414

Browse files
committed
Operate Gridmap in c++, setup 3d array for terrain
1 parent e0f01ae commit cfca414

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

Documentation/README_ARCHITECTURE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Godot Terrain Generator for 3D Tiled Maps
44

55
## Usage
66

7-
Building :
7+
Requires a C++ compiler such as MSVC or MSY2
88

9-
1. `scons use_mingw=yes`
9+
Building :
1010

11-
# Licensing
11+
1. `scons use_mingw=yes` or just `scons`

demo/terrain_gen_script.gd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extends TerrainGen
2-
2+
3+
34
func _ready():
4-
var temp : Array = generate(5, 5, 2, 0.005);
5-
print(temp);
5+
var seed_value = int(Time.get_unix_time_from_system()) % 1000000;
6+
generate(get_node("GridMap"), 5, 5, 3, seed_value, 2, 0.005);

src/TerrainGen/Terrain_Gen.cpp

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ TerrainGen::TerrainGen() {
99
TerrainGen::~TerrainGen() {
1010
}
1111

12-
float TerrainGen::get_noise_value(float x, float y, float z) {
13-
return noise->get_noise_3d(x, y, z);
14-
}
15-
16-
Array TerrainGen::generate(int height, int width, int noiseOctaves, float noiseFreq) {
12+
void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth, int seed, int noiseOctaves, float noiseFreq) {
1713
UtilityFunctions::print("Begin Terrain Generation!");
1814

1915
/*
@@ -22,6 +18,7 @@ Array TerrainGen::generate(int height, int width, int noiseOctaves, float noiseF
2218

2319
noise->set_noise_type(FastNoiseLite::NoiseType::TYPE_SIMPLEX);
2420
noise->set_fractal_type(FastNoiseLite::FractalType::FRACTAL_FBM);
21+
noise->set_seed(seed);
2522
noise->set_fractal_octaves(noiseOctaves);
2623
noise->set_fractal_lacunarity(2.0);
2724
noise->set_fractal_gain(0.5);
@@ -31,10 +28,24 @@ Array TerrainGen::generate(int height, int width, int noiseOctaves, float noiseF
3128
Function dependent Variables
3229
*/
3330

34-
vector<vector<int>> gridMap(height, vector<int>(width, 0));
31+
vector<vector<vector<int>>> gridMap(
32+
depth, vector<vector<int>>(height, vector<int>(width, 0)));
33+
34+
vector<vector<float>> heightMap(height, vector<float>(width, 0.0f));
35+
36+
/*
37+
Height Map Generation
38+
*/
39+
40+
for (int y = 0; y < height; y++) {
41+
for (int x = 0; x < width; x++) {
42+
float n = noise->get_noise_2d((float)x, (float)y);
43+
heightMap[y][x] = round(((n + 1.0f) / 2.0f) * (depth - 1));
44+
}
45+
}
3546

3647
/*
37-
The key rotations in Godot's Grid Map around the Y-axis are:
48+
The key rotations (Orientation) in Godot's Grid Map around the Y-axis are:
3849
- 0 → No rotation
3950
- 1 → 90° clockwise around Y
4051
- 2 → 180° around Y
@@ -43,25 +54,30 @@ Array TerrainGen::generate(int height, int width, int noiseOctaves, float noiseF
4354
- 11 → 90° counterclockwise around Y (alternative basis)
4455
- 9 → 90° clockwise around Y (alternative basis)
4556
46-
Which can be set with : `gridmap.set_cell_item(x, y, z, item_index, rotation_index)`
57+
Which can be set with : `myGridMap->set_cell_item(Vector3i(x, y, z), meshID, orientation);`
4758
*/
4859

4960
/*
5061
Return the Grid Map to Godot in a GDScript consumable manner
5162
*/
5263

53-
Array terrainMap;
54-
for (size_t i = 0; i < gridMap.size(); i++) { // Standard i++ format
55-
TypedArray<int> grid_row;
56-
for (size_t j = 0; j < gridMap[i].size(); j++) { // Standard j++ format
57-
grid_row.append(gridMap[i][j]); // Append value from 2D vector
58-
}
59-
terrainMap.append(grid_row);
60-
}
61-
62-
return terrainMap;
64+
// Array terrainMap;
65+
// for (size_t z = 0; z < depth; z++) { // Iterate over depth (Z-axis)
66+
// Array grid_layer;
67+
// for (size_t y = 0; y < height; y++) { // Iterate over height (Y-axis)
68+
// TypedArray<int> grid_row;
69+
// for (size_t x = 0; x < width; x++) { // Iterate over width (X-axis)
70+
// grid_row.append(gridMap[z][y][x]); // Append voxel values
71+
// }
72+
// grid_layer.append(grid_row);
73+
// }
74+
// terrainMap.append(grid_layer);
75+
// }
6376
}
6477

6578
void TerrainGen::_bind_methods() {
66-
ClassDB::bind_method(D_METHOD("generate", "height", "width", "noiseOctaves", "noiseFreq"), &TerrainGen::generate);
79+
ClassDB::bind_method(D_METHOD("generate", "Grid Map"
80+
"height",
81+
"width", "depth", "seed", "noiseOctaves", "noiseFreq"),
82+
&TerrainGen::generate);
6783
}

src/TerrainGen/Terrain_Gen.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define HELLOWORLD_H
33

44
#include <godot_cpp/classes/fast_noise_lite.hpp>
5+
#include <godot_cpp/classes/grid_map.hpp>
56
#include <godot_cpp/classes/node.hpp>
67
#include <godot_cpp/variant/utility_functions.hpp>
78

@@ -16,8 +17,6 @@ class TerrainGen : public Node {
1617
protected:
1718
Ref<FastNoiseLite> noise;
1819

19-
float get_noise_value(float x, float y, float z);
20-
2120
static void _bind_methods();
2221

2322
public:
@@ -26,7 +25,7 @@ class TerrainGen : public Node {
2625

2726
//Generate Terrain
2827
//Takes in a height & width for size of map on the X & Z axis
29-
Array generate(int height, int width, int noiseOctaves = 2, float noiseFreq = 0.005);
28+
void generate(GridMap *myGridMap, int height, int width, int depth, int seed, int noiseOctaves = 2, float noiseFreq = 0.005);
3029
};
3130

3231
#endif

0 commit comments

Comments
 (0)