source: ps/trunk/source/renderer/SceneRenderer.h@ 26858

Last change on this file since 26858 was 26858, checked in by Vladislav Belov, 2 years ago

Adds renderer backend interface and dummy backend.

Comments By: phosit, Stan
Tested By: Langbart, phosit

Differential Revision: https://code.wildfiregames.com/D4636

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