Skip to content

Commit 0d50f80

Browse files
authored
Merge pull request #79 from hotstreams/feature/terrain-system
Feature/terrain system
2 parents d9487ad + fd95db5 commit 0d50f80

63 files changed

Lines changed: 2231 additions & 647 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ set(ENGINE_UTIL
124124
src/limitless/util/thread_pool.cpp
125125
src/limitless/util/sorter.cpp
126126
src/limitless/util/renderer_helper.cpp
127-
src/limitless/renderer/color_picker.cpp
127+
src/limitless/renderer/color_picker.cpp
128128
src/limitless/util/frustum.cpp
129+
130+
src/limitless/util/geoclipmap.cpp
131+
129132
)
130133

131134
set(ENGINE_MS

assets/textures/dirt.png

21.5 MB
Loading

assets/textures/grass.png

9.3 MB
Loading

assets/textures/rock.png

21.8 MB
Loading

include/limitless/camera.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ namespace Limitless {
2424
float yaw;
2525

2626
float fov {90}; // degrees
27-
float near_distance {0.01f};
28-
float far_distance {100.0f};
27+
float near_distance {0.001f};
28+
float far_distance {1000.0f};
2929

3030
float move_speed {2.0f};
3131
float mouse_sence {0.5f};

include/limitless/core/texture/texture.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace Limitless {
2828
Depth32F = GL_DEPTH_COMPONENT32F,
2929
Depth24Stencil8 = GL_DEPTH24_STENCIL8,
3030
R = GL_RED,
31+
R32UI = GL_R32UI,
3132
R8 = GL_R8,
3233
RG8 = GL_RG8,
3334
RGB8 = GL_RGB8,
@@ -38,6 +39,7 @@ namespace Limitless {
3839
RGBA16F = GL_RGBA16F,
3940
RGB32F = GL_RGB32F,
4041

42+
R16 = GL_R16,
4143
RG8_SNORM = GL_RG8_SNORM,
4244

4345
RGB8_SNORM = GL_RGB8_SNORM,

include/limitless/instances/instance.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Limitless {
3232
* Unique instance identifier
3333
*/
3434
uint64_t id;
35-
protected:
35+
public:
3636
/**
3737
* Instance type
3838
*/
@@ -186,7 +186,8 @@ namespace Limitless {
186186
[[nodiscard]] const auto& getScale() const noexcept { return scale; }
187187

188188
[[nodiscard]] const auto& getTransformationMatrix() const noexcept { return transformation_matrix; }
189-
[[nodiscard]] const auto& getBoundingBox() noexcept { updateBoundingBox(); return bounding_box; }
189+
// [[nodiscard]] const auto& getBoundingBox() noexcept { updateBoundingBox(); return bounding_box; }
190+
[[nodiscard]] const auto& getBoundingBox() noexcept { return bounding_box; }
190191
[[nodiscard]] const auto& getFinalMatrix() const noexcept { return final_matrix; }
191192
[[nodiscard]] const auto& getModelMatrix() const noexcept { return model_matrix; }
192193

include/limitless/instances/instance_builder.hpp

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <limitless/instances/skeletal_instance.hpp>
55
#include <limitless/instances/effect_instance.hpp>
66
#include <limitless/instances/decal_instance.hpp>
7+
#include <limitless/instances/terrain_instance.hpp>
78
#include <limitless/models/model.hpp>
89

910
namespace Limitless {
@@ -60,10 +61,32 @@ namespace Limitless {
6061
std::shared_ptr<EffectInstance> effect_;
6162

6263
/**
63-
* Decal
64+
* Decal data
6465
*/
6566
uint8_t decal_proj_mask {0xFF};
6667

68+
/**
69+
* Terrain data
70+
*/
71+
float chunk_size_ {1024.0f};
72+
float vertex_spacing_ {0.5f};
73+
float height_scale_ {10.0f};
74+
float noise1_scale_ {0.225f};
75+
float noise2_scale_ {0.04f};
76+
float noise2_angle_ {0.0f};
77+
float noise2_offset_ {0.5f};
78+
float noise3_scale_ {0.076f};
79+
glm::vec3 macro_variation1_ = glm::vec3(0.5f);
80+
glm::vec3 macro_variation2_ = glm::vec3(0.33f);
81+
uint32_t mesh_size_ {64};
82+
uint32_t mesh_lod_count_ {6};
83+
std::shared_ptr<Texture> height_map_;
84+
std::shared_ptr<Texture> control_map_;
85+
std::shared_ptr<Texture> albedo_map_;
86+
std::shared_ptr<Texture> normal_map_;
87+
std::shared_ptr<Texture> orm_map_;
88+
std::shared_ptr<Texture> noise_;
89+
6790
void initialize(Instance& instance);
6891
void initialize(const std::shared_ptr<ModelInstance>& instance);
6992
public:
@@ -125,6 +148,39 @@ namespace Limitless {
125148
*/
126149
Builder& decal_projection_mask(uint8_t mask);
127150

151+
Builder& chunk_size(float chunk_size);
152+
153+
Builder& vertex_spacing(float vertex_spacing);
154+
155+
Builder& height_scale(float height_scale);
156+
157+
Builder& noise1_scale(float noise1_scale);
158+
159+
Builder& noise2_scale(float noise2_scale);
160+
161+
Builder& noise2_angle(float noise2_angle);
162+
163+
Builder& noise2_offset(float noise2_offset);
164+
165+
Builder& noise3_scale(float noise3_scale);
166+
167+
Builder& macro_variation1(const glm::vec3& macro_variation1);
168+
169+
Builder& macro_variation2(const glm::vec3& macro_variation2);
170+
171+
Builder& mesh_size(float mesh_size);
172+
Builder& mesh_lod_count(float mesh_lod_count);
173+
174+
Builder& height_map(const std::shared_ptr<Texture>& height_map);
175+
Builder& control_map(const std::shared_ptr<Texture>& control_map);
176+
Builder& albedo_map(const std::shared_ptr<Texture>& albedo_map);
177+
Builder& normal_map(const std::shared_ptr<Texture>& normal_map);
178+
Builder& orm_map(const std::shared_ptr<Texture>& orm_map);
179+
Builder& noise(const std::shared_ptr<Texture>& noise);
180+
181+
Builder& height(const float* data);
182+
Builder& control(const TerrainInstance::control_value* data);
183+
128184
/**
129185
* Replaces default mesh material with specified one
130186
*/
@@ -178,6 +234,6 @@ namespace Limitless {
178234
/**
179235
*
180236
*/
181-
std::shared_ptr<DecalInstance> asTerrain();
237+
std::shared_ptr<TerrainInstance> asTerrain(Assets& assets);
182238
};
183239
}

include/limitless/instances/model_instance.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ namespace Limitless {
2222
std::shared_ptr<AbstractModel> model;
2323

2424
void updateBoundingBox() noexcept override;
25-
ModelInstance(InstanceType shader, decltype(model) model, const glm::vec3& position);
25+
//ModelInstance(InstanceType shader, decltype(model) model, const glm::vec3& position);
2626
public:
27+
ModelInstance(InstanceType shader, decltype(model) model, const glm::vec3& position);
28+
2729
/**
2830
* Creates static model with specified loaded Model
2931
*/

include/limitless/instances/terrain_instance.hpp

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,167 @@
22

33
#include <limitless/instances/model_instance.hpp>
44
#include <utility>
5+
#include <limitless/models/model.hpp>
6+
7+
#include <limitless/assets.hpp>
8+
#include <limitless/ms/material.hpp>
9+
#include <limitless/core/texture/texture_builder.hpp>
10+
11+
#include <limitless/util/brush.hpp>
12+
#include "instanced_instance.hpp"
513

614
namespace Limitless {
7-
class TerrainInstance : public ModelInstance {
15+
class Assets;
16+
17+
class TerrainInstance : public Instance {
18+
public:
19+
struct control_value final {
20+
uint32_t base_id : 6;
21+
uint32_t extra_id : 6;
22+
uint32_t blend : 8;
23+
uint32_t reserved : 12;
24+
25+
static uint32_t encode(struct control_value ctrl) {
26+
uint32_t result = 0;
27+
result |= (ctrl.base_id & 0x3F);
28+
result |= (ctrl.extra_id & 0x3F) << 6;
29+
result |= (ctrl.blend & 0xFF) << 12;
30+
result |= (ctrl.reserved & 0xFFF) << 20;
31+
return result;
32+
}
33+
34+
static control_value decode(uint32_t value) {
35+
control_value ctrl {};
36+
ctrl.base_id = value & 0x3F;
37+
ctrl.extra_id = (value >> 6) & 0x3F;
38+
ctrl.blend = (value >> 12) & 0xFF;
39+
ctrl.reserved = (value >> 20) & 0xFFF;
40+
return ctrl;
41+
}
42+
};
843
public:
9-
TerrainInstance(decltype(model) model, const glm::vec3& position)
10-
: ModelInstance {InstanceType::Terrain, std::move(model), position} {
44+
// terrain size in world units
45+
float terrain_size = 128.0f;
46+
47+
// spacing between vertices
48+
float vertex_spacing = 1.0f;
49+
50+
// height scale factor
51+
float height_scale = 1.0f;
52+
53+
// texture chunk scale
54+
// to how many tiles is the texture applied
55+
float texture_scale = 4.0f;
56+
57+
float vertex_normals_distance = 20.0f;
58+
59+
// adds macro variation pattern
60+
bool macro_variation = false;
61+
62+
float noise1_scale = 0.225f;
63+
float noise2_scale = 0.04f;
64+
float noise2_angle = 0.0f;
65+
float noise2_offset = 0.5f;
66+
float noise3_scale = 0.076f;
67+
68+
glm::vec3 macro_variation1 = glm::vec3(0.5f);
69+
glm::vec3 macro_variation2 = glm::vec3(0.33f);
70+
71+
bool show_tiles = false;
72+
bool show_terrain_size = false;
73+
bool show_vertex_normals_distance = false;
74+
bool show_texture_chunks = false;
75+
76+
int mesh_size = 32;
77+
int mesh_lod_count = 6;
78+
79+
struct Mesh {
80+
std::shared_ptr<ModelInstance> cross;
81+
82+
std::shared_ptr<InstancedInstance> tiles;
83+
std::shared_ptr<InstancedInstance> fillers;
84+
std::shared_ptr<InstancedInstance> trims;
85+
std::vector<std::shared_ptr<ModelInstance>> trims_test;
86+
std::shared_ptr<InstancedInstance> seams;
87+
} mesh;
88+
89+
std::shared_ptr<Texture> height_map;
90+
std::shared_ptr<Texture> control;
91+
std::shared_ptr<Texture> albedo;
92+
std::shared_ptr<Texture> normal;
93+
std::shared_ptr<Texture> orm;
94+
std::shared_ptr<Texture> noise;
95+
96+
void snap(const Camera& p_cam_pos);
97+
public:
98+
TerrainInstance(
99+
std::shared_ptr<Texture> height_map,
100+
std::shared_ptr<Texture> control_map,
101+
std::shared_ptr<Texture> albedo_map,
102+
std::shared_ptr<Texture> normal_map,
103+
std::shared_ptr<Texture> orm_map,
104+
std::shared_ptr<Texture> noise
105+
);
106+
107+
void initializeMesh(Assets& assets);
108+
109+
void setHeightMap(const std::shared_ptr<Texture>& height_map);
110+
void setControlMap(const std::shared_ptr<Texture>& control_map);
111+
void setNoise(const std::shared_ptr<Texture>& noise);
112+
void setAlbedoMap(const std::shared_ptr<Texture>& albedo_map);
113+
void setNormalMap(const std::shared_ptr<Texture>& normal_map);
114+
void setOrmMap(const std::shared_ptr<Texture>& orm_map);
115+
116+
void setChunkSize(float chunkSize);
117+
void setVertexSpacing(float vertexSpacing);
118+
void setVertexNormalsDistance(float distance);
119+
120+
void setHeightScale(float heightScale);
121+
void setNoise1Scale(float noise1Scale);
122+
void setNoise2Scale(float noise2Scale);
123+
void setNoise2Angle(float noise2Angle);
124+
void setNoise2Offset(float noise2Offset);
125+
void setNoise3Scale(float noise3Scale);
126+
void setMacroVariation1(const glm::vec3 &macroVariation1);
127+
void setMacroVariation2(const glm::vec3 &macroVariation2);
128+
129+
void setMeshSize(int meshSize);
130+
void setMeshLodCount(int meshLodCount);
131+
132+
/**
133+
* Updates full height map from data
134+
*
135+
* expects pointer to UNSIGNED CHAR / std::byte
136+
*
137+
* you should convert your normalized floats in range [0, 1] to unsigned bytes [0, 255]
138+
*
139+
* to get height more than 1.0 set height_scale
140+
*/
141+
void updateHeight(const void* data);
142+
143+
/**
144+
* Updates part of height map with offset and size
145+
*/
146+
void updateHeight(glm::uvec2 offset, glm::uvec2 size, const void* data);
147+
148+
/**
149+
* Updates full control map from data
150+
*
151+
* expects pointer to TerrainInstance::control_value / uint32_t
152+
*/
153+
void updateControl(const void* data);
154+
155+
/**
156+
* Updates part of control map with offset and size
157+
*/
158+
void updateControl(glm::uvec2 offset, glm::uvec2 size, const void* data);
159+
160+
[[nodiscard]] const auto& getMesh() const noexcept { return mesh; }
161+
162+
void update(const Limitless::Camera &camera) override;
11163

164+
std::unique_ptr<Instance> clone() noexcept override {
165+
return std::make_unique<TerrainInstance>(*this);
12166
}
13167
};
14168
}

0 commit comments

Comments
 (0)