Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Vegetation.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#ifndef DVD_VEGETATION_H_
33#define DVD_VEGETATION_H_
34
36
42
43namespace eastl
44{
45template <>
46struct hash<Divide::vec2<Divide::F32>>
47{
48 size_t operator()( const Divide::vec2<Divide::F32>& a ) const
49 {
50 size_t h = 17;
52 return h;
53 }
54};
55} //namespace eastl
56
57namespace Divide {
58
59namespace GFX {
60 class CommandBuffer;
61};
62
63class Mesh;
64class Texture;
65class Terrain;
66class Pipeline;
67class GFXDevice;
68class SceneState;
69class RenderTarget;
70class TerrainChunk;
71class ShaderProgram;
72class SceneGraphNode;
73class PlatformContext;
74class GenericVertexData;
75enum class RenderStage : U8;
76
77FWD_DECLARE_MANAGED_CLASS(VertexBuffer);
79
81{
84 //x - array index, y - chunk ID, z - render flag, w - height scale
85 vec4<F32> _data = { 1.0f, 1.0f, 1.0f, 0.0f };
86};
87//RenderDoc: mat4 transform; vec4 posAndIndex; vec4 extentAndRender;
88
89class VegetationInstance;
90
94{
95 public:
96 explicit Vegetation( const ResourceDescriptor<Vegetation>& descriptor );
97 ~Vegetation() override;
98
99 void buildDrawCommands(SceneGraphNode* sgn, GenericDrawCommandContainer& cmdsOut) override;
100
101 bool load( PlatformContext & context ) override;
102 bool unload() override;
103
104 protected:
105 void prepareRender(SceneGraphNode* sgn,
106 RenderingComponent& rComp,
107 RenderPackage& pkg,
108 GFX::MemoryBarrierCommand& postDrawMemCmd,
109 RenderStagePass renderStagePass,
110 const CameraSnapshot& cameraSnapshot,
111 bool refreshData) override;
112
113 void sceneUpdate(U64 deltaTimeUS,
114 SceneGraphNode* sgn,
115 SceneState& sceneState) override;
116 private:
117 void uploadVegetationData(vector<Byte>& grassDataOut, vector<Byte> treeDataOut);
118 void prepareDraw(SceneGraphNode* sgn);
119
120 protected:
121 friend class VegetationInstance;
122
123 void registerInstance( U32 chunkID, VegetationInstance* instance);
124 void unregisterInstance(U32 chunkID);
125
126 vector<Str<256>> _treeMeshNames;
127
128 SharedMutex _instanceLock;
130
131 private:
132 const VegetationDescriptor _descriptor;
133
134 F32 _windX = 0.0f, _windZ = 0.0f, _windS = 0.0f;
135 U64 _stateRefreshIntervalUS = Time::SecondsToMicroseconds(1);
136 U64 _stateRefreshIntervalBufferUS = 0ULL;
137 vec4<F32> _grassExtents = VECTOR4_UNIT;
138 vec4<F32> _treeExtents = VECTOR4_UNIT;
139
140 SceneGraphNode* _treeParentNode = nullptr;
141
142 F32 _grassDistance = -1.0f;
143 F32 _treeDistance = -1.0f;
144 U32 _maxGrassInstances = 0u;
145 U32 _maxTreeInstances = 0u;
146
147 Pipeline* _cullPipelineGrass = nullptr;
148 Pipeline* _cullPipelineTrees = nullptr;
149 SharedMutex _treeMeshLock;
150
151 std::array<U16, 3> _lodPartitions;
152 VertexBuffer_ptr _buffer;
153 ShaderBuffer_uptr _treeData;
154 ShaderBuffer_uptr _grassData;
155 vector<Handle<Mesh>> _treeMeshes;
156
157 eastl::unordered_set<vec2<F32>> _grassPositions;
158 eastl::unordered_set<vec2<F32>> _treePositions;
159
160 Handle<ShaderProgram> _cullShaderGrass = INVALID_HANDLE<ShaderProgram>;
161 Handle<ShaderProgram> _cullShaderTrees = INVALID_HANDLE<ShaderProgram>;
162 Handle<Material> _treeMaterial = INVALID_HANDLE<Material>;
163 Handle<Material> _vegetationMaterial = INVALID_HANDLE<Material>;
164};
165
167
169{
170public:
173
174 void computeTransforms();
175
176private:
178
179protected:
180 friend class Vegetation;
181
184private:
185 const Handle<Vegetation> _parent = INVALID_HANDLE<Vegetation>;
186 const TerrainChunk* _chunk = nullptr;
187};
188
190
191} // namespace Divide
192
193#endif //DVD_VEGETATION_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
#define DEFINE_NODE_TYPE(Name, Enum)
Definition: SceneNodeFwd.h:120
PlatformContext & context() noexcept
const Handle< Vegetation > _parent
Definition: Vegetation.h:185
const TerrainChunk * _chunk
Definition: Vegetation.h:186
constexpr T SecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:767
void Hash_combine(size_t &seed, const T &v, const Rest &... rest) noexcept
a la Boost
Definition: MathHelper.inl:799
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
bool load() override
bool unload() override
uint8_t U8
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
eastl::vector< Type > vector
Definition: Vector.h:42
Project & parent
Definition: DefaultScene.h:41
static const vec4< F32 > VECTOR4_UNIT
Definition: MathVectors.h:1438
eastl::fixed_vector< GenericDrawCommand, 1, true > GenericDrawCommandContainer
uint32_t U32
uint64_t U64
vec4< F32 > _orientationQuat
Definition: Vegetation.h:83
vec4< F32 > _positionAndScale
Definition: Vegetation.h:82
size_t operator()(const Divide::vec2< Divide::F32 > &a) const
Definition: Vegetation.h:48