Skip to content

Commit

Permalink
Merge pull request #18 from mariofv/SpacePartitioning
Browse files Browse the repository at this point in the history
Space partitioning
  • Loading branch information
mariofv authored Dec 16, 2019
2 parents 3e2fc0a + d31fc8d commit ac49676
Show file tree
Hide file tree
Showing 35 changed files with 4,263 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TestResult.xml
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c

enc_temp_folder/
# Benchmark Results
BenchmarkDotNet.Artifacts/

Expand Down
14 changes: 8 additions & 6 deletions Engine/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "Application.h"
#include "Module/ModuleWindow.h"
#include "Module/ModuleRender.h"
#include "Module/ModuleInput.h"
#include "Module/ModuleProgram.h"
#include "Module/ModuleTexture.h"
#include "Module/ModuleEditor.h"
#include "Module/ModuleCamera.h"
#include "Module/ModuleDebug.h"
#include "Module/ModuleEditor.h"
#include "Module/ModuleInput.h"
#include "Module/ModuleModelLoader.h"
#include "Module/ModuleProgram.h"
#include "Module/ModuleRender.h"
#include "Module/ModuleScene.h"
#include "Module/ModuleTexture.h"
#include "Module/ModuleTime.h"
#include "Module/ModuleWindow.h"
#include "UI/EngineUI.h"
#include "UI/EngineLog.h"
#include "TimerUs.h"
Expand All @@ -30,6 +31,7 @@ Application::Application()
modules.emplace_back(program = new ModuleProgram());
modules.emplace_back(cameras = new ModuleCamera());
modules.emplace_back(model_loader = new ModuleModelLoader());
modules.emplace_back(debug = new ModuleDebug());

engine_log = new EngineLog();
ui = new EngineUI();
Expand Down
2 changes: 2 additions & 0 deletions Engine/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ModuleCamera;
class ModuleModelLoader;
class ModuleTime;
class ModuleScene;
class ModuleDebug;

class EngineLog;
class EngineUI;
Expand Down Expand Up @@ -46,6 +47,7 @@ class Application
ModuleModelLoader* model_loader = nullptr;
ModuleTime* time = nullptr;
ModuleScene* scene = nullptr;
ModuleDebug* debug = nullptr;

EngineUI* ui = nullptr;
EngineLog* engine_log = nullptr;
Expand Down
14 changes: 10 additions & 4 deletions Engine/Component/ComponentAABB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ void ComponentAABB::GenerateBoundingBox()
}

bounding_box.TransformAsAABB(owner->transform.GetGlobalModelMatrix());

float2 min_point2D = float2(bounding_box.MinX(), bounding_box.MinZ());
float2 max_point2D = float2(bounding_box.MaxX(), bounding_box.MaxZ());
bounding_box2D = AABB2D(min_point2D, max_point2D);
}

void ComponentAABB::GenerateBoundingBoxFromVertices(const std::vector<ComponentMesh::Vertex> & vertices)
Expand All @@ -69,16 +73,18 @@ bool ComponentAABB::IsEmpty() const

std::vector<float> ComponentAABB::GetVertices() const
{
float3 tmp_vertices[8];
static const int num_of_vertices = 8;
float3 tmp_vertices[num_of_vertices];
bounding_box.GetCornerPoints(&tmp_vertices[0]);

std::vector<float> vertices(24);
for (unsigned int i = 0; i < 8; ++i)
std::vector<float> vertices(num_of_vertices * 3);
for (unsigned int i = 0; i < num_of_vertices; ++i)
{
vertices[i * 3] = tmp_vertices[i].x;
vertices[i * 3 + 1] = tmp_vertices[i].y;
vertices[i * 3 + 2] = tmp_vertices[i].z;
}

return vertices;
}
}

1 change: 1 addition & 0 deletions Engine/Component/ComponentAABB.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ComponentAABB : public Component

public:
AABB bounding_box;
AABB2D bounding_box2D;

};

Expand Down
52 changes: 51 additions & 1 deletion Engine/Component/ComponentCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "Module/ModuleTime.h"
#include "Module/ModuleCamera.h"

#include "OLQuadTree.h" //TODO: This is not needed, nly for GetVertices()

ComponentCamera::ComponentCamera() : Component(nullptr, ComponentType::CAMERA)
{
glGenFramebuffers(1, &fbo);
Expand Down Expand Up @@ -415,6 +417,11 @@ bool ComponentCamera::IsInsideFrustum(const AABB& aabb) const
return CheckAABBCollision(aabb) != ComponentAABB::CollisionState::OUTSIDE;
}

bool ComponentCamera::IsInsideFrustum(const AABB2D& aabb) const
{
return CheckAABB2DCollision(aabb) != ComponentAABB::CollisionState::OUTSIDE;
}

ComponentAABB::CollisionState ComponentCamera::CheckAABBCollision(const AABB& reference_AABB) const
{
static const size_t number_of_corners = 8;
Expand Down Expand Up @@ -450,7 +457,50 @@ ComponentAABB::CollisionState ComponentCamera::CheckAABBCollision(const AABB& re
total_reference_planes_inside += is_plane_inside;
}
// so if total_reference_planes_inside is 6, then all are inside the view
if (total_reference_planes_inside == 6)
if (total_reference_planes_inside == number_of_planes)
{
return ComponentAABB::CollisionState::INSIDE;
}
// we must be partly in then otherwise
return ComponentAABB::CollisionState::INTERSECT;
}

ComponentAABB::CollisionState ComponentCamera::CheckAABB2DCollision(const AABB2D& reference_AABB) const
{
static const size_t number_of_corners = 4;
static const size_t number_of_planes = 4;

//Get own aabb planes
Plane own_frustum_planes[6];
camera_frustum.GetPlanes(own_frustum_planes);

//Get refence corners
std::vector<float> reference_aabb_corners = OLQuadTree::GetVertices(reference_AABB);

//Check if Corners are inside the planes
int total_reference_planes_inside = 0;
for (int p = 0; p < number_of_planes; ++p)
{
int points_inside_count = number_of_corners;
int is_plane_inside = 1;
for (int i = 0; i < number_of_corners; ++i)
{
float3 current_corner = float3(reference_aabb_corners[3 * i], reference_aabb_corners[3 * i + 1], reference_aabb_corners[3 * i + 2]);
if (own_frustum_planes[p].IsOnPositiveSide(current_corner)) //If true, the point is halfway or outside
{
// Plane is not inside
is_plane_inside = 0;
--points_inside_count;
}
}
if (points_inside_count == 0)
{
return ComponentAABB::CollisionState::OUTSIDE;
}
total_reference_planes_inside += is_plane_inside;
}
// so if total_reference_planes_inside is 6, then all are inside the view
if (total_reference_planes_inside == number_of_planes)
{
return ComponentAABB::CollisionState::INSIDE;
}
Expand Down
8 changes: 6 additions & 2 deletions Engine/Component/ComponentCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ class ComponentCamera : public Component
void GenerateMatrices();

std::vector<float> GetFrustumVertices() const;

bool ComponentCamera::IsInsideFrustum(const AABB& aabb) const;
ComponentAABB::CollisionState CheckAABBCollision(const AABB& reference_AABB) const;

bool ComponentCamera::IsInsideFrustum(const AABB2D& aabb) const;
ComponentAABB::CollisionState CheckAABB2DCollision(const AABB2D& reference_AABB) const;

void ShowComponentWindow() override;

private:
Expand All @@ -88,8 +92,8 @@ class ComponentCamera : public Component
const float CAMERA_MAXIMUN_MOVEMENT_SPEED = 1.0f;
const float CAMERA_MINIMUN_MOVEMENT_SPEED = 0.005f;

float camera_movement_speed = 0.25f;
float camera_zooming_speed = 0.25f;
float camera_movement_speed = 0.15f;
float camera_zooming_speed = 0.15f;
float camera_rotation_speed = 0.000625f;

float camera_clear_color[3] = {0.0f, 0.0f, 0.0f};
Expand Down
2 changes: 0 additions & 2 deletions Engine/Component/ComponentMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class ComponentMesh : public Component
std::vector<unsigned int> indices;
unsigned int material_index = -1;

AABB bounding_box;

private:
GLuint vao = 0;
GLuint vbo = 0;
Expand Down
27 changes: 26 additions & 1 deletion Engine/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ bool GameObject::IsEnabled() const
return active;
}

void GameObject::SetStatic(bool is_static)
{

SetHierarchyStatic(is_static);
App->renderer->GenerateQuadTree();
}

void GameObject::SetHierarchyStatic(bool is_static)
{
this->is_static = is_static;
for (auto & child : children)
{
child->SetStatic(is_static);
}
}

bool GameObject::IsStatic() const
{
return is_static;
}
void GameObject::Update()
{
transform.GenerateGlobalModelMatrix();
Expand Down Expand Up @@ -138,7 +158,6 @@ Component* GameObject::CreateComponent(const Component::ComponentType type)

created_component->owner = this;
components.push_back(created_component);

return created_component;
}

Expand Down Expand Up @@ -249,6 +268,12 @@ void GameObject::ShowPropertiesWindow()
ImGui::SameLine();
ImGui::InputText("###GameObject name Input", &name);

ImGui::SameLine();
if (ImGui::Checkbox("Static", &is_static))
{
SetStatic(is_static);
}

ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
Expand Down
7 changes: 7 additions & 0 deletions Engine/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class GameObject

bool IsEnabled() const;

void SetStatic(bool is_static);
bool IsStatic() const;

void Update();

void SetParent(GameObject *new_parent);
Expand All @@ -38,7 +41,10 @@ class GameObject

const GLuint GetMaterialTexture(const int material_index) const;


void ShowPropertiesWindow();
private:
void SetHierarchyStatic(bool is_static);

public:
std::string name = "";
Expand All @@ -54,6 +60,7 @@ class GameObject

private:
bool active = true;
bool is_static = false;
int hierarchy_depth = 0;
int hierarchy_branch = 0;
};
Expand Down
69 changes: 69 additions & 0 deletions Engine/GeometryRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
GeometryRenderer::GeometryRenderer()
{
InitHexahedron();
InitSquare();
}

GeometryRenderer::~GeometryRenderer()
{
delete hexahedron;
delete square;
}

void GeometryRenderer::InitHexahedron()
Expand All @@ -36,6 +38,24 @@ void GeometryRenderer::InitHexahedron()
glBindVertexArray(0);
}

void GeometryRenderer::InitSquare()
{
square = new Geometry;

square->num_indices = 4;
unsigned int indices[] = {
0,1,2,3
};

glBindVertexArray(square->vao);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, square->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindVertexArray(0);
}

void GeometryRenderer::RenderGeometry(const ComponentCamera &camera, const GeometryRenderer::Geometry &geometry) const
{
glUseProgram(App->program->default_program);
Expand Down Expand Up @@ -91,3 +111,52 @@ void GeometryRenderer::RenderHexahedron(const ComponentCamera &camera, const std

RenderGeometry(camera, *hexahedron);
}

void GeometryRenderer::RenderSquare(const ComponentCamera &camera, const std::vector<float> &vertices)
{
glBindVertexArray(square->vao);

glBindBuffer(GL_ARRAY_BUFFER, square->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 12, &vertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
0, // attribute
3, // number of elements per vertex, here (x,y,z)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

glUseProgram(App->program->default_program);

float4x4 model_matrix = float4x4::identity;

glUniformMatrix4fv(
glGetUniformLocation(App->program->default_program, "model"),
1,
GL_TRUE,
&model_matrix[0][0]
);
glUniformMatrix4fv(
glGetUniformLocation(App->program->default_program, "view"),
1,
GL_TRUE,
&camera.GetViewMatrix()[0][0]
);
glUniformMatrix4fv(
glGetUniformLocation(App->program->default_program, "proj"),
1,
GL_TRUE,
&camera.GetProjectionMatrix()[0][0]
);

glBindVertexArray(square->vao);
glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

glUseProgram(0);
}
3 changes: 3 additions & 0 deletions Engine/GeometryRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ class GeometryRenderer
~GeometryRenderer();

void InitHexahedron();
void InitSquare();

void RenderGeometry(const ComponentCamera &camera, const GeometryRenderer::Geometry &geometry) const;
void RenderHexahedron(const ComponentCamera &camera, const std::vector<float> &vertices);
void RenderSquare(const ComponentCamera &camera, const std::vector<float> &vertices);

private:
Geometry *hexahedron = nullptr; // Cube like shapes
Geometry *square = nullptr; // Cube like shapes

};

Expand Down
1 change: 1 addition & 0 deletions Engine/Module/ModuleCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bool ModuleCamera::Init()
scene_camera_game_object = new GameObject();
scene_camera_game_object->transform.SetTranslation(float3(0.5f, 2.f, -15.f));
scene_camera = (ComponentCamera*)scene_camera_game_object->CreateComponent(Component::ComponentType::CAMERA);
scene_camera->SetFarDistance(500);

skybox = new Skybox();

Expand Down
Loading

0 comments on commit ac49676

Please sign in to comment.