2019-09-11 09:09:36 +02:00
|
|
|
// Copyright (c) 2019 Lawnjelly
|
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
#include "lroom.h"
|
|
|
|
#include "core/engine.h"
|
|
|
|
#include "scene/3d/mesh_instance.h"
|
|
|
|
#include "lportal.h"
|
2019-09-13 20:15:25 +02:00
|
|
|
#include "lbitfield_dynamic.h"
|
2019-09-12 20:53:24 +02:00
|
|
|
#include "lroom_manager.h"
|
2019-09-11 09:09:36 +02:00
|
|
|
|
2019-09-12 16:51:33 +02:00
|
|
|
void LRoom::print(String sz)
|
|
|
|
{
|
2019-09-12 20:53:24 +02:00
|
|
|
LPortal::print(sz);
|
2019-09-12 16:51:33 +02:00
|
|
|
}
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
LRoom::LRoom() {
|
2019-09-15 16:39:01 +02:00
|
|
|
m_RoomID = -1;
|
2019-09-13 20:15:25 +02:00
|
|
|
m_uiFrameTouched = 0;
|
2019-09-15 16:39:01 +02:00
|
|
|
m_iFirstPortal = 0;
|
|
|
|
m_iNumPortals = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Spatial * LRoom::GetGodotRoom() const
|
|
|
|
{
|
|
|
|
Object *pObj = ObjectDB::get_instance(m_GodotID);
|
|
|
|
|
|
|
|
// assuming is a portal
|
|
|
|
Spatial * pSpat = Object::cast_to<Spatial>(pObj);
|
|
|
|
|
|
|
|
return pSpat;
|
2019-09-11 09:09:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
void LRoom::AddDOB(Spatial * pDOB)
|
|
|
|
{
|
|
|
|
LDob dob;
|
|
|
|
dob.m_ID = pDOB->get_instance_id();
|
|
|
|
|
|
|
|
m_DOBs.push_back(dob);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LRoom::RemoveDOB(Node * pDOB)
|
|
|
|
{
|
|
|
|
ObjectID id = pDOB->get_instance_id();
|
|
|
|
|
|
|
|
for (int n=0; n<m_DOBs.size(); n++)
|
|
|
|
{
|
|
|
|
if (m_DOBs[n].m_ID == id)
|
|
|
|
{
|
|
|
|
m_DOBs.remove(n);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// returns -1 if no change, or the linked room we are moving into
|
|
|
|
LRoom * LRoom::UpdateDOB(LRoomManager &manager, Spatial * pDOB)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
|
|
|
const Vector3 &pt = pDOB->get_global_transform().origin;
|
|
|
|
|
|
|
|
const float slop = 0.2f;
|
|
|
|
|
|
|
|
// the camera can't have slop because we might end up front side of a door without entering the room,
|
|
|
|
// hence can't see into the room through the portal!
|
|
|
|
// if (bCamera)
|
|
|
|
// slop = 0.0f;
|
|
|
|
|
|
|
|
// check each portal - has the object crossed it into the neighbouring room?
|
2019-09-15 16:39:01 +02:00
|
|
|
for (int p=0; p<m_iNumPortals; p++)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
const LPortal &port = manager.m_Portals[m_iFirstPortal + p];
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
float dist = port.m_Plane.distance_to(pt);
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
if (dist > slop)
|
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
print("DOB at pos " + pt + " ahead of portal " + port.get_name() + " by " + String(Variant(dist)));
|
|
|
|
|
|
|
|
// we want to move into the adjoining room
|
|
|
|
return &manager.Portal_GetLinkedRoom(port);
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
void LRoom::DetermineVisibility_Recursive(LRoomManager &manager, int depth, const LCamera &cam, const LVector<Plane> &planes, int portalID_from)
|
2019-09-11 09:09:36 +02:00
|
|
|
{
|
2019-09-12 12:07:43 +02:00
|
|
|
// prevent too much depth
|
|
|
|
if (depth >= 8)
|
|
|
|
{
|
2019-09-12 16:51:33 +02:00
|
|
|
print("\t\t\tDEPTH LIMIT REACHED");
|
2019-09-12 12:07:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-12 16:51:33 +02:00
|
|
|
print("DetermineVisibility_Recursive from " + get_name());
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
// set the frame counter
|
|
|
|
assert (manager.m_uiFrameCounter > m_uiFrameTouched);
|
|
|
|
m_uiFrameTouched = manager.m_uiFrameCounter;
|
|
|
|
|
2019-09-12 16:51:33 +02:00
|
|
|
// show this room and add to visible list of rooms
|
2019-09-15 16:39:01 +02:00
|
|
|
GetGodotRoom()->show();
|
|
|
|
manager.m_BF_visible_rooms.SetBit(m_RoomID, true);
|
2019-09-12 12:07:43 +02:00
|
|
|
|
2019-09-11 09:09:36 +02:00
|
|
|
// clip all objects in this room to the clipping planes
|
2019-09-15 16:39:01 +02:00
|
|
|
for (int n=0; n<m_SOBs.size(); n++)
|
2019-09-12 16:51:33 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
const LSob sob = m_SOBs[n];
|
|
|
|
Object * pNode = ObjectDB::get_instance(sob.m_ID);
|
2019-09-12 16:51:33 +02:00
|
|
|
|
|
|
|
VisualInstance * pObj = Object::cast_to<VisualInstance>(pNode);
|
2019-09-15 16:39:01 +02:00
|
|
|
|
|
|
|
// should always be a visual instance, only these are added as SOBs
|
2019-09-12 16:51:33 +02:00
|
|
|
if (pObj)
|
|
|
|
{
|
2019-09-12 20:53:24 +02:00
|
|
|
//Vector3 pt = pObj->get_global_transform().origin;
|
2019-09-12 16:51:33 +02:00
|
|
|
|
|
|
|
bool bShow = true;
|
|
|
|
|
|
|
|
|
|
|
|
// estimate the radius .. for now
|
|
|
|
AABB bb = pObj->get_transformed_aabb();
|
|
|
|
|
|
|
|
print("\t\t\tculling object " + pObj->get_name());
|
|
|
|
|
|
|
|
for (int p=0; p<planes.size(); p++)
|
|
|
|
{
|
|
|
|
// float dist = planes[p].distance_to(pt);
|
|
|
|
// print("\t\t\t\t" + itos(p) + " : dist " + String(Variant(dist)));
|
|
|
|
|
|
|
|
float r_min, r_max;
|
|
|
|
bb.project_range_in_plane(planes[p], r_min, r_max);
|
|
|
|
|
|
|
|
print("\t\t\t\t" + itos(p) + " : r_min " + String(Variant(r_min)) + ", r_max " + String(Variant(r_max)));
|
|
|
|
|
|
|
|
|
|
|
|
if (r_min > 0.0f)
|
|
|
|
{
|
|
|
|
bShow = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bShow)
|
|
|
|
pObj->show();
|
|
|
|
else
|
|
|
|
pObj->hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
for (int p=0; p<m_iNumPortals; p++)
|
2019-09-11 09:09:36 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
int port_id = m_iFirstPortal + p;
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
// ignore if the portal we are looking in from
|
2019-09-15 16:39:01 +02:00
|
|
|
if (port_id == portalID_from)
|
2019-09-11 09:09:36 +02:00
|
|
|
continue;
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
const LPortal &port = manager.m_Portals[port_id];
|
2019-09-11 09:09:36 +02:00
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
// have we already handled the room on this frame?
|
|
|
|
// get the room pointed to by the portal
|
2019-09-15 16:39:01 +02:00
|
|
|
LRoom * pLinkedRoom = &manager.Portal_GetLinkedRoom(port);
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
if (pLinkedRoom->m_uiFrameTouched == manager.m_uiFrameCounter)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// const Vector3 &portal_normal = pPortal->m_Plane.normal;
|
|
|
|
// print("\ttesting portal " + pPortal->get_name() + " normal " + portal_normal);
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
// direction with the camera? (might not need to check)
|
2019-09-13 20:15:25 +02:00
|
|
|
// float dot = cam.m_ptDir.dot(portal_normal);
|
|
|
|
// if (dot <= -0.0f) // 0.0
|
|
|
|
// {
|
|
|
|
// Variant vd = dot;
|
|
|
|
// print("\t\tportal culled (wrong direction) dot is " + String(vd));
|
|
|
|
// continue;
|
|
|
|
// }
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
// is it culled by the planes?
|
|
|
|
LPortal::eClipResult overall_res = LPortal::eClipResult::CLIP_INSIDE;
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
// for portals, we want to ignore the near clipping plane, as we might be right on the edge of a doorway
|
|
|
|
// and still want to look through the portal.
|
|
|
|
// So we are starting this loop from 1, ASSUMING that plane zero is the near clipping plane.
|
|
|
|
// If it isn't we would need a different strategy
|
|
|
|
for (int l=1; l<planes.size(); l++)
|
2019-09-11 09:09:36 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
LPortal::eClipResult res = port.ClipWithPlane(planes[l]);
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case LPortal::eClipResult::CLIP_OUTSIDE:
|
|
|
|
overall_res = res;
|
|
|
|
break;
|
|
|
|
case LPortal::eClipResult::CLIP_PARTIAL:
|
|
|
|
overall_res = res;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overall_res == LPortal::eClipResult::CLIP_OUTSIDE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this portal is culled
|
|
|
|
if (overall_res == LPortal::eClipResult::CLIP_OUTSIDE)
|
2019-09-12 12:07:43 +02:00
|
|
|
{
|
2019-09-12 16:51:33 +02:00
|
|
|
print("\t\tportal culled (outside planes)");
|
2019-09-11 09:09:36 +02:00
|
|
|
continue;
|
2019-09-12 12:07:43 +02:00
|
|
|
}
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
// else recurse into that portal
|
2019-09-12 20:53:24 +02:00
|
|
|
unsigned int uiPoolMem = manager.m_Pool.Request();
|
|
|
|
if (uiPoolMem != -1)
|
|
|
|
{
|
|
|
|
// get a vector of planes from the pool
|
|
|
|
LVector<Plane> &new_planes = manager.m_Pool.Get(uiPoolMem);
|
|
|
|
|
|
|
|
// copy the existing planes
|
|
|
|
new_planes.copy_from(planes);
|
|
|
|
|
|
|
|
// add the planes for the portal
|
2019-09-15 16:39:01 +02:00
|
|
|
port.AddPlanes(cam.m_ptPos, new_planes);
|
2019-09-11 09:09:36 +02:00
|
|
|
|
2019-09-12 20:53:24 +02:00
|
|
|
if (pLinkedRoom)
|
2019-09-15 16:39:01 +02:00
|
|
|
pLinkedRoom->DetermineVisibility_Recursive(manager, depth + 1, cam, new_planes, port_id);
|
2019-09-11 09:09:36 +02:00
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// we no longer need these planes
|
2019-09-12 20:53:24 +02:00
|
|
|
manager.m_Pool.Free(uiPoolMem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// planes pool is empty!
|
2019-09-15 16:39:01 +02:00
|
|
|
// This will happen if the view goes through shedloads of portals
|
|
|
|
// The solution is either to increase the plane pool size, or build levels
|
|
|
|
// with views through multiple portals. Looking through multiple portals is likely to be
|
|
|
|
// slow anyway because of the number of planes to test.
|
2019-09-12 20:53:24 +02:00
|
|
|
WARN_PRINT_ONCE("Planes pool is empty");
|
|
|
|
}
|
2019-09-11 09:09:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|