Moar mapgen progress; connected sectors work now

This commit is contained in:
KP 2024-07-22 16:47:39 -05:00
parent efccd934fb
commit 831cd9a83e
3 changed files with 151 additions and 31 deletions

View file

@ -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

View file

@ -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<c2t::Point> steiner_points;
for (const auto& st: flat.steiner_points)
steiner_points.emplace_back(st.x, st.z);
std::vector<std::vector<c2t::Point>> subsector_polygons; // unused for now
std::vector<Sector*> 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)
{
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;
l.textures[TEX_BACK] = nullptr;
}
}
subsector_polygons.push_back(ss);
}
}
#endif
for (Sector* sp : sector.children)
{
std::vector<c2t::Point> 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<c2t::Point> 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,14 +222,15 @@ 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);
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<std::vector<c2t::Point>> holes; // unused for now
// Begin: holes
for (Sector& s : sectors)
{
if (s.id == sector.id)
continue;
std::vector<c2t::Point> 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<c2t::Point> wall_polygon;
for (Vec3& p: points)
wall_polygon.emplace_back(p.x, p.y);
@ -252,11 +344,12 @@ 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));
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);

View file

@ -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<Sector*> children;
};
struct Skybox