summaryrefslogtreecommitdiff
path: root/game/terrain.cpp
blob: 91a228f61fcb12f1ae0c2c03e055dc6ffa8783a8 (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
#include "terrain.h"
#include "game/geoData.h"
#include "gfx/models/texture.h"
#include <algorithm>
#include <cstddef>
#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> tm) :
	geoData {std::move(tm)}, grass {std::make_shared<Texture>("grass.png")},
	water {std::make_shared<Texture>("water.png")}
{
	generateMeshes();
}

void
Terrain::generateMeshes()
{
	std::vector<unsigned int> indices;
	indices.reserve(geoData->n_faces() * 3);
	std::vector<Vertex> vertices;
	vertices.reserve(geoData->n_vertices());
	std::map<GeoData::VertexHandle, size_t> vertexIndex;
	std::transform(geoData->vertices_sbegin(), geoData->vertices_end(), std::back_inserter(vertices),
			[this, &vertexIndex](const GeoData::VertexHandle v) {
				vertexIndex.emplace(v, vertexIndex.size());
				const auto p = geoData->point(v);
				return Vertex {p, RelativePosition2D(p) / 10000.F, geoData->normal(v)};
			});
	std::for_each(
			geoData->faces_sbegin(), geoData->faces_end(), [this, &vertexIndex, &indices](const GeoData::FaceHandle f) {
				std::transform(geoData->fv_begin(f), geoData->fv_end(f), std::back_inserter(indices),
						[&vertexIndex](const GeoData::VertexHandle v) {
							return vertexIndex[v];
						});
			});
	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);
}