WIP FrustumCulling, most code added but not working now.

This commit is contained in:
William Moberg
2016-02-09 10:05:49 +01:00
parent 5987751add
commit b748deb1e1
4 changed files with 179 additions and 8 deletions
+117
View File
@@ -2,6 +2,7 @@
#define Octree_h__
#include <type_traits>
#include <bitset>
#include "../Common.h"
#include "AABB.h"
@@ -40,6 +41,8 @@ public:
//The type Box must be AABB, or inherit from AABB.
template<typename Box>
void ObjectsInSameRegion(const Box& box, std::vector<T>& outObjects);
//Get the objects that are inside the frustum defined by the viewProjection matrix, the objects are put in outObjects.
void ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector<T>& outObjects);
//Empty the tree of all objects, static and dynamic.
void ClearObjects();
//Empty the tree of all dynamic objects. Static objects remain in the tree.
@@ -68,6 +71,56 @@ struct Output
float CollideDistance;
};
//Contains points P in: dot(normal, P) + d = 0
struct Plane
{
glm::vec3 normal;
float distance;
};
//A frustum defined by 6 planes.
struct Frustum
{
enum Output
{
Inside,
Outside,
Intersects
};
Plane planes[6];
Output VsAABB(const AABB& box) const
{
const glm::vec3& maxCorner = box.MaxCorner();
const glm::vec3& minCorner = box.MinCorner();
bool completelyInside = true;
for (const Plane& p : planes) {
bool anyWasInside = false;
bool anyWasOutside = false;
//If points are on both sides of the plane, we can stop.
for (int i = 0; i < 8 && (!anyWasInside || !anyWasOutside); ++i) {
std::bitset<3> bits(i);
glm::vec3 corner;
corner.x = bits.test(0) ? maxCorner.x : minCorner.x;
corner.y = bits.test(1) ? maxCorner.y : minCorner.y;
corner.z = bits.test(2) ? maxCorner.z : minCorner.z;
if (glm::dot(p.normal, corner) > p.distance) {
anyWasInside = true;
} else {
anyWasOutside = true;
}
}
if (!anyWasInside) {
return Outside;
}
if (anyWasOutside) {
completelyInside = false;
}
}
return completelyInside ? Inside : Intersects;
}
};
struct ContainedObject
{
ContainedObject()
@@ -97,6 +150,8 @@ struct Child
void AddStaticObject(const AABB& box);
template<typename T, typename Box>
void ObjectsInSameRegion(const Box& box, std::vector<T>& outObjects) const;
template<typename T>
void ObjectsInFrustum(const Frustum& frustum, std::vector<T>& outObjects, bool takeAllDontTest) const;
void ClearObjects();
void ClearDynamicObjects();
bool RayCollides(const Ray& ray, Output& data) const;
@@ -154,6 +209,26 @@ void Octree<T>::ObjectsInSameRegion(const Box& box, std::vector<T>& outObjects)
m_Root->ObjectsInSameRegion(box, outObjects);
}
template<typename T>
void Octree<T>::ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector<T>& outObjects)
{
falsifyObjectChecks();
OctSpace::Frustum frustum;
for (int i = 0; i < 6; ++i) {
int sign = 2 * (i % 2) - 1;
int index = i / 2;
OctSpace::Plane& plane = frustum.planes[i];
plane.normal.x = viewProj[0].w + sign * viewProj[0][index];
plane.normal.y = viewProj[1].w + sign * viewProj[1][index];
plane.normal.z = viewProj[2].w + sign * viewProj[2][index];
plane.distance = viewProj[3].w + sign * viewProj[3][index];
float divByNormalLength = 1.0f / glm::length(plane.normal);
plane.normal *= divByNormalLength;
plane.distance *= divByNormalLength;
}
m_Root->ObjectsInFrustum(frustum, outObjects, false);
}
template<typename T>
void Octree<T>::ClearObjects()
{
@@ -230,4 +305,46 @@ void OctSpace::Child::ObjectsInSameRegion(const Box& box, std::vector<T>& outObj
}
}
template<typename T>
void OctSpace::Child::ObjectsInFrustum(const Frustum& frustum, std::vector<T>& outObjects, bool takeAllDontTest) const
{
if (hasChildren()) {
for (const Child* c : m_Children) {
Frustum::Output out = Frustum::Inside;
if (!takeAllDontTest) {
out = frustum.VsAABB(c->m_Box);
if (out == Frustum::Outside) {
continue;
}
}
c->ObjectsInFrustum(frustum, outObjects, out == Frustum::Inside);
}
} else {
size_t startIndex = outObjects.size();
int numDuplicates = 0;
outObjects.resize(outObjects.size() + m_StaticObjIndices.size() + m_DynamicObjIndices.size());
for (size_t i = 0; i < m_StaticObjIndices.size(); ++i) {
ContainedObject& obj = m_StaticObjectsRef[m_StaticObjIndices[i]];
if (obj.Checked || !frustum.VsAABB(obj.Box)) {
++numDuplicates;
} else {
obj.Checked = true;
outObjects[startIndex + i - numDuplicates] = *static_cast<T*>(obj.Box.get());
}
}
for (size_t i = 0; i < m_DynamicObjIndices.size(); ++i) {
ContainedObject& obj = m_DynamicObjectsRef[m_DynamicObjIndices[i]];
if (obj.Checked || !frustum.VsAABB(obj.Box)) {
++numDuplicates;
} else {
obj.Checked = true;
outObjects[startIndex + i - numDuplicates] = *static_cast<T*>(obj.Box.get());
}
}
for (size_t i = 0; i < numDuplicates; ++i) {
outObjects.pop_back();
}
}
}
#endif
+6 -1
View File
@@ -16,11 +16,13 @@
#include "PointLightJob.h"
#include "../Core/Transform.h"
#include "../Core/EPlayerSpawned.h"
#include "../Core/Octree.h"
#include "../Collision/EntityAABB.h"
class RenderSystem : public ImpureSystem
{
public:
RenderSystem(World* world, EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame);
RenderSystem(World* world, EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame, Octree<EntityAABB>* frustumCullOctree);
~RenderSystem();
virtual void Update(double dt) override;
@@ -29,9 +31,12 @@ private:
const IRenderer* m_Renderer;
RenderFrame* m_RenderFrame;
Camera* m_Camera;
Camera* m_LastCullCamera;
Camera** m_FrustumCamPtr;
World* m_World;
EntityWrapper m_CurrentCamera = EntityWrapper::Invalid;
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
Octree<EntityAABB>* m_Octree;
EventRelay<RenderSystem, Events::SetCamera> m_ESetCamera;
bool OnSetCamera(Events::SetCamera &event);