WIP, debugging code added, still not working.
This commit is contained in:
@@ -74,8 +74,8 @@ struct Output
|
|||||||
//Contains points P in: dot(normal, P) + d = 0
|
//Contains points P in: dot(normal, P) + d = 0
|
||||||
struct Plane
|
struct Plane
|
||||||
{
|
{
|
||||||
glm::vec3 normal;
|
glm::vec3 Normal;
|
||||||
float distance;
|
float Distance;
|
||||||
};
|
};
|
||||||
|
|
||||||
//A frustum defined by 6 planes.
|
//A frustum defined by 6 planes.
|
||||||
@@ -87,14 +87,14 @@ struct Frustum
|
|||||||
Outside,
|
Outside,
|
||||||
Intersects
|
Intersects
|
||||||
};
|
};
|
||||||
Plane planes[6];
|
Plane Planes[6];
|
||||||
|
|
||||||
Output VsAABB(const AABB& box) const
|
Output VsAABB(const AABB& box) const
|
||||||
{
|
{
|
||||||
const glm::vec3& maxCorner = box.MaxCorner();
|
const glm::vec3& maxCorner = box.MaxCorner();
|
||||||
const glm::vec3& minCorner = box.MinCorner();
|
const glm::vec3& minCorner = box.MinCorner();
|
||||||
bool completelyInside = true;
|
bool completelyInside = true;
|
||||||
for (const Plane& p : planes) {
|
for (const Plane& p : Planes) {
|
||||||
bool anyWasInside = false;
|
bool anyWasInside = false;
|
||||||
bool anyWasOutside = false;
|
bool anyWasOutside = false;
|
||||||
//If points are on both sides of the plane, we can stop.
|
//If points are on both sides of the plane, we can stop.
|
||||||
@@ -104,7 +104,7 @@ struct Frustum
|
|||||||
corner.x = bits.test(0) ? maxCorner.x : minCorner.x;
|
corner.x = bits.test(0) ? maxCorner.x : minCorner.x;
|
||||||
corner.y = bits.test(1) ? maxCorner.y : minCorner.y;
|
corner.y = bits.test(1) ? maxCorner.y : minCorner.y;
|
||||||
corner.z = bits.test(2) ? maxCorner.z : minCorner.z;
|
corner.z = bits.test(2) ? maxCorner.z : minCorner.z;
|
||||||
if (glm::dot(p.normal, corner) > p.distance) {
|
if (glm::dot(p.Normal, corner) > p.Distance) {
|
||||||
anyWasInside = true;
|
anyWasInside = true;
|
||||||
} else {
|
} else {
|
||||||
anyWasOutside = true;
|
anyWasOutside = true;
|
||||||
@@ -214,17 +214,18 @@ void Octree<T>::ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector<T>& ou
|
|||||||
{
|
{
|
||||||
falsifyObjectChecks();
|
falsifyObjectChecks();
|
||||||
OctSpace::Frustum frustum;
|
OctSpace::Frustum frustum;
|
||||||
|
//Order: Right, left, top, bottom, far, near.
|
||||||
for (int i = 0; i < 6; ++i) {
|
for (int i = 0; i < 6; ++i) {
|
||||||
int sign = 2 * (i % 2) - 1;
|
int sign = 2 * (i % 2) - 1;
|
||||||
int index = i / 2;
|
int index = i / 2;
|
||||||
OctSpace::Plane& plane = frustum.planes[i];
|
OctSpace::Plane& plane = frustum.Planes[i];
|
||||||
plane.normal.x = viewProj[0].w + sign * viewProj[0][index];
|
plane.Normal.x = viewProj[0].w + sign * viewProj[0][index];
|
||||||
plane.normal.y = viewProj[1].w + sign * viewProj[1][index];
|
plane.Normal.y = viewProj[1].w + sign * viewProj[1][index];
|
||||||
plane.normal.z = viewProj[2].w + sign * viewProj[2][index];
|
plane.Normal.z = viewProj[2].w + sign * viewProj[2][index];
|
||||||
plane.distance = viewProj[3].w + sign * viewProj[3][index];
|
plane.Distance = viewProj[3].w + sign * viewProj[3][index];
|
||||||
float divByNormalLength = 1.0f / glm::length(plane.normal);
|
float divByNormalLength = 1.0f / glm::length(plane.Normal);
|
||||||
plane.normal *= divByNormalLength;
|
plane.Normal *= divByNormalLength;
|
||||||
plane.distance *= divByNormalLength;
|
plane.Distance *= divByNormalLength;
|
||||||
}
|
}
|
||||||
m_Root->ObjectsInFrustum(frustum, outObjects, false);
|
m_Root->ObjectsInFrustum(frustum, outObjects, false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ private:
|
|||||||
Camera* m_Camera;
|
Camera* m_Camera;
|
||||||
Camera* m_LastCullCamera;
|
Camera* m_LastCullCamera;
|
||||||
Camera** m_FrustumCamPtr;
|
Camera** m_FrustumCamPtr;
|
||||||
|
EntityWrapper frustumEntity;
|
||||||
World* m_World;
|
World* m_World;
|
||||||
EntityWrapper m_CurrentCamera = EntityWrapper::Invalid;
|
EntityWrapper m_CurrentCamera = EntityWrapper::Invalid;
|
||||||
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
|
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
|
||||||
|
|||||||
@@ -25,6 +25,19 @@ RenderSystem::~RenderSystem()
|
|||||||
|
|
||||||
bool RenderSystem::OnSetCamera(Events::SetCamera& e)
|
bool RenderSystem::OnSetCamera(Events::SetCamera& e)
|
||||||
{
|
{
|
||||||
|
//Right now, lets set the camera to cull away stuff if it is connected to a player.
|
||||||
|
//TODO: This won't work with spectators, or death anim.
|
||||||
|
if (e.CameraEntity.FirstParentWithComponent("Player").Valid()) {
|
||||||
|
m_FrustumCamPtr = &m_Camera;
|
||||||
|
LOG_INFO("Setting frustum to new camera.");
|
||||||
|
} else if (e.CameraEntity != m_CurrentCamera) {
|
||||||
|
//If the camera has no parents, i.e. a free camera,
|
||||||
|
//then we cull from the last camera, so we can see if the culling works.
|
||||||
|
//Copy the camera into the last frustum camera, without allocating new memory.
|
||||||
|
new ((void*)m_LastCullCamera) Camera(*m_Camera);
|
||||||
|
m_FrustumCamPtr = &m_LastCullCamera;
|
||||||
|
LOG_INFO("New camera, frustum remains at old camera.");
|
||||||
|
}
|
||||||
ComponentWrapper cTransform = e.CameraEntity["Transform"];
|
ComponentWrapper cTransform = e.CameraEntity["Transform"];
|
||||||
ComponentWrapper cCamera = e.CameraEntity["Camera"];
|
ComponentWrapper cCamera = e.CameraEntity["Camera"];
|
||||||
m_Camera->SetFOV((double)cCamera["FOV"]);
|
m_Camera->SetFOV((double)cCamera["FOV"]);
|
||||||
@@ -33,17 +46,6 @@ bool RenderSystem::OnSetCamera(Events::SetCamera& e)
|
|||||||
m_Camera->SetPosition(cTransform["Position"]);
|
m_Camera->SetPosition(cTransform["Position"]);
|
||||||
m_Camera->SetOrientation(glm::quat((const glm::vec3&)cTransform["Orientation"]));
|
m_Camera->SetOrientation(glm::quat((const glm::vec3&)cTransform["Orientation"]));
|
||||||
m_CurrentCamera = e.CameraEntity;
|
m_CurrentCamera = e.CameraEntity;
|
||||||
//Right now, lets set the camera to cull away stuff if it is connected to anything.
|
|
||||||
//TODO: This won't work with spectators, or death anim.
|
|
||||||
if (m_CurrentCamera.Parent().Valid()) {
|
|
||||||
//Copy the camera into the last frustum camera, without allocating new memory.
|
|
||||||
new ((void*)m_LastCullCamera) Camera(*m_Camera);
|
|
||||||
m_FrustumCamPtr = &m_Camera;
|
|
||||||
} else {
|
|
||||||
//If the camera has no parents, i.e. a free camera,
|
|
||||||
//then we cull from the last camera, so we can see if the culling works.
|
|
||||||
m_FrustumCamPtr = &m_LastCullCamera;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,26 +58,45 @@ bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity)
|
|||||||
return entity == m_CurrentCamera || entity.IsChildOf(m_CurrentCamera);
|
return entity == m_CurrentCamera || entity.IsChildOf(m_CurrentCamera);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float frustrumTODO = 0.f;
|
||||||
|
|
||||||
void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs, std::list<std::shared_ptr<RenderJob>>& transparentJobs)
|
void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs, std::list<std::shared_ptr<RenderJob>>& transparentJobs)
|
||||||
{
|
{
|
||||||
|
if (!frustumEntity.Valid() && m_World->GetComponentPools().size() > 0) {
|
||||||
|
frustumEntity = EntityWrapper(m_World, m_World->CreateEntity());
|
||||||
|
m_World->AttachComponent(frustumEntity.ID, "Transform");
|
||||||
|
m_World->AttachComponent(frustumEntity.ID, "Model");
|
||||||
|
frustumEntity["Model"]["Resource"] = "Models/Core/UnitCube.mesh";
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<EntityAABB> seenEntities;
|
std::vector<EntityAABB> seenEntities;
|
||||||
//m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix(), seenEntities);
|
//m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix(), seenEntities);
|
||||||
//m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ViewMatrix() * (*m_FrustumCamPtr)->ProjectionMatrix(), seenEntities);
|
|
||||||
|
|
||||||
glm::mat4x4 viewProj = (*m_FrustumCamPtr)->ViewMatrix() * (*m_FrustumCamPtr)->ProjectionMatrix();
|
glm::mat4x4 viewProj = (*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix();
|
||||||
OctSpace::Frustum frustum;
|
OctSpace::Frustum frustum;
|
||||||
|
//Order: Right, left, top, bottom, far, near.
|
||||||
|
int sign = 1;
|
||||||
for (int i = 0; i < 6; ++i) {
|
for (int i = 0; i < 6; ++i) {
|
||||||
int sign = 2 * (i % 2) - 1;
|
sign = -sign;
|
||||||
int index = i / 2;
|
int index = i / 2;
|
||||||
OctSpace::Plane& plane = frustum.planes[i];
|
OctSpace::Plane& plane = frustum.Planes[i];
|
||||||
plane.normal.x = viewProj[0].w + sign * viewProj[0][index];
|
plane.Normal.x = viewProj[0].w + sign * viewProj[0][index];
|
||||||
plane.normal.y = viewProj[1].w + sign * viewProj[1][index];
|
plane.Normal.y = viewProj[1].w + sign * viewProj[1][index];
|
||||||
plane.normal.z = viewProj[2].w + sign * viewProj[2][index];
|
plane.Normal.z = viewProj[2].w + sign * viewProj[2][index];
|
||||||
plane.distance = viewProj[3].w + sign * viewProj[3][index];
|
plane.Distance = viewProj[3].w + sign * viewProj[3][index];
|
||||||
float divByNormalLength = 1.0f / glm::length(plane.normal);
|
float divByNormalLength = 1.0f / glm::length(plane.Normal);
|
||||||
plane.normal *= divByNormalLength;
|
plane.Normal *= divByNormalLength;
|
||||||
plane.distance *= divByNormalLength;
|
plane.Distance *= divByNormalLength;
|
||||||
|
}
|
||||||
|
if (frustumEntity.Valid()) {
|
||||||
|
int planeI = 0;
|
||||||
|
glm::vec3 pos = (*m_FrustumCamPtr)->Position() + frustrumTODO * (*m_FrustumCamPtr)->Forward();
|
||||||
|
float dist = glm::dot(frustum.Planes[planeI].Normal, pos) + frustum.Planes[planeI].Distance;
|
||||||
|
frustumEntity["Transform"]["Position"] = pos - dist * frustum.Planes[planeI].Normal;
|
||||||
|
frustumEntity["Transform"]["Scale"] = glm::vec3(0.15f);
|
||||||
|
}
|
||||||
|
if (++frustrumTODO > 75) {
|
||||||
|
frustrumTODO = 0.f;
|
||||||
}
|
}
|
||||||
|
|
||||||
//for (auto& seenEntity : seenEntities) {
|
//for (auto& seenEntity : seenEntities) {
|
||||||
@@ -109,11 +130,11 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs,
|
|||||||
|
|
||||||
if (entity.HasComponent("AABB")) {
|
if (entity.HasComponent("AABB")) {
|
||||||
OctSpace::Frustum::Output o = frustum.VsAABB(*Collision::EntityAbsoluteAABB(entity));
|
OctSpace::Frustum::Output o = frustum.VsAABB(*Collision::EntityAbsoluteAABB(entity));
|
||||||
if (o == OctSpace::Frustum::Outside) {
|
if (o == OctSpace::Frustum::Outside && entity != frustumEntity) {
|
||||||
continue;
|
resource = "Models/Core/UnitRaptor.mesh";
|
||||||
}
|
}
|
||||||
} else {
|
} else if (entity != frustumEntity){
|
||||||
continue;
|
resource = "Models/Core/Error.mesh";
|
||||||
}
|
}
|
||||||
|
|
||||||
Model* model;
|
Model* model;
|
||||||
|
|||||||
Reference in New Issue
Block a user