diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-07 12:04:33 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-07 12:04:33 +0000 |
commit | 68832526efafb4db60e5627eba6a9051585a4eb9 (patch) | |
tree | cb6059828786a60fa10c2017f43b7baf98ca86cc /game/network/rail.cpp | |
parent | Links have length, ends have entry direction (diff) | |
download | ilt-68832526efafb4db60e5627eba6a9051585a4eb9.tar.bz2 ilt-68832526efafb4db60e5627eba6a9051585a4eb9.tar.xz ilt-68832526efafb4db60e5627eba6a9051585a4eb9.zip |
Factor a lot of maths and stuff into utility
Diffstat (limited to 'game/network/rail.cpp')
-rw-r--r-- | game/network/rail.cpp | 85 |
1 files changed, 5 insertions, 80 deletions
diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 0858b66..e66b5c8 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -3,27 +3,21 @@ #include <GL/glew.h> #include <array> #include <cache.h> -#include <cmath> #include <gfx/gl/shader.h> #include <gfx/models/texture.h> #include <gfx/models/vertex.hpp> -#include <glm/gtc/constants.hpp> -#include <glm/gtx/rotate_vector.hpp> #include <glm/gtx/transform.hpp> -#include <glm/gtx/vector_angle.hpp> -#include <numbers> +#include <maths.h> #include <type_traits> #include <utility> RailLinks::RailLinks() : texture {Texture::cachedTexture.get("rails.jpg")} { } void RailLinks::tick(TickDuration) { } -static const auto identityModel {glm::identity<glm::mat4>()}; - void RailLinks::render(const Shader & shader) const { - shader.setModel(identityModel); + shader.setModel(glm::identity<glm::mat4>()); texture->Bind(); links.apply(&RailLink::render, shader); } @@ -43,44 +37,12 @@ constexpr const std::array<std::pair<glm::vec3, float>, 4> railCrossSection {{ {{.75F, .25F, 0.F}, 0.875F}, {{1.F, 0.F, 0.F}, 1.F}, }}; -constexpr const glm::vec3 up {0, 1, 0}; -constexpr const glm::vec3 north {0, 0, 1}; -const auto oneeighty {glm::rotate(std::numbers::pi_v<float>, up)}; -constexpr auto half_pi {glm::half_pi<float>()}; -constexpr auto pi {glm::pi<float>()}; -constexpr auto two_pi {glm::two_pi<float>()}; constexpr auto sleepers {5.F}; // There are 5 repetitions of sleepers in the texture -template<typename V> -auto -flat_orientation(const V & diff) -{ - const auto flatdiff {glm::normalize(glm::vec3 {diff.x, 0, diff.z})}; - auto e {glm::orientation(flatdiff, north)}; - // Handle if diff is exactly opposite to north - return (std::isnan(e[0][0])) ? oneeighty : e; -} - -template<typename V> -auto -flat_angle(const V & diff) -{ - const auto flatdiff {glm::normalize(glm::vec3 {diff.x, 0, diff.z})}; - return glm::orientedAngle(flatdiff, north, up); -} - -template<typename T> -constexpr auto -round_frac(const T & v, const T & frac) -{ - return std::round(v / frac) * frac; -} - -template<typename T> -constexpr auto -round_sleepers(const T & v) +inline auto +round_sleepers(const float v) { - return std::round(v / sleepers) * sleepers; + return round_frac(v, sleepers); } RailLinkStraight::RailLinkStraight(const NodePtr & a, const NodePtr & b) : RailLinkStraight(a, b, b->pos - a->pos) { } @@ -106,43 +68,6 @@ RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff) meshes.create<Mesh>(vertices, indices, GL_TRIANGLE_STRIP); } -constexpr inline glm::vec3 -operator!(const glm::vec2 & v) -{ - return {v.x, 0, v.y}; -} - -constexpr inline float -arc_length(const Arc & arc) -{ - return arc.second - arc.first; -} - -constexpr inline float -normalize(float ang) -{ - while (ang > pi) { - ang -= two_pi; - } - while (ang <= -pi) { - ang += two_pi; - } - return ang; -} - -inline Arc -create_arc(const glm::vec3 centre3, glm::vec3 e0p, glm::vec3 e1p) -{ - const auto diffa = centre3 - e0p; - const auto diffb = centre3 - e1p; - const auto anga = flat_angle(diffa); - const auto angb = [&diffb, &anga]() { - const auto angb = flat_angle(diffb); - return (angb < anga) ? angb + two_pi : angb; - }(); - return {anga, angb}; -} - RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec2 c) : RailLinkCurve(a, b, {c.x, a->pos.y, c.y}, create_arc(!c, a->pos, b->pos)) { |