diff --git a/Data/sandbox-log.txt b/Data/sandbox-log.txt index 05e2957..6411b5c 100644 --- a/Data/sandbox-log.txt +++ b/Data/sandbox-log.txt @@ -1,19 +1,20 @@ -[02:58:42 PM] Info: Starting... +[04:45:28 PM] Info: Starting... KP3D version 2 =============================== Copyright (C) kpworld.xyz 2018-2024 Contact me! @kp_cftsz -[02:58:42 PM] Info: Initializing SDL -[02:58:42 PM] Info: Initializing OpenGL -[02:58:42 PM] Info: OpenGL version: 4.6.0 NVIDIA 517.70 -[02:58:42 PM] Info: Initializing GLEW -[02:58:42 PM] Info: Initializing SDL_mixer -[02:58:43 PM] Info: Reticulating splines... -[02:58:43 PM] Info: Ready! -[02:58:43 PM] Info: BUILDING SECTOR: 1 -[02:58:43 PM] Info: BUILDING SECTOR: 2 -[02:58:43 PM] Info: BUILDING SECTOR: 3 -[02:58:43 PM] Info: BUILDING SECTOR: 4 -[02:58:43 PM] Info: BUILDING SECTOR: 5 +[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 diff --git a/KP3Dii/src/KP3D_Map.cpp b/KP3Dii/src/KP3D_Map.cpp index 37af8d4..4a6a60b 100644 --- a/KP3Dii/src/KP3D_Map.cpp +++ b/KP3Dii/src/KP3D_Map.cpp @@ -90,12 +90,17 @@ void Map::BuildFlat(Sector& sector, Flat& flat, bool invert) { const float e = 0.001f; + if (sector.inverted) + invert ^= 1; + std::vector steiner_points; for (const auto& st: flat.steiner_points) steiner_points.emplace_back(st.x, st.z); std::vector> subsector_polygons; // unused for now + std::vector secs_inside; +#if 0 for (Sector& s : sectors) { if (s.id == sector.id) @@ -111,18 +116,64 @@ void Map::BuildFlat(Sector& sector, Flat& flat, bool invert) } } - if (ss.size() == s.walls.size()) + if (ss.size() > 0 && s.children.empty())//== s.walls.size()) { - + secs_inside.push_back(&s); for (Wall& l : s.walls) { - l.textures[TEX_FRONT] = nullptr; - l.textures[TEX_BACK] = nullptr; + if (s.inverted) + { + if (FloatCmp(s.floor.base_height, sector.floor.base_height)) + s.floor.texture = nullptr; + //if (s.ceiling.base_height <= sector.ceiling.base_height + 0.000001f) + // s.ceiling.texture = nullptr; + continue; + } + else + { + + l.textures[TEX_FRONT] = nullptr; + } } subsector_polygons.push_back(ss); } } +#endif + for (Sector* sp : sector.children) + { + std::vector ss; + Sector& s = *sp; + if (!s.children.empty()) + continue; + for (Wall& l : s.walls) + { + if (PointInPolygon(sector.walls, l.start)) + { + ss.push_back({ l.start.x, l.start.y }); + } + } + for (Wall& l : s.walls) + { + if (s.inverted) + { + if (!FloatCmp(s.floor.base_height, sector.floor.base_height)) + s.floor.texture = nullptr; + if (!FloatCmp(s.ceiling.base_height, sector.ceiling.base_height)) + s.ceiling.texture = nullptr; + //if (s.ceiling.base_height <= sector.ceiling.base_height + 0.000001f) + // s.ceiling.texture = nullptr; + continue; + } + else + { + + l.textures[TEX_FRONT] = nullptr; + } + } + + subsector_polygons.push_back(ss); + } std::vector sector_polygons; for (Wall& l: sector.walls) @@ -149,7 +200,6 @@ void Map::BuildFlat(Sector& sector, Flat& flat, bool invert) float cu = c.x * 0.5f, cv = c.y * 0.5f; // Build mesh data for floor - if (flat.texture) { Vec3 fva = Vec3(a.x, flat.base_height, a.y); Vec3 fvb = Vec3(b.x, flat.base_height, b.y); @@ -172,15 +222,16 @@ void Map::BuildFlat(Sector& sector, Flat& flat, bool invert) if (FloatCmp(max_height, -MAX)) max_height = flat.base_height; // Fix up the UVs so they keep the right scale - float tw = texture_scale / flat.texture->GetWidth(); - float th = texture_scale / flat.texture->GetHeight(); + float tw = flat.texture ? texture_scale / flat.texture->GetWidth() : 0.0f; + float th = flat.texture ? texture_scale / flat.texture->GetHeight() : 0.0f; Vertex3D vtxa = Vertex3D(fva, Vec2(au * tw, av * th)); Vertex3D vtxb = Vertex3D(fvb, Vec2(bu * tw, bv * th)); Vertex3D vtxc = Vertex3D(fvc, Vec2(cu * tw, cv * th)); flat.triangulated_data.push_back(vtxa); flat.triangulated_data.push_back(vtxb); flat.triangulated_data.push_back(vtxc); - m_mesh.AddBatch(flat.texture, {vtxa, vtxb, vtxc}, !invert); + if (flat.texture) + m_mesh.AddBatch(flat.texture, {vtxa, vtxb, vtxc}, !invert); } } } @@ -196,6 +247,9 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos if (!texture) return; + if (sector.inverted) + flip ^= 1; + Vec3 p1 = Vec3(pos_a.x, pos_a.y, pos_a.z); Vec3 p2 = Vec3(pos_a.x, pos_b.y, pos_a.z); Vec3 p3 = Vec3(pos_b.x, pos_a.y, pos_b.z); @@ -228,6 +282,44 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos 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) wall_polygon.emplace_back(p.x, p.y); @@ -252,12 +344,13 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos Vec3 fvc = Vec3(c.x, c.y, 0.0f).Rotated({ 0, 1, 0 }, -ToDegrees(angle)); fvc.x += p1.x; fvc.z += p1.z; // Fix up the UVs so they keep the right scale - float tw = texture_scale / texture->GetWidth(); - float th = texture_scale / texture->GetHeight(); + float tw = texture ? texture_scale / texture->GetWidth() : 0.0f; + float th = texture ? texture_scale / texture->GetHeight() : 0.0f; Vertex3D vtxa = Vertex3D(fva, Vec2(au * tw, av * th)); Vertex3D vtxb = Vertex3D(fvb, Vec2(bu * tw, bv * th)); Vertex3D vtxc = Vertex3D(fvc, Vec2(cu * tw, cv * th)); - m_mesh.AddBatch(texture, {vtxa, vtxb, vtxc}, flip); + if (texture) + m_mesh.AddBatch(texture, {vtxa, vtxb, vtxc}, flip); } } @@ -308,7 +401,7 @@ void Map::Init() tex4.Load("block.png"); m_dot.Load("dot.png"); - auto build_sector = [&](Texture* wall, Texture* floor, Texture* ceil, float floor_height, float ceil_height, int id, XYf* points, size_t num_points) { + auto build_sector = [&](Texture* wall, Texture* floor, Texture* ceil, float floor_height, float ceil_height, int id, XYf* points, size_t num_points, bool inverted = false) { Sector s; s.ceiling.texture = ceil; s.floor.texture = floor; @@ -316,12 +409,13 @@ void Map::Init() s.ceiling.base_height = ceil_height; s.id = id; s.parent_id = 0; + s.inverted = inverted; float scl = 1.0f; for (size_t i = 0; i < num_points; i++) { XYf start = points[i]; XYf end = points[(i + 1) % num_points]; - s.walls.push_back(Wall{ {nullptr,wall,nullptr,nullptr}, {{0, 0}}, start, end, Wall::NO_FLAGS, (uint)i }); + s.walls.push_back(Wall{ {nullptr,wall,nullptr}, {{0, 0}}, start, end, Wall::NO_FLAGS, (uint)i }); } sectors.push_back(s); @@ -329,10 +423,32 @@ void Map::Init() build_sector(&tex3, &tex, &tex2, -1.0f, 5.0f, 1, points, std::size(points)); build_sector(&tex4, &tex2, &tex2, -1.5f, 4.0f, 2, points2, std::size(points2)); - build_sector(&tex4, &tex, &tex2, -1.5f, 4.0f, 3, points3, std::size(points3)); - build_sector(&tex4, &tex, &tex2, -1.5f, 4.0f, 4, points4, std::size(points4)); + build_sector(&tex4, &tex, &tex2, -1.5f, 4.0f, 3, points3, std::size(points3), true); + build_sector(&tex4, &tex, &tex2, -1.0f, 4.0f, 4, points4, std::size(points4), true); build_sector(&tex3, &tex, &tex2, -0.5f, 3.0f, 5, points5, std::size(points5)); + // Build tree + for (Sector& sp: sectors) + { + for (Sector& s: sectors) + { + if (s.id == sp.id) + continue; + + int cnt = 0; + for (Wall& l: s.walls) + { + if (PointInPolygon(sp.walls, l.start)) + { + cnt++; + } + } + if (cnt == s.walls.size()) + sp.children.push_back(&s); + } + } + + // Preproc for (Sector& s : sectors) { KP3D_LOG_INFO("BUILDING SECTOR: {}", s.id); diff --git a/KP3Dii/src/KP3D_Map.h b/KP3Dii/src/KP3D_Map.h index ff34557..dd1562b 100644 --- a/KP3Dii/src/KP3D_Map.h +++ b/KP3Dii/src/KP3D_Map.h @@ -13,7 +13,6 @@ enum WallTextureIndex { TEX_UPPER, TEX_FRONT, - TEX_BACK, TEX_LOWER }; @@ -25,8 +24,8 @@ struct Wall // ... }; - const Texture* textures[4]; - XYf uv_offset[4]; + const Texture* textures[3]; + XYf uv_offset[3]; XYf start; XYf end; @@ -66,6 +65,10 @@ struct Sector Flat floor; Flat ceiling; + bool inverted = false; + + std::vector children; + }; struct Skybox