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