source: ps/trunk/source/renderer/Renderer.h@ 25261

Last change on this file since 25261 was 25261, checked in by Vladislav Belov, 3 years ago

Removes unused shadow settings forgotten in rP13877, removes direct SkipSubmit access.

  • Property svn:eol-style set to native
File size: 12.5 KB
Line 
1/* Copyright (C) 2021 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 * higher level interface on top of OpenGL to render basic objects:
20 * terrain, models, sprites, particles etc.
21 */
22
23#ifndef INCLUDED_RENDERER
24#define INCLUDED_RENDERER
25
26#include "graphics/Camera.h"
27#include "graphics/SColor.h"
28#include "graphics/ShaderDefines.h"
29#include "graphics/ShaderProgramPtr.h"
30#include "lib/file/vfs/vfs_path.h"
31#include "lib/res/handle.h"
32#include "ps/Singleton.h"
33#include "renderer/RenderingOptions.h"
34#include "renderer/Scene.h"
35
36class CFontManager;
37class CLightEnv;
38class CMaterial;
39class CMaterialManager;
40class CModel;
41class CParticleManager;
42class CPatch;
43class CPostprocManager;
44class CShaderManager;
45class CSimulation2;
46class CTextureManager;
47class CTimeManager;
48class RenderPathVertexShader;
49class ShadowMap;
50class SkyManager;
51class TerrainRenderer;
52class WaterManager;
53
54// rendering modes
55enum ERenderMode { WIREFRAME, SOLID, EDGED_FACES };
56
57// transparency modes
58enum ETransparentMode { TRANSPARENT, TRANSPARENT_OPAQUE, TRANSPARENT_BLEND };
59
60// access to sole renderer object
61#define g_Renderer CRenderer::GetSingleton()
62
63///////////////////////////////////////////////////////////////////////////////////////////
64// CRenderer: base renderer class - primary interface to the rendering engine
65struct CRendererInternals;
66
67class CRenderer :
68 public Singleton<CRenderer>,
69 private SceneCollector
70{
71public:
72 // various enumerations and renderer related constants
73 enum { NumAlphaMaps=14 };
74
75 enum CullGroup
76 {
77 CULL_DEFAULT,
78 CULL_SHADOWS,
79 CULL_REFLECTIONS,
80 CULL_REFRACTIONS,
81 CULL_SILHOUETTE_OCCLUDER,
82 CULL_SILHOUETTE_CASTER,
83 CULL_MAX
84 };
85
86 // stats class - per frame counts of number of draw calls, poly counts etc
87 struct Stats
88 {
89 // set all stats to zero
90 void Reset() { memset(this, 0, sizeof(*this)); }
91 // number of draw calls per frame - total DrawElements + Begin/End immediate mode loops
92 size_t m_DrawCalls;
93 // number of terrain triangles drawn
94 size_t m_TerrainTris;
95 // number of water triangles drawn
96 size_t m_WaterTris;
97 // number of (non-transparent) model triangles drawn
98 size_t m_ModelTris;
99 // number of overlay triangles drawn
100 size_t m_OverlayTris;
101 // number of splat passes for alphamapping
102 size_t m_BlendSplats;
103 // number of particles
104 size_t m_Particles;
105 };
106
107 struct Caps
108 {
109 bool m_VBO;
110 bool m_ARBProgram;
111 bool m_ARBProgramShadow;
112 bool m_VertexShader;
113 bool m_FragmentShader;
114 bool m_Shadows;
115 bool m_PrettyWater;
116 };
117
118public:
119 // constructor, destructor
120 CRenderer();
121 ~CRenderer();
122
123 // open up the renderer: performs any necessary initialisation
124 bool Open(int width,int height);
125
126 // resize renderer view
127 void Resize(int width,int height);
128
129 // return view width
130 int GetWidth() const { return m_Width; }
131 // return view height
132 int GetHeight() const { return m_Height; }
133 // return view aspect ratio
134 float GetAspect() const { return float(m_Width)/float(m_Height); }
135
136 // signal frame start
137 void BeginFrame();
138 // signal frame end
139 void EndFrame();
140
141 /**
142 * Should be called after each SwapBuffers call.
143 */
144 void OnSwapBuffers();
145
146 /**
147 * Set simulation context for rendering purposes.
148 * Must be called at least once when the game has started and before
149 * frames are rendered.
150 */
151 void SetSimulation(CSimulation2* simulation);
152
153 // set color used to clear screen in BeginFrame()
154 void SetClearColor(SColor4ub color);
155
156 // trigger a reload of shaders (when parameters they depend on have changed)
157 void MakeShadersDirty();
158
159 /**
160 * Set up the camera used for rendering the next scene; this includes
161 * setting OpenGL state like viewport, projection and modelview matrices.
162 *
163 * @param viewCamera this camera determines the eye position for rendering
164 * @param cullCamera this camera determines the frustum for culling in the renderer and
165 * for shadow calculations
166 */
167 void SetSceneCamera(const CCamera& viewCamera, const CCamera& cullCamera);
168
169 // set the viewport
170 void SetViewport(const SViewPort &);
171
172 // get the last viewport
173 SViewPort GetViewport();
174
175 /**
176 * Render the given scene immediately.
177 * @param scene a Scene object describing what should be rendered.
178 */
179 void RenderScene(Scene& scene);
180
181 /**
182 * Return the scene that is currently being rendered.
183 * Only valid when the renderer is in a RenderScene call.
184 */
185 Scene& GetScene();
186
187 /**
188 * Render text overlays on top of the scene.
189 * Assumes the caller has set up the GL environment for orthographic rendering
190 * with texturing and blending.
191 */
192 void RenderTextOverlays();
193
194 // set the current lighting environment; (note: the passed pointer is just copied to a variable within the renderer,
195 // so the lightenv passed must be scoped such that it is not destructed until after the renderer is no longer rendering)
196 void SetLightEnv(CLightEnv* lightenv)
197 {
198 m_LightEnv = lightenv;
199 }
200
201 // set the mode to render subsequent terrain patches
202 void SetTerrainRenderMode(ERenderMode mode) { m_TerrainRenderMode = mode; }
203 // get the mode to render subsequent terrain patches
204 ERenderMode GetTerrainRenderMode() const { return m_TerrainRenderMode; }
205
206 // set the mode to render subsequent water patches
207 void SetWaterRenderMode(ERenderMode mode) { m_WaterRenderMode = mode; }
208 // get the mode to render subsequent water patches
209 ERenderMode GetWaterRenderMode() const { return m_WaterRenderMode; }
210
211 // set the mode to render subsequent models
212 void SetModelRenderMode(ERenderMode mode) { m_ModelRenderMode = mode; }
213 // get the mode to render subsequent models
214 ERenderMode GetModelRenderMode() const { return m_ModelRenderMode; }
215
216 // Get the mode to render subsequent overlays.
217 ERenderMode GetOverlayRenderMode() const { return m_OverlayRenderMode; }
218 // Set the mode to render subsequent overlays.
219 void SetOverlayRenderMode(ERenderMode mode) { m_OverlayRenderMode = mode; }
220
221 // debugging
222 void SetDisplayTerrainPriorities(bool enabled) { m_DisplayTerrainPriorities = enabled; }
223
224 // bind a GL texture object to active unit
225 void BindTexture(int unit, unsigned int tex);
226
227 // load the default set of alphamaps.
228 // return a negative error code if anything along the way fails.
229 // called via delay-load mechanism.
230 int LoadAlphaMaps();
231 void UnloadAlphaMaps();
232
233 // return stats accumulated for current frame
234 Stats& GetStats() { return m_Stats; }
235
236 // return the current light environment
237 const CLightEnv &GetLightEnv() { return *m_LightEnv; }
238
239 // return the current view camera
240 const CCamera& GetViewCamera() const { return m_ViewCamera; }
241 // replace the current view camera
242 void SetViewCamera(const CCamera& camera) { m_ViewCamera = camera; }
243
244 // return the current cull camera
245 const CCamera& GetCullCamera() const { return m_CullCamera; }
246
247 /**
248 * GetWaterManager: Return the renderer's water manager.
249 *
250 * @return the WaterManager object used by the renderer
251 */
252 WaterManager* GetWaterManager() { return m_WaterManager; }
253
254 /**
255 * GetSkyManager: Return the renderer's sky manager.
256 *
257 * @return the SkyManager object used by the renderer
258 */
259 SkyManager* GetSkyManager() { return m_SkyManager; }
260
261 CTextureManager& GetTextureManager();
262
263 CShaderManager& GetShaderManager();
264
265 CParticleManager& GetParticleManager();
266
267 TerrainRenderer& GetTerrainRenderer();
268
269 CMaterialManager& GetMaterialManager();
270
271 CFontManager& GetFontManager();
272
273 CShaderDefines GetSystemShaderDefines() { return m_SystemShaderDefines; }
274
275 CTimeManager& GetTimeManager();
276
277 CPostprocManager& GetPostprocManager();
278
279 /**
280 * GetCapabilities: Return which OpenGL capabilities are available and enabled.
281 *
282 * @return capabilities structure
283 */
284 const Caps& GetCapabilities() const { return m_Caps; }
285
286 ShadowMap& GetShadowMap();
287
288 /**
289 * Resets the render state to default, that was before a game started
290 */
291 void ResetState();
292
293 /**
294 * m_SkipSubmit: Disable the actual submission of rendering commands to OpenGL.
295 * All state setup is still performed as usual.
296 */
297 bool DoSkipSubmit() const { return m_SkipSubmit; }
298
299protected:
300 friend struct CRendererInternals;
301 friend class CVertexBuffer;
302 friend class CPatchRData;
303 friend class CDecalRData;
304 friend class FixedFunctionModelRenderer;
305 friend class ModelRenderer;
306 friend class PolygonSortModelRenderer;
307 friend class SortModelRenderer;
308 friend class RenderPathVertexShader;
309 friend class HWLightingModelRenderer;
310 friend class ShaderModelVertexRenderer;
311 friend class InstancingModelRenderer;
312 friend class ShaderInstancingModelRenderer;
313 friend class TerrainRenderer;
314 friend class WaterRenderer;
315 friend class CRenderingOptions;
316
317 //BEGIN: Implementation of SceneCollector
318 void Submit(CPatch* patch);
319 void Submit(SOverlayLine* overlay);
320 void Submit(SOverlayTexturedLine* overlay);
321 void Submit(SOverlaySprite* overlay);
322 void Submit(SOverlayQuad* overlay);
323 void Submit(CModelDecal* decal);
324 void Submit(CParticleEmitter* emitter);
325 void Submit(SOverlaySphere* overlay);
326 void SubmitNonRecursive(CModel* model);
327 //END: Implementation of SceneCollector
328
329 // render any batched objects
330 void RenderSubmissions(const CBoundingBoxAligned& waterScissor);
331
332 // patch rendering stuff
333 void RenderPatches(const CShaderDefines& context, int cullGroup);
334
335 // model rendering stuff
336 void RenderModels(const CShaderDefines& context, int cullGroup);
337 void RenderTransparentModels(const CShaderDefines& context, int cullGroup, ETransparentMode transparentMode, bool disableFaceCulling);
338
339 void RenderSilhouettes(const CShaderDefines& context);
340
341 void RenderParticles(int cullGroup);
342
343 // shadow rendering stuff
344 void RenderShadowMap(const CShaderDefines& context);
345
346 // render water reflection and refraction textures
347 void RenderReflections(const CShaderDefines& context, const CBoundingBoxAligned& scissor);
348 void RenderRefractions(const CShaderDefines& context, const CBoundingBoxAligned& scissor);
349
350 void ComputeReflectionCamera(CCamera& camera, const CBoundingBoxAligned& scissor) const;
351 void ComputeRefractionCamera(CCamera& camera, const CBoundingBoxAligned& scissor) const;
352
353 // debugging
354 void DisplayFrustum();
355
356 // enable oblique frustum clipping with the given clip plane
357 void SetObliqueFrustumClipping(CCamera& camera, const CVector4D& clipPlane) const;
358
359 void SetRenderPath(RenderPath rp);
360
361 void ReloadShaders();
362 void RecomputeSystemShaderDefines();
363
364 // hotloading
365 static Status ReloadChangedFileCB(void* param, const VfsPath& path);
366
367 // RENDERER DATA:
368 /// Private data that is not needed by inline functions
369 CRendererInternals* m;
370 // view width
371 int m_Width;
372 // view height
373 int m_Height;
374
375 // Current terrain rendering mode.
376 ERenderMode m_TerrainRenderMode;
377 // Current water rendering mode.
378 ERenderMode m_WaterRenderMode;
379 // Current model rendering mode.
380 ERenderMode m_ModelRenderMode;
381 // Current overlay rendering mode.
382 ERenderMode m_OverlayRenderMode;
383
384 CShaderDefines m_SystemShaderDefines;
385
386 SViewPort m_Viewport;
387
388 /**
389 * m_ViewCamera: determines the eye position for rendering
390 *
391 * @see CGameView::m_ViewCamera
392 */
393 CCamera m_ViewCamera;
394
395 /**
396 * m_CullCamera: determines the frustum for culling and shadowmap calculations
397 *
398 * @see CGameView::m_ViewCamera
399 */
400 CCamera m_CullCamera;
401
402 // only valid inside a call to RenderScene
403 Scene* m_CurrentScene;
404 int m_CurrentCullGroup;
405
406 // color used to clear screen in BeginFrame
407 float m_ClearColor[4];
408 // current lighting setup
409 CLightEnv* m_LightEnv;
410 // ogl_tex handle of composite alpha map (all the alpha maps packed into one texture)
411 Handle m_hCompositeAlphaMap;
412 // coordinates of each (untransformed) alpha map within the packed texture
413 struct {
414 float u0,u1,v0,v1;
415 } m_AlphaMapCoords[NumAlphaMaps];
416 // card capabilities
417 Caps m_Caps;
418 // build card cap bits
419 void EnumCaps();
420 // per-frame renderer stats
421 Stats m_Stats;
422
423 /**
424 * m_WaterManager: the WaterManager object used for water textures and settings
425 * (e.g. water color, water height)
426 */
427 WaterManager* m_WaterManager;
428
429 /**
430 * m_SkyManager: the SkyManager object used for sky textures and settings
431 */
432 SkyManager* m_SkyManager;
433
434 /**
435 * Enable rendering of terrain tile priority text overlay, for debugging.
436 */
437 bool m_DisplayTerrainPriorities;
438
439 bool m_SkipSubmit;
440};
441
442#endif // INCLUDED_RENDERER
Note: See TracBrowser for help on using the repository browser.