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