Pyrogenesis  trunk
WaterManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Water settings (speed, height) and texture management
20  */
21 
22 #ifndef INCLUDED_WATERMANAGER
23 #define INCLUDED_WATERMANAGER
24 
25 #include "graphics/Color.h"
26 #include "graphics/Texture.h"
27 #include "maths/Matrix3D.h"
28 #include "maths/Vector2D.h"
34 
35 #include <memory>
36 #include <vector>
37 
38 class CFrustum;
39 
40 struct WaveObject;
41 
42 /**
43  * Class WaterManager: Maintain rendering-related water settings and textures
44  * Anything that affects gameplay should go in CcmpWaterManager.cpp and passed to this (possibly as copy).
45  */
46 
48 {
49 public:
52 
53  // How strong the waves are at point X. % of waviness.
54  std::unique_ptr<float[]> m_WindStrength;
55  // How far from the shore a point is. Manhattan.
56  std::unique_ptr<float[]> m_DistanceHeightmap;
57 
58  // Waves vertex buffers
59  // TODO: measure storing value instead of pointer.
60  std::vector<std::unique_ptr<WaveObject>> m_ShoreWaves;
61  // Waves indices buffer. Only one since All Wave Objects have the same.
63 
65 
66  size_t m_MapSize;
68 
71 
72  std::unique_ptr<Renderer::Backend::ITexture> m_FancyTexture;
73  std::unique_ptr<Renderer::Backend::ITexture> m_FancyTextureDepth;
74  std::unique_ptr<Renderer::Backend::ITexture> m_ReflFboDepthTexture;
75  std::unique_ptr<Renderer::Backend::ITexture> m_RefrFboDepthTexture;
76 
77  // used to know what to update when updating parts of the terrain only.
82 
84 
85  // If disabled, force the use of the simple water shader for rendering.
87  // Those variables register the current quality level. If there is a change, I have to recompile the shader.
88  // Use real depth or use the fake precomputed one.
90  // Use fancy shore effects and show trails behind ships
92  // Use refractions instead of simply making the water more or less transparent.
94  // Use complete reflections instead of showing merely the sky.
96 
98  // requires also recreating the super fancy information.
100 
102 
105 
106  // Reflection and refraction textures for fancy water
107  std::unique_ptr<Renderer::Backend::ITexture> m_ReflectionTexture;
108  std::unique_ptr<Renderer::Backend::ITexture> m_RefractionTexture;
110 
111  // framebuffer objects
112  std::unique_ptr<Renderer::Backend::IFramebuffer> m_RefractionFramebuffer;
113  std::unique_ptr<Renderer::Backend::IFramebuffer> m_ReflectionFramebuffer;
114  std::unique_ptr<Renderer::Backend::IFramebuffer> m_FancyEffectsFramebuffer;
115  std::unique_ptr<Renderer::Backend::IFramebuffer> m_FancyEffectsOccludersFramebuffer;
116 
117  // Model-view-projection matrices for reflected & refracted cameras
118  // (used to let the vertex shader do projective texturing)
123 
124  // Water parameters
125  std::wstring m_WaterType; // Which texture to use.
126  CColor m_WaterColor; // Color of the water without refractions. This is what you're seeing when the water's deep or murkiness high.
127  CColor m_WaterTint; // Tint of refraction in the water.
128  float m_Waviness; // How big the waves are.
129  float m_Murkiness; // How murky the water is.
130  float m_WindAngle; // In which direction the water waves go.
131 
132 public:
133  WaterManager();
134  ~WaterManager();
135 
136  void Initialize();
137 
138  /**
139  * LoadWaterTextures: Load water textures from within the
140  * progressive load framework.
141  *
142  * @return 0 if loading has completed, a value from 1 to 100 (in percent of completion)
143  * if more textures need to be loaded and a negative error value on failure.
144  */
145  int LoadWaterTextures();
146 
147  /**
148  * Recreates/loads needed water textures.
149  */
151 
152  /**
153  * ReloadWaterNormalTextures: Reload the normal textures so that changing
154  * water type in Atlas will actually do the right thing.
155  */
157 
158  /**
159  * UnloadWaterTextures: Free any loaded water textures and reset the internal state
160  * so that another call to LoadWaterTextures will begin progressive loading.
161  */
162  void UnloadWaterTextures();
163 
164  /**
165  * RecomputeWaterData: calculates all derived data from the waterheight
166  */
167  void RecomputeWaterData();
168 
169  /**
170  * RecomputeWindStrength: calculates the intensity of waves
171  */
172  void RecomputeWindStrength();
173 
174  /**
175  * RecomputeDistanceHeightmap: recalculates (or calculates) the distance heightmap.
176  */
178 
179  /**
180  * CreateWaveMeshes: Creates the waves objects (and meshes).
181  */
182  void CreateWaveMeshes();
183 
184  /**
185  * Updates the map size. Will trigger a complete recalculation of fancy water information the next turn.
186  */
187  void SetMapSize(size_t size);
188 
189  /**
190  * Updates the settings to the one from the renderer, and sets m_NeedsReloading.
191  */
192  void UpdateQuality();
193 
194  /**
195  * Returns true if fancy water shaders will be used (i.e. the hardware is capable
196  * and it hasn't been configured off)
197  */
198  bool WillRenderFancyWater() const;
199 
200  void RenderWaves(
201  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
202  const CFrustum& frustrum);
203 
204  /**
205  * Returns an index of the current texture that should be used for rendering
206  * water.
207  */
208  size_t GetCurrentTextureIndex(const double& period) const;
209  size_t GetNextTextureIndex(const double& period) const;
210 };
211 
212 
213 #endif // INCLUDED_WATERMANAGER
std::unique_ptr< Renderer::Backend::ITexture > m_RefrFboDepthTexture
Definition: WaterManager.h:75
CTexturePtr m_WaterTexture[60]
Definition: WaterManager.h:50
std::unique_ptr< Renderer::Backend::ITexture > m_ReflectionTexture
Definition: WaterManager.h:107
bool m_WaterFancyEffects
Definition: WaterManager.h:91
bool m_NeedInfoUpdate
Definition: WaterManager.h:99
u32 m_updatei1
Definition: WaterManager.h:80
float m_RepeatPeriod
Definition: WaterManager.h:104
void RenderWaves(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CFrustum &frustrum)
Definition: WaterManager.cpp:884
CTexturePtr m_FoamTex
Definition: WaterManager.h:70
size_t GetNextTextureIndex(const double &period) const
Definition: WaterManager.cpp:1142
Definition: Color.h:42
void ReloadWaterNormalTextures()
ReloadWaterNormalTextures: Reload the normal textures so that changing water type in Atlas will actua...
Definition: WaterManager.cpp:392
bool m_WaterRealDepth
Definition: WaterManager.h:89
u32 m_updatei0
Definition: WaterManager.h:78
void RecomputeWaterData()
RecomputeWaterData: calculates all derived data from the waterheight.
Definition: WaterManager.cpp:955
float m_Waviness
Definition: WaterManager.h:128
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:73
std::unique_ptr< Renderer::Backend::ITexture > m_FancyTextureDepth
Definition: WaterManager.h:73
Definition: Frustum.h:36
CTexturePtr m_NormalMap[60]
Definition: WaterManager.h:51
std::unique_ptr< Renderer::Backend::ITexture > m_RefractionTexture
Definition: WaterManager.h:108
~WaterManager()
Definition: WaterManager.cpp:107
bool m_WaterEffects
Definition: WaterManager.h:86
float m_WaterHeight
Definition: WaterManager.h:101
CMatrix3D m_ReflectionMatrix
Definition: WaterManager.h:119
CVertexBufferManager::Handle m_ShoreWavesVBIndices
Definition: WaterManager.h:62
void SetMapSize(size_t size)
Updates the map size.
Definition: WaterManager.cpp:1084
void RecreateOrLoadTexturesIfNeeded()
Recreates/loads needed water textures.
Definition: WaterManager.cpp:215
Definition: Matrix3D.h:33
u32 m_updatej1
Definition: WaterManager.h:81
bool m_NeedsReloading
Definition: WaterManager.h:97
Definition: VertexBufferManager.h:46
float m_Murkiness
Definition: WaterManager.h:129
std::wstring m_WaterType
Definition: WaterManager.h:125
void UpdateQuality()
Updates the settings to the one from the renderer, and sets m_NeedsReloading.
Definition: WaterManager.cpp:1100
CMatrix3D m_RefractionViewInvMatrix
Definition: WaterManager.h:122
uint32_t u32
Definition: types.h:39
size_t GetCurrentTextureIndex(const double &period) const
Returns an index of the current texture that should be used for rendering water.
Definition: WaterManager.cpp:1136
bool m_RenderWater
Definition: WaterManager.h:83
CMatrix3D m_RefractionProjInvMatrix
Definition: WaterManager.h:121
CMatrix3D m_RefractionMatrix
Definition: WaterManager.h:120
CColor m_WaterTint
Definition: WaterManager.h:127
CTexturePtr m_WaveTex
Definition: WaterManager.h:69
int LoadWaterTextures()
LoadWaterTextures: Load water textures from within the progressive load framework.
Definition: WaterManager.cpp:165
size_t m_RefTextureSize
Definition: WaterManager.h:109
std::unique_ptr< Renderer::Backend::IFramebuffer > m_FancyEffectsFramebuffer
Definition: WaterManager.h:114
bool m_WaterRefraction
Definition: WaterManager.h:93
std::unique_ptr< Renderer::Backend::IFramebuffer > m_ReflectionFramebuffer
Definition: WaterManager.h:113
WaterManager()
Definition: WaterManager.cpp:72
std::unique_ptr< Renderer::Backend::ITexture > m_ReflFboDepthTexture
Definition: WaterManager.h:74
void UnloadWaterTextures()
UnloadWaterTextures: Free any loaded water textures and reset the internal state so that another call...
Definition: WaterManager.cpp:411
intptr_t ssize_t
Definition: wposix_types.h:82
void CreateWaveMeshes()
CreateWaveMeshes: Creates the waves objects (and meshes).
Definition: WaterManager.cpp:499
Renderer::Backend::IVertexInputLayout * m_ShoreVertexInputLayout
Definition: WaterManager.h:64
std::unique_ptr< Renderer::Backend::ITexture > m_FancyTexture
Definition: WaterManager.h:72
std::unique_ptr< Renderer::Backend::IFramebuffer > m_FancyEffectsOccludersFramebuffer
Definition: WaterManager.h:115
std::vector< std::unique_ptr< WaveObject > > m_ShoreWaves
Definition: WaterManager.h:60
bool m_WaterReflection
Definition: WaterManager.h:95
CColor m_WaterColor
Definition: WaterManager.h:126
std::unique_ptr< float[]> m_DistanceHeightmap
Definition: WaterManager.h:56
double m_WaterTexTimer
Definition: WaterManager.h:103
void Initialize()
Definition: WaterManager.cpp:129
std::unique_ptr< Renderer::Backend::IFramebuffer > m_RefractionFramebuffer
Definition: WaterManager.h:112
void RecomputeWindStrength()
RecomputeWindStrength: calculates the intensity of waves.
Definition: WaterManager.cpp:967
std::unique_ptr< float[]> m_WindStrength
Definition: WaterManager.h:54
bool WillRenderFancyWater() const
Returns true if fancy water shaders will be used (i.e.
Definition: WaterManager.cpp:1129
Definition: IDeviceCommandContext.h:40
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:22
void RecomputeDistanceHeightmap()
RecomputeDistanceHeightmap: recalculates (or calculates) the distance heightmap.
Definition: WaterManager.cpp:472
size_t m_MapSize
Definition: WaterManager.h:66
float m_WindAngle
Definition: WaterManager.h:130
u32 m_updatej0
Definition: WaterManager.h:79
ssize_t m_TexSize
Definition: WaterManager.h:67
Definition: WaterManager.cpp:64
Class WaterManager: Maintain rendering-related water settings and textures Anything that affects game...
Definition: WaterManager.h:47