Line data Source code
1 : /* Copyright (C) 2022 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 : #ifndef INCLUDED_RENDERER_SCENERENDERER
19 : #define INCLUDED_RENDERER_SCENERENDERER
20 :
21 : #include "graphics/Camera.h"
22 : #include "graphics/ShaderDefines.h"
23 : #include "graphics/ShaderProgramPtr.h"
24 : #include "maths/BoundingBoxAligned.h"
25 : #include "ps/Singleton.h"
26 : #include "renderer/backend/IDeviceCommandContext.h"
27 : #include "renderer/RenderingOptions.h"
28 : #include "renderer/Scene.h"
29 :
30 : #include <memory>
31 :
32 : class CCanvas2D;
33 : class CLightEnv;
34 : class CMaterial;
35 : class CMaterialManager;
36 : class CModel;
37 : class CParticleManager;
38 : class CPatch;
39 : class CSimulation2;
40 : class ShadowMap;
41 : class SkyManager;
42 : class TerrainRenderer;
43 : class WaterManager;
44 :
45 : // rendering modes
46 : enum ERenderMode { WIREFRAME, SOLID, EDGED_FACES };
47 :
48 : // transparency modes
49 : enum ETransparentMode { TRANSPARENT, TRANSPARENT_OPAQUE, TRANSPARENT_BLEND };
50 :
51 : class CSceneRenderer : public SceneCollector
52 : {
53 : public:
54 : enum CullGroup
55 : {
56 : CULL_DEFAULT,
57 : CULL_SHADOWS_CASCADE_0,
58 : CULL_SHADOWS_CASCADE_1,
59 : CULL_SHADOWS_CASCADE_2,
60 : CULL_SHADOWS_CASCADE_3,
61 : CULL_REFLECTIONS,
62 : CULL_REFRACTIONS,
63 : CULL_SILHOUETTE_OCCLUDER,
64 : CULL_SILHOUETTE_CASTER,
65 : CULL_MAX
66 : };
67 :
68 : CSceneRenderer();
69 : ~CSceneRenderer();
70 :
71 : void Initialize();
72 : void Resize(int width, int height);
73 :
74 : void BeginFrame();
75 : void EndFrame();
76 :
77 : /**
78 : * Set simulation context for rendering purposes.
79 : * Must be called at least once when the game has started and before
80 : * frames are rendered.
81 : */
82 : void SetSimulation(CSimulation2* simulation);
83 :
84 : // trigger a reload of shaders (when parameters they depend on have changed)
85 : void MakeShadersDirty();
86 :
87 : /**
88 : * Set up the camera used for rendering the next scene; this includes
89 : * setting OpenGL state like viewport, projection and modelview matrices.
90 : *
91 : * @param viewCamera this camera determines the eye position for rendering
92 : * @param cullCamera this camera determines the frustum for culling in the renderer and
93 : * for shadow calculations
94 : */
95 : void SetSceneCamera(const CCamera& viewCamera, const CCamera& cullCamera);
96 :
97 : /**
98 : * Enumerate and submit all objects of the given scene which should be rendered.
99 : * Must be called before RenderScene.
100 : */
101 : void PrepareScene(
102 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, Scene& scene);
103 :
104 : /**
105 : * Render submitted objects of the previously given scene.
106 : */
107 : void RenderScene(
108 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
109 :
110 : /**
111 : * Render overlays of the previously given scene.
112 : * Must be called after RenderScene.
113 : */
114 : void RenderSceneOverlays(
115 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
116 :
117 : /**
118 : * Return the scene that is currently being rendered.
119 : * Only valid when the renderer is in a RenderScene call.
120 : */
121 : Scene& GetScene();
122 :
123 : /**
124 : * Render text overlays on top of the scene.
125 : * Assumes the caller has set up the GL environment for orthographic rendering
126 : * with texturing and blending.
127 : */
128 : void RenderTextOverlays(CCanvas2D& canvas);
129 :
130 : // set the current lighting environment; (note: the passed pointer is just copied to a variable within the renderer,
131 : // so the lightenv passed must be scoped such that it is not destructed until after the renderer is no longer rendering)
132 6 : void SetLightEnv(CLightEnv* lightenv)
133 : {
134 6 : m_LightEnv = lightenv;
135 6 : }
136 :
137 : // set the mode to render subsequent terrain patches
138 0 : void SetTerrainRenderMode(ERenderMode mode) { m_TerrainRenderMode = mode; }
139 : // get the mode to render subsequent terrain patches
140 : ERenderMode GetTerrainRenderMode() const { return m_TerrainRenderMode; }
141 :
142 : // set the mode to render subsequent water patches
143 0 : void SetWaterRenderMode(ERenderMode mode) { m_WaterRenderMode = mode; }
144 : // get the mode to render subsequent water patches
145 0 : ERenderMode GetWaterRenderMode() const { return m_WaterRenderMode; }
146 :
147 : // set the mode to render subsequent models
148 0 : void SetModelRenderMode(ERenderMode mode) { m_ModelRenderMode = mode; }
149 : // get the mode to render subsequent models
150 0 : ERenderMode GetModelRenderMode() const { return m_ModelRenderMode; }
151 :
152 : // Get the mode to render subsequent overlays.
153 0 : ERenderMode GetOverlayRenderMode() const { return m_OverlayRenderMode; }
154 : // Set the mode to render subsequent overlays.
155 0 : void SetOverlayRenderMode(ERenderMode mode) { m_OverlayRenderMode = mode; }
156 :
157 : // debugging
158 0 : void SetDisplayTerrainPriorities(bool enabled) { m_DisplayTerrainPriorities = enabled; }
159 :
160 : // return the current light environment
161 0 : const CLightEnv &GetLightEnv() { return *m_LightEnv; }
162 :
163 : // return the current view camera
164 0 : const CCamera& GetViewCamera() const { return m_ViewCamera; }
165 : // replace the current view camera
166 0 : void SetViewCamera(const CCamera& camera) { m_ViewCamera = camera; }
167 :
168 : // return the current cull camera
169 0 : const CCamera& GetCullCamera() const { return m_CullCamera; }
170 :
171 : /**
172 : * GetWaterManager: Return the renderer's water manager.
173 : *
174 : * @return the WaterManager object used by the renderer
175 : */
176 : WaterManager& GetWaterManager();
177 :
178 : /**
179 : * GetSkyManager: Return the renderer's sky manager.
180 : *
181 : * @return the SkyManager object used by the renderer
182 : */
183 : SkyManager& GetSkyManager();
184 :
185 : CParticleManager& GetParticleManager();
186 :
187 : TerrainRenderer& GetTerrainRenderer();
188 :
189 : CMaterialManager& GetMaterialManager();
190 :
191 : ShadowMap& GetShadowMap();
192 :
193 : /**
194 : * Resets the render state to default, that was before a game started
195 : */
196 : void ResetState();
197 :
198 : void ReloadShaders();
199 :
200 : protected:
201 : void Submit(CPatch* patch) override;
202 : void Submit(SOverlayLine* overlay) override;
203 : void Submit(SOverlayTexturedLine* overlay) override;
204 : void Submit(SOverlaySprite* overlay) override;
205 : void Submit(SOverlayQuad* overlay) override;
206 : void Submit(CModelDecal* decal) override;
207 : void Submit(CParticleEmitter* emitter) override;
208 : void Submit(SOverlaySphere* overlay) override;
209 : void SubmitNonRecursive(CModel* model) override;
210 :
211 : /**
212 : * Update and upload all needed data for submitted objects.
213 : */
214 : void PrepareSubmissions(
215 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
216 : const CBoundingBoxAligned& waterScissor);
217 :
218 : // render any batched objects
219 : void RenderSubmissions(
220 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
221 : const CBoundingBoxAligned& waterScissor);
222 :
223 : // patch rendering stuff
224 : void RenderPatches(
225 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
226 : const CShaderDefines& context, int cullGroup);
227 :
228 : // model rendering stuff
229 : void RenderModels(
230 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
231 : const CShaderDefines& context, int cullGroup);
232 : void RenderTransparentModels(
233 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
234 : const CShaderDefines& context, int cullGroup, ETransparentMode transparentMode);
235 :
236 : void RenderSilhouettes(
237 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
238 : const CShaderDefines& context);
239 :
240 : void RenderParticles(
241 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
242 : int cullGroup);
243 :
244 : // shadow rendering stuff
245 : void RenderShadowMap(
246 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
247 : const CShaderDefines& context);
248 :
249 : // render water reflection and refraction textures
250 : void RenderReflections(
251 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
252 : const CShaderDefines& context, const CBoundingBoxAligned& scissor);
253 : void RenderRefractions(
254 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
255 : const CShaderDefines& context, const CBoundingBoxAligned& scissor);
256 :
257 : void ComputeReflectionCamera(CCamera& camera, const CBoundingBoxAligned& scissor) const;
258 : void ComputeRefractionCamera(CCamera& camera, const CBoundingBoxAligned& scissor) const;
259 :
260 : // debugging
261 : void DisplayFrustum();
262 :
263 : // enable oblique frustum clipping with the given clip plane
264 : void SetObliqueFrustumClipping(CCamera& camera, const CVector4D& clipPlane) const;
265 :
266 : // Private data that is not needed by inline functions.
267 : class Internals;
268 : std::unique_ptr<Internals> m;
269 :
270 : // Current terrain rendering mode.
271 : ERenderMode m_TerrainRenderMode;
272 : // Current water rendering mode.
273 : ERenderMode m_WaterRenderMode;
274 : // Current model rendering mode.
275 : ERenderMode m_ModelRenderMode;
276 : // Current overlay rendering mode.
277 : ERenderMode m_OverlayRenderMode;
278 :
279 : /**
280 : * m_ViewCamera: determines the eye position for rendering
281 : *
282 : * @see CGameView::m_ViewCamera
283 : */
284 : CCamera m_ViewCamera;
285 :
286 : /**
287 : * m_CullCamera: determines the frustum for culling and shadowmap calculations
288 : *
289 : * @see CGameView::m_ViewCamera
290 : */
291 : CCamera m_CullCamera;
292 :
293 : // only valid inside a call to RenderScene
294 : Scene* m_CurrentScene;
295 : int m_CurrentCullGroup;
296 :
297 : CBoundingBoxAligned m_WaterScissor;
298 :
299 : // current lighting setup
300 : CLightEnv* m_LightEnv;
301 :
302 : /**
303 : * Enable rendering of terrain tile priority text overlay, for debugging.
304 : */
305 : bool m_DisplayTerrainPriorities;
306 : };
307 :
308 : #endif // INCLUDED_RENDERER_SCENERENDERER
|