Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Terrain.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_TERRAIN_H_
34#define DVD_TERRAIN_H_
35
36#include "TileRing.h"
37#include "TerrainDescriptor.h"
43
44namespace Divide {
45
46template <typename T>
47T TER_COORD(T x, T y, T width) noexcept {
48 return x + width * y;
49}
50
56 COUNT
57};
58
59class Vegetation;
60class VertexBuffer;
61class TerrainChunk;
62class ShaderProgram;
63
65FWD_DECLARE_MANAGED_CLASS(GenericVertexData);
66
67namespace Attorney
68{
69 class TerrainChunk;
70}
71
73{
74 PROPERTY_RW(vec2<F32>, WorldScale);
75 PROPERTY_RW(vec2<F32>, SnapGridSize);
76 PROPERTY_RW(U32, tessellatedTriangleWidth, 32u);
77
78 static constexpr U8 VTX_PER_TILE_EDGE = 9; // overlap => -2
80 static constexpr U16 QUAD_LIST_INDEX_COUNT = (VTX_PER_TILE_EDGE - 1) * (VTX_PER_TILE_EDGE - 1) * 4;
81
82 void fromDescriptor(const TerrainDescriptor& descriptor) noexcept;
83};
84
86{
87 friend class Attorney::TerrainChunk;
88
89 public:
90 enum class WireframeMode : U8
91 {
92 NONE = 0,
93 EDGES,
94 NORMALS,
95 LODS,
96 TESS_LEVELS,
97 BLEND_MAP,
98 COUNT
99 };
100
101 enum class ParallaxMode : U8
102 {
103 NONE = 0,
104 NORMAL,
106 };
107
108 enum class TextureUsageType : U8
109 {
110 ALBEDO_ROUGHNESS = 0,
111 NORMAL,
112 DISPLACEMENT_AO,
113 COUNT
114 };
115
116 struct Vert {
117 vec3<F32> _position;
118 vec3<F32> _normal;
119 vec3<F32> _tangent;
120 };
121
122 public:
123 explicit Terrain( const ResourceDescriptor<Terrain>& descriptor );
124 bool unload() override;
125
126 void toggleBoundingBoxes();
127
128 [[nodiscard]] const vector<VertexBuffer::Vertex>& getVerts() const noexcept;
129 [[nodiscard]] Vert getVert(F32 x_clampf, F32 z_clampf, bool smooth) const;
130 [[nodiscard]] Vert getVertFromGlobal(F32 x, F32 z, bool smooth) const;
131 [[nodiscard]] vec2<U16> getDimensions() const noexcept;
132 [[nodiscard]] vec2<F32> getAltitudeRange() const noexcept;
133
134 [[nodiscard]] const Quadtree& getQuadtree() const noexcept { return _terrainQuadtree; }
135
136 void getVegetationStats(U32& maxGrassInstances, U32& maxTreeInstances) const;
137
138 [[nodiscard]] const vector<TerrainChunk*>& terrainChunks() const noexcept { return _terrainChunks; }
139 [[nodiscard]] const TerrainDescriptor& descriptor() const noexcept { return _descriptor; }
140
141 void saveToXML(boost::property_tree::ptree& pt) const override;
142 void loadFromXML(const boost::property_tree::ptree& pt) override;
143
144 protected:
145 [[nodiscard]] Vert getVert(F32 x_clampf, F32 z_clampf) const;
146 [[nodiscard]] Vert getSmoothVert(F32 x_clampf, F32 z_clampf) const;
147
148 bool load( PlatformContext& context ) override;
149 void postBuild( PlatformContext& context );
150
151 void buildDrawCommands(SceneGraphNode* sgn, GenericDrawCommandContainer& cmdsOut) override;
152
153 void prepareRender(SceneGraphNode* sgn,
154 RenderingComponent& rComp,
155 RenderPackage& pkg,
156 GFX::MemoryBarrierCommand& postDrawMemCmd,
157 RenderStagePass renderStagePass,
158 const CameraSnapshot& cameraSnapshot,
159 bool refreshData) override;
160
161 void postLoad(SceneGraphNode* sgn) override;
162
163 void onEditorChange(std::string_view field);
164
165 [[nodiscard]] bool loadResources( PlatformContext& context );
166 [[nodiscard]] void createVegetation( PlatformContext& context );
167
168 PROPERTY_R(TessellationParams, tessParams);
169
170 protected:
171 friend class ResourceCache;
172 bool postLoad() override;
173
174 public:
175 vector<VertexBuffer::Vertex> _physicsVerts;
176
177 protected:
178 Quadtree _terrainQuadtree;
179 vector<TerrainChunk*> _terrainChunks;
180 GenericVertexData_ptr _terrainBuffer = nullptr;
181 vector<TileRing> _tileRings;
182
183 bool _initialSetupDone = false;
184
185 SceneGraphNode* _vegetationGrassNode = nullptr;
186 TerrainDescriptor _descriptor;
187 //0 - normal, 1 - wireframe, 2 - normals
188 std::array<Handle<ShaderProgram>, to_base(WireframeMode::COUNT)> _terrainColourShader;
189 std::array<Handle<ShaderProgram>, to_base(WireframeMode::COUNT)> _terrainPrePassShader;
190
191 Handle<Vegetation> _vegetation = INVALID_HANDLE<Vegetation>;
192};
193
194inline size_t GetHash( Terrain* terrain ) noexcept
195{
196 return GetHash( terrain->descriptor() );
197}
198
199namespace Attorney {
201private:
202 static void registerTerrainChunk(Terrain& terrain, Divide::TerrainChunk* const chunk)
203 {
204 terrain._terrainChunks.push_back(chunk);
205 }
206
208};
209
210} // namespace Attorney
211} // namespace Divide
212
213#endif //DVD_TERRAIN_H_
#define PROPERTY_R(...)
Convenience method to add a class member with public read access but protected write access.
#define FWD_DECLARE_MANAGED_CLASS(T)
#define DEFINE_3D_OBJECT_TYPE(Name, Enum)
Definition: SceneNodeFwd.h:128
static void registerTerrainChunk(Terrain &terrain, Divide::TerrainChunk *const chunk)
Definition: Terrain.h:202
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
bool load() override
bool unload() override
uint8_t U8
size_t GetHash(const PropertyDescriptor< T > &descriptor) noexcept
Definition: Resource.inl:40
eastl::vector< Type > vector
Definition: Vector.h:42
uint16_t U16
eastl::fixed_vector< GenericDrawCommand, 1, true > GenericDrawCommandContainer
T TER_COORD(T x, T y, T width) noexcept
Definition: Terrain.h:47
TerrainTextureChannel
Definition: Terrain.h:51
uint32_t U32
constexpr auto to_base(const Type value) -> Type
static constexpr U16 QUAD_LIST_INDEX_COUNT
Definition: Terrain.h:80
static constexpr U8 PATCHES_PER_TILE_EDGE
Definition: Terrain.h:79
PROPERTY_RW(vec2< F32 >, SnapGridSize)
PROPERTY_RW(vec2< F32 >, WorldScale)
PROPERTY_RW(U32, tessellatedTriangleWidth, 32u)
void fromDescriptor(const TerrainDescriptor &descriptor) noexcept
Definition: Terrain.cpp:92
static constexpr U8 VTX_PER_TILE_EDGE
Definition: Terrain.h:78