From f0b58ab16a45017bec4c1adec69a61fabda34138 Mon Sep 17 00:00:00 2001 From: KP Date: Mon, 22 Jul 2024 20:20:31 -0500 Subject: [PATCH] Moar progress --- Data/sandbox-log.txt | 28 +++---- KP3Dii/src/KP3D_Camera.cpp | 2 +- KP3Dii/src/KP3D_Map.cpp | 159 +++++++++++++++++++++++++++---------- KP3Dii/src/KP3D_Map.h | 4 +- 4 files changed, 133 insertions(+), 60 deletions(-) diff --git a/Data/sandbox-log.txt b/Data/sandbox-log.txt index 6411b5c..47b2766 100644 --- a/Data/sandbox-log.txt +++ b/Data/sandbox-log.txt @@ -1,20 +1,20 @@ -[04:45:28 PM] Info: Starting... +[08:20:13 PM] Info: Starting... KP3D version 2 =============================== Copyright (C) kpworld.xyz 2018-2024 Contact me! @kp_cftsz -[04:45:28 PM] Info: Initializing SDL -[04:45:28 PM] Info: Initializing OpenGL -[04:45:28 PM] Info: OpenGL version: 4.6.0 NVIDIA 517.70 -[04:45:28 PM] Info: Initializing GLEW -[04:45:28 PM] Info: Initializing SDL_mixer -[04:45:29 PM] Info: Reticulating splines... -[04:45:29 PM] Info: Ready! -[04:45:29 PM] Info: BUILDING SECTOR: 1 -[04:45:29 PM] Info: 1 IS TOUCHING: 5 -[04:45:29 PM] Info: BUILDING SECTOR: 2 -[04:45:29 PM] Info: BUILDING SECTOR: 3 -[04:45:29 PM] Info: BUILDING SECTOR: 4 -[04:45:29 PM] Info: BUILDING SECTOR: 5 +[08:20:13 PM] Info: Initializing SDL +[08:20:13 PM] Info: Initializing OpenGL +[08:20:13 PM] Info: OpenGL version: 4.6.0 NVIDIA 536.23 +[08:20:13 PM] Info: Initializing GLEW +[08:20:13 PM] Info: Initializing SDL_mixer +[08:20:13 PM] Info: Reticulating splines... +[08:20:13 PM] Info: Ready! +[08:20:13 PM] Info: BUILDING SECTOR: 1 +[08:20:13 PM] Info: 1 IS TOUCHING: 5 +[08:20:13 PM] Info: BUILDING SECTOR: 2 +[08:20:13 PM] Info: BUILDING SECTOR: 3 +[08:20:13 PM] Info: BUILDING SECTOR: 4 +[08:20:13 PM] Info: BUILDING SECTOR: 5 diff --git a/KP3Dii/src/KP3D_Camera.cpp b/KP3Dii/src/KP3D_Camera.cpp index e9223f6..729f1ca 100644 --- a/KP3Dii/src/KP3D_Camera.cpp +++ b/KP3Dii/src/KP3D_Camera.cpp @@ -18,7 +18,7 @@ Camera::Camera(Vec3 position, Vec3 forward, Vec3 right, Vec3 up): forward(forward), right(right), up(up), - yaw(90.0f), + yaw(-90.0f), pitch(0.0f) { UpdateMatrix(); diff --git a/KP3Dii/src/KP3D_Map.cpp b/KP3Dii/src/KP3D_Map.cpp index 4a6a60b..fc84681 100644 --- a/KP3Dii/src/KP3D_Map.cpp +++ b/KP3Dii/src/KP3D_Map.cpp @@ -59,6 +59,22 @@ bool PointInLine(float x1, float y1, float x2, float y2, float x0, float y0) return kp3d::FloatCmp((y2 - y1) * x0 + (x1 - x2) * y0 + (x2 * y1 - x1 * y2), 0.0f, 0.0001f) && in_bounds; } + +void InsertLine(std::vector& lines, size_t position, const Wall& newLine) { + if (position > 0 && position < lines.size()) { + // Update the end position of the line before the insertion point + lines[position - 1].end = newLine.start; + } + + if (position < lines.size()) { + // Update the start position of the line after the insertion point + lines[position].start = newLine.end; + } + + // Insert the new line into the vector + lines.insert(lines.begin() + position, newLine); +} + } // namespace namespace kp3d { @@ -271,54 +287,27 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos points.push_back(mpos); } - points.emplace_back(Distance({p4.x, p4.z}, {p1.x, p1.z}), p4.y, 0); - points.emplace_back(Distance({p2.x, p2.z}, {p1.x, p1.z}), p2.y, 0); + std::vector top_points; + top_points.emplace_back(Distance({ p4.x, p4.z }, { p1.x, p1.z }), p4.y, 0); + // points.emplace_back(Distance({ p2.x, p2.z }, { p1.x, p1.z }), p2.y, 0); + for (const Vertex3D& v: sector.ceiling.triangulated_data) + { + if (!PointInLine(v.position.x, v.position.z, pos_a.x, pos_a.z, pos_b.x, pos_b.z)) + continue; + // Project 3D point into 2D + Vec3 mpos = {Distance({v.position.x, v.position.z}, {p1.x, p1.z}), v.position.y, 0}; + top_points.push_back(mpos); + } + std::sort(points.begin() + 1, points.end(), [&](Vec3 a, Vec3 b) { return a.x < b.x; }); + std::sort(top_points.begin() + 1, top_points.end(), [&](Vec3 a, Vec3 b) { return a.x > b.x; }); + points.insert(points.end(), top_points.begin(), top_points.end()); - std::sort(points.begin() + 1, points.end() - 2, [&](Vec3 a, Vec3 b) { return a.x < b.x; }); - - // Test: Put points back in their original positions float angle = atan2({ p4.z - p1.z }, { p4.x - p1.x }); points.erase(unique(points.begin(), points.end()), points.end()); std::vector> holes; // unused for now // Begin: holes - for (Sector& s : sectors) - { - if (s.id == sector.id) - continue; - std::vector hole; - - float yb = s.floor.base_height; - float yt = s.ceiling.base_height; - for (Wall& l : s.walls) - { - //if (PointInLine(pos_a.x, pos_a.z, l.start.x, l.start.y, l.end.x, l.end.y)) - if (PointInLine(l.start.x, l.start.y, pos_a.x, pos_a.z, pos_b.x, pos_b.z) && - PointInLine(l.end.x, l.end.y, pos_a.x, pos_a.z, pos_b.x, pos_b.z) && - !PosCmp({ l.start.x, 0.0f, l.start.y }, {pos_a.x, 0.0f, pos_a.z}) && - !PosCmp({ l.start.x, 0.0f, l.start.y }, {pos_b.x, 0.0f, pos_b.z}) && - !PosCmp({ l.end.x, 0.0f, l.end.y }, {pos_b.x, 0.0f, pos_b.z}) && - !PosCmp({ l.end.x, 0.0f, l.end.y }, {pos_a.x, 0.0f, pos_a.z})) - { - KP3D_LOG_INFO("{} IS TOUCHING: {}", sector.id, s.id); - c2t::Point pl0 = { Distance({l.start.x, l.start.y}, {p1.x, p1.z}), yb }; - c2t::Point pl1 = { Distance({l.start.x, l.start.y}, {p1.x, p1.z}), yt }; - c2t::Point pl2 = { Distance({l.end.x, l.end.y}, {p1.x, p1.z}), yt }; - c2t::Point pl3 = { Distance({l.end.x, l.end.y}, {p1.x, p1.z}), yb }; - hole.push_back(pl0); - hole.push_back(pl1); - hole.push_back(pl2); - hole.push_back(pl3); - m_dots.push_back({ l.start.x, yb, -l.start.y }); - m_dots.push_back({ l.start.x, yt, -l.start.y }); - m_dots.push_back({ l.end.x, yt, -l.end.y }); - m_dots.push_back({ l.end.x, yb, -l.end.y }); - l.textures[TEX_FRONT] = nullptr; - } - } - holes.push_back(hole); - } // End: holes std::vector wall_polygon; for (Vec3& p: points) @@ -361,6 +350,82 @@ void Map::BuildWall(Sector& sector, Wall& wall) BuildQuad(sector, wall.textures[TEX_FRONT], a, b, false, false, false, wall.uv_offset[TEX_FRONT]); } +void Map::JoinSectors(Sector& sector) +{ + //for (Wall& ld: sector.walls) + for (int i = 0; i < sector.walls.size(); i++) + { + Wall& ld = sector.walls[i]; + for (Sector& s : sectors) + { + if (s.id == sector.id) + continue; + + Vec3 pos_a = {ld.start.x, sector.floor.base_height, ld.start.y}; + Vec3 pos_b = {ld.end.x, sector.ceiling.base_height, ld.end.y}; + + float yb = s.floor.base_height; + float yt = s.ceiling.base_height; + + for (Wall& l : s.walls) + { + if (PointInLine(l.start.x, l.start.y, pos_a.x, pos_a.z, pos_b.x, pos_b.z) && + PointInLine(l.end.x, l.end.y, pos_a.x, pos_a.z, pos_b.x, pos_b.z) && + !PosCmp({ l.start.x, 0.0f, l.start.y }, { pos_a.x, 0.0f, pos_a.z }) && + !PosCmp({ l.start.x, 0.0f, l.start.y }, { pos_b.x, 0.0f, pos_b.z }) && + !PosCmp({ l.end.x, 0.0f, l.end.y }, { pos_b.x, 0.0f, pos_b.z }) && + !PosCmp({ l.end.x, 0.0f, l.end.y }, { pos_a.x, 0.0f, pos_a.z })) + { + KP3D_LOG_INFO("{} IS TOUCHING: {}", sector.id, s.id); + // c2t::Point pl0 = { Distance({l.start.x, l.start.y}, {pos_a.x, pos_a.z}), yb }; + // c2t::Point pl1 = { Distance({l.start.x, l.start.y}, {pos_a.x, pos_a.z}), yt }; + // c2t::Point pl2 = { Distance({l.end.x, l.end.y}, {pos_a.x, pos_a.z}), yt }; + // c2t::Point pl3 = { Distance({l.end.x, l.end.y}, {pos_a.x, pos_a.z}), yb }; + // hole.push_back(pl0); + // hole.push_back(pl1); + // hole.push_back(pl2); + // hole.push_back(pl3); + // m_dots.push_back({ l.start.x, yb, -l.start.y }); + // m_dots.push_back({ l.start.x, yt, -l.start.y }); + // m_dots.push_back({ l.end.x, yt, -l.end.y }); + // m_dots.push_back({ l.end.x, yb, -l.end.y }); + + // Note: l is the joined linedef, ld is the original one leading into this one. + // Whatever I do may need to be adjusted as I create more maps... + + //ld.start.x = l.start.x; + //ld.start.y = l.start.y; + XYf old_end = ld.end; + ld.end.x = l.end.x; + ld.end.y = l.end.y; + Wall w1 = ld; + w1.start.x = ld.end.x; + w1.start.y = ld.end.y; + w1.end.x = l.start.x; + w1.end.y = l.start.y; + w1.textures[TEX_FRONT] = nullptr; + Wall w2 = w1; + w2.textures[TEX_FRONT] = ld.textures[TEX_FRONT]; + w2.start.x = w1.end.x; + w2.start.y = w1.end.y; + w2.end.x = old_end.x; + w2.end.y = old_end.y; + + InsertLine(sector.walls, i + 1, w1); + InsertLine(sector.walls, i + 2, w2); + + //sector.walls.insert(sector.walls.begin() + i, 1, w1); + //i++; + //sector.walls.insert(sector.walls.begin() + i + 1, 1, w2); + //i++; + + l.textures[TEX_FRONT] = nullptr; + } + } + } + } +} + void Map::Init() { XYf points[7] = { @@ -478,12 +543,18 @@ void Map::Init() if (bail) continue; - float yv = PerlinNoise2D(steiner_point.x, steiner_point.y, 1.0, 10.0); // Temp if (s.id == 1) - s.floor.steiner_points.push_back({ steiner_point.x, yv, steiner_point.y }); + { + float yv0 = PerlinNoise2D(steiner_point.x, steiner_point.y, 1.0, 10.0); + float yv1 = -PerlinNoise2D(steiner_point.x, steiner_point.y, 0.5, 9.0); + s.floor.steiner_points.push_back({ steiner_point.x, yv0, steiner_point.y }); + s.ceiling.steiner_points.push_back({ steiner_point.x, yv1, steiner_point.y }); + } } } + JoinSectors(s); + // Build level geometry BuildFlat(s, s.floor, false); BuildFlat(s, s.ceiling, true); @@ -557,7 +628,7 @@ void Map::Render() for (Vec3 v : m_dots) { - kp3d::Renderer3D::DrawBillboard(m_dot, v, {0.1f, 0.1f}, true); + kp3d::Renderer3D::DrawBillboard(m_dot, {v.x, v.y, -v.z}, {0.1f, 0.1f}, true); } // Optional wireframe copy diff --git a/KP3Dii/src/KP3D_Map.h b/KP3Dii/src/KP3D_Map.h index dd1562b..39990dd 100644 --- a/KP3Dii/src/KP3D_Map.h +++ b/KP3Dii/src/KP3D_Map.h @@ -20,7 +20,8 @@ struct Wall { enum Flags: byte { - NO_FLAGS + NO_FLAGS, + LD_TOUCHED // ... }; @@ -90,6 +91,7 @@ public: void BuildFlat(Sector& sector, Flat& flat, bool invert); void BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos_b, bool flip, bool flip_u, bool flip_v, XYf uv_offset); void BuildWall(Sector& sector, Wall& wall); + void JoinSectors(Sector& sector); void Init();