Pyrogenesis  trunk
PatchRData.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 <vector>
22 #include "graphics/SColor.h"
23 #include "maths/Vector3D.h"
24 #include "maths/Vector2D.h"
27 #include "VertexBufferManager.h"
28 
29 class CPatch;
30 class CSimulation2;
32 class CTextRenderer;
33 class ShadowMap;
34 
35 //////////////////////////////////////////////////////////////////////////////////////////////////
36 // CPatchRData: class encapsulating logic for rendering terrain patches; holds per
37 // patch data, plus some supporting static functions for batching, etc
38 class CPatchRData : public CRenderData
39 {
40 public:
41  CPatchRData(CPatch* patch, CSimulation2* simulation);
42  ~CPatchRData();
43 
44  void Update(CSimulation2* simulation);
45  void RenderOutline();
46  void RenderSides(CShaderProgramPtr& shader);
47  void RenderPriorities(CTextRenderer& textRenderer);
48 
49  void RenderWater(CShaderProgramPtr& shader, bool onlyShore = false, bool fixedPipeline = false);
50 
51  static void RenderBases(const std::vector<CPatchRData*>& patches, const CShaderDefines& context,
52  ShadowMap* shadow, bool isDummyShader=false, const CShaderProgramPtr& dummy=CShaderProgramPtr());
53  static void RenderBlends(const std::vector<CPatchRData*>& patches, const CShaderDefines& context,
54  ShadowMap* shadow, bool isDummyShader=false, const CShaderProgramPtr& dummy=CShaderProgramPtr());
55  static void RenderStreams(const std::vector<CPatchRData*>& patches, const CShaderProgramPtr& shader, int streamflags);
56 
57  CPatch* GetPatch() { return m_Patch; }
58 
59  static void PrepareShader(const CShaderProgramPtr& shader, ShadowMap* shadow);
60 
62 
63 private:
64  friend struct SBlendStackItem;
65 
66  struct SSplat {
68 
69  // texture to apply during splat
71  // offset into the index array for this patch where splat starts
72  size_t m_IndexStart;
73  // number of indices used by splat
74  size_t m_IndexCount;
75  };
76 
77  struct SBaseVertex {
78  // vertex position
80  // diffuse color from sunlight
83  // pad to a power of two
84  u8 m_Padding[4];
85  };
86  cassert(sizeof(SBaseVertex) == 32);
87 
88  struct SSideVertex {
89  // vertex position
91  // pad to a power of two
92  u8 m_Padding[4];
93  };
94  cassert(sizeof(SSideVertex) == 16);
95 
96  struct SBlendVertex {
97  // vertex position
99  // diffuse color from sunlight
101  // vertex uvs for alpha texture
102  float m_AlphaUVs[2];
104  // pad to a power of two
105  u8 m_Padding[28];
106  };
107  cassert(sizeof(SBlendVertex) == 64);
108 
109  // Mixed Fancy/Simple water vertex description data structure
110  struct SWaterVertex {
111  // vertex position
114  // pad to a power of two
115  u8 m_Padding[12];
116  };
117  cassert(sizeof(SWaterVertex) == 32);
118 
119  // build this renderdata object
120  void Build();
121 
122  void AddBlend(std::vector<SBlendVertex>& blendVertices, std::vector<u16>& blendIndices,
123  u16 i, u16 j, u8 shape, CTerrainTextureEntry* texture);
124 
125  void BuildBlends();
126  void BuildIndices();
127  void BuildVertices();
128  void BuildSides();
129 
130  void BuildSide(std::vector<SSideVertex>& vertices, CPatchSideFlags side);
131 
132  // owner patch
134 
135  // vertex buffer handle for side vertices
137 
138  // vertex buffer handle for base vertices
140 
141  // vertex buffer handle for base vertex indices
143 
144  // vertex buffer handle for blend vertices
146 
147  // vertex buffer handle for blend vertex indices
149 
150  // list of base splats to apply to this patch
151  std::vector<SSplat> m_Splats;
152 
153  // splats used in blend pass
154  std::vector<SSplat> m_BlendSplats;
155 
156  // boundary of water in this patch
158 
159  // Water vertex buffer
162 
163  // Water indices buffer
166 
168 
169  // Build water vertices and indices (vertex buffer and data vector)
170  void BuildWater();
171 
172  // parameter allowing a varying number of triangles per patch for LOD
173  // MUST be an exact divisor of PATCH_SIZE
174  // compiled const for the moment until/if dynamic water LOD is offered
175  // savings would be mostly beneficial for GPU or simple water
176  static const ssize_t water_cell_size = 1;
177 };
178 
179 #endif
static const ssize_t water_cell_size
Definition: PatchRData.h:176
CVector3D m_Normal
Definition: PatchRData.h:82
void BuildWater()
Definition: PatchRData.cpp:1280
static void RenderBases(const std::vector< CPatchRData * > &patches, const CShaderDefines &context, ShadowMap *shadow, bool isDummyShader=false, const CShaderProgramPtr &dummy=CShaderProgramPtr())
Definition: PatchRData.cpp:727
const CBoundingBoxAligned & GetWaterBounds() const
Definition: PatchRData.h:61
CBoundingBoxAligned m_WaterBounds
Definition: PatchRData.h:157
void BuildBlends()
Definition: PatchRData.cpp:146
uint16_t u16
Definition: types.h:38
void BuildVertices()
Definition: PatchRData.cpp:528
static void PrepareShader(const CShaderProgramPtr &shader, ShadowMap *shadow)
SSplat()
Definition: PatchRData.h:67
Definition: Vector3D.h:28
CVertexBuffer::VBChunk * m_VBWaterIndicesShore
Definition: PatchRData.h:165
Definition: PatchRData.h:96
SColor4ub m_DiffuseColor
Definition: PatchRData.h:81
CPatchRData(CPatch *patch, CSimulation2 *simulation)
Definition: PatchRData.cpp:62
CPatch * GetPatch()
Definition: PatchRData.h:57
Definition: TerrainTextureEntry.h:38
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup, including matrix calculations.
Definition: ShadowMap.h:39
CVertexBuffer::VBChunk * m_VBWaterIndices
Definition: PatchRData.h:164
VBChunk: describes a portion of this vertex buffer.
Definition: VertexBuffer.h:62
void Build()
Definition: PatchRData.cpp:659
CVertexBuffer::VBChunk * m_VBBlends
Definition: PatchRData.h:145
uint8_t u8
Definition: types.h:37
CPatchSideFlags
Definition: Patch.h:38
CVector3D m_Position
Definition: PatchRData.h:79
Definition: TextRenderer.h:28
void AddBlend(std::vector< SBlendVertex > &blendVertices, std::vector< u16 > &blendIndices, u16 i, u16 j, u8 shape, CTerrainTextureEntry *texture)
Definition: PatchRData.cpp:317
Public API for simulation system.
Definition: Simulation2.h:47
void BuildSide(std::vector< SSideVertex > &vertices, CPatchSideFlags side)
Definition: PatchRData.cpp:578
Definition: RenderableObject.h:41
cassert(sizeof(SBaseVertex)==32)
std::vector< SSplat > m_BlendSplats
Definition: PatchRData.h:154
Definition: PatchRData.h:77
CVector3D m_Position
Definition: PatchRData.h:90
void Update(CSimulation2 *simulation)
Definition: PatchRData.cpp:668
Definition: PatchRData.h:66
Definition: PatchRData.h:110
Definition: PatchRData.h:38
Definition: SColor.h:30
~CPatchRData()
Definition: PatchRData.cpp:76
Definition: Patch.h:48
CTerrainTextureEntry * m_Texture
Definition: PatchRData.h:70
CVertexBuffer::VBChunk * m_VBWaterShore
Definition: PatchRData.h:161
CVector2D m_WaterData
Definition: PatchRData.h:113
size_t m_IndexCount
Definition: PatchRData.h:74
void RenderSides(CShaderProgramPtr &shader)
Definition: PatchRData.cpp:1223
intptr_t ssize_t
Definition: wposix_types.h:82
size_t m_IndexStart
Definition: PatchRData.h:72
static void RenderBlends(const std::vector< CPatchRData * > &patches, const CShaderDefines &context, ShadowMap *shadow, bool isDummyShader=false, const CShaderProgramPtr &dummy=CShaderProgramPtr())
Definition: PatchRData.cpp:904
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:133
Definition: Vector2D.h:31
void BuildIndices()
Definition: PatchRData.cpp:431
CVertexBuffer::VBChunk * m_VBBase
Definition: PatchRData.h:139
CVector3D m_Normal
Definition: PatchRData.h:103
Definition: PatchRData.h:88
Definition: BoundingBoxAligned.h:35
CVertexBuffer::VBChunk * m_VBBlendIndices
Definition: PatchRData.h:148
std::vector< SSplat > m_Splats
Definition: PatchRData.h:151
void RenderPriorities(CTextRenderer &textRenderer)
Definition: PatchRData.cpp:1248
CVertexBuffer::VBChunk * m_VBSides
Definition: PatchRData.h:136
void RenderOutline()
Definition: PatchRData.cpp:1183
CSimulation2 * m_Simulation
Definition: PatchRData.h:167
void BuildSides()
Definition: PatchRData.cpp:624
CVertexBuffer::VBChunk * m_VBWater
Definition: PatchRData.h:160
CVertexBuffer::VBChunk * m_VBBaseIndices
Definition: PatchRData.h:142
static void RenderStreams(const std::vector< CPatchRData * > &patches, const CShaderProgramPtr &shader, int streamflags)
Definition: PatchRData.cpp:1116
Helper structure for RenderBlends.
Definition: PatchRData.cpp:890
SColor4ub m_DiffuseColor
Definition: PatchRData.h:100
CVector3D m_Position
Definition: PatchRData.h:112
void RenderWater(CShaderProgramPtr &shader, bool onlyShore=false, bool fixedPipeline=false)
Definition: PatchRData.cpp:1462
CPatch * m_Patch
Definition: PatchRData.h:133
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:25
CVector3D m_Position
Definition: PatchRData.h:98