From 68832526efafb4db60e5627eba6a9051585a4eb9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Feb 2021 12:04:33 +0000 Subject: Factor a lot of maths and stuff into utility --- game/network/link.h | 8 ----- game/network/rail.cpp | 85 +++------------------------------------------------ game/network/rail.h | 4 ++- utility/maths.cpp | 54 ++++++++++++++++++++++++++++++++ utility/maths.h | 38 +++++++++++++++++++++++ utility/sorting.hpp | 12 ++++++++ 6 files changed, 112 insertions(+), 89 deletions(-) create mode 100644 utility/maths.cpp create mode 100644 utility/maths.h create mode 100644 utility/sorting.hpp diff --git a/game/network/link.h b/game/network/link.h index b22d0d0..287ba14 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -37,14 +37,6 @@ public: float length; }; -template struct PtrSorter { - bool - operator()(const T & a, const T & b) const - { - return *a < *b; - } -}; - bool operator<(const glm::vec3 & a, const glm::vec3 & b); bool operator<(const Node & a, const Node & b); 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 #include #include -#include #include #include #include -#include -#include #include -#include -#include +#include #include #include RailLinks::RailLinks() : texture {Texture::cachedTexture.get("rails.jpg")} { } void RailLinks::tick(TickDuration) { } -static const auto identityModel {glm::identity()}; - void RailLinks::render(const Shader & shader) const { - shader.setModel(identityModel); + shader.setModel(glm::identity()); texture->Bind(); links.apply(&RailLink::render, shader); } @@ -43,44 +37,12 @@ constexpr const std::array, 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, up)}; -constexpr auto half_pi {glm::half_pi()}; -constexpr auto pi {glm::pi()}; -constexpr auto two_pi {glm::two_pi()}; constexpr auto sleepers {5.F}; // There are 5 repetitions of sleepers in the texture -template -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 -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 -constexpr auto -round_frac(const T & v, const T & frac) -{ - return std::round(v / frac) * frac; -} - -template -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(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)) { diff --git a/game/network/rail.h b/game/network/rail.h index 3ab0c83..8fee27f 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -8,10 +8,13 @@ #include "gfx/renderable.h" #include "link.h" #include +#include #include #include +#include #include #include + class Shader; class Texture; @@ -36,7 +39,6 @@ private: RailLinkStraight(NodePtr, NodePtr, const glm::vec3 & diff); }; -using Arc = std::pair; class RailLinkCurve : public RailLink { public: RailLinkCurve(const NodePtr &, const NodePtr &, glm::vec2); diff --git a/utility/maths.cpp b/utility/maths.cpp new file mode 100644 index 0000000..a3315a3 --- /dev/null +++ b/utility/maths.cpp @@ -0,0 +1,54 @@ +#include "maths.h" +#include +#include +#include +#include +#include + +glm::mat4 +flat_orientation(const glm::vec3 & diff) +{ + static const auto oneeighty {glm::rotate(pi, up)}; + 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; +} + +float +flat_angle(const glm::vec3 & diff) +{ + const auto flatdiff {glm::normalize(glm::vec3 {diff.x, 0, diff.z})}; + return glm::orientedAngle(flatdiff, north, up); +} + +float +round_frac(const float & v, const float & frac) +{ + return std::round(v / frac) * frac; +} + +float +normalize(float ang) +{ + while (ang > pi) { + ang -= two_pi; + } + while (ang <= -pi) { + ang += two_pi; + } + return ang; +} + +Arc +create_arc(const glm::vec3 & centre3, const glm::vec3 & e0p, const 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}; +} diff --git a/utility/maths.h b/utility/maths.h new file mode 100644 index 0000000..0b379d0 --- /dev/null +++ b/utility/maths.h @@ -0,0 +1,38 @@ +#ifndef MATH_H +#define MATH_H + +#include +#include +#include + +using Arc = std::pair; + +constexpr const glm::vec3 up {0, 1, 0}; +constexpr const glm::vec3 north {0, 0, 1}; +constexpr auto half_pi {glm::half_pi()}; +constexpr auto pi {glm::pi()}; +constexpr auto two_pi {glm::two_pi()}; + +glm::mat4 flat_orientation(const glm::vec3 & diff); + +float flat_angle(const glm::vec3 & diff); + +float round_frac(const float & v, const float & frac); + +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; +} + +float normalize(float ang); + +Arc create_arc(const glm::vec3 & centre3, const glm::vec3 & e0p, const glm::vec3 & e1p); + +#endif diff --git a/utility/sorting.hpp b/utility/sorting.hpp new file mode 100644 index 0000000..0cb0eaf --- /dev/null +++ b/utility/sorting.hpp @@ -0,0 +1,12 @@ +#ifndef SORTING_H +#define SORTING_H + +template struct PtrSorter { + bool + operator()(const T & a, const T & b) const + { + return *a < *b; + } +}; + +#endif -- cgit v1.2.3