Frustum culling
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "Camera.h"
|
||||
#include "src/Core/Maths.h"
|
||||
#include "../Core/Input.h"
|
||||
#include <glm\ext\vector_float3.hpp>
|
||||
#include <glm\ext\matrix_clip_space.hpp>
|
||||
@@ -6,6 +7,7 @@
|
||||
#include <glm\ext\matrix_transform.hpp>
|
||||
#include <GLFW\glfw3.h>
|
||||
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
Camera::Camera(CAMERA_TYPE type, glm::vec3 position)
|
||||
@@ -17,6 +19,8 @@ namespace Nuake
|
||||
cameraRight = glm::normalize(glm::cross(up, cameraDirection));
|
||||
cameraUp = glm::cross(cameraDirection, cameraRight);
|
||||
cameraFront = cameraDirection;
|
||||
|
||||
m_Frustum = Frustum(GetTransform());
|
||||
}
|
||||
|
||||
Camera::Camera()
|
||||
@@ -53,32 +57,37 @@ namespace Nuake
|
||||
cameraRight = glm::normalize(glm::cross(up, cameraFront));
|
||||
}
|
||||
|
||||
glm::vec3 Camera::GetTranslation() {
|
||||
Vector3 Camera::GetTranslation() {
|
||||
return Translation;
|
||||
}
|
||||
|
||||
glm::vec3 Camera::GetDirection() {
|
||||
Vector3 Camera::GetDirection() {
|
||||
return cameraDirection;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::GetPerspective()
|
||||
Matrix4 Camera::GetPerspective()
|
||||
{
|
||||
//TODO: Add perspective options
|
||||
m_Perspective = glm::perspectiveFov(glm::radians(Fov), 9.0f * AspectRatio, 9.0f, 0.1f, 1000.0f);
|
||||
//m_Perspective = glm::ortho(-8.0f, 8.0f, -4.5f, 4.5f, -1.0f, 1.0f);
|
||||
return m_Perspective;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::GetTransform()
|
||||
Matrix4 Camera::GetTransform()
|
||||
{
|
||||
glm::mat4 tr = lookAt(Translation, Translation + cameraFront, cameraUp);
|
||||
return tr;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::GetTransformRotation()
|
||||
Matrix4 Camera::GetTransformRotation()
|
||||
{
|
||||
return lookAt(glm::vec3(), cameraFront, cameraUp);
|
||||
}
|
||||
|
||||
bool Camera::BoxFrustumCheck(const AABB& aabb)
|
||||
{
|
||||
m_Frustum = Frustum(GetPerspective() * GetTransform());
|
||||
return m_Frustum.IsBoxVisible(aabb.Min, aabb.Max);
|
||||
}
|
||||
|
||||
json Camera::Serialize()
|
||||
{
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "src/Core/Maths.h"
|
||||
#include "src/Resource/Serializable.h"
|
||||
#include "Frustum.h"
|
||||
#include <src/Rendering/AABB.h>
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
@@ -18,14 +19,13 @@ namespace Nuake
|
||||
{
|
||||
private:
|
||||
CAMERA_TYPE m_Type;
|
||||
Frustum m_Frustum;
|
||||
|
||||
Vector3 Rotation = { 0.0f, 0.0f, 0.0f };
|
||||
Vector3 Scale = { 1.0f, 1.0f, 1.0f };
|
||||
Matrix4 m_Perspective;
|
||||
|
||||
public:
|
||||
Frustum m_Frustum;
|
||||
|
||||
float AspectRatio = 16.0f / 9.0f;
|
||||
// TODO: remove duplicate direction and have a proper api.
|
||||
Vector3 up = Vector3(0.0f, 1.0f, 0.0f);
|
||||
@@ -54,6 +54,8 @@ namespace Nuake
|
||||
Matrix4 GetPerspective();
|
||||
Matrix4 GetTransform();
|
||||
Matrix4 GetTransformRotation();
|
||||
|
||||
bool BoxFrustumCheck(const AABB& aabb);
|
||||
|
||||
json Serialize() override;
|
||||
bool Deserialize(const std::string& str) override;
|
||||
|
||||
@@ -12,10 +12,31 @@ namespace Nuake
|
||||
|
||||
MaterialManager::Get()->RegisterMaterial(material);
|
||||
|
||||
setupMesh();
|
||||
SetupMesh();
|
||||
CalculateAABB();
|
||||
}
|
||||
|
||||
void Mesh::CalculateAABB()
|
||||
{
|
||||
float minX = 0.0f;
|
||||
float minY = 0.0f;
|
||||
float minZ = 0.0f;
|
||||
float maxX = 0.0f;
|
||||
float maxY = 0.0f;
|
||||
float maxZ = 0.0f;
|
||||
for (Vertex& v : m_Vertices)
|
||||
{
|
||||
minX = v.position.x < minX ? v.position.x : minX;
|
||||
minY = v.position.y < minY ? v.position.y : minY;
|
||||
minZ = v.position.z < minZ ? v.position.z : minZ;
|
||||
maxX = v.position.x > maxX ? v.position.x : maxX;
|
||||
maxY = v.position.y > maxY ? v.position.y : maxY;
|
||||
maxZ = v.position.z > maxZ ? v.position.z : maxZ;
|
||||
}
|
||||
this->m_AABB = { Vector3(minX, minY, minZ), Vector3(maxX, maxY, maxZ) };
|
||||
}
|
||||
|
||||
void Mesh::setupMesh()
|
||||
void Mesh::SetupMesh()
|
||||
{
|
||||
m_VertexArray = new VertexArray();
|
||||
m_VertexArray->Bind();
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "src/Rendering/Buffers/VertexBuffer.h"
|
||||
#include "src/Rendering/Buffers/VertexArray.h"
|
||||
#include "src/Rendering/Buffers/VertexBufferLayout.h"
|
||||
|
||||
#include "src/Rendering/AABB.h"
|
||||
namespace Nuake
|
||||
{
|
||||
class Mesh
|
||||
@@ -17,18 +17,22 @@ namespace Nuake
|
||||
std::vector<unsigned int> m_Indices;
|
||||
std::vector<Vertex> m_Vertices;
|
||||
Ref<Material> m_Material;
|
||||
|
||||
|
||||
Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, Ref<Material> material);
|
||||
|
||||
void Draw(bool bindMaterial = true);
|
||||
void DebugDraw();
|
||||
|
||||
|
||||
inline AABB GetAABB() const { return m_AABB; }
|
||||
private:
|
||||
VertexBuffer* m_VertexBuffer;
|
||||
VertexArray* m_VertexArray;
|
||||
VertexBuffer* m_ElementBuffer;
|
||||
|
||||
|
||||
unsigned int VAO, VBO, EBO;
|
||||
void setupMesh();
|
||||
void SetupMesh();
|
||||
|
||||
AABB m_AABB;
|
||||
void CalculateAABB();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ namespace Nuake
|
||||
{
|
||||
struct Vertex
|
||||
{
|
||||
glm::vec3 position;
|
||||
glm::vec2 uv;
|
||||
glm::vec3 normal;
|
||||
glm::vec3 tangent;
|
||||
glm::vec3 bitangent;
|
||||
Vector3 position;
|
||||
Vector2 uv;
|
||||
Vector3 normal;
|
||||
Vector3 tangent;
|
||||
Vector3 bitangent;
|
||||
float texture;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "Engine.h"
|
||||
#include "Engine.h"
|
||||
#include "src/Core/FileSystem.h"
|
||||
#include "Components/Components.h"
|
||||
@@ -42,11 +41,10 @@ namespace Nuake {
|
||||
m_Environement = CreateRef<Environment>();
|
||||
m_Interfaces = std::vector<Ref<UI::UserInterface>>();
|
||||
|
||||
// Adding systems
|
||||
// Adding systems - Order is important
|
||||
m_Systems.push_back(CreateRef<TransformSystem>(this));
|
||||
m_Systems.push_back(CreateRef<ScriptingSystem>(this));
|
||||
m_Systems.push_back(CreateRef<PhysicsSystem>(this));
|
||||
|
||||
}
|
||||
|
||||
Scene::~Scene() {}
|
||||
@@ -95,7 +93,6 @@ namespace Nuake {
|
||||
{
|
||||
for (auto& system : m_Systems)
|
||||
system->Exit();
|
||||
|
||||
}
|
||||
|
||||
void Scene::Update(Timestep ts)
|
||||
@@ -123,15 +120,16 @@ namespace Nuake {
|
||||
void Scene::UpdatePositions()
|
||||
{
|
||||
auto transformView = m_Registry.view<ParentComponent, TransformComponent>();
|
||||
for (auto e : transformView) {
|
||||
for (auto e : transformView)
|
||||
{
|
||||
auto [parent, transform] = transformView.get<ParentComponent, TransformComponent>(e);
|
||||
Entity currentParent = Entity{ e, this };
|
||||
Vector3 globalPos = Vector3();
|
||||
if (parent.HasParent)
|
||||
{
|
||||
while (currentParent.GetComponent<ParentComponent>().HasParent) {
|
||||
while (currentParent.GetComponent<ParentComponent>().HasParent)
|
||||
{
|
||||
currentParent = currentParent.GetComponent<ParentComponent>().Parent;
|
||||
|
||||
globalPos += currentParent.GetComponent<TransformComponent>().Translation;
|
||||
}
|
||||
|
||||
@@ -186,14 +184,16 @@ namespace Nuake {
|
||||
continue;
|
||||
|
||||
light.CalculateViewProjection(cam->GetTransform(), cam->GetPerspective());
|
||||
|
||||
|
||||
for (int i = 0; i < CSM_AMOUNT; i++)
|
||||
{
|
||||
light.m_Framebuffers[i]->Bind();
|
||||
light.m_Framebuffers[i]->Clear();
|
||||
|
||||
|
||||
Frustum lightFrustum = Frustum(light.mViewProjections[i]);
|
||||
shadowShader->SetUniformMat4f("u_LightTransform", light.mViewProjections[i]);
|
||||
|
||||
|
||||
for (auto e : meshView)
|
||||
{
|
||||
auto [transform, mesh] = meshView.get<TransformComponent, MeshComponent>(e);
|
||||
@@ -209,8 +209,15 @@ namespace Nuake {
|
||||
if (model.IsTransparent)
|
||||
continue;
|
||||
|
||||
for (auto& m : model.Meshes)
|
||||
Renderer::SubmitMesh(m, transform.GetTransform());
|
||||
for (Ref<Mesh>& m : model.Meshes)
|
||||
{
|
||||
AABB modelAABB = m->GetAABB();
|
||||
modelAABB.Transform(transform.GetTransform());
|
||||
|
||||
bool isInFrustum = lightFrustum.IsBoxVisible(modelAABB.Min, modelAABB.Max);
|
||||
if (isInFrustum)
|
||||
Renderer::SubmitMesh(m, transform.GetTransform());
|
||||
}
|
||||
}
|
||||
|
||||
Renderer::Flush(shadowShader, true);
|
||||
@@ -295,7 +302,12 @@ namespace Nuake {
|
||||
continue;
|
||||
|
||||
for (auto& b : model.Meshes)
|
||||
Renderer::SubmitMesh(b, transform.GetTransform());
|
||||
{
|
||||
AABB aabb = b->GetAABB();
|
||||
aabb.Transform(transform.GetTransform());
|
||||
if(cam->BoxFrustumCheck(aabb))
|
||||
Renderer::SubmitMesh(b, transform.GetTransform());
|
||||
}
|
||||
}
|
||||
|
||||
Renderer::Flush(pbrShader);
|
||||
@@ -403,12 +415,16 @@ namespace Nuake {
|
||||
if (!model.IsTransparent)
|
||||
continue;
|
||||
|
||||
|
||||
flatShader->SetUniformMat4f("u_Model", transform.GetTransform());
|
||||
flatShader->SetUniform4f("u_Color", 1.f, 0.0f, 0.1f, 0.5f);
|
||||
|
||||
for (auto& e : model.Meshes)
|
||||
Renderer::SubmitMesh(e, transform.GetTransform());
|
||||
for (auto& b : model.Meshes)
|
||||
{
|
||||
AABB aabb = b->GetAABB();
|
||||
aabb.Transform(transform.GetTransform());
|
||||
if (m_EditorCamera->BoxFrustumCheck(aabb))
|
||||
Renderer::SubmitMesh(b, transform.GetTransform());
|
||||
}
|
||||
}
|
||||
|
||||
Renderer::Flush(flatShader);
|
||||
@@ -454,12 +470,6 @@ namespace Nuake {
|
||||
}
|
||||
}
|
||||
|
||||
Entity Scene::CreateEmptyEntity()
|
||||
{
|
||||
Entity entity = { m_Registry.create(), this };
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity Scene::CreateEntity(const std::string& name)
|
||||
{
|
||||
Entity entity = { m_Registry.create(), this };
|
||||
@@ -593,7 +603,7 @@ namespace Nuake {
|
||||
for (json e : j["Entities"])
|
||||
{
|
||||
std::string name = e["NameComponent"]["Name"];
|
||||
Entity ent = CreateEmptyEntity();
|
||||
Entity ent = { m_Registry.create(), this };
|
||||
ent.Deserialize(e.dump());
|
||||
}
|
||||
|
||||
|
||||
@@ -62,8 +62,6 @@ namespace Nuake {
|
||||
std::vector<Entity> GetAllEntities();
|
||||
Entity GetEntity(const std::string& name);
|
||||
|
||||
// TODO: This shouldnt be allowed
|
||||
Entity CreateEmptyEntity();
|
||||
Entity CreateEntity(const std::string& name);
|
||||
|
||||
// TODO: Could be moved to transform component directly.
|
||||
|
||||
Reference in New Issue
Block a user