Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
TerrainRenderer.h
Go to the documentation of this file.
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/*
19 * Terrain rendering (everything related to patches and water) is
20 * encapsulated in TerrainRenderer
21 */
22
23#ifndef INCLUDED_TERRAINRENDERER
24#define INCLUDED_TERRAINRENDERER
25
26#include "graphics/Color.h"
27
29class CCamera;
30class CCanvas2D;
31class CModelDecal;
32class CPatch;
33class CShaderDefines;
34class CSimulation2;
35class CVector2D;
36class ShadowMap;
37namespace Renderer::Backend { class IDeviceCommandContext; }
38namespace Renderer::Backend { class IShaderProgram; }
39namespace Renderer::Backend { class ITexture; }
41
42/**
43 * Class TerrainRenderer: Render everything related to the terrain,
44 * especially patches and water.
45 */
47{
48 friend class CPatchRData;
49 friend class CDecalRData;
50
51public:
54
55 void Initialize();
56
57 /**
58 * Set the simulation context for this frame.
59 * Call at start of frame, before any other Submits.
60 */
61 void SetSimulation(CSimulation2* simulation);
62
63 /**
64 * Submit: Add a patch for rendering in this frame.
65 *
66 * preconditions : PrepareForRendering must not have been called
67 * for this frame yet.
68 * The patch must not have been submitted in this frame yet (i.e. you
69 * can only submit a frame once).
70 *
71 * @param patch the patch
72 */
73 void Submit(int cullGroup, CPatch* patch);
74
75 /**
76 * Submit: Add a terrain decal for rendering in this frame.
77 */
78 void Submit(int cullGroup, CModelDecal* decal);
79
80 /**
81 * PrepareForRendering: Prepare internal data structures like vertex
82 * buffers for rendering.
83 *
84 * All patches must have been submitted before the call to
85 * PrepareForRendering.
86 * PrepareForRendering must be called before any rendering calls.
87 */
89
90 /**
91 * EndFrame: Remove all patches from the list of submitted patches.
92 */
93 void EndFrame();
94
95 /**
96 * Render textured terrain (including blends between
97 * different terrain types).
98 *
99 * preconditions : PrepareForRendering must have been called this
100 * frame before calling RenderTerrain.
101 *
102 * @param deviceCommandContext A context to submit commands.
103 * @param shadow A prepared shadow map, in case rendering with shadows is enabled.
104 */
106 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
107 const CShaderDefines& context, int cullGroup, ShadowMap* shadow,
108 const bool wireframe);
109
110 /**
111 * RenderPatches: Render all patches un-textured as polygons.
112 *
113 * preconditions : PrepareForRendering must have been called this
114 * frame before calling RenderPatches.
115 *
116 * @param filtered If true then only render objects that passed CullPatches.
117 * @param color Fill color of the patches.
118 */
119 void RenderPatches(
120 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
121 int cullGroup, const CShaderDefines& defines,
122 const CColor& color, const bool wireframe);
123
124 /**
125 * RenderOutlines: Render the outline of patches as lines.
126 *
127 * preconditions : PrepareForRendering must have been called this
128 * frame before calling RenderOutlines.
129 *
130 * @param filtered If true then only render objects that passed CullPatches.
131 */
132 void RenderOutlines(
133 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
134 int cullGroup);
135
136 /**
137 * RenderWater: Render water for all patches that have been submitted
138 * this frame.
139 *
140 * preconditions : PrepareForRendering must have been called this
141 * frame before calling RenderWater.
142 */
143 void RenderWater(
144 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
145 const CShaderDefines& context, int cullGroup, ShadowMap* shadow);
146
147 /**
148 * Renders terrain to a framebuffer to occlude shore foams.
149 */
151 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
152 int cullGroup);
153
154 /**
155 * Calculate a scissor rectangle for the visible water patches.
156 */
157 CBoundingBoxAligned ScissorWater(int cullGroup, const CCamera& camera);
158
159 /**
160 * Render priority text for all submitted patches, for debugging.
161 */
162 void RenderPriorities(CCanvas2D& canvas, int cullGroup);
163
164 /**
165 * Render texture unit 0 over the terrain mesh, with UV coords calculated
166 * by the given texture matrix.
167 * Intended for use by TerrainTextureOverlay.
168 */
170 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
171 int cullGroup, const CVector2D& textureTransform, Renderer::Backend::ITexture* texture);
172
173private:
175
176 /**
177 * RenderFancyWater: internal rendering method for fancy water.
178 * Returns false if unable to render with fancy water.
179 */
180 bool RenderFancyWater(
181 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
182 const CShaderDefines& context, int cullGroup, ShadowMap* shadow);
183
184 /**
185 * RenderSimpleWater: internal rendering method for water
186 */
188 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
189 int cullGroup);
190
191 static void PrepareShader(
192 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
194};
195
196#endif // INCLUDED_TERRAINRENDERER
Definition: BoundingBoxAligned.h:35
Definition: Camera.h:46
Definition: Canvas2D.h:36
Definition: DecalRData.h:37
Definition: Decal.h:54
Definition: PatchRData.h:46
Definition: Patch.h:50
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:153
Public API for simulation system.
Definition: Simulation2.h:57
Definition: Vector2D.h:32
Definition: IDeviceCommandContext.h:46
IShaderProgram is a container for multiple shaders of different types.
Definition: IShaderProgram.h:85
Definition: ITexture.h:33
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,...
Definition: ShadowMap.h:38
Class TerrainRenderer: Render everything related to the terrain, especially patches and water.
Definition: TerrainRenderer.h:47
void RenderPriorities(CCanvas2D &canvas, int cullGroup)
Render priority text for all submitted patches, for debugging.
Definition: TerrainRenderer.cpp:812
void Initialize()
Definition: TerrainRenderer.cpp:145
void RenderWater(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CShaderDefines &context, int cullGroup, ShadowMap *shadow)
RenderWater: Render water for all patches that have been submitted this frame.
Definition: TerrainRenderer.cpp:749
void EndFrame()
EndFrame: Remove all patches from the list of submitted patches.
Definition: TerrainRenderer.cpp:228
void RenderPatches(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, int cullGroup, const CShaderDefines &defines, const CColor &color, const bool wireframe)
RenderPatches: Render all patches un-textured as polygons.
Definition: TerrainRenderer.cpp:387
void SetSimulation(CSimulation2 *simulation)
Set the simulation context for this frame.
Definition: TerrainRenderer.cpp:176
void Submit(int cullGroup, CPatch *patch)
Submit: Add a patch for rendering in this frame.
Definition: TerrainRenderer.cpp:183
TerrainRendererInternals * m
Definition: TerrainRenderer.h:174
~TerrainRenderer()
Definition: TerrainRenderer.cpp:140
void PrepareForRendering()
PrepareForRendering: Prepare internal data structures like vertex buffers for rendering.
Definition: TerrainRenderer.cpp:219
void RenderWaterFoamOccluders(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, int cullGroup)
Renders terrain to a framebuffer to occlude shore foams.
Definition: TerrainRenderer.cpp:761
void RenderTerrainOverlayTexture(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, int cullGroup, const CVector2D &textureTransform, Renderer::Backend::ITexture *texture)
Render texture unit 0 over the terrain mesh, with UV coords calculated by the given texture matrix.
Definition: TerrainRenderer.cpp:241
bool RenderFancyWater(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CShaderDefines &context, int cullGroup, ShadowMap *shadow)
RenderFancyWater: internal rendering method for fancy water.
Definition: TerrainRenderer.cpp:487
void RenderOutlines(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, int cullGroup)
RenderOutlines: Render the outline of patches as lines.
Definition: TerrainRenderer.cpp:446
void RenderTerrainShader(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CShaderDefines &context, int cullGroup, ShadowMap *shadow, const bool wireframe)
Render textured terrain (including blends between different terrain types).
Definition: TerrainRenderer.cpp:332
CBoundingBoxAligned ScissorWater(int cullGroup, const CCamera &camera)
Calculate a scissor rectangle for the visible water patches.
Definition: TerrainRenderer.cpp:465
static void PrepareShader(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IShaderProgram *shader, ShadowMap *shadow)
Set up all the uniforms for a shader pass.
Definition: TerrainRenderer.cpp:304
TerrainRenderer()
Definition: TerrainRenderer.cpp:134
void RenderSimpleWater(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, int cullGroup)
RenderSimpleWater: internal rendering method for water.
Definition: TerrainRenderer.cpp:694
Definition: Canvas2D.h:30
Definition: Color.h:46
Struct TerrainRendererInternals: Internal variables used by the TerrainRenderer class.
Definition: TerrainRenderer.cpp:97