diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-21 18:30:16 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-21 18:30:16 +0100 |
commit | 56100b1c4cb02db7608763dddd77f8052a533dae (patch) | |
tree | ea8322a78134e1eb9ecca3e34672313cb5740ad5 | |
parent | Randomise for many trees, positions, rotations (diff) | |
download | ilt-56100b1c4cb02db7608763dddd77f8052a533dae.tar.bz2 ilt-56100b1c4cb02db7608763dddd77f8052a533dae.tar.xz ilt-56100b1c4cb02db7608763dddd77f8052a533dae.zip |
Further template maths functions
-rw-r--r-- | assetFactory/cylinder.cpp | 2 | ||||
-rw-r--r-- | game/geoData.cpp | 2 | ||||
-rw-r--r-- | game/network/link.cpp | 4 | ||||
-rw-r--r-- | game/network/network.cpp | 8 | ||||
-rw-r--r-- | game/network/rail.cpp | 8 | ||||
-rw-r--r-- | gfx/followCameraController.cpp | 2 | ||||
-rw-r--r-- | lib/maths.h | 53 | ||||
-rw-r--r-- | ui/manualCameraController.cpp | 2 |
8 files changed, 45 insertions, 36 deletions
diff --git a/assetFactory/cylinder.cpp b/assetFactory/cylinder.cpp index f41bfd4..432fb16 100644 --- a/assetFactory/cylinder.cpp +++ b/assetFactory/cylinder.cpp @@ -12,7 +12,7 @@ Cylinder::createMesh(ModelFactoryMesh & mesh, Scale3D lodf) const // Generate 2D circumference points std::vector<RelativePosition2D> circumference(P); std::generate(circumference.begin(), circumference.end(), [a = 0.F, step]() mutable { - return sincosf(a += step) * .5F; + return sincos(a += step) * .5F; }); CreatedFaces surface; diff --git a/game/geoData.cpp b/game/geoData.cpp index 72aa056..d212651 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -559,7 +559,7 @@ GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip, const const auto limit = std::ceil(arc.length() * 5.F / pi); const auto inc = arc.length() / limit; for (float step = 0; step <= limit; step += 1.F) { - addExtrusionFor(sincosf(arc.first + (step * inc))); + addExtrusionFor(sincos(arc.first + (step * inc))); } } else { diff --git a/game/network/link.cpp b/game/network/link.cpp index 745896c..79af92a 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -46,7 +46,7 @@ LinkCurve::positionAt(float dist, unsigned char start) const const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; const auto as {std::make_pair(arc[start], arc[1 - start])}; const auto ang {as.first + ((as.second - as.first) * frac)}; - const auto relPos {(sincosf(ang) || 0.F) * radius}; + const auto relPos {(sincos(ang) || 0.F) * radius}; const auto relClimb {vehiclePositionOffset() + RelativePosition3D {0, 0, static_cast<RelativeDistance>(es.first->pos.z - centreBase.z) @@ -69,7 +69,7 @@ LinkCurve::intersectRay(const Ray<GlobalPosition3D> & ray) const points.reserve(segCount); for (std::remove_const_t<decltype(step)> swing = {arc.first, centreBase.z - e0p.z}; segCount; swing += step, --segCount) { - points.emplace_back(centreBase + ((sincosf(swing.x) * radius) || swing.y)); + points.emplace_back(centreBase + ((sincos(swing.x) * radius) || swing.y)); } return ray.passesCloseToEdges(points, 1.F); } diff --git a/game/network/network.cpp b/game/network/network.cpp index fa86cb5..6ba3ed6 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -121,16 +121,16 @@ Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & en }; if (const auto radii = find_arcs_radius(flatStart, startDir, flatEnd, endDir); radii.first < radii.second) { const auto radius {radii.first}; - const auto c1 = flatStart + (sincosf(startDir + half_pi) * radius); - const auto c2 = flatEnd + (sincosf(endDir + half_pi) * radius); + const auto c1 = flatStart + (sincos(startDir + half_pi) * radius); + const auto c2 = flatEnd + (sincos(endDir + half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); return {{start, midh, c1}, {end, midh, c2}}; } else { const auto radius {radii.second}; - const auto c1 = flatStart + (sincosf(startDir - half_pi) * radius); - const auto c2 = flatEnd + (sincosf(endDir - half_pi) * radius); + const auto c1 = flatStart + (sincos(startDir - half_pi) * radius); + const auto c2 = flatEnd + (sincos(endDir - half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); return {{midh, start, c1}, {midh, end, c2}}; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index c29217a..6f04070 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -45,8 +45,8 @@ RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) 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 + (sincosf(dir + half_pi) * radius); - const auto c2 = flatEnd + (sincosf(dir2 + half_pi) * radius); + const auto c1 = flatStart + (sincos(dir + half_pi) * radius); + const auto c2 = flatEnd + (sincos(dir2 + half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); addLink<RailLinkCurve>(start, midh, c1); @@ -54,8 +54,8 @@ RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) } else { const auto radius {radii.second}; - const auto c1 = flatStart + (sincosf(dir - half_pi) * radius); - const auto c2 = flatEnd + (sincosf(dir2 - half_pi) * radius); + const auto c1 = flatStart + (sincos(dir - half_pi) * radius); + const auto c2 = flatEnd + (sincos(dir2 - half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); addLink<RailLinkCurve>(midh, start, c1); diff --git a/gfx/followCameraController.cpp b/gfx/followCameraController.cpp index 52dfb35..cf6da34 100644 --- a/gfx/followCameraController.cpp +++ b/gfx/followCameraController.cpp @@ -24,7 +24,7 @@ FollowCameraController::updateCamera(Camera * camera) const break; case Mode::Ride: - camera->setView(pos + (up * 4.8F), -sincosf(rot.y) || 0.F); + camera->setView(pos + (up * 4.8F), -sincos(rot.y) || 0.F); break; case Mode::ISO: diff --git a/lib/maths.h b/lib/maths.h index 94e357b..90ddb69 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -87,43 +87,52 @@ glm::mat4 flat_orientation(const Rotation3D & diff); namespace { // Helpers // C++ wrapper for C's sincosf, but with references, not pointers - constexpr auto - sincosf(float angle, float & sinOut, float & cosOut) + template<std::floating_point T> + constexpr void + sincos(T angle, T & sinOut, T & cosOut) { if consteval { - sinOut = ::sinf(angle); - cosOut = ::cosf(angle); + sinOut = std::sin(angle); + cosOut = std::cos(angle); } else { - ::sincosf(angle, &sinOut, &cosOut); + if constexpr (std::is_same_v<T, float>) { + ::sincosf(angle, &sinOut, &cosOut); + } + else if constexpr (std::is_same_v<T, double>) { + ::sincos(angle, &sinOut, &cosOut); + } + else if constexpr (std::is_same_v<T, long double>) { + ::sincosl(angle, &sinOut, &cosOut); + } } } - template<std::floating_point T> + template<std::floating_point T, glm::qualifier Q = glm::qualifier::defaultp> constexpr auto - sincosf(const T angle) + sincos(const T angle) { - Rotation2D sincosOut; - sincosf(angle, sincosOut.x, sincosOut.y); + glm::vec<2, T, Q> sincosOut {}; + sincos(angle, sincosOut.x, sincosOut.y); return sincosOut; } // Helper to lookup into a matrix given an xy vector coordinate - template<typename M, typename I> + template<glm::length_t C, glm::length_t R, typename T, glm::qualifier Q, std::integral I = glm::length_t> constexpr auto & - operator^(M & matrix, glm::vec<2, I> rowCol) + operator^(glm::mat<C, R, T, Q> & matrix, const glm::vec<2, I> rowCol) { return matrix[rowCol.x][rowCol.y]; } // Create a matrix for the angle, given the targets into the matrix - template<typename M, typename I> + template<glm::length_t D, std::floating_point T, glm::qualifier Q, std::integral I = glm::length_t> constexpr auto - rotation(typename M::value_type angle, glm::vec<2, I> cos1, glm::vec<2, I> sin1, glm::vec<2, I> cos2, - glm::vec<2, I> negSin1) + rotation(const T angle, const glm::vec<2, I> cos1, const glm::vec<2, I> sin1, const glm::vec<2, I> cos2, + const glm::vec<2, I> negSin1) { - M out(1); - sincosf(angle, out ^ sin1, out ^ cos1); + glm::mat<D, D, T, Q> out(1); + sincos(angle, out ^ sin1, out ^ cos1); out ^ cos2 = out ^ cos1; out ^ negSin1 = -(out ^ sin1); return out; @@ -136,7 +145,7 @@ template<glm::length_t D = 2, glm::qualifier Q = glm::qualifier::defaultp, std:: constexpr auto rotate_flat(const T angle) { - return rotation<glm::mat<D, D, T, Q>, glm::length_t>(angle, {0, 0}, {0, 1}, {1, 1}, {1, 0}); + return rotation<D, T, Q>(angle, {0, 0}, {0, 1}, {1, 1}, {1, 0}); } // Create a yaw transformation matrix @@ -145,7 +154,7 @@ template<glm::length_t D = 3, glm::qualifier Q = glm::qualifier::defaultp, std:: constexpr auto rotate_yaw(const T angle) { - return rotation<glm::mat<D, D, T, Q>, glm::length_t>(angle, {0, 0}, {1, 0}, {1, 1}, {0, 1}); + return rotation<D, T, Q>(angle, {0, 0}, {1, 0}, {1, 1}, {0, 1}); } // Create a roll transformation matrix @@ -154,7 +163,7 @@ template<glm::length_t D = 3, glm::qualifier Q = glm::qualifier::defaultp, std:: constexpr auto rotate_roll(const T angle) { - return rotation<glm::mat<D, D, T, Q>, glm::length_t>(angle, {0, 0}, {2, 0}, {2, 2}, {0, 2}); + return rotation<D, T, Q>(angle, {0, 0}, {2, 0}, {2, 2}, {0, 2}); } // Create a pitch transformation matrix @@ -163,7 +172,7 @@ template<glm::length_t D = 3, glm::qualifier Q = glm::qualifier::defaultp, std:: constexpr auto rotate_pitch(const T angle) { - return rotation<glm::mat<D, D, T, Q>, glm::length_t>(angle, {1, 1}, {1, 2}, {2, 2}, {2, 1}); + return rotation<D, T, Q>(angle, {1, 1}, {1, 2}, {2, 2}, {2, 1}); } // Create a combined yaw, pitch, roll transformation matrix @@ -337,7 +346,7 @@ find_arc_centre(glm::vec<2, T, Q> start, Angle entrys, glm::vec<2, T, Q> end, An if (start == end) { return {start, false}; } - return find_arc_centre(start, sincosf(entrys + half_pi), end, sincosf(entrye - half_pi)); + return find_arc_centre(start, sincos(entrys + half_pi), end, sincos(entrye - half_pi)); } template<typename T, glm::qualifier Q> @@ -370,7 +379,7 @@ std::pair<Angle, Angle> find_arcs_radius(glm::vec<2, T, Q> start, Angle entrys, glm::vec<2, T, Q> end, Angle entrye) { const auto getrad = [&](auto leftOrRight) { - return find_arcs_radius(start, sincosf(entrys + leftOrRight), end, sincosf(entrye + leftOrRight)); + return find_arcs_radius(start, sincos(entrys + leftOrRight), end, sincos(entrye + leftOrRight)); }; return {getrad(-half_pi), getrad(half_pi)}; } diff --git a/ui/manualCameraController.cpp b/ui/manualCameraController.cpp index 065e1d8..53905eb 100644 --- a/ui/manualCameraController.cpp +++ b/ui/manualCameraController.cpp @@ -79,6 +79,6 @@ ManualCameraController::render(const UIShader &, const Position &) const void ManualCameraController::updateCamera(Camera * camera) const { - const auto forward = glm::normalize(sincosf(direction) || -sin(pitch)); + const auto forward = glm::normalize(sincos(direction) || -sin(pitch)); camera->setView((focus || 0) - (forward * 3.F * std::pow(dist, 1.3F)), forward); } |