map.cpp 10.2 KB
Newer Older
1
2
#include "map.h"

unknown's avatar
unknown committed
3
#include <pugixml/pugixml.hpp>
4
#include <core/res/resources.h>
unknown's avatar
unknown committed
5
6
7
#include <util/color.h>
#include <util/files.h>

8
#include <core/res/texture2d.h>
unknown's avatar
unknown committed
9
10
11
12
13
14

namespace glare
{
	namespace game
	{
		Map::Map(const fs::path &path)
Johannes Braun's avatar
Johannes Braun committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
		{
			pugi::xml_document document;
			document.load_file(path.c_str());

			auto map_node = document.child("map");
			m_version = map_node.attribute("version").as_string();

			auto height_node = map_node.child("height");
			m_heightmap_path = path.parent_path() / height_node.attribute("src").as_string();
			m_height_scale = height_node.attribute("scale").as_float(100);

			auto sector_style_node = map_node.child("sector-style");
			m_sector_size = sector_style_node.child("size").attribute("value").as_float(40);
			m_sector_border = sector_style_node.child("border").attribute("value").as_float(8);
			m_sector_uv_scale = sector_style_node.child("uv-scale").attribute("value").as_float(1);

			auto possession_node = map_node.child("possession");
			m_possessionmap_path = path.parent_path() / possession_node.attribute("src").as_string();
			for (const auto &child : possession_node.children())
			{
				unsigned team_id = child.attribute("id").as_uint();
36
				color::rgba8u mapto_color = color::hex<color::rgba8u>(child.attribute("mapto").as_string("#f"));
Johannes Braun's avatar
Johannes Braun committed
37
38
39
				m_team_to_pcolor.emplace(team_id, mapto_color);
			}

unknown's avatar
unknown committed
40
			auto data = core::textures::loadData<core::TextureDataRGBA_32F>(m_heightmap_path);
Johannes Braun's avatar
Johannes Braun committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

			m_width = unsigned(data->width());
			m_height = unsigned(data->height());

			m_heights.resize(data->width() + 1);

			for (int x = 0; x < int(data->width()) + 1; ++x) {
				m_heights[x].resize(int(data->height()) + 1);
				for (int y = 0; y < int(data->height()) + 1; ++y) {
					if (int(data->width()) == x || int(data->height()) == y) {
						m_heights[x][y] = data->get({ glm::max(0, x - 1), glm::max(y - 1, 0) }).r*m_height_scale;
					}
					else
					{
						m_heights[x][y] = data->get({ x, y }).r*m_height_scale;
					}
				}
unknown's avatar
unknown committed
58
59
60
61
62
63
64
65
			}
		}

		std::shared_ptr<core::MeshRenderer> Map::getMesh(bool rebuild)
		{
			if (!rebuild && m_mesh_renderer)
			{
				return m_mesh_renderer;
Johannes Braun's avatar
Johannes Braun committed
66
67
68
			}

			std::vector<math::Vertex> m_vertices;
unknown's avatar
unknown committed
69
			std::vector<unsigned> m_indices;
Johannes Braun's avatar
Johannes Braun committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

			const int v_count = 18;

			const float offset = m_sector_border / 2.f;
			const float uv_scaling = m_sector_uv_scale * (m_sector_border / (m_sector_size + m_sector_border));
			const float size = m_sector_size;

			math::Vertex vertex;
			for (int y = 0; y < int(m_height); ++y) {
				for (int x = 0; x < int(m_width); ++x) {

					const unsigned index_offset = (unsigned(m_width)*y + x) * v_count;

					//Base plateau
					{
						vertex.normal = { 0, 1, 0, 0 }; //up

						vertex.position = { size * x + (size / 2) - offset, m_heights[x][y], size * y - (size / 2) + offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ 0, size };
						m_vertices.push_back(vertex);

						vertex.position = { size * x - (size / 2) + offset, m_heights[x][y],  size * y - (size / 2) + offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ 0, 0 };
						m_vertices.push_back(vertex);

						vertex.position = { size * x - (size / 2) + offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, 0 };
						m_vertices.push_back(vertex);

						vertex.position = { size * x + (size / 2) - offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, size };
						m_vertices.push_back(vertex);

						m_indices.push_back(index_offset + 0);
						m_indices.push_back(index_offset + 1);
						m_indices.push_back(index_offset + 2);

						m_indices.push_back(index_offset + 2);
						m_indices.push_back(index_offset + 3);
						m_indices.push_back(index_offset + 0);
					}
					 
					// Connection on the upper side
					{
						float h1 = m_heights[x][y];
						float h2 = m_heights[x + 1][y];
						float delta = 2 * offset;

						float dy = delta;
						float dx = h1 - h2;
						vertex.normal = glm::normalize(glm::vec4{ dx, dy, 0, 0 });

						//4
						vertex.position = { size * x + (size / 2) - offset, m_heights[x][y], size * y - (size / 2) + offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ 0, size };
						m_vertices.push_back(vertex);

						//5
						vertex.position = { size * x + (size / 2) - offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, size };
						m_vertices.push_back(vertex);

						//6
						vertex.position = { size * (x + 1) - (size / 2) + offset, m_heights[x + 1][y],  size * y - (size / 2) + offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ 0, size + 2 * offset };
						m_vertices.push_back(vertex);

						//7
						vertex.position = { size * (x + 1) - (size / 2) + offset,  m_heights[x + 1][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, size + 2 * offset };
						m_vertices.push_back(vertex);

						m_indices.push_back(index_offset + 4);
						m_indices.push_back(index_offset + 5);
						m_indices.push_back(index_offset + 6);

						m_indices.push_back(index_offset + 6);
						m_indices.push_back(index_offset + 5);
						m_indices.push_back(index_offset + 7);
					}
					 
					// Connection on the right side
					{
						float h1 = m_heights[x][y];
						float h2 = m_heights[x][y + 1];
						float delta = 2 * offset;

						float dy = delta;
						float dz = h1 - h2;
						vertex.normal = glm::normalize(glm::vec4{ 0, dy, dz, 0 });
						//8
						vertex.position = { size * x - (size / 2) + offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, 0 };
						m_vertices.push_back(vertex);

						//9
						vertex.position = { size * x + (size / 2) - offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, size };
						m_vertices.push_back(vertex);

						//10
						vertex.position = { size * x + (size / 2) - offset, m_heights[x][y + 1], size * (y + 1) - (size / 2) + offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size + 2 * offset, size };
						m_vertices.push_back(vertex);

						//11
						vertex.position = { size * x - (size / 2) + offset, m_heights[x][y + 1],  size * (y + 1) - (size / 2) + offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size + 2 * offset,  0 };
						m_vertices.push_back(vertex);

						m_indices.push_back(index_offset + 9);
						m_indices.push_back(index_offset + 8);
						m_indices.push_back(index_offset + 11);

						m_indices.push_back(index_offset + 11);
						m_indices.push_back(index_offset + 10);
						m_indices.push_back(index_offset + 9);
					}

					// Connection in the upper right corner
					{

						auto p1 = glm::vec4{ size * (x + 1) - (size / 2) + offset,  m_heights[x + 1][y], size * y + (size / 2) - offset, 1 };
						auto p2 = glm::vec4{ size * x + (size / 2) - offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						auto p3 = glm::vec4{ size * (x + 1) - (size / 2) + offset, m_heights[x + 1][y + 1],  size * (y + 1) - (size / 2) + offset, 1 };

						vertex.normal = glm::vec4(glm::normalize(glm::cross(glm::vec3(p2 - p1), glm::vec3(p3 - p1))), 0);

						//12
						vertex.position = { size * (x + 1) - (size / 2) + offset,  m_heights[x + 1][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, size + 2 * offset };
						m_vertices.push_back(vertex);

						//13
						vertex.position = { size * x + (size / 2) - offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size, size };
						m_vertices.push_back(vertex);

						//14
						vertex.position = { size * (x + 1) - (size / 2) + offset, m_heights[x + 1][y + 1],  size * (y + 1) - (size / 2) + offset, 1 };
						vertex.texcoord = uv_scaling*glm::vec2{ size + 2 * offset, size + 2 * offset };
						m_vertices.push_back(vertex);

						m_indices.push_back(index_offset + 12);
						m_indices.push_back(index_offset + 13);
						m_indices.push_back(index_offset + 14);

						p1 = glm::vec4{ size * x + (size / 2) - offset,  m_heights[x][y], size * y + (size / 2) - offset, 1 };
						p2 = glm::vec4{ size * x + (size / 2) - offset, m_heights[x][y + 1], size * (y + 1) - (size / 2) + offset, 1 };
						p3 = glm::vec4{ size * (x + 1) - (size / 2) + offset, m_heights[x + 1][y + 1],  size * (y + 1) - (size / 2) + offset, 1 };

						vertex.normal = glm::vec4(glm::normalize(glm::cross(glm::vec3(p2 - p1), glm::vec3(p3 - p1))), 0);

						//15
						vertex.position = p1;
						vertex.texcoord = uv_scaling*glm::vec2{ size, size };
						m_vertices.push_back(vertex);

						//16
						vertex.position = p2;
						vertex.texcoord = uv_scaling*glm::vec2{ size + 2 * offset, size };
						m_vertices.push_back(vertex);

						// 17
						vertex.position = p3;
						vertex.texcoord = uv_scaling*glm::vec2{ size + 2 * offset, size + 2 * offset };
						m_vertices.push_back(vertex);

						m_indices.push_back(index_offset + 15);
						m_indices.push_back(index_offset + 16);
						m_indices.push_back(index_offset + 17);
					}

				}
			}

unknown's avatar
unknown committed
246
247
248
249
250
251
252
253
254
			auto mesh = std::make_shared<core::Mesh>(m_vertices, m_indices);

			m_mesh_renderer = std::make_shared<core::MeshRenderer>(mesh, makeMaterial());

			return m_mesh_renderer;
		}

		std::shared_ptr<core::Material> Map::makeMaterial()
		{
Johannes Braun's avatar
Johannes Braun committed
255
			std::shared_ptr<core::Material> material = std::make_shared<core::Material>("terrain");
256
257
			material->color_diffuse = color::preset::white.rgb;
			material->color_specular = 2 * color::preset::white.rgb;
Johannes Braun's avatar
Johannes Braun committed
258
			material->specular_exponent = 80;
259
260
261
262
			material->map_diffuse = core::Resources::getInstance().getTexture(files::asset("meshes/scenery/gravel_medium2048.png"));
			material->map_normal = core::Resources::getInstance().getTexture(files::asset("meshes/scenery/gravel_medium2048_n.png"));
			material->map_displacement = core::Resources::getInstance().getTexture(files::asset("meshes/scenery/gravel_medium2048_h.png"));
			material->map_specular = core::Resources::getInstance().getTexture(files::asset("meshes/scenery/gravel_medium2048_s.png"));
unknown's avatar
unknown committed
263
264
265
266
267
268
269
270
271
272
273
274

			return material;
		}

		glm::vec3 Map::getSectorCenter(unsigned x, unsigned z) const
		{
			float cx = x * (m_sector_size + m_sector_border) + m_sector_size / 2;
			float cz = z * (m_sector_size + m_sector_border) + m_sector_size / 2;
			return glm::vec3(cx, m_heights[x][z], cz);
		}
	}
}