Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
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
38class CFrustum;
39
40struct 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{
49public:
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
132public:
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 */
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
211private:
213};
214
215
216#endif // INCLUDED_WATERMANAGER
Definition: Frustum.h:37
Definition: Matrix3D.h:34
Definition: VertexBufferManager.h:49
Definition: IDeviceCommandContext.h:42
Definition: IDevice.h:48
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:74
Class WaterManager: Maintain rendering-related water settings and textures Anything that affects game...
Definition: WaterManager.h:48
void RenderWaves(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CFrustum &frustrum)
Definition: WaterManager.cpp:895
std::unique_ptr< Renderer::Backend::ITexture > m_FancyTextureDepth
Definition: WaterManager.h:73
float m_Murkiness
Definition: WaterManager.h:129
std::unique_ptr< Renderer::Backend::IFramebuffer > m_FancyEffectsFramebuffer
Definition: WaterManager.h:114
size_t m_MapSize
Definition: WaterManager.h:66
std::unique_ptr< Renderer::Backend::IFramebuffer > m_ReflectionFramebuffer
Definition: WaterManager.h:113
bool m_WaterRefraction
Definition: WaterManager.h:93
void RecreateOrLoadTexturesIfNeeded()
Recreates/loads needed water textures.
Definition: WaterManager.cpp:216
Renderer::Backend::IDevice * m_Device
Definition: WaterManager.h:212
size_t m_RefTextureSize
Definition: WaterManager.h:109
std::unique_ptr< Renderer::Backend::IFramebuffer > m_FancyEffectsOccludersFramebuffer
Definition: WaterManager.h:115
float m_WindAngle
Definition: WaterManager.h:130
CTexturePtr m_FoamTex
Definition: WaterManager.h:70
void RecomputeWaterData()
RecomputeWaterData: calculates all derived data from the waterheight.
Definition: WaterManager.cpp:966
void RecomputeWindStrength()
RecomputeWindStrength: calculates the intensity of waves.
Definition: WaterManager.cpp:978
size_t GetCurrentTextureIndex(const double &period) const
Returns an index of the current texture that should be used for rendering water.
Definition: WaterManager.cpp:1148
void CreateWaveMeshes()
CreateWaveMeshes: Creates the waves objects (and meshes).
Definition: WaterManager.cpp:500
bool WillRenderFancyWater() const
Returns true if fancy water shaders will be used (i.e.
Definition: WaterManager.cpp:1141
u32 m_updatej1
Definition: WaterManager.h:81
CColor m_WaterColor
Definition: WaterManager.h:126
std::unique_ptr< Renderer::Backend::ITexture > m_RefrFboDepthTexture
Definition: WaterManager.h:75
~WaterManager()
Definition: WaterManager.cpp:108
std::unique_ptr< Renderer::Backend::IFramebuffer > m_RefractionFramebuffer
Definition: WaterManager.h:112
bool m_RenderWater
Definition: WaterManager.h:83
CTexturePtr m_WaveTex
Definition: WaterManager.h:69
void RecomputeDistanceHeightmap()
RecomputeDistanceHeightmap: recalculates (or calculates) the distance heightmap.
Definition: WaterManager.cpp:473
WaterManager(Renderer::Backend::IDevice *device)
Definition: WaterManager.cpp:72
float m_RepeatPeriod
Definition: WaterManager.h:104
CColor m_WaterTint
Definition: WaterManager.h:127
std::unique_ptr< Renderer::Backend::ITexture > m_ReflFboDepthTexture
Definition: WaterManager.h:74
void ReloadWaterNormalTextures()
ReloadWaterNormalTextures: Reload the normal textures so that changing water type in Atlas will actua...
Definition: WaterManager.cpp:393
bool m_WaterRealDepth
Definition: WaterManager.h:89
CTexturePtr m_WaterTexture[60]
Definition: WaterManager.h:50
size_t GetNextTextureIndex(const double &period) const
Definition: WaterManager.cpp:1154
void Initialize()
Definition: WaterManager.cpp:130
bool m_NeedInfoUpdate
Definition: WaterManager.h:99
CTexturePtr m_NormalMap[60]
Definition: WaterManager.h:51
std::unique_ptr< float[]> m_WindStrength
Definition: WaterManager.h:54
u32 m_updatei0
Definition: WaterManager.h:78
Renderer::Backend::IVertexInputLayout * m_ShoreVertexInputLayout
Definition: WaterManager.h:64
bool m_WaterReflection
Definition: WaterManager.h:95
std::vector< std::unique_ptr< WaveObject > > m_ShoreWaves
Definition: WaterManager.h:60
bool m_NeedsReloading
Definition: WaterManager.h:97
u32 m_updatei1
Definition: WaterManager.h:80
std::wstring m_WaterType
Definition: WaterManager.h:125
void SetMapSize(size_t size)
Updates the map size.
Definition: WaterManager.cpp:1096
int LoadWaterTextures()
LoadWaterTextures: Load water textures from within the progressive load framework.
Definition: WaterManager.cpp:166
CMatrix3D m_RefractionViewInvMatrix
Definition: WaterManager.h:122
CMatrix3D m_RefractionProjInvMatrix
Definition: WaterManager.h:121
float m_Waviness
Definition: WaterManager.h:128
ssize_t m_TexSize
Definition: WaterManager.h:67
double m_WaterTexTimer
Definition: WaterManager.h:103
void UnloadWaterTextures()
UnloadWaterTextures: Free any loaded water textures and reset the internal state so that another call...
Definition: WaterManager.cpp:412
float m_WaterHeight
Definition: WaterManager.h:101
std::unique_ptr< Renderer::Backend::ITexture > m_FancyTexture
Definition: WaterManager.h:72
std::unique_ptr< Renderer::Backend::ITexture > m_RefractionTexture
Definition: WaterManager.h:108
CMatrix3D m_RefractionMatrix
Definition: WaterManager.h:120
bool m_WaterFancyEffects
Definition: WaterManager.h:91
bool m_WaterEffects
Definition: WaterManager.h:86
u32 m_updatej0
Definition: WaterManager.h:79
void UpdateQuality()
Updates the settings to the one from the renderer, and sets m_NeedsReloading.
Definition: WaterManager.cpp:1112
std::unique_ptr< Renderer::Backend::ITexture > m_ReflectionTexture
Definition: WaterManager.h:107
std::unique_ptr< float[]> m_DistanceHeightmap
Definition: WaterManager.h:56
CVertexBufferManager::Handle m_ShoreWavesVBIndices
Definition: WaterManager.h:62
CMatrix3D m_ReflectionMatrix
Definition: WaterManager.h:119
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:23
Definition: Color.h:43
Definition: WaterManager.cpp:65
uint32_t u32
Definition: types.h:39
intptr_t ssize_t
Definition: wposix_types.h:82