Fix certain broken normals

This commit is contained in:
RaveYard 2022-06-22 13:21:06 +02:00
parent 038259a590
commit 114b5c68d2
3 changed files with 37 additions and 5 deletions

View file

@ -539,7 +539,7 @@ void LevelMesh::CreateSideSurfaces(FLevel &doomMap, IntSideDef *side)
surf->verts[1].z = xfloor->floorplane.zAt(v1.x, v1.y); surf->verts[1].z = xfloor->floorplane.zAt(v1.x, v1.y);
surf->verts[2].z = xfloor->ceilingplane.zAt(v2.x, v2.y); surf->verts[2].z = xfloor->ceilingplane.zAt(v2.x, v2.y);
surf->verts[3].z = xfloor->ceilingplane.zAt(v1.x, v1.y); surf->verts[3].z = xfloor->ceilingplane.zAt(v1.x, v1.y);
surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2]); surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2], surf->verts[3]);
surf->plane.SetDistance(surf->verts[0]); surf->plane.SetDistance(surf->verts[0]);
float texZ = surf->verts[0].z; float texZ = surf->verts[0].z;
@ -589,7 +589,7 @@ void LevelMesh::CreateSideSurfaces(FLevel &doomMap, IntSideDef *side)
surf->verts[2].z = v1BottomBack; surf->verts[2].z = v1BottomBack;
surf->verts[3].z = v2BottomBack; surf->verts[3].z = v2BottomBack;
surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2]); surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2], surf->verts[3]);
surf->plane.SetDistance(surf->verts[0]); surf->plane.SetDistance(surf->verts[0]);
surf->type = ST_LOWERSIDE; surf->type = ST_LOWERSIDE;
surf->typeIndex = typeIndex; surf->typeIndex = typeIndex;
@ -647,7 +647,7 @@ void LevelMesh::CreateSideSurfaces(FLevel &doomMap, IntSideDef *side)
surf->verts[2].z = v1Top; surf->verts[2].z = v1Top;
surf->verts[3].z = v2Top; surf->verts[3].z = v2Top;
surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2]); surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2], surf->verts[3]);
surf->plane.SetDistance(surf->verts[0]); surf->plane.SetDistance(surf->verts[0]);
surf->type = ST_UPPERSIDE; surf->type = ST_UPPERSIDE;
surf->typeIndex = typeIndex; surf->typeIndex = typeIndex;
@ -695,7 +695,7 @@ void LevelMesh::CreateSideSurfaces(FLevel &doomMap, IntSideDef *side)
surf->verts[2].z = v1Top; surf->verts[2].z = v1Top;
surf->verts[3].z = v2Top; surf->verts[3].z = v2Top;
surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2]); surf->plane.SetNormal(surf->verts[0], surf->verts[1], surf->verts[2], surf->verts[3]);
surf->plane.SetDistance(surf->verts[0]); surf->plane.SetDistance(surf->verts[0]);
surf->type = ST_MIDDLESIDE; surf->type = ST_MIDDLESIDE;
surf->typeIndex = typeIndex; surf->typeIndex = typeIndex;

View file

@ -98,7 +98,8 @@ public:
const vec3 &Normal() const; const vec3 &Normal() const;
vec3 &Normal(); vec3 &Normal();
Plane &SetNormal(const vec3 &normal); Plane &SetNormal(const vec3 &normal);
Plane &SetNormal(const vec3 &pt1, const vec3 &pt2, const vec3 &pt3); Plane& SetNormal(const vec3& pt1, const vec3& pt2, const vec3& pt3);
Plane& SetNormal(const vec3& v1, const vec3& v2, const vec3& v3, const vec3& v4);
float Distance(const vec3 &point); float Distance(const vec3 &point);
Plane &SetDistance(const vec3 &point); Plane &SetDistance(const vec3 &point);
bool IsFacing(const float yaw); bool IsFacing(const float yaw);

View file

@ -86,6 +86,37 @@ Plane &Plane::SetNormal(const vec3 &pt1, const vec3 &pt2, const vec3 &pt3)
return *this; return *this;
} }
Plane& Plane::SetNormal(const vec3& v1, const vec3& v2, const vec3& v3, const vec3& v4)
{
// Handle cases where two vertices share the same position
// TODO fix this hack
// Certain vertex positions are close enough to cause NaN, but aren't equal
auto iv1 = ivec3(v1);
auto iv2 = ivec3(v2);
auto iv3 = ivec3(v3);
auto iv4 = ivec3(v4);
if (iv1 == iv3)
{
return SetNormal(v1, v2, v4);
}
else if (iv2 == iv4)
{
return SetNormal(v1, v2, v3);
}
else if (iv1 == iv2)
{
return SetNormal(v2, v3, v4);
}
else if (iv2 == iv3)
{
return SetNormal(v1, v2, v4);
}
return SetNormal(v1, v2, v3);
}
vec3 const &Plane::Normal() const vec3 const &Plane::Normal() const
{ {
return *reinterpret_cast<const vec3*>(&a); return *reinterpret_cast<const vec3*>(&a);