Skip to content

Commit

Permalink
Merge more faces. Export to jmf.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Mar 5, 2024
1 parent a079bd3 commit d4d21ee
Show file tree
Hide file tree
Showing 13 changed files with 1,040 additions and 342 deletions.
1,116 changes: 847 additions & 269 deletions src/bsp/Bsp.cpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class Bsp
void delete_model(int modelIdx);
int merge_all_texinfos();
int merge_all_verts(float epsilon = 1.0f);
void round_all_verts(int digits = 8);

// conditionally deletes hulls for entities that aren't using them
STRUCTCOUNT delete_unused_hulls(bool noProgress = false);
Expand Down
7 changes: 7 additions & 0 deletions src/bsp/bsptypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ bool BSPPLANE::update_plane(vec3 newNormal, float fdist, bool flip)
return shouldFlip;
}


bool BSPPLANE::update_plane(bool flip)
{
return update_plane(vNormal, fDist, flip);
}


bool BSPLEAF16::isEmpty()
{
BSPLEAF16 emptyLeaf;
Expand Down
1 change: 1 addition & 0 deletions src/bsp/bsptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ struct BSPPLANE
int nType;

// returns true if the plane was flipped
bool update_plane(bool flip);
bool update_plane(vec3 newNormal, float fdist, bool flip = true);

BSPPLANE() :vNormal(vec3())
Expand Down
7 changes: 5 additions & 2 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,17 +1381,20 @@ void BspRenderer::generateClipnodeBufferForHull(int modelIdx, int hullIdx)
}

std::vector<vec3> faceVerts;
g_mutex_list[2].lock();
for (auto vertIdx : uniqueFaceVerts)
{
faceVerts.push_back(mesh.verts[vertIdx].pos);
}
g_mutex_list[2].unlock();

if (faceVerts.size() < 1)
{
// print_log(get_localized_string(LANG_0282));
continue;
}


faceVerts = getSortedPlanarVerts(faceVerts);

if (faceVerts.size() < 3)
Expand Down Expand Up @@ -1484,8 +1487,8 @@ void BspRenderer::generateClipnodeBufferForHull(int modelIdx, int hullIdx)

/*if (modelIdx > 0 && hullIdx == 0)
{
allVerts = stretch_model(allVerts, 0.1f);
wireframeVerts = stretch_model(wireframeVerts, 0.1f);
allVerts = scaleVerts(allVerts, 0.1f);
wireframeVerts = scaleVerts(wireframeVerts, 0.1f);
}*/

cVert* output = new cVert[allVerts.size()];
Expand Down
7 changes: 3 additions & 4 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2779,9 +2779,9 @@ void Gui::drawMenuBar()
if (map->ents.size())
{
std::string wadstr = map->ents[0]->keyvalues["wad"];
if (wadstr.find(map->bsp_name + ".wad" + ";") == std::string::npos)
if (wadstr.find(map->bsp_name + ".wad;") == std::string::npos)
{
map->ents[0]->keyvalues["wad"] += map->bsp_name + ".wad" + ";";
map->ents[0]->keyvalues["wad"] += map->bsp_name + ".wad;";
}
}
}
Expand Down Expand Up @@ -2820,7 +2820,7 @@ void Gui::drawMenuBar()
ImGui::EndTooltip();
}

static bool merge_faces = false;
static bool merge_faces = true;

if (ImGui::BeginMenu("ValveHammerEditor (.map) [WIP]", map && !map->is_mdl_model))
{
Expand All @@ -2829,7 +2829,6 @@ void Gui::drawMenuBar()
// merge_faces = !merge_faces
}


if (ImGui::MenuItem("Full .map"))
{
map->ExportToMapWIP(g_working_dir, false, merge_faces);
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
// Removing HULL 0 from solid model crashes game when standing on it


std::string g_version_string = "NewBSPGuy v4.18";
std::string g_version_string = "NewBSPGuy v4.19";

bool g_verbose = false;

Expand Down
139 changes: 96 additions & 43 deletions src/qtools/winding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "bsptypes.h"
#include "Bsp.h"


Winding& Winding::operator=(const Winding& other)
{
if (&other == this)
Expand Down Expand Up @@ -40,7 +41,7 @@ Winding::Winding(const BSPPLANE& plane, float epsilon)
org = vright = vup = vec3();
// find the major axis

max = -BOGUS_RANGE;
max = -FLT_MAX_COORD;
int x = -1;
for (i = 0; i < 3; i++)
{
Expand All @@ -55,9 +56,6 @@ Winding::Winding(const BSPPLANE& plane, float epsilon)
{
print_log(get_localized_string(LANG_1008));
}



switch (x)
{
case 0:
Expand Down Expand Up @@ -96,22 +94,14 @@ Winding::Winding(const BSPPLANE& plane, float epsilon)
VectorSubtract(m_Points[3], vup, m_Points[3]);
}

void Winding::getPlane(BSPPLANE& plane)
void Winding::getPlane(BSPPLANE& plane) const
{
vec3 v1, v2;
vec3 plane_normal;
v1 = v2 = plane_normal = vec3();
//hlassert(m_NumPoints >= 3);

if (m_Points.size() >= 3)
{
VectorSubtract(m_Points[2], m_Points[1], v1);
VectorSubtract(m_Points[0], m_Points[1], v2);

CrossProduct(v2, v1, plane_normal);
VectorNormalize(plane_normal);
VectorCopy(plane_normal, plane.vNormal); // change from vec_t
plane.fDist = DotProduct(m_Points[0], plane.vNormal);
vec3 v1 = m_Points[2] - m_Points[1];
vec3 v2 = m_Points[0] - m_Points[1];
plane.vNormal = crossProduct(v2, v1).normalize();
plane.fDist = dotProduct(m_Points[0], plane.vNormal);
}
else
{
Expand Down Expand Up @@ -181,35 +171,32 @@ void Winding::MergeVerts(Bsp* src, float epsilon)
// Remove the colinear point of any three points that forms a triangle which is thinner than ON_EPSILON
void Winding::RemoveColinearPoints(float epsilon)
{
int i;
vec3 v1, v2;
vec3 p1, p2, p3;

size_t NumPoints = m_Points.size();

for (i = 0; i < NumPoints; i++)
for (int i = 0; i < NumPoints; i++)
{
p1 = m_Points[(i + NumPoints - 1) % NumPoints];
p2 = m_Points[i];
p3 = m_Points[(i + 1) % NumPoints];
VectorSubtract(p2, p1, v1);
VectorSubtract(p3, p2, v2);
vec3 p1 = m_Points[(i + NumPoints - 1) % NumPoints];
vec3 p2 = m_Points[i];
vec3 p3 = m_Points[(i + 1) % NumPoints];
vec3 v1 = p2 - p1;
vec3 v2 = p3 - p2;
// v1 or v2 might be close to 0
if (DotProduct(v1, v2) * DotProduct(v1, v2) >= DotProduct(v1, v1) * DotProduct(v2, v2)
- epsilon * epsilon * (DotProduct(v1, v1) + DotProduct(v2, v2) + epsilon * epsilon))
if (dotProduct(v1, v2) * dotProduct(v1, v2) >= dotProduct(v1, v1) * dotProduct(v2, v2)
- epsilon * epsilon * (dotProduct(v1, v1) + dotProduct(v2, v2) + epsilon * epsilon))
// v2 == k * v1 + v3 && abs (v3) < ON_EPSILON || v1 == k * v2 + v3 && abs (v3) < ON_EPSILON
{
NumPoints--;
for (; i < NumPoints; i++)
{
VectorCopy(m_Points[i + 1], m_Points[i]);
m_Points[i] = m_Points[i + 1];
}
i = -1;
continue;
}
}

m_Points.resize(NumPoints);

}

bool Winding::Clip(BSPPLANE& split, bool keepon, float epsilon)
Expand Down Expand Up @@ -339,21 +326,52 @@ void Winding::Round(float epsilon)
}
}

bool Winding::IsConvex(const BSPPLANE& plane, float epsilon)
void Winding::Offset(vec3 Offset)
{
vec3 normal = plane.vNormal;
vec3 delta;
float dot;

for (int i = 0; i < m_Points.size(); i++)
for (auto& p : m_Points)
{
delta = m_Points[i] - m_Points[(i + 1) % m_Points.size()];
dot = dotProduct(delta, normal);
if (dot > epsilon)
return false;
p += Offset;
}
}

return true;
bool Winding::IsConvex()
{
int numPoint = static_cast<int>(m_Points.size());
float positiveArea = 0.0;
float negativeArea = 0.0;

static double tolerance = 1.0e-12;

float a2 = 0.0f;
vec3 maxCross(0.0, 0.0, 0.0);

vec3 vecA = m_Points[1] - m_Points[0];
vec3 vecB;
for (int i = 2; i < numPoint; i++, vecA = vecB) {
vecB = m_Points[i] - m_Points[0];
vec3 c = crossProduct(vecA, vecB);
float b2 = (c.x * c.x + c.y * c.y + c.z * c.z);
if (b2 > a2) {
a2 = b2;
maxCross = c;
}
}

vec3 unitNormal = maxCross.normalize();
vecA = m_Points[0] - m_Points[numPoint - 1];
for (int i = 1; i <= numPoint; i++, vecA = vecB) {
vecB = m_Points[i % numPoint] - m_Points[i - 1];
vec3 c = crossProduct(vecA, vecB);
float b = dotProduct(c, unitNormal);
if (b >= 0.0) {
positiveArea += b;
}
else {
negativeArea += b;
}
}

return fabs(negativeArea) < tolerance * positiveArea;
}

bool ArePointsOnALine(const std::vector<vec3>& points)
Expand Down Expand Up @@ -447,7 +465,6 @@ Winding* Winding::Merge(const Winding& other, const BSPPLANE& plane, float epsil
return NULL; // not a convex polygon

bool keep2 = (dot < -epsilon);

//
// build the new polygon
//
Expand All @@ -470,5 +487,41 @@ Winding* Winding::Merge(const Winding& other, const BSPPLANE& plane, float epsil
newf->m_Points.push_back(other.m_Points[l]);
}

return newf;
newf->RemoveColinearPoints();


if (newf->m_Points.size() >= 3)
{
for (i = 0; i < m_Points.size(); i++)
{
for (j = i + 1; j < m_Points.size(); j++)
{
if (j != i)
{
if (m_Points[i].equal(m_Points[j], 0.01f))
{
// Has duplicate points (NO NORMAL FOR PLANE!)
delete newf;
return NULL;
}
}
}
}
if (!newf->IsConvex())
{
// not a convex polygon
delete newf;
return NULL;
}
vec3 norm;
float dist;
if (getPlaneFromVerts(newf->m_Points, norm, dist) && norm.length() > 0.01f)
{
return newf;
}
// (NO NORMAL FOR PLANE!)
}

delete newf;
return NULL;
}
5 changes: 3 additions & 2 deletions src/qtools/winding.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ class Winding
Winding(const BSPPLANE& plane, float epsilon = ON_EPSILON);
Winding();
Winding(const Winding& other);
void getPlane(BSPPLANE& plane);
void getPlane(BSPPLANE& plane) const;
Winding& operator=(const Winding& other);

Winding * Merge(const Winding& other, const BSPPLANE& plane, float epsilon = ON_EPSILON);

bool IsConvex(const BSPPLANE& plane, float epsilon = ON_EPSILON);
bool IsConvex();
void MergeVerts(Bsp * src, float epsilon = ON_EPSILON);
void RemoveColinearPoints(float epsilon = ON_EPSILON);
bool Clip(BSPPLANE& split, bool keepon, float epsilon = ON_EPSILON);
void Round(float epsilon = ON_EPSILON);
void Offset(vec3 offset);
};
Loading

0 comments on commit d4d21ee

Please sign in to comment.