Visual debugging of room bounds

This commit is contained in:
lawnjelly 2019-09-23 12:31:26 +01:00 committed by GitHub
parent ebff0a04e7
commit cd4deb39e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 29 deletions

View File

@ -113,6 +113,8 @@ public:
LRoom();
Spatial * GetGodotRoom() const;
// retained purely for debugging visualization
Geometry::MeshData m_Bound_MeshData;
};

View File

@ -253,6 +253,22 @@ bool LRoomConverter::Convert_Bound(LRoom &lroom, MeshInstance * pMI)
}
LPRINT(2, "\t\t\tcontained " + itos(lroom.m_Bound.m_Planes.size()) + " planes.");
// make a copy of the mesh data for debugging
// note this could be avoided in release builds? NYI
lroom.m_Bound_MeshData = md;
// for (int f=0; f<md.faces.size(); f++)
// {
// String sz;
// sz = "face " + itos (f) + ", indices ";
// for (int i=0; i<md.faces[f].indices.size(); i++)
// {
// sz += itos(md.faces[f].indices[i]) + ", ";
// }
// LPRINT(2, sz);
// }
return true;
}
}

View File

@ -40,6 +40,7 @@ LRoomManager::LRoomManager()
m_pPrev_VisibleRoomList = &m_VisibleRoomList_B;
m_bDebugPlanes = false;
m_bDebugBounds = false;
}
int LRoomManager::FindClosestRoom(const Vector3 &pt) const
@ -206,10 +207,20 @@ void LRoomManager::CreateDebug()
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
m_mat_Debug_Planes->set_albedo(Color(1, 0, 1, 1));
p->set_material_override(m_mat_Debug_Planes);
p->hide();
ImmediateGeometry * b = memnew(ImmediateGeometry);
b->set_name("debug_bounds");
add_child(b);
m_ID_DebugBounds = b->get_instance_id();
m_mat_Debug_Bounds = Ref<SpatialMaterial>(memnew(SpatialMaterial));
//m_mat_Debug_Bounds->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
m_mat_Debug_Bounds->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
m_mat_Debug_Bounds->set_albedo(Color(0, 0, 1, 0.4));
b->set_material_override(m_mat_Debug_Bounds);
b->hide();
}
@ -424,6 +435,22 @@ Node * LRoomManager::rooms_get_room(int room_id)
return pRoom->GetGodotRoom();
}
void LRoomManager::rooms_set_debug_bounds(bool bActive)
{
m_bDebugBounds = bActive;
Object * pObj = ObjectDB::get_instance(m_ID_DebugBounds);
ImmediateGeometry * im = Object::cast_to<ImmediateGeometry>(pObj);
if (!im)
return;
if (bActive)
im->show();
else
im->hide();
}
void LRoomManager::rooms_set_debug_planes(bool bActive)
{
m_bDebugPlanes = bActive;
@ -437,7 +464,6 @@ void LRoomManager::rooms_set_debug_planes(bool bActive)
im->show();
else
im->hide();
}
@ -648,39 +674,70 @@ void LRoomManager::FrameUpdate()
// hide all the DOB
// draw debug
FrameUpdate_DrawDebug(cam);
FrameUpdate_DrawDebug(cam, *pRoom);
// when running, emit less debugging output so as not to choke the IDE
Lawn::LDebug::m_bRunning = true;
}
void LRoomManager::FrameUpdate_DrawDebug(const LCamera &cam)
void LRoomManager::FrameUpdate_DrawDebug(const LCamera &cam, const LRoom &lroom)
{
if (!m_bDebugPlanes)
return;
Vector3 ptCam = cam.m_ptPos;
// slight adjustment to prevent parallel lines in viewport
ptCam += (cam.m_ptDir * 0.1f);
Object * pObj = ObjectDB::get_instance(m_ID_DebugPlanes);
ImmediateGeometry * im = Object::cast_to<ImmediateGeometry>(pObj);
if (!im)
return;
im->clear();
im->begin(Mesh::PRIMITIVE_LINES, NULL);
int nVerts = m_DebugPlanes.size();
for (int n=0; n<nVerts; n++)
if (m_bDebugPlanes)
{
im->add_vertex(ptCam);
im->add_vertex(m_DebugPlanes[n]);
Vector3 ptCam = cam.m_ptPos;
// slight adjustment to prevent parallel lines in viewport
ptCam += (cam.m_ptDir * 0.1f);
Object * pObj = ObjectDB::get_instance(m_ID_DebugPlanes);
ImmediateGeometry * im = Object::cast_to<ImmediateGeometry>(pObj);
if (!im)
return;
im->clear();
im->begin(Mesh::PRIMITIVE_LINES, NULL);
int nVerts = m_DebugPlanes.size();
for (int n=0; n<nVerts; n++)
{
im->add_vertex(ptCam);
im->add_vertex(m_DebugPlanes[n]);
}
im->end();
}
// if debug bounds are on and there is a bound for this room
const Geometry::MeshData &md = lroom.m_Bound_MeshData;
if (m_bDebugBounds && md.faces.size())
{
Object * pObj = ObjectDB::get_instance(m_ID_DebugBounds);
ImmediateGeometry * im = Object::cast_to<ImmediateGeometry>(pObj);
if (!im)
return;
im->clear();
im->begin(Mesh::PRIMITIVE_TRIANGLES, NULL);
for (int n=0; n<md.faces.size(); n++)
{
const Geometry::MeshData::Face &f = md.faces[n];
int numTris = f.indices.size() - 2;
for (int t=0; t<numTris; t++)
{
im->set_normal(f.plane.normal);
im->add_vertex(md.vertices[f.indices[0]]);
im->add_vertex(md.vertices[f.indices[t+1]]);
im->add_vertex(md.vertices[f.indices[t+2]]);
}
}
im->end();
}
im->end();
}
void LRoomManager::_notification(int p_what) {
@ -718,6 +775,7 @@ void LRoomManager::_bind_methods()
ClassDB::bind_method(D_METHOD("rooms_log_frame"), &LRoomManager::rooms_log_frame);
ClassDB::bind_method(D_METHOD("rooms_set_active"), &LRoomManager::rooms_set_active);
ClassDB::bind_method(D_METHOD("rooms_set_debug_planes"), &LRoomManager::rooms_set_debug_planes);
ClassDB::bind_method(D_METHOD("rooms_set_debug_bounds"), &LRoomManager::rooms_set_debug_bounds);
// functions to add dynamic objects to the culling system

View File

@ -81,8 +81,8 @@ public:
// turn on and off culling for debugging
void rooms_set_active(bool bActive);
void rooms_set_debug_planes(bool bActive);
void rooms_set_debug_bounds(bool bActive);
// 0 to 6 .. defaults to 4 which is (2) in our priorities (i.e. 6 - level)
void rooms_set_logging(int level);
@ -148,7 +148,7 @@ private:
void FrameUpdate_FrustumOnly();
// draw planes and room hulls
void FrameUpdate_DrawDebug(const LCamera &cam);
void FrameUpdate_DrawDebug(const LCamera &cam, const LRoom &lroom);
// find which room is linked by a portal
LRoom &Portal_GetLinkedRoom(const LPortal &port);
@ -160,12 +160,15 @@ private:
public:
// whether debug planes is switched on
bool m_bDebugPlanes;
bool m_bDebugBounds;
// the planes are shown as a list of lines from the camera to the portal verts
LVector<Vector3> m_DebugPlanes;
private:
ObjectID m_ID_DebugPlanes;
ObjectID m_ID_DebugBounds;
Ref<SpatialMaterial> m_mat_Debug_Planes;
Ref<SpatialMaterial> m_mat_Debug_Bounds;
};
#endif