Merge 'remotes/trunk'
[0ad.git] / source / renderer / Renderer.h
bloba72a41a721b0d27162abfc71448313bca210f470
1 /* Copyright (C) 2017 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * higher level interface on top of OpenGL to render basic objects:
20 * terrain, models, sprites, particles etc.
23 #ifndef INCLUDED_RENDERER
24 #define INCLUDED_RENDERER
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"
33 #include "graphics/ShaderDefines.h"
34 #include "renderer/Scene.h"
36 // necessary declarations
37 class CFontManager;
38 class CLightEnv;
39 class CMaterial;
40 class CMaterialManager;
41 class CModel;
42 class CParticleManager;
43 class CPatch;
44 class CPostprocManager;
45 class CShaderManager;
46 class CSimulation2;
47 class CTextureManager;
48 class CTimeManager;
49 class RenderPathVertexShader;
50 class ScriptInterface;
51 class ShadowMap;
52 class SkyManager;
53 class TerrainRenderer;
54 class WaterManager;
56 // rendering modes
57 enum ERenderMode { WIREFRAME, SOLID, EDGED_FACES };
59 // transparency modes
60 enum ETransparentMode { TRANSPARENT, TRANSPARENT_OPAQUE, TRANSPARENT_BLEND };
62 // access to sole renderer object
63 #define g_Renderer CRenderer::GetSingleton()
65 ///////////////////////////////////////////////////////////////////////////////////////////
66 // CRenderer: base renderer class - primary interface to the rendering engine
67 struct CRendererInternals;
69 class CRenderer :
70 public Singleton<CRenderer>,
71 private SceneCollector
73 public:
74 // various enumerations and renderer related constants
75 enum { NumAlphaMaps=14 };
76 enum Option {
77 OPT_NOVBO,
78 OPT_SHADOWS,
79 OPT_WATEREFFECTS,
80 OPT_WATERFANCYEFFECTS,
81 OPT_WATERREALDEPTH,
82 OPT_WATERREFLECTION,
83 OPT_WATERREFRACTION,
84 OPT_SHADOWSONWATER,
85 OPT_SHADOWPCF,
86 OPT_PARTICLES,
87 OPT_PREFERGLSL,
88 OPT_SILHOUETTES,
89 OPT_SHOWSKY,
90 OPT_SMOOTHLOS,
91 OPT_POSTPROC,
92 OPT_DISPLAYFRUSTUM,
95 enum CullGroup {
96 CULL_DEFAULT,
97 CULL_SHADOWS,
98 CULL_REFLECTIONS,
99 CULL_REFRACTIONS,
100 CULL_SILHOUETTE_OCCLUDER,
101 CULL_SILHOUETTE_CASTER,
102 CULL_MAX
105 enum RenderPath {
106 // If no rendering path is configured explicitly, the renderer
107 // will choose the path when Open() is called.
108 RP_DEFAULT,
110 // Classic fixed function.
111 RP_FIXED,
113 // Use new ARB/GLSL system
114 RP_SHADER
117 // stats class - per frame counts of number of draw calls, poly counts etc
118 struct Stats {
119 // set all stats to zero
120 void Reset() { memset(this, 0, sizeof(*this)); }
121 // number of draw calls per frame - total DrawElements + Begin/End immediate mode loops
122 size_t m_DrawCalls;
123 // number of terrain triangles drawn
124 size_t m_TerrainTris;
125 // number of water triangles drawn
126 size_t m_WaterTris;
127 // number of (non-transparent) model triangles drawn
128 size_t m_ModelTris;
129 // number of overlay triangles drawn
130 size_t m_OverlayTris;
131 // number of splat passes for alphamapping
132 size_t m_BlendSplats;
133 // number of particles
134 size_t m_Particles;
137 // renderer options
138 struct Options {
139 bool m_NoVBO;
140 bool m_Shadows;
142 bool m_WaterEffects;
143 bool m_WaterFancyEffects;
144 bool m_WaterRealDepth;
145 bool m_WaterRefraction;
146 bool m_WaterReflection;
147 bool m_WaterShadows;
149 RenderPath m_RenderPath;
150 bool m_ShadowAlphaFix;
151 bool m_ARBProgramShadow;
152 bool m_ShadowPCF;
153 bool m_Particles;
154 bool m_PreferGLSL;
155 bool m_ForceAlphaTest;
156 bool m_GPUSkinning;
157 bool m_Silhouettes;
158 bool m_SmoothLOS;
159 bool m_ShowSky;
160 bool m_Postproc;
161 bool m_DisplayFrustum;
162 } m_Options;
164 struct Caps {
165 bool m_VBO;
166 bool m_ARBProgram;
167 bool m_ARBProgramShadow;
168 bool m_VertexShader;
169 bool m_FragmentShader;
170 bool m_Shadows;
171 bool m_PrettyWater;
174 public:
175 // constructor, destructor
176 CRenderer();
177 ~CRenderer();
179 // open up the renderer: performs any necessary initialisation
180 bool Open(int width,int height);
182 // resize renderer view
183 void Resize(int width,int height);
185 // set/get boolean renderer option
186 void SetOptionBool(enum Option opt, bool value);
187 bool GetOptionBool(enum Option opt) const;
188 void SetRenderPath(RenderPath rp);
189 RenderPath GetRenderPath() const { return m_Options.m_RenderPath; }
190 static CStr GetRenderPathName(RenderPath rp);
191 static RenderPath GetRenderPathByName(const CStr& name);
193 // return view width
194 int GetWidth() const { return m_Width; }
195 // return view height
196 int GetHeight() const { return m_Height; }
197 // return view aspect ratio
198 float GetAspect() const { return float(m_Width)/float(m_Height); }
200 // signal frame start
201 void BeginFrame();
202 // signal frame end
203 void EndFrame();
206 * Set simulation context for rendering purposes.
207 * Must be called at least once when the game has started and before
208 * frames are rendered.
210 void SetSimulation(CSimulation2* simulation);
212 // set color used to clear screen in BeginFrame()
213 void SetClearColor(SColor4ub color);
215 // trigger a reload of shaders (when parameters they depend on have changed)
216 void MakeShadersDirty();
219 * Set up the camera used for rendering the next scene; this includes
220 * setting OpenGL state like viewport, projection and modelview matrices.
222 * @param viewCamera this camera determines the eye position for rendering
223 * @param cullCamera this camera determines the frustum for culling in the renderer and
224 * for shadow calculations
226 void SetSceneCamera(const CCamera& viewCamera, const CCamera& cullCamera);
228 // set the viewport
229 void SetViewport(const SViewPort &);
231 // get the last viewport
232 SViewPort GetViewport();
235 * Render the given scene immediately.
236 * @param scene a Scene object describing what should be rendered.
238 void RenderScene(Scene& scene);
241 * Return the scene that is currently being rendered.
242 * Only valid when the renderer is in a RenderScene call.
244 Scene& GetScene();
247 * Render text overlays on top of the scene.
248 * Assumes the caller has set up the GL environment for orthographic rendering
249 * with texturing and blending.
251 void RenderTextOverlays();
253 // set the current lighting environment; (note: the passed pointer is just copied to a variable within the renderer,
254 // so the lightenv passed must be scoped such that it is not destructed until after the renderer is no longer rendering)
255 void SetLightEnv(CLightEnv* lightenv) {
256 m_LightEnv=lightenv;
259 // set the mode to render subsequent terrain patches
260 void SetTerrainRenderMode(ERenderMode mode) { m_TerrainRenderMode = mode; }
261 // get the mode to render subsequent terrain patches
262 ERenderMode GetTerrainRenderMode() const { return m_TerrainRenderMode; }
264 // set the mode to render subsequent water patches
265 void SetWaterRenderMode(ERenderMode mode) { m_WaterRenderMode = mode; }
266 // get the mode to render subsequent water patches
267 ERenderMode GetWaterRenderMode() const { return m_WaterRenderMode; }
269 // set the mode to render subsequent models
270 void SetModelRenderMode(ERenderMode mode) { m_ModelRenderMode = mode; }
271 // get the mode to render subsequent models
272 ERenderMode GetModelRenderMode() const { return m_ModelRenderMode; }
274 // debugging
275 void SetDisplayTerrainPriorities(bool enabled) { m_DisplayTerrainPriorities = enabled; }
277 // bind a GL texture object to active unit
278 void BindTexture(int unit, unsigned int tex);
280 // load the default set of alphamaps.
281 // return a negative error code if anything along the way fails.
282 // called via delay-load mechanism.
283 int LoadAlphaMaps();
284 void UnloadAlphaMaps();
286 // return stats accumulated for current frame
287 Stats& GetStats() { return m_Stats; }
289 // return the current light environment
290 const CLightEnv &GetLightEnv() { return *m_LightEnv; }
292 // return the current view camera
293 const CCamera& GetViewCamera() const { return m_ViewCamera; }
294 // replace the current view camera
295 void SetViewCamera(const CCamera& camera) { m_ViewCamera = camera; }
297 // return the current cull camera
298 const CCamera& GetCullCamera() const { return m_CullCamera; }
301 * GetWaterManager: Return the renderer's water manager.
303 * @return the WaterManager object used by the renderer
305 WaterManager* GetWaterManager() { return m_WaterManager; }
308 * GetSkyManager: Return the renderer's sky manager.
310 * @return the SkyManager object used by the renderer
312 SkyManager* GetSkyManager() { return m_SkyManager; }
314 CTextureManager& GetTextureManager();
316 CShaderManager& GetShaderManager();
318 CParticleManager& GetParticleManager();
320 TerrainRenderer& GetTerrainRenderer();
322 CMaterialManager& GetMaterialManager();
324 CFontManager& GetFontManager();
326 CShaderDefines GetSystemShaderDefines() { return m_SystemShaderDefines; }
328 CTimeManager& GetTimeManager();
330 CPostprocManager& GetPostprocManager();
333 * GetCapabilities: Return which OpenGL capabilities are available and enabled.
335 * @return capabilities structure
337 const Caps& GetCapabilities() const { return m_Caps; }
339 ShadowMap& GetShadowMap();
342 * Resets the render state to default, that was before a game started
344 void ResetState();
346 protected:
347 friend struct CRendererInternals;
348 friend class CVertexBuffer;
349 friend class CPatchRData;
350 friend class CDecalRData;
351 friend class FixedFunctionModelRenderer;
352 friend class ModelRenderer;
353 friend class PolygonSortModelRenderer;
354 friend class SortModelRenderer;
355 friend class RenderPathVertexShader;
356 friend class HWLightingModelRenderer;
357 friend class ShaderModelVertexRenderer;
358 friend class InstancingModelRenderer;
359 friend class ShaderInstancingModelRenderer;
360 friend class TerrainRenderer;
361 friend class WaterRenderer;
363 //BEGIN: Implementation of SceneCollector
364 void Submit(CPatch* patch);
365 void Submit(SOverlayLine* overlay);
366 void Submit(SOverlayTexturedLine* overlay);
367 void Submit(SOverlaySprite* overlay);
368 void Submit(SOverlayQuad* overlay);
369 void Submit(CModelDecal* decal);
370 void Submit(CParticleEmitter* emitter);
371 void Submit(SOverlaySphere* overlay);
372 void SubmitNonRecursive(CModel* model);
373 //END: Implementation of SceneCollector
375 // render any batched objects
376 void RenderSubmissions(const CBoundingBoxAligned& waterScissor);
378 // patch rendering stuff
379 void RenderPatches(const CShaderDefines& context, int cullGroup);
381 // model rendering stuff
382 void RenderModels(const CShaderDefines& context, int cullGroup);
383 void RenderTransparentModels(const CShaderDefines& context, int cullGroup, ETransparentMode transparentMode, bool disableFaceCulling);
385 void RenderSilhouettes(const CShaderDefines& context);
387 void RenderParticles(int cullGroup);
389 // shadow rendering stuff
390 void RenderShadowMap(const CShaderDefines& context);
392 // render water reflection and refraction textures
393 void RenderReflections(const CShaderDefines& context, const CBoundingBoxAligned& scissor);
394 void RenderRefractions(const CShaderDefines& context, const CBoundingBoxAligned& scissor);
396 void ComputeReflectionCamera(CCamera& camera, const CBoundingBoxAligned& scissor) const;
397 void ComputeRefractionCamera(CCamera& camera, const CBoundingBoxAligned& scissor) const;
399 // debugging
400 void DisplayFrustum();
402 // enable oblique frustum clipping with the given clip plane
403 void SetObliqueFrustumClipping(CCamera& camera, const CVector4D& clipPlane) const;
405 void ReloadShaders();
406 void RecomputeSystemShaderDefines();
408 // hotloading
409 static Status ReloadChangedFileCB(void* param, const VfsPath& path);
411 // RENDERER DATA:
412 /// Private data that is not needed by inline functions
413 CRendererInternals* m;
414 // view width
415 int m_Width;
416 // view height
417 int m_Height;
418 // current terrain rendering mode
419 ERenderMode m_TerrainRenderMode;
420 // current water rendering mode
421 ERenderMode m_WaterRenderMode;
422 // current model rendering mode
423 ERenderMode m_ModelRenderMode;
425 CShaderDefines m_SystemShaderDefines;
427 SViewPort m_Viewport;
430 * m_ViewCamera: determines the eye position for rendering
432 * @see CGameView::m_ViewCamera
434 CCamera m_ViewCamera;
437 * m_CullCamera: determines the frustum for culling and shadowmap calculations
439 * @see CGameView::m_ViewCamera
441 CCamera m_CullCamera;
443 // only valid inside a call to RenderScene
444 Scene* m_CurrentScene;
445 int m_CurrentCullGroup;
447 // color used to clear screen in BeginFrame
448 float m_ClearColor[4];
449 // current lighting setup
450 CLightEnv* m_LightEnv;
451 // ogl_tex handle of composite alpha map (all the alpha maps packed into one texture)
452 Handle m_hCompositeAlphaMap;
453 // coordinates of each (untransformed) alpha map within the packed texture
454 struct {
455 float u0,u1,v0,v1;
456 } m_AlphaMapCoords[NumAlphaMaps];
457 // card capabilities
458 Caps m_Caps;
459 // build card cap bits
460 void EnumCaps();
461 // per-frame renderer stats
462 Stats m_Stats;
465 * m_WaterManager: the WaterManager object used for water textures and settings
466 * (e.g. water color, water height)
468 WaterManager* m_WaterManager;
471 * m_SkyManager: the SkyManager object used for sky textures and settings
473 SkyManager* m_SkyManager;
476 * Enable rendering of terrain tile priority text overlay, for debugging.
478 bool m_DisplayTerrainPriorities;
480 public:
482 * m_ShadowZBias: Z bias used when rendering shadows into a depth texture.
483 * This can be used to control shadowing artifacts.
485 * Can be accessed via JS as renderer.shadowZBias
486 * ShadowMap uses this for matrix calculation.
488 float m_ShadowZBias;
491 * m_ShadowMapSize: Size of shadow map, or 0 for default. Typically slow but useful
492 * for high-quality rendering. Changes don't take effect until the shadow map
493 * is regenerated.
495 * Can be accessed via JS as renderer.shadowMapSize
497 int m_ShadowMapSize;
500 * m_SkipSubmit: Disable the actual submission of rendering commands to OpenGL.
501 * All state setup is still performed as usual.
503 * Can be accessed via JS as renderer.skipSubmit
505 bool m_SkipSubmit;
509 #endif