Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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"
25 #include "graphics/ShaderProgram.h"
26 #include "renderer/ShadowMap.h"
27 #include "VertexBufferManager.h"
28 
29 class CPatch;
30 class CSimulation2;
32 class CTextRenderer;
33 
34 //////////////////////////////////////////////////////////////////////////////////////////////////
35 // CPatchRData: class encapsulating logic for rendering terrain patches; holds per
36 // patch data, plus some supporting static functions for batching, etc
37 class CPatchRData : public CRenderData
38 {
39 public:
40  CPatchRData(CPatch* patch, CSimulation2* simulation);
41  ~CPatchRData();
42 
43  void Update(CSimulation2* simulation);
44  void RenderOutline();
45  void RenderSides(CShaderProgramPtr& shader);
46  void RenderPriorities(CTextRenderer& textRenderer);
47 
48  void RenderWater(CShaderProgramPtr& shader);
49 
50  static void RenderBases(const std::vector<CPatchRData*>& patches, const CShaderDefines& context,
51  ShadowMap* shadow, bool isDummyShader=false, const CShaderProgramPtr& dummy=CShaderProgramPtr());
52  static void RenderBlends(const std::vector<CPatchRData*>& patches, const CShaderDefines& context,
53  ShadowMap* shadow, bool isDummyShader=false, const CShaderProgramPtr& dummy=CShaderProgramPtr());
54  static void RenderStreams(const std::vector<CPatchRData*>& patches, const CShaderProgramPtr& shader, int streamflags);
55 
56  CPatch* GetPatch() { return m_Patch; }
57 
58  static void PrepareShader(const CShaderProgramPtr& shader, ShadowMap* shadow);
59 
61 
62 private:
63  friend struct SBlendStackItem;
64 
65  struct SSplat {
67 
68  // texture to apply during splat
70  // offset into the index array for this patch where splat starts
71  size_t m_IndexStart;
72  // number of indices used by splat
73  size_t m_IndexCount;
74  };
75 
76  struct SBaseVertex {
77  // vertex position
79  // diffuse color from sunlight
82  };
83  cassert(sizeof(SBaseVertex) == 28);
84 
85  struct SSideVertex {
86  // vertex position
88  // add some padding
90  };
91  cassert(sizeof(SSideVertex) == 16);
92 
93  struct SBlendVertex {
94  // vertex position
96  // diffuse color from sunlight
98  // vertex uvs for alpha texture
99  float m_AlphaUVs[2];
101  };
102  cassert(sizeof(SBlendVertex) == 36);
103 
104  // Mixed Fancy/Simple water vertex description data structure
105  struct SWaterVertex {
106  // vertex position
108  // (p,q,r, a) where
109  // p*255 + q*-255 + r = depth of water
110  // a = depth-dependent alpha
113  };
114  cassert(sizeof(SWaterVertex) == 32);
115 
116  // build this renderdata object
117  void Build();
118 
119  void AddBlend(std::vector<SBlendVertex>& blendVertices, std::vector<u16>& blendIndices,
120  u16 i, u16 j, u8 shape, CTerrainTextureEntry* texture);
121 
122  void BuildBlends();
123  void BuildIndices();
124  void BuildVertices();
125  void BuildSides();
126 
127  void BuildSide(std::vector<SSideVertex>& vertices, CPatchSideFlags side);
128 
129  // owner patch
131 
132  // vertex buffer handle for side vertices
134 
135  // vertex buffer handle for base vertices
137 
138  // vertex buffer handle for base vertex indices
140 
141  // vertex buffer handle for blend vertices
143 
144  // vertex buffer handle for blend vertex indices
146 
147  // list of base splats to apply to this patch
148  std::vector<SSplat> m_Splats;
149 
150  // splats used in blend pass
151  std::vector<SSplat> m_BlendSplats;
152 
153  // boundary of water in this patch
155 
156  // Water vertex buffer
158 
159  // Water indices buffer
161 
163 
164  // Build water vertices and indices (vertex buffer and data vector)
165  void BuildWater();
166 
167  // parameter allowing a varying number of triangles per patch for LOD
168  // MUST be an exact divisor of PATCH_SIZE
169  // compiled const for the moment until/if dynamic water LOD is offered
170  // savings would be mostly beneficial for GPU or simple water
171  static const ssize_t water_cell_size = 1;
172 };
173 
174 #endif
static const ssize_t water_cell_size
Definition: PatchRData.h:171
void BuildWater()
#define u8
Definition: types.h:39
static void RenderBases(const std::vector< CPatchRData * > &patches, const CShaderDefines &context, ShadowMap *shadow, bool isDummyShader=false, const CShaderProgramPtr &dummy=CShaderProgramPtr())
Definition: PatchRData.cpp:721
void RenderWater(CShaderProgramPtr &shader)
const CBoundingBoxAligned & GetWaterBounds() const
Definition: PatchRData.h:60
CBoundingBoxAligned m_WaterBounds
Definition: PatchRData.h:154
void BuildBlends()
Definition: PatchRData.cpp:143
void BuildVertices()
Definition: PatchRData.cpp:522
static void PrepareShader(const CShaderProgramPtr &shader, ShadowMap *shadow)
CPatchRData(CPatch *patch, CSimulation2 *simulation)
Definition: PatchRData.cpp:62
CPatch * GetPatch()
Definition: PatchRData.h:56
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:160
VBChunk: describes a portion of this vertex buffer.
Definition: VertexBuffer.h:47
void Build()
Definition: PatchRData.cpp:653
CVertexBuffer::VBChunk * m_VBBlends
Definition: PatchRData.h:142
CPatchSideFlags
Definition: Patch.h:38
void AddBlend(std::vector< SBlendVertex > &blendVertices, std::vector< u16 > &blendIndices, u16 i, u16 j, u8 shape, CTerrainTextureEntry *texture)
Definition: PatchRData.cpp:314
Public API for simulation system.
Definition: Simulation2.h:46
void BuildSide(std::vector< SSideVertex > &vertices, CPatchSideFlags side)
Definition: PatchRData.cpp:572
std::vector< SSplat > m_BlendSplats
Definition: PatchRData.h:151
void Update(CSimulation2 *simulation)
Definition: PatchRData.cpp:662
Definition: Patch.h:48
CTerrainTextureEntry * m_Texture
Definition: PatchRData.h:69
void RenderSides(CShaderProgramPtr &shader)
intptr_t ssize_t
Definition: wposix_types.h:82
static void RenderBlends(const std::vector< CPatchRData * > &patches, const CShaderDefines &context, ShadowMap *shadow, bool isDummyShader=false, const CShaderProgramPtr &dummy=CShaderProgramPtr())
Definition: PatchRData.cpp:898
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
#define u16
Definition: types.h:40
void BuildIndices()
Definition: PatchRData.cpp:428
CVertexBuffer::VBChunk * m_VBBase
Definition: PatchRData.h:136
#define u32
Definition: types.h:41
CVertexBuffer::VBChunk * m_VBBlendIndices
Definition: PatchRData.h:145
std::vector< SSplat > m_Splats
Definition: PatchRData.h:148
void RenderPriorities(CTextRenderer &textRenderer)
CVertexBuffer::VBChunk * m_VBSides
Definition: PatchRData.h:133
void RenderOutline()
CSimulation2 * m_Simulation
Definition: PatchRData.h:162
void BuildSides()
Definition: PatchRData.cpp:618
CVertexBuffer::VBChunk * m_VBWater
Definition: PatchRData.h:157
CVertexBuffer::VBChunk * m_VBBaseIndices
Definition: PatchRData.h:139
static void RenderStreams(const std::vector< CPatchRData * > &patches, const CShaderProgramPtr &shader, int streamflags)
Helper structure for RenderBlends.
Definition: PatchRData.cpp:884
shared_ptr< CShaderProgram > CShaderProgramPtr
cassert(sizeof(SBaseVertex)==28)
CPatch * m_Patch
Definition: PatchRData.h:130