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