Line data Source code
1 : /* Copyright (C) 2022 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_PATCHRDATA
19 : #define INCLUDED_PATCHRDATA
20 :
21 : #include "graphics/Patch.h"
22 : #include "graphics/RenderableObject.h"
23 : #include "graphics/ShaderProgramPtr.h"
24 : #include "maths/Vector2D.h"
25 : #include "maths/Vector3D.h"
26 : #include "renderer/backend/gl/DeviceCommandContext.h"
27 : #include "renderer/VertexBufferManager.h"
28 :
29 : #include <vector>
30 :
31 : class CPatch;
32 : class CShaderDefines;
33 : class CSimulation2;
34 : class CTerrainTextureEntry;
35 : class CTextRenderer;
36 : class ShadowMap;
37 :
38 : //////////////////////////////////////////////////////////////////////////////////////////////////
39 : // CPatchRData: class encapsulating logic for rendering terrain patches; holds per
40 : // patch data, plus some supporting static functions for batching, etc
41 0 : class CPatchRData : public CRenderData
42 : {
43 : public:
44 : CPatchRData(CPatch* patch, CSimulation2* simulation);
45 : ~CPatchRData();
46 :
47 : void Update(CSimulation2* simulation);
48 : void RenderOutline();
49 : void RenderPriorities(CTextRenderer& textRenderer);
50 :
51 : void RenderWaterSurface(
52 : Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
53 : const CShaderProgramPtr& shader, const bool bindWaterData);
54 : void RenderWaterShore(
55 : Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
56 : const CShaderProgramPtr& shader);
57 :
58 : CPatch* GetPatch() { return m_Patch; }
59 :
60 0 : const CBoundingBoxAligned& GetWaterBounds() const { return m_WaterBounds; }
61 :
62 : static void RenderBases(
63 : Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
64 : const std::vector<CPatchRData*>& patches, const CShaderDefines& context, ShadowMap* shadow);
65 : static void RenderBlends(
66 : Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
67 : const std::vector<CPatchRData*>& patches, const CShaderDefines& context, ShadowMap* shadow);
68 : static void RenderStreams(
69 : Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
70 : const std::vector<CPatchRData*>& patches, const CShaderProgramPtr& shader,
71 : const bool bindPositionAsTexCoord);
72 : static void RenderSides(
73 : Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
74 : const std::vector<CPatchRData*>& patches, const CShaderProgramPtr& shader);
75 :
76 : static void PrepareShader(const CShaderProgramPtr& shader, ShadowMap* shadow);
77 :
78 : private:
79 : friend struct SBlendStackItem;
80 :
81 : struct SSplat
82 : {
83 0 : SSplat() : m_Texture(0), m_IndexCount(0) {}
84 :
85 : // texture to apply during splat
86 : CTerrainTextureEntry* m_Texture;
87 : // offset into the index array for this patch where splat starts
88 : size_t m_IndexStart;
89 : // number of indices used by splat
90 : size_t m_IndexCount;
91 : };
92 :
93 0 : struct SBaseVertex
94 : {
95 : // vertex position
96 : CVector3D m_Position;
97 : CVector3D m_Normal;
98 : // pad to a power of two
99 : u8 m_Padding[8];
100 : };
101 : cassert(sizeof(SBaseVertex) == 32);
102 :
103 0 : struct SSideVertex
104 : {
105 : // vertex position
106 : CVector3D m_Position;
107 : // pad to a power of two
108 : u8 m_Padding[4];
109 : };
110 : cassert(sizeof(SSideVertex) == 16);
111 :
112 0 : struct SBlendVertex
113 : {
114 : // vertex position
115 : CVector3D m_Position;
116 : // vertex uvs for alpha texture
117 : float m_AlphaUVs[2];
118 : CVector3D m_Normal;
119 : };
120 : cassert(sizeof(SBlendVertex) == 32);
121 :
122 : // Mixed Fancy/Simple water vertex description data structure
123 0 : struct SWaterVertex
124 : {
125 : // vertex position
126 : CVector3D m_Position;
127 : CVector2D m_WaterData;
128 : // pad to a power of two
129 : u8 m_Padding[12];
130 : };
131 : cassert(sizeof(SWaterVertex) == 32);
132 :
133 : // build this renderdata object
134 : void Build();
135 :
136 : void AddBlend(std::vector<SBlendVertex>& blendVertices, std::vector<u16>& blendIndices,
137 : u16 i, u16 j, u8 shape, CTerrainTextureEntry* texture);
138 :
139 : void BuildBlends();
140 : void BuildIndices();
141 : void BuildVertices();
142 : void BuildSides();
143 :
144 : void BuildSide(std::vector<SSideVertex>& vertices, CPatchSideFlags side);
145 :
146 : // owner patch
147 : CPatch* m_Patch;
148 :
149 : // vertex buffer handle for side vertices
150 : CVertexBufferManager::Handle m_VBSides;
151 :
152 : // vertex buffer handle for base vertices
153 : CVertexBufferManager::Handle m_VBBase;
154 :
155 : // vertex buffer handle for base vertex indices
156 : CVertexBufferManager::Handle m_VBBaseIndices;
157 :
158 : // vertex buffer handle for blend vertices
159 : CVertexBufferManager::Handle m_VBBlends;
160 :
161 : // vertex buffer handle for blend vertex indices
162 : CVertexBufferManager::Handle m_VBBlendIndices;
163 :
164 : // list of base splats to apply to this patch
165 : std::vector<SSplat> m_Splats;
166 :
167 : // splats used in blend pass
168 : std::vector<SSplat> m_BlendSplats;
169 :
170 : // boundary of water in this patch
171 : CBoundingBoxAligned m_WaterBounds;
172 :
173 : // Water vertex buffer
174 : CVertexBufferManager::Handle m_VBWater;
175 : CVertexBufferManager::Handle m_VBWaterShore;
176 :
177 : // Water indices buffer
178 : CVertexBufferManager::Handle m_VBWaterIndices;
179 : CVertexBufferManager::Handle m_VBWaterIndicesShore;
180 :
181 : CSimulation2* m_Simulation;
182 :
183 : // Build water vertices and indices (vertex buffer and data vector)
184 : void BuildWater();
185 :
186 : // parameter allowing a varying number of triangles per patch for LOD
187 : // MUST be an exact divisor of PATCH_SIZE
188 : // compiled const for the moment until/if dynamic water LOD is offered
189 : // savings would be mostly beneficial for GPU or simple water
190 : static const ssize_t water_cell_size = 1;
191 : };
192 :
193 : #endif // INCLUDED_PATCHRDATA
|