summaryrefslogtreecommitdiff
path: root/game/water.cpp
blob: f720e3eee015501da484f49fd0d75484b2dd70fa (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
104
105
106
107
108
109
110
#include "water.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 <location.h>
#include <maths.h>
#include <set>
#include <utility>
#include <vector>

namespace glm {
	bool
	operator<(const GlobalPosition2D a, const GlobalPosition2D b)
	{
		return std::tie(a.x, a.y) < std::tie(b.x, b.y);
	}
}

template<>
VertexArrayObject &
VertexArrayObject::addAttribsFor<Water::Vertex>(const GLuint arrayBuffer, const GLuint divisor)
{
	return addAttribs<Water::Vertex, &Water::Vertex::pos>(arrayBuffer, divisor);
}

Water::Water(std::shared_ptr<GeoData> tm) : geoData {std::move(tm)}, water {std::make_shared<Texture>("water.png")}
{
	generateMeshes();
}

static constexpr GlobalDistance MIN_HEIGHT = 1'000;
static constexpr GlobalDistance TILE_SIZE = 8'192;
static constexpr GlobalDistance BORDER = TILE_SIZE / 2;

void
Water::generateMeshes()
{
	// Map out where a water square needs to exist to cover all terrain faces with a low vertex
	std::set<GlobalPosition2D> waterPositions;
	std::for_each(geoData->vertices_sbegin(), geoData->vertices_end(), [this, &waterPositions](const auto vh) {
		if (geoData->point(vh).z < MIN_HEIGHT) {
			std::for_each(geoData->vf_begin(vh), geoData->vf_end(vh),
					[done = std::set<OpenMesh::FaceHandle>(), this, &waterPositions](const auto fh) mutable {
						if (done.insert(fh).second) {
							const auto getrange = [this, fh](glm::length_t axis) {
								const auto mme = std::minmax_element(geoData->fv_begin(fh), geoData->fv_end(fh),
										[this, axis](const auto vh1, const auto vh2) {
											return geoData->point(vh1)[axis] < geoData->point(vh2)[axis];
										});

								return std::make_pair((geoData->point(*mme.first)[axis] - BORDER) / TILE_SIZE,
										(geoData->point(*mme.second)[axis] + BORDER) / TILE_SIZE);
							};
							const auto xrange = getrange(0);
							const auto yrange = getrange(1);
							for (auto x = xrange.first; x < xrange.second; x++) {
								for (auto y = yrange.first; y < yrange.second; y++) {
									waterPositions.emplace(x, y);
								}
							}
						}
					});
		}
	});

	std::vector<unsigned int> indices;
	std::vector<Vertex> vertices;
	std::map<GlobalPosition2D, size_t> vertexIndex;
	std::for_each(waterPositions.begin(), waterPositions.end(),
			[&indices, &vertices, &vertexIndex, extents = geoData->getExtents(), this](const GlobalPosition2D p) {
				std::array<unsigned int, 4> currentIndices {};
				auto out = currentIndices.begin();
				for (auto x : {0, TILE_SIZE}) {
					for (auto y : {0, TILE_SIZE}) {
						const auto pos = (p * TILE_SIZE) + GlobalPosition2D {x, y};
						const auto v = vertexIndex.emplace(pos, vertices.size());
						if (v.second) {
							const auto cpos = glm::clamp(pos, std::get<0>(extents).xy(), std::get<1>(extents).xy());
							vertices.emplace_back(geoData->positionAt(cpos));
						}
						*out++ = static_cast<unsigned int>(v.first->second);
					}
				}
				for (const auto i : {0U, 3U, 1U, 0U, 2U, 3U}) {
					indices.push_back(currentIndices[i]);
				}
			});
	meshes.create<MeshT<Vertex>>(vertices, indices);
}

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

void
Water::render(const SceneShader & shader) const
{
	shader.water.use(waveCycle);
	water->bind();
	meshes.apply(&MeshT<GlobalPosition3D>::Draw);
}