Merge 'remotes/trunk'
[0ad.git] / source / renderer / WaterManager.h
blobb7b74b7f70b7705ae1c6ac0fccf3d28d9b275471
1 /* Copyright (C) 2017 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * Water settings (speed, height) and texture management
22 #ifndef INCLUDED_WATERMANAGER
23 #define INCLUDED_WATERMANAGER
25 #include "graphics/Texture.h"
26 #include "lib/ogl.h"
27 #include "maths/Matrix3D.h"
28 #include "maths/Vector2D.h"
29 #include "ps/Shapes.h"
30 #include "renderer/VertexBufferManager.h"
32 class CSimulation2;
33 class CFrustum;
35 struct CoastalPoint;
36 struct WaveObject;
38 /**
39 * Class WaterManager: Maintain rendering-related water settings and textures
40 * Anything that affects gameplay should go in CcmpWaterManager.cpp and passed to this (possibly as copy).
43 class WaterManager
45 public:
46 CTexturePtr m_WaterTexture[60];
47 CTexturePtr m_NormalMap[60];
49 float* m_WindStrength; // How strong the waves are at point X. % of waviness.
50 float* m_DistanceHeightmap; // How far from the shore a point is. Manhattan
51 CVector3D* m_BlurredNormalMap; // Cache a slightly blurred map of the normals of the terrain.
53 // Waves vertex buffers
54 std::vector< WaveObject* > m_ShoreWaves; // TODO: once we get C++11, remove pointer
55 // Waves indices buffer. Only one since All Wave Objects have the same.
56 CVertexBuffer::VBChunk* m_ShoreWaves_VBIndices;
58 size_t m_MapSize;
59 ssize_t m_TexSize;
61 CTexturePtr m_WaveTex;
62 CTexturePtr m_FoamTex;
64 GLuint m_depthTT;
65 GLuint m_FancyTextureNormal;
66 GLuint m_FancyTextureOther;
67 GLuint m_FancyTextureDepth;
68 GLuint m_ReflFboDepthTexture;
69 GLuint m_RefrFboDepthTexture;
71 // used to know what to update when updating parts of the terrain only.
72 u32 m_updatei0;
73 u32 m_updatej0;
74 u32 m_updatei1;
75 u32 m_updatej1;
77 int m_WaterCurrentTex;
78 bool m_RenderWater;
80 // If disabled, force the use of the fixed function for rendering.
81 bool m_WaterEffects;
82 // Those variables register the current quality level. If there is a change, I have to recompile the shader.
83 // Use real depth or use the fake precomputed one.
84 bool m_WaterRealDepth;
85 // Use fancy shore effects and show trails behind ships
86 bool m_WaterFancyEffects;
87 // Use refractions instead of simply making the water more or less transparent.
88 bool m_WaterRefraction;
89 // Use complete reflections instead of showing merely the sky.
90 bool m_WaterReflection;
91 // Show shadows on the water.
92 bool m_WaterShadows;
94 bool m_NeedsReloading;
95 // requires also recreating the super fancy information.
96 bool m_NeedInfoUpdate;
98 float m_WaterHeight;
100 double m_WaterTexTimer;
101 float m_RepeatPeriod;
103 // Reflection and refraction textures for fancy water
104 GLuint m_ReflectionTexture;
105 GLuint m_RefractionTexture;
106 size_t m_RefTextureSize;
108 // framebuffer objects
109 GLuint m_RefractionFbo;
110 GLuint m_ReflectionFbo;
111 GLuint m_FancyEffectsFBO;
113 // Model-view-projection matrices for reflected & refracted cameras
114 // (used to let the vertex shader do projective texturing)
115 CMatrix3D m_ReflectionMatrix;
116 CMatrix3D m_RefractionMatrix;
118 // Water parameters
119 std::wstring m_WaterType; // Which texture to use.
120 CColor m_WaterColor; // Color of the water without refractions. This is what you're seeing when the water's deep or murkiness high.
121 CColor m_WaterTint; // Tint of refraction in the water.
122 float m_Waviness; // How big the waves are.
123 float m_Murkiness; // How murky the water is.
124 float m_WindAngle; // In which direction the water waves go.
126 public:
127 WaterManager();
128 ~WaterManager();
131 * LoadWaterTextures: Load water textures from within the
132 * progressive load framework.
134 * @return 0 if loading has completed, a value from 1 to 100 (in percent of completion)
135 * if more textures need to be loaded and a negative error value on failure.
137 int LoadWaterTextures();
140 * Resize: Updates the fancy water textures so that water will render correctly
141 * with fancy water.
143 void Resize();
146 * ReloadWaterNormalTextures: Reload the normal textures so that changing
147 * water type in Atlas will actually do the right thing.
149 void ReloadWaterNormalTextures();
152 * UnloadWaterTextures: Free any loaded water textures and reset the internal state
153 * so that another call to LoadWaterTextures will begin progressive loading.
155 void UnloadWaterTextures();
158 * RecomputeWaterData: calculates all derived data from the waterheight
160 void RecomputeWaterData();
163 * RecomputeWindStrength: calculates the intensity of waves
165 void RecomputeWindStrength();
168 * RecomputeDistanceHeightmap: recalculates (or calculates) the distance heightmap.
170 void RecomputeDistanceHeightmap();
173 * RecomputeBlurredNormalMap: calculates the blurred normal map of the terrain. Slow.
175 void RecomputeBlurredNormalMap();
178 * CreateWaveMeshes: Creates the waves objects (and meshes).
180 void CreateWaveMeshes();
183 * Updates the map size. Will trigger a complete recalculation of fancy water information the next turn.
185 void SetMapSize(size_t size);
188 * Updates the settings to the one from the renderer, and sets m_NeedsReloading.
190 void UpdateQuality();
193 * Returns true if fancy water shaders will be used (i.e. the hardware is capable
194 * and it hasn't been configured off)
196 bool WillRenderFancyWater();
198 void RenderWaves(const CFrustum& frustrum);
202 #endif // INCLUDED_WATERMANAGER