summaryrefslogtreecommitdiff
path: root/game/terrain.cpp
blob: f3bec1e4cb54271189efdffdf9ccea1793763c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
#include "terrain.h"
#include "game/geoData.h"
#include "gfx/models/texture.h"
#include <algorithm>
#include <array>
#include <cache.h>
#include <cstddef>
#include <filesystem>
#include <gfx/gl/sceneShader.h>
#include <gfx/gl/shadowMapper.h>
#include <gfx/image.h>
#include <gfx/models/mesh.h>
#include <gfx/models/vertex.h>
#include <glm/glm.hpp>
#include <iterator>
#include <location.h>
#include <maths.h>
#include <utility>
#include <vector>

Terrain::Terrain(std::shared_ptr<GeoData> gd) :
	geoData {std::move(gd)}, grass {Texture::cachedTexture.get("grass.png")},
	water {Texture::cachedTexture.get("water.png")}
{
	generateMeshes();
}

void
Terrain::generateMeshes()
{
	std::vector<unsigned int> indices;
	const auto isize = geoData->getSize() - glm::uvec2 {1, 1};
	indices.reserve(static_cast<std::size_t>(isize.x * isize.y) * 6);

	const auto limit = geoData->getLimit();
	//  Indices
	constexpr std::array<glm::ivec2, 6> indices_offsets {{
			{0, 0},
			{1, 0},
			{1, 1},
			{0, 0},
			{1, 1},
			{0, 1},
	}};
	for (auto y = limit.first.y; y < limit.second.y; y += 1) {
		for (auto x = limit.first.x; x < limit.second.x; x += 1) {
			std::transform(indices_offsets.begin(), indices_offsets.end(), std::back_inserter(indices),
					[this, x, y](const auto off) {
						return geoData->at(x + off.x, y + off.y);
					});
		}
	}

	const auto nodes = geoData->getNodes();
	const auto scale = geoData->getScale();
	std::vector<Vertex> vertices;
	vertices.reserve(nodes.size());
	// Positions
	for (auto y = limit.first.y; y <= limit.second.y; y += 1) {
		for (auto x = limit.first.x; x <= limit.second.x; x += 1) {
			const glm::vec2 xy {x, y};
			vertices.emplace_back((xy * scale) ^ nodes[geoData->at(x, y)].height, xy, ::up);
		}
	}
	// Normals
	const glm::uvec2 size = geoData->getSize();
	for (auto y = limit.first.y + 1; y < limit.second.y; y += 1) {
		for (auto x = limit.first.x + 1; x < limit.second.x; x += 1) {
			const auto n {geoData->at(x, y)};
			const auto a = vertices[n - 1].pos;
			const auto b = vertices[n - size.x].pos;
			const auto c = vertices[n + 1].pos;
			const auto d = vertices[n + size.x].pos;
			vertices[n].normal = -glm::normalize(glm::cross(b - d, a - c));
		}
	}
	meshes.create<Mesh>(vertices, indices);
}

void
Terrain::tick(TickDuration dur)
{
	waveCycle += dur.count();
}

void
Terrain::render(const SceneShader & shader) const
{
	shader.landmass.use();
	grass->bind();
	meshes.apply(&Mesh::Draw);

	shader.water.use(waveCycle);
	water->bind();
	meshes.apply(&Mesh::Draw);
}

void
Terrain::shadows(const ShadowMapper & shadowMapper) const
{
	shadowMapper.fixedPoint.use();
	meshes.apply(&Mesh::Draw);
}