summaryrefslogtreecommitdiff
path: root/game/network/rail.cpp
blob: efe4e2d33acc12b4ebc112592aaee55129c6f512 (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
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
#include "rail.h"
#include "network.h"
#include <array>
#include <cmath>
#include <collection.h>
#include <cstddef>
#include <game/network/link.h>
#include <game/network/network.impl.h> // IWYU pragma: keep
#include <gfx/models/vertex.h>
#include <glad/gl.h>
#include <glm/gtx/transform.hpp>
#include <maths.h>
#include <utility>
#include <vector>

template class NetworkOf<RailLink, RailLinkStraight, RailLinkCurve>;

constexpr auto RAIL_CROSSSECTION_VERTICES {5U};
constexpr Size3D RAIL_HEIGHT {0, 0, 250.F};

RailLinks::RailLinks() : NetworkOf<RailLink, RailLinkStraight, RailLinkCurve> {"rails.jpg"} { }

void
RailLinks::tick(TickDuration)
{
}

std::shared_ptr<RailLink>
RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end)
{
	auto node1ins = newNodeAt(start), node2ins = newNodeAt(end);
	if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::NotInNetwork) {
		// Both nodes are new, direct link, easy
		return addLink<RailLinkStraight>(start, end);
	}
	if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::InNetwork) {
		// node1 is new, node2 exists, but we build from existing outwards
		std::swap(node1ins, node2ins);
		std::swap(start, end);
	}
	// Find start link/end - opposite entry dir to existing link; so pi +...
	const Angle dir = pi + findNodeDirection(node1ins.first);
	if (dir == vector_yaw(end - start)) {
		return addLink<RailLinkStraight>(start, end);
	}
	const auto flatStart {start.xy()}, flatEnd {end.xy()};
	if (node2ins.second == NodeIs::InNetwork) {
		auto midheight = [&](auto mid) {
			const auto sm = glm::length(RelativePosition2D(flatStart - mid)),
					   em = glm::length(RelativePosition2D(flatEnd - mid));
			return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (sm / (sm + em)));
		};
		const float dir2 = pi + findNodeDirection(node2ins.first);
		if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) {
			const auto radius {radii.first};
			const auto c1 = flatStart + GlobalPosition2D(sincosf(dir + half_pi) * radius);
			const auto c2 = flatEnd + GlobalPosition2D(sincosf(dir2 + half_pi) * radius);
			const auto mid = (c1 + c2) / 2;
			const auto midh = mid || midheight(mid);
			addLink<RailLinkCurve>(start, midh, c1);
			return addLink<RailLinkCurve>(end, midh, c2);
		}
		else {
			const auto radius {radii.second};
			const auto c1 = flatStart + GlobalPosition2D(sincosf(dir - half_pi) * radius);
			const auto c2 = flatEnd + GlobalPosition2D(sincosf(dir2 - half_pi) * radius);
			const auto mid = (c1 + c2) / 2;
			const auto midh = mid || midheight(mid);
			addLink<RailLinkCurve>(midh, start, c1);
			return addLink<RailLinkCurve>(midh, end, c2);
		}
	}
	const auto diff {end - start};
	const auto vy {vector_yaw(diff)};
	const auto n2ed {(vy * 2) - dir - pi};
	const auto centre {find_arc_centre(flatStart, dir, flatEnd, n2ed)};

	if (centre.second) { // right hand arc
		std::swap(start, end);
	}
	return addLink<RailLinkCurve>(start, end, centre.first);
}

Mesh::Ptr
RailLink::defaultMesh(const std::span<Vertex> vertices)
{
	std::vector<unsigned int> indices;
	for (auto n = RAIL_CROSSSECTION_VERTICES; n < vertices.size(); n += 1) {
		indices.push_back(n - RAIL_CROSSSECTION_VERTICES);
		indices.push_back(n);
	}

	return std::make_unique<Mesh>(vertices, indices, GL_TRIANGLE_STRIP);
}

void
RailLink::render(const SceneShader &) const
{
	mesh->Draw();
}

constexpr const std::array<std::pair<RelativePosition3D, float>, RAIL_CROSSSECTION_VERTICES> railCrossSection {{
		//   ___________
		// _/           \_
		//  left to right
		{{-1900.F, 0.F, 0.F}, 0.F},
		{{-608.F, 0.F, RAIL_HEIGHT.z}, .34F},
		{{0, 0.F, RAIL_HEIGHT.z * .7F}, .5F},
		{{608.F, 0.F, RAIL_HEIGHT.z}, .66F},
		{{1900.F, 0.F, 0.F}, 1.F},
}};
constexpr auto sleepers {5.F}; // There are 5 repetitions of sleepers in the texture

inline auto
round_sleepers(const float v)
{
	return round_frac(v, sleepers);
}

RailLinkStraight::RailLinkStraight(
		NetworkLinkHolder<RailLinkStraight> & instances, const Node::Ptr & a, const Node::Ptr & b) :
	RailLinkStraight(instances, a, b, b->pos - a->pos)
{
}

RailLinkStraight::RailLinkStraight(
		NetworkLinkHolder<RailLinkStraight> & instances, Node::Ptr a, Node::Ptr b, const RelativePosition3D & diff) :
	Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)),
	instance {instances.vertices.acquire(
			ends[0].node->pos, ends[1].node->pos, flat_orientation(diff), round_sleepers(length / 2000.F))}
{
	if (glGenVertexArrays) {
		std::vector<::Vertex> vertices;
		vertices.reserve(2 * railCrossSection.size());
		const auto len = round_sleepers(length / 2000.F);
		const glm::mat3 trans {flat_orientation(diff)};
		for (auto ei : {1U, 0U}) {
			for (const auto & rcs : railCrossSection) {
				const auto m {ends[ei].node->pos + GlobalPosition3D(trans * rcs.first)};
				vertices.emplace_back(m, TextureRelCoord {rcs.second, len * static_cast<float>(ei)}, up);
			}
		}
		mesh = defaultMesh(vertices);
	}
}

RailLinkCurve::RailLinkCurve(
		NetworkLinkHolder<RailLinkCurve> & instances, const Node::Ptr & a, const Node::Ptr & b, GlobalPosition2D c) :
	RailLinkCurve(instances, a, b, c || a->pos.z, {c || 0, a->pos, b->pos})
{
}

RailLinkCurve::RailLinkCurve(NetworkLinkHolder<RailLinkCurve> & instances, const Node::Ptr & a, const Node::Ptr & b,
		GlobalPosition3D c, const Arc arc) :
	Link({a, normalize(arc.first + half_pi)}, {b, normalize(arc.second - half_pi)},
			glm::length(RelativePosition3D(a->pos - c)) * arc_length(arc)),
	LinkCurve {c, glm::length(RelativePosition3D(ends[0].node->pos - c)), arc},
	instance {instances.vertices.acquire(ends[0].node->pos, ends[1].node->pos, c, round_sleepers(length / 2000.F))}

{
	if (glGenVertexArrays) {
		const auto & e0p {ends[0].node->pos};
		const auto & e1p {ends[1].node->pos};
		const auto slength = round_sleepers(length / 2.F);
		const auto segs = std::round(slength / std::pow(radius, 0.7F));
		const auto step {RelativePosition3D {arc_length(arc), e1p.z - e0p.z, slength / 1000.F} / segs};

		auto segCount = static_cast<std::size_t>(std::lround(segs)) + 1;
		std::vector<::Vertex> vertices;
		vertices.reserve(segCount * railCrossSection.size());
		for (RelativePosition3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) {
			const auto t {
					glm::rotate(half_pi - swing.x, up) * glm::translate(RelativePosition3D {radius, 0.F, swing.y})};
			for (const auto & rcs : railCrossSection) {
				const auto m {centreBase + GlobalPosition3D(t * (rcs.first || 1.F))};
				vertices.emplace_back(m, TextureRelCoord {rcs.second, swing.z}, up);
			}
		}
		mesh = defaultMesh(vertices);
	}
}

RelativePosition3D
RailLink::vehiclePositionOffset() const
{
	return RAIL_HEIGHT;
}

template<> NetworkLinkHolder<RailLinkStraight>::NetworkLinkHolder()
{
	VertexArrayObject {vao}
			.addAttribs<RailLinkStraight::Vertex, &RailLinkStraight::Vertex::a, &RailLinkStraight::Vertex::b,
					&RailLinkStraight::Vertex::rotation, &RailLinkStraight::Vertex::textureRepeats>(
					vertices.bufferName());
}

template<> NetworkLinkHolder<RailLinkCurve>::NetworkLinkHolder()
{
	VertexArrayObject {vao}
			.addAttribs<RailLinkCurve::Vertex, &RailLinkCurve::Vertex::a, &RailLinkCurve::Vertex::b,
					&RailLinkCurve::Vertex::c, &RailLinkCurve::Vertex::textureRepeats>(vertices.bufferName());
}

namespace {
	template<typename LinkType>
	void
	renderType(const NetworkLinkHolder<LinkType> & n, auto & s)
	{
		if (auto count = n.vertices.size()) {
			s.use();
			glBindVertexArray(n.vao);
			glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(count));
		}
	};
}

void
RailLinks::render(const SceneShader & shader) const
{
	if (!links.objects.empty()) {
		texture->bind();
		renderType<RailLinkStraight>(*this, shader.networkStraight);
		renderType<RailLinkCurve>(*this, shader.networkCurve);
		glBindVertexArray(0);
	}
}