mirror of
https://github.com/Relintai/godot-lportal.git
synced 2024-11-11 10:52:09 +01:00
Debug lines to show clipping planes to portals
This commit is contained in:
parent
024aef2a1e
commit
d9822e7037
13
lportal.cpp
13
lportal.cpp
@ -22,6 +22,7 @@
|
||||
#include "core/engine.h"
|
||||
#include "lroom.h"
|
||||
#include "ldebug.h"
|
||||
#include "lroom_manager.h"
|
||||
|
||||
|
||||
bool LPortal::NameStartsWith(Node * pNode, String szSearch)
|
||||
@ -71,7 +72,7 @@ String LPortal::FindNameAfter(Node * pNode, String szStart)
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
// add clipping planes to the vector formed by each portal edge and the camera
|
||||
void LPortal::AddPlanes(const Vector3 &ptCam, LVector<Plane> &planes) const
|
||||
void LPortal::AddPlanes(LRoomManager &manager, const Vector3 &ptCam, LVector<Plane> &planes) const
|
||||
{
|
||||
// short version
|
||||
const Vector<Vector3> &pts = m_ptsWorld;
|
||||
@ -97,6 +98,16 @@ void LPortal::AddPlanes(const Vector3 &ptCam, LVector<Plane> &planes) const
|
||||
// first and last
|
||||
p = Plane(ptCam, pts[0], pts[nPoints-1]);
|
||||
planes.push_back(p);
|
||||
|
||||
// debug
|
||||
if (!manager.m_bDebugPlanes)
|
||||
return;
|
||||
|
||||
for (int n=0; n<nPoints; n++)
|
||||
{
|
||||
manager.m_DebugPlanes.push_back(pts[n]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "lvector.h"
|
||||
|
||||
class LRoom;
|
||||
class LRoomManager;
|
||||
|
||||
class LPortal {
|
||||
public:
|
||||
@ -50,7 +51,7 @@ public:
|
||||
String m_szName;
|
||||
|
||||
LPortal::eClipResult ClipWithPlane(const Plane &p) const;
|
||||
void AddPlanes(const Vector3 &ptCam, LVector<Plane> &planes) const;
|
||||
void AddPlanes(LRoomManager &manager, const Vector3 &ptCam, LVector<Plane> &planes) const;
|
||||
|
||||
// normal determined by winding order
|
||||
Vector<Vector3> m_ptsWorld;
|
||||
|
@ -90,7 +90,7 @@ LRoom * LRoom::DOB_Update(LRoomManager &manager, Spatial * pDOB)
|
||||
const Vector3 &pt = pDOB->get_global_transform().origin;
|
||||
|
||||
// is it the camera?
|
||||
bool bCamera = pDOB->get_instance_id() == manager.m_cameraID;
|
||||
bool bCamera = pDOB->get_instance_id() == manager.m_ID_camera;
|
||||
float slop = 0.2f;
|
||||
if (bCamera)
|
||||
slop = 0.0f;
|
||||
@ -144,7 +144,7 @@ void LRoom::FinalizeVisibility(LRoomManager &manager)
|
||||
const LDob &dob = m_DOBs[n];
|
||||
|
||||
// don't cull the main camera
|
||||
if (dob.m_ID == manager.m_cameraID)
|
||||
if (dob.m_ID == manager.m_ID_camera)
|
||||
continue;
|
||||
|
||||
Spatial * pS = dob.GetSpatial();
|
||||
@ -464,7 +464,7 @@ void LRoom::DetermineVisibility_Recursive(LRoomManager &manager, int depth, cons
|
||||
new_planes.copy_from(planes);
|
||||
|
||||
// add the planes for the portal
|
||||
port.AddPlanes(cam.m_ptPos, new_planes);
|
||||
port.AddPlanes(manager, cam.m_ptPos, new_planes);
|
||||
|
||||
|
||||
if (pLinkedRoom)
|
||||
|
12
lroom.h
12
lroom.h
@ -51,11 +51,10 @@ public:
|
||||
|
||||
class LRoom
|
||||
{
|
||||
friend class LPortal;
|
||||
friend class LRoomManager;
|
||||
friend class LRoomConverter;
|
||||
private:
|
||||
|
||||
public:
|
||||
|
||||
// static objects
|
||||
LVector<LSob> m_SOBs;
|
||||
|
||||
@ -82,12 +81,12 @@ private:
|
||||
// when registering DOBs and teleporting them
|
||||
LBound m_Bound;
|
||||
|
||||
String m_szName;
|
||||
|
||||
public:
|
||||
String m_szName;
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
const String &get_name() const {return m_szName;}
|
||||
|
||||
protected:
|
||||
// main function
|
||||
void DetermineVisibility_Recursive(LRoomManager &manager, int depth, const LCamera &cam, const LVector<Plane> &planes, int portalID_from = -1);
|
||||
void FirstTouch(LRoomManager &manager);
|
||||
@ -111,7 +110,6 @@ protected:
|
||||
bool DOB_Remove(unsigned int ui);
|
||||
LRoom * DOB_Update(LRoomManager &manager, Spatial * pDOB);
|
||||
|
||||
public:
|
||||
LRoom();
|
||||
Spatial * GetGodotRoom() const;
|
||||
|
||||
|
@ -89,7 +89,7 @@ int LRoomConverter::FindRoom_ByName(String szName) const
|
||||
{
|
||||
for (int n=0; n<LMAN->m_Rooms.size(); n++)
|
||||
{
|
||||
if (LMAN->m_Rooms[n].m_szName == szName)
|
||||
if (LMAN->m_Rooms[n].m_szName == szName)
|
||||
return n;
|
||||
}
|
||||
|
||||
|
@ -24,10 +24,12 @@
|
||||
#include "scene/3d/mesh_instance.h"
|
||||
#include "lroom_converter.h"
|
||||
#include "ldebug.h"
|
||||
#include "scene/3d/immediate_geometry.h"
|
||||
|
||||
LRoomManager::LRoomManager()
|
||||
{
|
||||
m_cameraID = 0;
|
||||
m_ID_camera = 0;
|
||||
m_ID_DebugPlanes = 0;
|
||||
m_uiFrameCounter = 0;
|
||||
m_iLoggingLevel = 2;
|
||||
m_bActive = true;
|
||||
@ -36,6 +38,8 @@ LRoomManager::LRoomManager()
|
||||
// to know which rooms to hide we keep track of which were shown this, and the previous frame
|
||||
m_pCurr_VisibleRoomList = &m_VisibleRoomList_A;
|
||||
m_pPrev_VisibleRoomList = &m_VisibleRoomList_B;
|
||||
|
||||
m_bDebugPlanes = false;
|
||||
}
|
||||
|
||||
int LRoomManager::FindClosestRoom(const Vector3 &pt) const
|
||||
@ -185,6 +189,30 @@ bool LRoomManager::dob_register_hint(Node * pDOB, float radius, Node * pRoom)
|
||||
}
|
||||
|
||||
|
||||
void LRoomManager::CreateDebug()
|
||||
{
|
||||
ImmediateGeometry * p = memnew(ImmediateGeometry);
|
||||
p->set_name("debug_planes");
|
||||
add_child(p);
|
||||
|
||||
m_ID_DebugPlanes = p->get_instance_id();
|
||||
|
||||
// m_mat_Debug_Planes->set_as_toplevel(true);
|
||||
|
||||
m_mat_Debug_Planes = Ref<SpatialMaterial>(memnew(SpatialMaterial));
|
||||
m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
|
||||
// m_mat_Debug_Planes->set_line_width(6.0);
|
||||
// m_mat_Debug_Planes->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
bool LRoomManager::DobRegister(Spatial * pDOB, float radius, int iRoom)
|
||||
{
|
||||
//LPRINT(3, "register_dob " + pDOB->get_name());
|
||||
@ -234,22 +262,6 @@ bool LRoomManager::dob_register(Node * pDOB, float radius)
|
||||
LPRINT(2, "dob_register closest room " + itos(iRoomNum));
|
||||
|
||||
return DobRegister(pSpat, radius, iRoomNum);
|
||||
// if (iRoomNum == -1)
|
||||
// return false;
|
||||
|
||||
// LRoom * pRoom = GetRoom(iRoomNum);
|
||||
// if (!pRoom)
|
||||
// return false;
|
||||
|
||||
// LDob dob;
|
||||
// dob.m_ID = pSpat->get_instance_id();
|
||||
// dob.m_fRadius = radius;
|
||||
|
||||
// pRoom->DOB_Add(dob);
|
||||
|
||||
// // save the room ID on the dob metadata
|
||||
// Obj_SetRoomNum(pSpat, iRoomNum);
|
||||
// return true;
|
||||
}
|
||||
|
||||
|
||||
@ -380,33 +392,6 @@ bool LRoomManager::dob_teleport(Node * pDOB)
|
||||
return false;
|
||||
|
||||
return DobTeleport(pSpat, iRoomNum);
|
||||
|
||||
// // old room
|
||||
// LRoom * pOldRoom = GetRoomFromDOB(pDOB);
|
||||
// if (!pOldRoom)
|
||||
// return false;
|
||||
|
||||
|
||||
// LRoom * pNewRoom = GetRoom(iRoomNum);
|
||||
// if (!pNewRoom)
|
||||
// return false;
|
||||
|
||||
// // detach from old room, add to new room
|
||||
// // get dob data to move to new room
|
||||
// unsigned int dob_id = pOldRoom->DOB_Find(pDOB);
|
||||
// assert (dob_id != -1);
|
||||
|
||||
// // copy across data before removing
|
||||
// const LDob &data = pOldRoom->DOB_Get(dob_id);
|
||||
// pNewRoom->DOB_Add(data);
|
||||
|
||||
// // remove from old room
|
||||
// pOldRoom->DOB_Remove(dob_id);
|
||||
|
||||
// // save the room ID on the dob metadata
|
||||
// Obj_SetRoomNum(pSpat, iRoomNum);
|
||||
|
||||
// return true;
|
||||
}
|
||||
|
||||
|
||||
@ -439,6 +424,22 @@ Node * LRoomManager::rooms_get_room(int room_id)
|
||||
return pRoom->GetGodotRoom();
|
||||
}
|
||||
|
||||
void LRoomManager::rooms_set_debug_planes(bool bActive)
|
||||
{
|
||||
m_bDebugPlanes = bActive;
|
||||
|
||||
Object * pObj = ObjectDB::get_instance(m_ID_DebugPlanes);
|
||||
ImmediateGeometry * im = Object::cast_to<ImmediateGeometry>(pObj);
|
||||
if (!im)
|
||||
return;
|
||||
|
||||
if (bActive)
|
||||
im->show();
|
||||
else
|
||||
im->hide();
|
||||
|
||||
}
|
||||
|
||||
|
||||
// turn on and off culling for debugging
|
||||
void LRoomManager::rooms_set_active(bool bActive)
|
||||
@ -480,7 +481,7 @@ void LRoomManager::rooms_log_frame()
|
||||
|
||||
void LRoomManager::rooms_set_camera(Node * pCam)
|
||||
{
|
||||
m_cameraID = 0;
|
||||
m_ID_camera = 0;
|
||||
|
||||
if (!pCam)
|
||||
return;
|
||||
@ -492,7 +493,7 @@ void LRoomManager::rooms_set_camera(Node * pCam)
|
||||
return;
|
||||
}
|
||||
|
||||
m_cameraID = pCam->get_instance_id();
|
||||
m_ID_camera = pCam->get_instance_id();
|
||||
|
||||
// use this temporarily to force debug
|
||||
// rooms_log_frame();
|
||||
@ -531,6 +532,9 @@ void LRoomManager::FrameUpdate()
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_bDebugPlanes)
|
||||
m_DebugPlanes.clear();
|
||||
|
||||
// we keep a frame counter to prevent visiting things multiple times on the same frame in recursive functions
|
||||
m_uiFrameCounter++;
|
||||
|
||||
@ -541,9 +545,9 @@ void LRoomManager::FrameUpdate()
|
||||
|
||||
// get the camera desired and make into lcamera
|
||||
Camera * pCamera = 0;
|
||||
if (m_cameraID)
|
||||
if (m_ID_camera)
|
||||
{
|
||||
Object *pObj = ObjectDB::get_instance(m_cameraID);
|
||||
Object *pObj = ObjectDB::get_instance(m_ID_camera);
|
||||
pCamera = Object::cast_to<Camera>(pObj);
|
||||
}
|
||||
else
|
||||
@ -588,7 +592,7 @@ void LRoomManager::FrameUpdate()
|
||||
assert (pCamera);
|
||||
Transform tr = pCamera->get_global_transform();
|
||||
cam.m_ptPos = tr.origin;
|
||||
cam.m_ptDir = tr.basis.get_row(2); // or possibly get_axis .. z is what we want
|
||||
cam.m_ptDir = -tr.basis.get_axis(2); // or possibly get_axis .. z is what we want
|
||||
|
||||
// luckily godot already has a function to return a list of the camera clipping planes
|
||||
planes.copy_from(pCamera->get_frustum());
|
||||
@ -643,20 +647,57 @@ void LRoomManager::FrameUpdate()
|
||||
|
||||
// hide all the DOB
|
||||
|
||||
// draw debug
|
||||
FrameUpdate_DrawDebug(cam);
|
||||
|
||||
// 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)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
im->add_vertex(ptCam);
|
||||
im->add_vertex(m_DebugPlanes[n]);
|
||||
}
|
||||
im->end();
|
||||
}
|
||||
|
||||
void LRoomManager::_notification(int p_what) {
|
||||
|
||||
switch (p_what) {
|
||||
case NOTIFICATION_ENTER_TREE: {
|
||||
// turn on process, unless we are in the editor
|
||||
if (!Engine::get_singleton()->is_editor_hint())
|
||||
{
|
||||
set_process_internal(true);
|
||||
CreateDebug();
|
||||
}
|
||||
else
|
||||
set_process_internal(false);
|
||||
|
||||
|
||||
|
||||
} break;
|
||||
case NOTIFICATION_INTERNAL_PROCESS: {
|
||||
FrameUpdate();
|
||||
@ -676,6 +717,7 @@ void LRoomManager::_bind_methods()
|
||||
ClassDB::bind_method(D_METHOD("rooms_set_logging"), &LRoomManager::rooms_set_logging);
|
||||
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);
|
||||
|
||||
|
||||
// functions to add dynamic objects to the culling system
|
||||
|
@ -44,7 +44,7 @@ class LRoomManager : public Spatial {
|
||||
|
||||
|
||||
// godot ID of the camera (which should be registered as a DOB to allow moving between rooms)
|
||||
ObjectID m_cameraID;
|
||||
ObjectID m_ID_camera;
|
||||
|
||||
// keep track of which rooms are visible, so we can hide ones that aren't hit that were previously on
|
||||
Lawn::LBitField_Dynamic m_BF_visible_rooms;
|
||||
@ -82,6 +82,8 @@ public:
|
||||
// turn on and off culling for debugging
|
||||
void rooms_set_active(bool bActive);
|
||||
|
||||
void rooms_set_debug_planes(bool bActive);
|
||||
|
||||
// 0 to 6 .. defaults to 4 which is (2) in our priorities (i.e. 6 - level)
|
||||
void rooms_set_logging(int level);
|
||||
|
||||
@ -124,6 +126,7 @@ private:
|
||||
// internal
|
||||
bool DobRegister(Spatial * pDOB, float radius, int iRoom);
|
||||
bool DobTeleport(Spatial * pDOB, int iNewRoomID);
|
||||
void CreateDebug();
|
||||
|
||||
|
||||
// helper funcs
|
||||
@ -144,12 +147,25 @@ private:
|
||||
// debugging emulate view frustum
|
||||
void FrameUpdate_FrustumOnly();
|
||||
|
||||
// draw planes and room hulls
|
||||
void FrameUpdate_DrawDebug(const LCamera &cam);
|
||||
|
||||
// find which room is linked by a portal
|
||||
LRoom &Portal_GetLinkedRoom(const LPortal &port);
|
||||
|
||||
// lists of rooms and portals, contiguous list so cache friendly
|
||||
LVector<LRoom> m_Rooms;
|
||||
LVector<LPortal> m_Portals;
|
||||
|
||||
public:
|
||||
// whether debug planes is switched on
|
||||
bool m_bDebugPlanes;
|
||||
|
||||
// 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;
|
||||
Ref<SpatialMaterial> m_mat_Debug_Planes;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user