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
40 class CMaterialManager
;
42 class CParticleManager
;
44 class CPostprocManager
;
47 class CTextureManager
;
49 class RenderPathVertexShader
;
50 class ScriptInterface
;
53 class TerrainRenderer
;
57 enum ERenderMode
{ WIREFRAME
, SOLID
, EDGED_FACES
};
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
;
70 public Singleton
<CRenderer
>,
71 private SceneCollector
74 // various enumerations and renderer related constants
75 enum { NumAlphaMaps
=14 };
80 OPT_WATERFANCYEFFECTS
,
100 CULL_SILHOUETTE_OCCLUDER
,
101 CULL_SILHOUETTE_CASTER
,
106 // If no rendering path is configured explicitly, the renderer
107 // will choose the path when Open() is called.
110 // Classic fixed function.
113 // Use new ARB/GLSL system
117 // stats class - per frame counts of number of draw calls, poly counts etc
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
123 // number of terrain triangles drawn
124 size_t m_TerrainTris
;
125 // number of water triangles drawn
127 // number of (non-transparent) model triangles drawn
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
143 bool m_WaterFancyEffects
;
144 bool m_WaterRealDepth
;
145 bool m_WaterRefraction
;
146 bool m_WaterReflection
;
149 RenderPath m_RenderPath
;
150 bool m_ShadowAlphaFix
;
151 bool m_ARBProgramShadow
;
155 bool m_ForceAlphaTest
;
161 bool m_DisplayFrustum
;
167 bool m_ARBProgramShadow
;
169 bool m_FragmentShader
;
175 // constructor, destructor
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
);
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
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
);
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.
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
) {
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
; }
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.
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
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;
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();
409 static Status
ReloadChangedFileCB(void* param
, const VfsPath
& path
);
412 /// Private data that is not needed by inline functions
413 CRendererInternals
* m
;
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
456 } m_AlphaMapCoords
[NumAlphaMaps
];
459 // build card cap bits
461 // per-frame renderer 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
;
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.
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
495 * Can be accessed via JS as renderer.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