1 /* Copyright (C) 2017 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/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"
31 
32 class CSimulation2;
33 class CFrustum;
34 
35 struct CoastalPoint;
36 struct WaveObject;
37 
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).
41  */
42 
43 class WaterManager
44 {
45 public:
46 	CTexturePtr m_WaterTexture[60];
47 	CTexturePtr m_NormalMap[60];
48 
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.
52 
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;
57 
58 	size_t m_MapSize;
59 	ssize_t m_TexSize;
60 
61 	CTexturePtr m_WaveTex;
62 	CTexturePtr m_FoamTex;
63 
64 	GLuint m_depthTT;
65 	GLuint m_FancyTextureNormal;
66 	GLuint m_FancyTextureOther;
67 	GLuint m_FancyTextureDepth;
68 	GLuint m_ReflFboDepthTexture;
69 	GLuint m_RefrFboDepthTexture;
70 
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;
76 
77 	int m_WaterCurrentTex;
78 	bool m_RenderWater;
79 
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;
93 
94 	bool m_NeedsReloading;
95 	// requires also recreating the super fancy information.
96 	bool m_NeedInfoUpdate;
97 
98 	float m_WaterHeight;
99 
100 	double m_WaterTexTimer;
101 	float m_RepeatPeriod;
102 
103 	// Reflection and refraction textures for fancy water
104 	GLuint m_ReflectionTexture;
105 	GLuint m_RefractionTexture;
106 	size_t m_RefTextureSize;
107 
108 	// framebuffer objects
109 	GLuint m_RefractionFbo;
110 	GLuint m_ReflectionFbo;
111 	GLuint m_FancyEffectsFBO;
112 
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;
117 
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.
125 
126 public:
127 	WaterManager();
128 	~WaterManager();
129 
130 	/**
131 	 * LoadWaterTextures: Load water textures from within the
132 	 * progressive load framework.
133 	 *
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.
136 	 */
137 	int LoadWaterTextures();
138 
139 	/**
140 	 * Resize: Updates the fancy water textures so that water will render correctly
141 	 * with fancy water.
142 	 */
143 	void Resize();
144 
145 	/**
146 	 * ReloadWaterNormalTextures: Reload the normal textures so that changing
147 	 * water type in Atlas will actually do the right thing.
148 	 */
149 	void ReloadWaterNormalTextures();
150 
151 	/**
152 	 * UnloadWaterTextures: Free any loaded water textures and reset the internal state
153 	 * so that another call to LoadWaterTextures will begin progressive loading.
154 	 */
155 	void UnloadWaterTextures();
156 
157 	/**
158 	 * RecomputeWaterData: calculates all derived data from the waterheight
159 	 */
160 	void RecomputeWaterData();
161 
162 	/**
163 	 * RecomputeWindStrength: calculates the intensity of waves
164 	 */
165 	void RecomputeWindStrength();
166 
167 	/**
168 	 * RecomputeDistanceHeightmap: recalculates (or calculates) the distance heightmap.
169 	 */
170 	void RecomputeDistanceHeightmap();
171 
172 	/**
173 	 * RecomputeBlurredNormalMap: calculates the blurred normal map of the terrain. Slow.
174 	 */
175 	void RecomputeBlurredNormalMap();
176 
177 	/**
178 	 * CreateWaveMeshes: Creates the waves objects (and meshes).
179 	 */
180 	void CreateWaveMeshes();
181 
182 	/**
183 	 * Updates the map size. Will trigger a complete recalculation of fancy water information the next turn.
184 	 */
185 	void SetMapSize(size_t size);
186 
187 	/**
188 	 * Updates the settings to the one from the renderer, and sets m_NeedsReloading.
189 	 */
190 	void UpdateQuality();
191 
192 	/**
193 	 * Returns true if fancy water shaders will be used (i.e. the hardware is capable
194 	 * and it hasn't been configured off)
195 	 */
196 	bool WillRenderFancyWater();
197 
198 	void RenderWaves(const CFrustum& frustrum);
199 };
200 
201 
202 #endif // INCLUDED_WATERMANAGER
203