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"
25 #include "graphics/RenderableObject.h"
26 #include "graphics/ShaderProgramPtr.h"
27 #include "VertexBufferManager.h"
28 
29 class CPatch;
30 class CSimulation2;
31 class CTerrainTextureEntry;
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 
GetPatch()57 	CPatch* GetPatch() { return m_Patch; }
58 
59 	static void PrepareShader(const CShaderProgramPtr& shader, ShadowMap* shadow);
60 
GetWaterBounds()61 	const CBoundingBoxAligned& GetWaterBounds() const { return m_WaterBounds; }
62 
63 private:
64 	friend struct SBlendStackItem;
65 
66 	struct SSplat {
SSplatSSplat67 		SSplat() : m_Texture(0), m_IndexCount(0) {}
68 
69 		// texture to apply during splat
70 		CTerrainTextureEntry* m_Texture;
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
79 		CVector3D m_Position;
80 		// diffuse color from sunlight
81 		SColor4ub m_DiffuseColor;
82 		CVector3D m_Normal;
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
90 		CVector3D m_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
98 		CVector3D m_Position;
99 		// diffuse color from sunlight
100 		SColor4ub m_DiffuseColor;
101 		// vertex uvs for alpha texture
102 		float m_AlphaUVs[2];
103 		CVector3D m_Normal;
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
112 		CVector3D m_Position;
113 		CVector2D m_WaterData;
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
133 	CPatch* m_Patch;
134 
135 	// vertex buffer handle for side vertices
136 	CVertexBuffer::VBChunk* m_VBSides;
137 
138 	// vertex buffer handle for base vertices
139 	CVertexBuffer::VBChunk* m_VBBase;
140 
141 	// vertex buffer handle for base vertex indices
142 	CVertexBuffer::VBChunk* m_VBBaseIndices;
143 
144 	// vertex buffer handle for blend vertices
145 	CVertexBuffer::VBChunk* m_VBBlends;
146 
147 	// vertex buffer handle for blend vertex indices
148 	CVertexBuffer::VBChunk* m_VBBlendIndices;
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
157 	CBoundingBoxAligned m_WaterBounds;
158 
159 	// Water vertex buffer
160 	CVertexBuffer::VBChunk* m_VBWater;
161 	CVertexBuffer::VBChunk* m_VBWaterShore;
162 
163 	// Water indices buffer
164 	CVertexBuffer::VBChunk* m_VBWaterIndices;
165 	CVertexBuffer::VBChunk* m_VBWaterIndicesShore;
166 
167 	CSimulation2* m_Simulation;
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
180