This commit is contained in:
KP 2024-07-22 22:25:33 -05:00
parent f0b58ab16a
commit 85e0661dc5
8 changed files with 83 additions and 62 deletions

View file

@ -41,7 +41,7 @@ float dist(vec2 p0, vec2 pf){return sqrt((pf.x-p0.x)*(pf.x-p0.x)+(pf.y-p0.y)*(pf
void main()
{
Light light;
light.position = vec3(5.0, -4.0, 5.0);
light.position = vec3(5.0, 4.0, 5.0);
light.diffuse = vec3(1.0, 1.0, 1.0);

View file

@ -1,20 +1,14 @@
[08:20:13 PM] Info: Starting...
[10:24:19 PM] Info: Starting...
KP3D version 2
===============================
Copyright (C) kpworld.xyz 2018-2024
Contact me! @kp_cftsz
[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
[10:24:19 PM] Info: Initializing SDL
[10:24:20 PM] Info: Initializing OpenGL
[10:24:20 PM] Info: OpenGL version: 4.6.0 NVIDIA 536.23
[10:24:20 PM] Info: Initializing GLEW
[10:24:20 PM] Info: Initializing SDL_mixer
[10:24:20 PM] Info: Reticulating splines...
[10:24:20 PM] Info: Ready!

View file

@ -106,6 +106,11 @@ void Sweep::EdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point)
{
if (!triangle)
{
return;
}
if (IsEdgeSideOfTriangle(*triangle, ep, eq)) {
return;
}

View file

@ -258,7 +258,7 @@ void Map::BuildFlat(Sector& sector, Flat& flat, bool invert)
* To do this we perform a similar approach to BuildFlat, but because the walls aren't strictly along one axis we have to
* "flatten" them to perform the math to triangulate them. This is done by projecting the 3D points to 2D.
*/
void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos_b, bool flip, bool flip_u, bool flip_v, XYf uv_offset)
void Map::BuildQuad(Sector& sector, Flat& flat_top, Flat& flat_bottom, const Texture* texture, Vec3 pos_a, Vec3 pos_b, bool flip, bool flip_u, bool flip_v, XYf uv_offset)
{
if (!texture)
return;
@ -273,7 +273,7 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos
std::vector<Vec3> points;
points.emplace_back(0.0f);
for (const Vertex3D& v: sector.floor.triangulated_data)
for (const Vertex3D& v: flat_bottom.triangulated_data)
{
if (!PointInLine(v.position.x, v.position.z, pos_a.x, pos_a.z, pos_b.x, pos_b.z))
continue;
@ -288,9 +288,8 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos
points.push_back(mpos);
}
std::vector<Vec3> 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)
//top_points.emplace_back(Distance({ p4.x, p4.z }, { p1.x, p1.z }), p4.y, 0); // This may be re-enabled, BUT
for (const Vertex3D& v: flat_top.triangulated_data)
{
if (!PointInLine(v.position.x, v.position.z, pos_a.x, pos_a.z, pos_b.x, pos_b.z))
continue;
@ -299,7 +298,7 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos
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; });
std::sort(top_points.begin() , top_points.end(), [&](Vec3 a, Vec3 b) { return a.x > b.x; }); // ^^^^ If you do, add +1 to begin()
points.insert(points.end(), top_points.begin(), top_points.end());
float angle = atan2({ p4.z - p1.z }, { p4.x - p1.x });
@ -307,8 +306,6 @@ 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
// End: holes
std::vector<c2t::Point> wall_polygon;
for (Vec3& p: points)
wall_polygon.emplace_back(p.x, p.y);
@ -323,6 +320,12 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos
c2t::Point b = clipper_out.at(i + 1);
c2t::Point c = clipper_out.at(i + 2);
if (flip)
{
std::swap(b, c);
}
float au = a.x * 0.5f, av = a.y * 0.5f;
float bu = b.x * 0.5f, bv = b.y * 0.5f;
float cu = c.x * 0.5f, cv = c.y * 0.5f;
@ -339,20 +342,32 @@ void Map::BuildQuad(Sector& sector, const Texture* texture, Vec3 pos_a, Vec3 pos
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);
m_mesh.AddBatch(texture, {vtxa, vtxb, vtxc}, false);
}
}
void Map::BuildWall(Sector& sector, Wall& wall)
{
Vec3 a = {wall.start.x, sector.floor.base_height, wall.start.y};
Vec3 b = {wall.end.x, sector.ceiling.base_height, wall.end.y};
BuildQuad(sector, wall.textures[TEX_FRONT], a, b, false, false, false, wall.uv_offset[TEX_FRONT]);
Vec3 a = {wall.start.x, 0.0f, wall.start.y};
Vec3 b = {wall.end.x, 0.0f, wall.end.y};
BuildQuad(sector, sector.floor, sector.ceiling, wall.textures[TEX_FRONT], a, b, false, false, false, wall.uv_offset[TEX_FRONT]);
if (wall.flags & Wall::OPENING)
{
bool flip = sector.floor.base_height > wall.portal->floor.base_height;
bool flip2 = sector.ceiling.base_height < wall.portal->ceiling.base_height;
//if (flip)
// flip2 ^= 1;
//if (og_flip2)
// flip ^= 1;
BuildQuad(sector, sector.floor, wall.portal->floor, wall.textures[TEX_LOWER], a, b, flip, false, false, wall.uv_offset[TEX_LOWER]);
BuildQuad(sector, sector.ceiling, wall.portal->ceiling, wall.textures[TEX_UPPER], a, b, flip2, false, false, wall.uv_offset[TEX_UPPER]);
}
}
void Map::JoinSectors(Sector& sector)
{
//for (Wall& ld: sector.walls)
// "Connect" sectors by splitting up their walls where other sectors intersect
for (int i = 0; i < sector.walls.size(); i++)
{
Wall& ld = sector.walls[i];
@ -376,25 +391,7 @@ void Map::JoinSectors(Sector& sector)
!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;
//KP3D_LOG_INFO("{} IS TOUCHING: {}", sector.id, s.id);
XYf old_end = ld.end;
ld.end.x = l.end.x;
ld.end.y = l.end.y;
@ -404,8 +401,9 @@ void Map::JoinSectors(Sector& sector)
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];
w1.portal = &s;
w1.flags = Wall::OPENING;
Wall w2 = ld;
w2.start.x = w1.end.x;
w2.start.y = w1.end.y;
w2.end.x = old_end.x;
@ -414,11 +412,6 @@ void Map::JoinSectors(Sector& sector)
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;
}
}
@ -428,6 +421,9 @@ void Map::JoinSectors(Sector& sector)
void Map::Init()
{
sectors.clear();
m_mesh.Reset();
XYf points[7] = {
{1, 1 },
{6, 4 },
@ -470,6 +466,7 @@ void Map::Init()
Sector s;
s.ceiling.texture = ceil;
s.floor.texture = floor;
s.floor.floor = true;
s.floor.base_height = floor_height;
s.ceiling.base_height = ceil_height;
s.id = id;
@ -480,7 +477,7 @@ void Map::Init()
{
XYf start = points[i];
XYf end = points[(i + 1) % num_points];
s.walls.push_back(Wall{ {nullptr,wall,nullptr}, {{0, 0}}, start, end, Wall::NO_FLAGS, (uint)i });
s.walls.push_back(Wall{ {wall,wall,wall}, {{0, 0}}, start, end, Wall::NO_FLAGS, (uint)i });
}
sectors.push_back(s);
@ -490,7 +487,8 @@ void Map::Init()
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), 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_sector(&tex3, &tex, &tex2, -1.2f, 3.0f, 5, points5, std::size(points5));
build_sector(&tex3, &tex, &tex2, test_u, test_l, 5, points5, std::size(points5), false);
// Build tree
for (Sector& sp: sectors)
@ -516,7 +514,7 @@ void Map::Init()
// Preproc
for (Sector& s : sectors)
{
KP3D_LOG_INFO("BUILDING SECTOR: {}", s.id);
// KP3D_LOG_INFO("BUILDING SECTOR: {}", s.id);
// Build up "steiner points" for terrain only
// We'll do this along a grid for now; it should be noted that this will be expected to be part of the sector data later
@ -543,7 +541,7 @@ void Map::Init()
if (bail)
continue;
if (s.id == 1)
if (s.id == 1 || s.id == 5)
{
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);
@ -558,10 +556,11 @@ void Map::Init()
// Build level geometry
BuildFlat(s, s.floor, false);
BuildFlat(s, s.ceiling, true);
}
for (Sector& s : sectors)
for (Wall& ld : s.walls)
BuildWall(s, ld);
}
// Go through all of the vertices and align them to a grid (very roughly; might want to revisit this later)
#if 0

View file

@ -16,12 +16,14 @@ enum WallTextureIndex
TEX_LOWER
};
struct Sector;
struct Wall
{
enum Flags: byte
{
NO_FLAGS,
LD_TOUCHED
OPENING
// ...
};
@ -33,6 +35,8 @@ struct Wall
byte flags;
uint uid;
Sector* portal = nullptr;
};
struct Flat
@ -43,6 +47,7 @@ struct Flat
float max_height;
std::vector<Vec3> steiner_points;
std::vector<Vertex3D> triangulated_data;
bool floor = false;
};
struct Sector
@ -89,7 +94,7 @@ public:
ErrCode SaveToFile(const std::string& path);
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 BuildQuad(Sector& sector, Flat& flat_top, Flat& flat_bottom, 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);
@ -108,6 +113,8 @@ public:
bool render_wireframe = true;
float texture_scale = 128.0f;
float test_u = -1.5f, test_l = 3.0f;
private:
StaticMesh m_mesh;
Skybox m_skybox_data;

View file

@ -219,7 +219,7 @@ void StaticMesh::Finalize(NormalGenType gen_normals)
for (size_t j = 0; j < current.vertex_data.size(); j++)
{
auto& jv = current.vertex_data[j];
jv.normal = {0,1,0};
jv.normal = {0.5,0.5,0.5};
}
// The following just generates face normals for each triangle
for (size_t j = 0; j < current.vertex_data.size(); j += 3)

View file

@ -58,6 +58,7 @@ struct RenderBatch3D
std::vector<uint> index_data;
GLuint gl_bbo_id = 0;
std::vector<VertexBoneData> bone_data;
bool flipped = false;
};
enum NormalGenType

View file

@ -54,6 +54,21 @@ void Sandbox::Update()
map.Rebuild(kp3d::GEN_SMOOTH_NORMALS);
KeyReset(kp3d::KEY_F3);
}
if (IsKeyDown(kp3d::KEY_B))
{
map.Init();
//map.Rebuild(kp3d::GEN_NORMALS);
//KeyReset(kp3d::KEY_B);
}
if (IsKeyDown(kp3d::KEY_Y))
map.test_u += 0.1f;
if (IsKeyDown(kp3d::KEY_H))
map.test_u -= 0.1f;
if (IsKeyDown(kp3d::KEY_U))
map.test_l += 0.1f;
if (IsKeyDown(kp3d::KEY_J))
map.test_l -= 0.1f;
// Mouse-look
if (IsKeyDown(kp3d::KEY_ESCAPE))