From b7fadd730a78671a0eaf55c36df24c04661ef2c3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 16 Nov 2021 00:07:48 +0000 Subject: Swap y,z axis This was a pain... but all the coords make much more sense now and a lot of mystery negation has disappeared. --- game/network/link.cpp | 8 ++-- game/network/rail.cpp | 77 ++++++++++++++++++++++---------------- game/terrain.cpp | 59 ++++++++++++++--------------- game/vehicles/railVehicle.cpp | 2 +- game/vehicles/railVehicleClass.cpp | 8 ++-- 5 files changed, 82 insertions(+), 72 deletions(-) (limited to 'game') diff --git a/game/network/link.cpp b/game/network/link.cpp index e45126f..f3a79a6 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -12,7 +12,7 @@ bool operator<(const glm::vec3 & a, const glm::vec3 & b) { // NOLINTNEXTLINE(hicpp-use-nullptr,modernize-use-nullptr) - return std::tie(a.x, a.z, a.y) < std::tie(b.x, b.z, b.y); + return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z); } bool @@ -27,7 +27,7 @@ LinkStraight::positionAt(float dist, unsigned char start) const const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; const auto diff {es.second->pos - es.first->pos}; const auto dir {glm::normalize(diff)}; - return Location {es.first->pos + vehiclePositionOffset() + dir * dist, {-vector_pitch(dir), vector_yaw(dir), 0}}; + return Location {es.first->pos + vehiclePositionOffset() + dir * dist, {vector_pitch(dir), vector_yaw(dir), 0}}; } Location @@ -40,7 +40,7 @@ LinkCurve::positionAt(float dist, unsigned char start) const const auto ang {as.first + ((as.second - as.first) * frac)}; const auto relPos {!sincosf(ang) * radius}; const auto relClimb {vehiclePositionOffset() - + glm::vec3 {0, -centreBase.y + es.first->pos.y + ((es.second->pos.y - es.first->pos.y) * frac), 0}}; - const auto pitch {vector_pitch({0, (es.first->pos.y - es.second->pos.y) / length, 0})}; + + glm::vec3 {0, 0, es.first->pos.z - centreBase.z + ((es.second->pos.z - es.first->pos.z) * frac)}}; + const auto pitch {vector_pitch({0, 0, (es.second->pos.z - es.first->pos.z) / length})}; return Location {relPos + relClimb + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; } diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 1f432cb..5127e34 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -15,10 +15,13 @@ template class NetworkOf; constexpr auto RAIL_CROSSSECTION_VERTICES {5U}; -constexpr glm::vec3 RAIL_HEIGHT {0, .25F, 0}; +constexpr glm::vec3 RAIL_HEIGHT {0, 0, .25F}; RailLinks::RailLinks() : NetworkOf {"rails.jpg"} { } -void RailLinks::tick(TickDuration) { } +void +RailLinks::tick(TickDuration) +{ +} std::shared_ptr RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) @@ -46,11 +49,14 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) throw std::runtime_error("Node exists but couldn't find it"); }; float dir = pi + findDir(node1ins.first); + if (dir == vector_yaw(end - start)) { + return addLink(start, end); + } const glm::vec2 flatStart {!start}, flatEnd {!end}; if (!node2ins.second) { auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); - return start.y + ((end.y - start.y) * (sm / (sm + em))); + return start.z + ((end.z - start.z) * (sm / (sm + em))); }; float dir2 = pi + findDir(node2ins.first); if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) { @@ -106,9 +112,9 @@ constexpr const std::array, RAIL_CROSSSECTION_VERTIC // _/ \_ // left to right {{-1.9F, 0.F, 0.F}, 0.F}, - {{-.608F, RAIL_HEIGHT.y, 0.F}, 0.34F}, - {{0, RAIL_HEIGHT.y * .7F, 0.F}, 0.5F}, - {{.608F, RAIL_HEIGHT.y, 0.F}, 0.66F}, + {{-.608F, 0.F, RAIL_HEIGHT.z}, 0.34F}, + {{0, 0.F, RAIL_HEIGHT.z * .7F}, 0.5F}, + {{.608F, 0.F, RAIL_HEIGHT.z}, 0.66F}, {{1.9F, 0.F, 0.F}, 1.F}, }}; constexpr auto sleepers {5.F}; // There are 5 repetitions of sleepers in the texture @@ -124,22 +130,24 @@ RailLinkStraight::RailLinkStraight(const NodePtr & a, const NodePtr & b) : RailL RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff) : Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)) { - std::vector vertices; - vertices.reserve(2 * railCrossSection.size()); - const auto len = round_sleepers(length / 2.F); - const auto e {flat_orientation(diff)}; - for (int ei = 0; ei < 2; ei++) { - const auto trans {glm::translate(ends[ei].node->pos) * e}; - for (const auto & rcs : railCrossSection) { - const glm::vec3 m {(trans * glm::vec4 {rcs.first, 1})}; - vertices.emplace_back(m, glm::vec2 {rcs.second, ei ? len : 0.F}, up); + if (glGenVertexArrays) { + std::vector vertices; + vertices.reserve(2 * railCrossSection.size()); + const auto len = round_sleepers(length / 2.F); + const auto e {flat_orientation(diff)}; + for (int ei : {1, 0}) { + const auto trans {glm::translate(ends[ei].node->pos) * e}; + for (const auto & rcs : railCrossSection) { + const glm::vec3 m {(trans * glm::vec4 {rcs.first, 1})}; + vertices.emplace_back(m, glm::vec2 {rcs.second, len * static_cast(ei)}, up); + } } + mesh = defaultMesh(vertices); } - mesh = defaultMesh(vertices); } RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec2 c) : - RailLinkCurve(a, b, {c.x, a->pos.y, c.y}, {!c, a->pos, b->pos}) + RailLinkCurve(a, b, c ^ a->pos.z, {!c, a->pos, b->pos}) { } @@ -148,24 +156,27 @@ RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec3 c, (glm::length(a->pos - c)) * arc_length(arc)), LinkCurve {c, glm::length(ends[0].node->pos - c), arc} { - 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(5.F * slength / std::pow(radius, 0.7F)); - const auto step {glm::vec3 {-arc_length(arc), e0p.y - e1p.y, slength} / segs}; - const auto trans {glm::translate(centreBase)}; - - auto segCount = std::lround(segs); - std::vector vertices; - vertices.reserve((segCount + 1) * railCrossSection.size()); - for (glm::vec3 swing = {arc.second, e1p.y - centreBase.y, 0.F}; segCount >= 0; swing += step, --segCount) { - const auto t {trans * glm::rotate(swing.x - half_pi, up) * glm::translate(glm::vec3 {radius, swing.y, 0.F})}; - for (const auto & rcs : railCrossSection) { - const glm::vec3 m {(t * glm::vec4 {rcs.first, 1})}; - vertices.emplace_back(m, glm::vec2 {rcs.second, swing.z}, up); + 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(15.F * slength / std::pow(radius, 0.7F)); + const auto step {glm::vec3 {arc_length(arc), e1p.z - e0p.z, slength} / segs}; + const auto trans {glm::translate(centreBase)}; + + auto segCount = std::lround(segs); + std::vector vertices; + vertices.reserve((segCount + 1) * railCrossSection.size()); + for (glm::vec3 swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount >= 0; swing += step, --segCount) { + const auto t { + trans * glm::rotate(half_pi - swing.x, up) * glm::translate(glm::vec3 {radius, 0.F, swing.y})}; + for (const auto & rcs : railCrossSection) { + const glm::vec3 m {(t * glm::vec4 {rcs.first, 1})}; + vertices.emplace_back(m, glm::vec2 {rcs.second, swing.z}, up); + } } + mesh = defaultMesh(vertices); } - mesh = defaultMesh(vertices); } glm::vec3 diff --git a/game/terrain.cpp b/game/terrain.cpp index ddfa31b..ed8bca5 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -23,12 +23,12 @@ Terrain::Terrain() : grass {Texture::cachedTexture.get("grass.png")}, water {Tex vertices.resize(verticesCount, {{}, {}, {}}); // Initial coordinates - for (auto z = 0; z < size; z += 1) { + for (auto y = 0; y < size; y += 1) { for (auto x = 0; x < size; x += 1) { - auto & vertex = vertices[x + (z * size)]; - vertex.pos = {resolution * (x - offset), -1.5, resolution * (z - offset)}; - vertex.normal = {0, 1, 0}; - vertex.texCoord = {x, z}; + auto & vertex = vertices[x + (y * size)]; + vertex.pos = {resolution * (x - offset), resolution * (y - offset), -1.5}; + vertex.normal = up; + vertex.texCoord = {x, y}; } } // Add hills @@ -43,15 +43,15 @@ Terrain::Terrain() : grass {Texture::cachedTexture.get("grass.png")}, water {Tex if (const auto lim2 = hpos + hsize; lim2.x < size && lim2.y < size) { const auto height = (float)rheight(gen); const glm::ivec2 hsizesqrd {hsize.x * hsize.x, hsize.y * hsize.y}; - for (auto z = lim1.y; z < lim2.y; z += 1) { + for (auto y = lim1.y; y < lim2.y; y += 1) { for (auto x = lim1.x; x < lim2.x; x += 1) { - const auto dist {hpos - glm::ivec2 {x, z}}; + const auto dist {hpos - glm::ivec2 {x, y}}; const glm::ivec2 distsqrd {dist.x * dist.x, dist.y * dist.y}; - const auto out {rdiv(sq(x - hpos.x), sq(hsize.x)) + rdiv(sq(z - hpos.y), sq(hsize.y))}; + const auto out {rdiv(sq(x - hpos.x), sq(hsize.x)) + rdiv(sq(y - hpos.y), sq(hsize.y))}; if (out <= 1.0) { - auto & vertex = vertices[x + (z * size)]; + auto & vertex = vertices[x + (y * size)]; const auto m {1.F / (7.F * out - 8.F) + 1.F}; - vertex.pos.y += height * m; + vertex.pos.z += height * m; } } } @@ -72,12 +72,11 @@ Terrain::Terrain(const std::string & fileName) : std::vector vertices; vertices.reserve((map.width * map.height) + 4); - for (auto z = 0; z < map.height; z += 1) { + for (auto y = 0; y < map.height; y += 1) { for (auto x = 0; x < map.width; x += 1) { - vertices.emplace_back( - glm::vec3 {resolution * (x - (map.width / 2)), ((float)map.data[x + (z * map.width)] * 0.1F) - 1.5F, - resolution * (z - (map.height / 2))}, - glm::vec2 {(x % 2) / 2.01, (z % 2) / 2.01}, glm::vec3 {0, 1, 0}); + vertices.emplace_back(glm::vec3 {resolution * (x - (map.width / 2)), resolution * (y - (map.height / 2)), + ((float)map.data[x + (y * map.width)] * 0.1F) - 1.5F}, + glm::vec2 {(x % 2) / 2.01, (y % 2) / 2.01}, up); } } @@ -93,28 +92,28 @@ Terrain::finish(unsigned int width, unsigned int height, std::vector & v std::vector indices; indices.reserve(indicesCount + 6); // Indices - for (auto z = 0U; z < height - 1; z += 1) { + for (auto y = 0U; y < height - 1; y += 1) { for (auto x = 0U; x < width - 1; x += 1) { - indices.push_back(x + (z * width)); - indices.push_back((x + 1) + ((z + 1) * width)); - indices.push_back((x + 1) + (z * width)); - indices.push_back(x + (z * width)); - indices.push_back(x + ((z + 1) * width)); - indices.push_back((x + 1) + ((z + 1) * width)); + indices.push_back(x + (y * width)); + indices.push_back((x + 1) + (y * width)); + indices.push_back((x + 1) + ((y + 1) * width)); + indices.push_back(x + (y * width)); + indices.push_back((x + 1) + ((y + 1) * width)); + indices.push_back(x + ((y + 1) * width)); } } // Normals - auto v = [&vertices](unsigned int width, unsigned int x, unsigned int z) -> Vertex & { - return vertices[x + (z * width)]; + auto v = [&vertices](unsigned int width, unsigned int x, unsigned int y) -> Vertex & { + return vertices[x + (y * width)]; }; - for (auto z = 1U; z < height - 1; z += 1) { + for (auto y = 1U; y < height - 1; y += 1) { for (auto x = 1U; x < width - 1; x += 1) { - const auto a = v(width, x - 1, z).pos; - const auto b = v(width, x, z - 1).pos; - const auto c = v(width, x + 1, z).pos; - const auto d = v(width, x, z + 1).pos; - v(width, x, z).normal = -glm::normalize(glm::cross(c - a, d - b)); + const auto a = v(width, x - 1, y).pos; + const auto b = v(width, x, y - 1).pos; + const auto c = v(width, x + 1, y).pos; + const auto d = v(width, x, y + 1).pos; + v(width, x, y).normal = -glm::normalize(glm::cross(b - d, a - c)); } } meshes.create(vertices, indices); diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index a2373e7..aea7f8c 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -21,6 +21,6 @@ RailVehicle::move(const Train * t, float & trailBy) const auto & b2Pos = bogies[1] = t->getBogiePosition(t->linkDist, trailBy += rvClass->wheelBase); const auto diff = glm::normalize(b2Pos.pos - b1Pos.pos); location.pos = (b1Pos.pos + b2Pos.pos) / 2.F; - location.rot = {-vector_pitch(diff), vector_yaw(diff), 0}; + location.rot = {vector_pitch(diff), vector_yaw(diff), 0}; trailBy += 0.6F + overhang; } diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 83eb9a0..52b7dbe 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -60,12 +60,12 @@ RailVehicleClass::bogieOffset(ObjParser & o) std::set> vertexIds; for (const auto & face : object.second) { for (const auto & faceElement : face) { - vertexIds.emplace(o.vertices[faceElement.x - 1].z, faceElement.x - 1); + vertexIds.emplace(o.vertices[faceElement.x - 1].y, faceElement.x - 1); } } const auto offset = (vertexIds.begin()->first + vertexIds.rbegin()->first) / 2; for (const auto & v : vertexIds) { - o.vertices[v.second].z -= offset; + o.vertices[v.second].y -= offset; } wheelBase += std::abs(offset); } @@ -76,7 +76,7 @@ float RailVehicleClass::objectLength(ObjParser & o) { const auto mme = std::minmax_element(o.vertices.begin(), o.vertices.end(), [](const auto & v1, const auto & v2) { - return v1.z < v2.z; + return v1.y < v2.y; }); - return mme.second->z - mme.first->z; + return mme.second->y - mme.first->y; } -- cgit v1.2.3