Merge remote-tracking branch 'origin/master' into Rendering
# Conflicts: # include/Engine/Editor/EditorSystem.h # include/Engine/Rendering/EPicking.h # include/Engine/Rendering/Renderer.h # include/Game/Game.h # resources/Schema/Components.xsd # resources/Schema/Entities/Test.xml # src/Engine/Editor/EditorSystem.cpp # src/Engine/Rendering/RawModel.cpp # src/Engine/Rendering/RenderQueueFactory.cpp # src/Engine/Rendering/Renderer.cpp # src/Game/Game.cpp
This commit is contained in:
@@ -12,6 +12,7 @@ include_directories(
|
||||
)
|
||||
|
||||
file(GLOB SOURCE_FILES
|
||||
"*.h"
|
||||
"*.cpp"
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
//#define BOOST_TEST_MODULE collTest
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/execution_monitor.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include "Engine/Collision/Collision.h"
|
||||
#include "Engine/Core/AABB.h"
|
||||
#include "Engine/Core/Ray.h"
|
||||
#include <stdlib.h>//srand
|
||||
#include "Engine/Core/OctTree.h"
|
||||
//vs model
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
//ray vs model
|
||||
#include "Engine\Core\ResourceManager.h"
|
||||
#include "Engine\Rendering\Model.h"
|
||||
#include "Engine\Core\Ray.h"
|
||||
|
||||
//vs memleaks
|
||||
//#define _CRTDBG_MAP_ALLOC
|
||||
//#include <stdlib.h>
|
||||
//#include <crtdbg.h>
|
||||
//#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
|
||||
//#define new DEBUG_CLIENTBLOCK
|
||||
|
||||
void RayTest(std::string fileName) {
|
||||
//simple box test
|
||||
Ray ray(glm::vec3(-50, 0, 0), glm::vec3(1, 0, 0));
|
||||
//using a rawmodel here, else we have to init the renderingsystem
|
||||
ResourceManager::RegisterType<RawModel>("RawModel");
|
||||
auto unitBox = ResourceManager::Load<RawModel>(fileName);
|
||||
BOOST_REQUIRE(unitBox != nullptr);
|
||||
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
BOOST_CHECK(hit);
|
||||
ray.SetDirection(glm::vec3(-1, 0, 0));
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
BOOST_CHECK(!hit);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(collisionTests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(collisionTest)
|
||||
{
|
||||
//memleak
|
||||
int* globalLeak = new int[5];
|
||||
|
||||
//fixed seed
|
||||
srand(2);
|
||||
AABB someAABB;
|
||||
glm::vec3 minPos;
|
||||
glm::vec3 maxPos;
|
||||
bool z;
|
||||
int test = 0;
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
{
|
||||
Ray ray(
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100),
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100)
|
||||
);
|
||||
minPos.x = rand() % 100;
|
||||
minPos.y = rand() % 100;
|
||||
minPos.z = rand() % 100;
|
||||
maxPos.x = rand() % 100;
|
||||
maxPos.y = rand() % 100;
|
||||
maxPos.z = rand() % 100;
|
||||
|
||||
someAABB = AABB(minPos, maxPos);
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
if (z) ++test;
|
||||
}
|
||||
BOOST_CHECK(test >= 0);
|
||||
|
||||
//_CrtDumpMemoryLeaks();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(collisionTest2)
|
||||
{
|
||||
//fixed seed
|
||||
srand(2);
|
||||
AABB someAABB;
|
||||
glm::vec3 minPos;
|
||||
glm::vec3 maxPos;
|
||||
bool z;
|
||||
int test = 0;
|
||||
for (size_t i = 0; i < 1000000; i++)
|
||||
{
|
||||
Ray ray(
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100),
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100)
|
||||
);
|
||||
minPos.x = rand() % 100;
|
||||
minPos.y = rand() % 100;
|
||||
minPos.z = rand() % 100;
|
||||
maxPos.x = rand() % 100;
|
||||
maxPos.y = rand() % 100;
|
||||
maxPos.z = rand() % 100;
|
||||
|
||||
someAABB = AABB(minPos, maxPos);
|
||||
z = Collision::RayAABBIntr(ray, someAABB);
|
||||
if (z) ++test;
|
||||
}
|
||||
BOOST_CHECK(test >= 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest)
|
||||
{
|
||||
//simple box test
|
||||
RayTest("Models/Core/UnitCube.obj");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest2)
|
||||
{
|
||||
//advanced test, this will check so rayVSAABB and rayVsModel(with boxmodel) gives the same result (hit/miss)
|
||||
//testing with different seeds
|
||||
// srand(7676762);
|
||||
// srand(7676462);
|
||||
// srand(7462);
|
||||
srand(72);
|
||||
AABB someAABB;
|
||||
glm::vec3 minPos;
|
||||
glm::vec3 maxPos;
|
||||
bool z;
|
||||
int test = 0;
|
||||
//min/max is the same as the rawmodels boundaries ofcourse
|
||||
minPos = glm::vec3(-0.5f, -0.5f, -0.5f);
|
||||
maxPos = glm::vec3(0.5f, 0.5f, 0.5f);
|
||||
someAABB = AABB(minPos, maxPos);
|
||||
//using a rawmodel here, else we have to init the renderingsystem
|
||||
ResourceManager::RegisterType<RawModel>("RawModel");
|
||||
auto unitBox = ResourceManager::Load<RawModel>("Models/Core/UnitCube.obj");
|
||||
BOOST_CHECK(unitBox != nullptr);
|
||||
|
||||
for (size_t i = 0; i < 1000000; i++)
|
||||
{
|
||||
Ray ray(
|
||||
glm::vec3(-2, 0, 0),
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100)
|
||||
);
|
||||
//if we normalize the ray.direction when its 0,0,0 then we get nan,nan,nan - thus we have this check to prevent that
|
||||
if (glm::any(glm::isnan(ray.Direction())))
|
||||
continue;
|
||||
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
if (z) {
|
||||
//hit
|
||||
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
if (!hit) {
|
||||
//if rayvsaabb hit but rayvvmodel didnt hit, we get to here
|
||||
glm::vec3 outtttttttt;
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices, outtttttttt);
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
}
|
||||
else {
|
||||
hit = hit;
|
||||
}
|
||||
BOOST_CHECK(hit);
|
||||
}
|
||||
////breakpoint test
|
||||
//if (!z) {
|
||||
// z = z;
|
||||
//}
|
||||
//
|
||||
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
////breakpoint test
|
||||
//if (!hit) {
|
||||
// hit = hit;
|
||||
//}
|
||||
if (hit) {
|
||||
//hit
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
if (!z) {
|
||||
//if rayvsmodel hit but rayvsaabb didnt hit then we get to here
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
glm::vec3 outtttttttt;
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices, outtttttttt);
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
}
|
||||
else {
|
||||
z = z;
|
||||
}
|
||||
BOOST_CHECK(hit);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest3)
|
||||
{
|
||||
//simple test
|
||||
RayTest("Models/Core/UnitSphere.obj");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest4)
|
||||
{
|
||||
//simple test
|
||||
RayTest("Models/Core/UnitCylinder.obj");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest5)
|
||||
{
|
||||
//simple test
|
||||
RayTest("Models/Core/UnitRaptor.obj");
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(octTest)
|
||||
{
|
||||
glm::vec3 mini = glm::vec3(-1, -1, -1);
|
||||
glm::vec3 maxi = glm::vec3(1, 1, 1);
|
||||
OctTree tree(AABB(mini, maxi), 2);
|
||||
tree.AddDynamicObject(AABB(mini, -0.9f*maxi));
|
||||
OctTree::Output data;
|
||||
glm::vec3 origin = 3.0f * mini;
|
||||
bool rayIntersected = tree.RayCollides(Ray(origin , mini - origin), data);
|
||||
BOOST_CHECK(rayIntersected);
|
||||
tree.ClearDynamicObjects();
|
||||
rayIntersected = tree.RayCollides(Ray(origin, mini - origin), data);
|
||||
BOOST_CHECK(!rayIntersected);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/execution_monitor.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include <stdlib.h>//srand
|
||||
|
||||
//#define private public
|
||||
#include "Engine\Core\ConfigFile.h"
|
||||
|
||||
#define _CRTDBG_MAP_ALLOC
|
||||
#include <crtdbg.h>
|
||||
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
|
||||
#define new DEBUG_CLIENTBLOCK
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(confTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(configFileTest)
|
||||
{
|
||||
//note: this ConfigFileclass currently has memleaks!
|
||||
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
auto m_Config = ResourceManager::Load<ConfigFile>("ConfigTest.ini");
|
||||
|
||||
//bägge måste vara av samma typ, T typen är string
|
||||
//http://www.boost.org/doc/libs/1_42_0/doc/html/boost_propertytree/tutorial.html
|
||||
//"Note that we construct the path to the value by separating the individual keys with dots"
|
||||
|
||||
//get from tree tests
|
||||
auto getSomething = m_Config->Get("Test.Test1", 0);
|
||||
BOOST_CHECK(getSomething == 423);
|
||||
|
||||
auto getSomething2 = m_Config->Get("fsdfdsfd.T", std::string(""));
|
||||
BOOST_CHECK(getSomething2 == "\"gfdjakflsdl!\"");
|
||||
|
||||
//set/get tests
|
||||
m_Config->Set("Test.4321", 123);
|
||||
auto getSomething3 = m_Config->Get("Test.4321", 0);
|
||||
BOOST_CHECK(getSomething3 == 123);
|
||||
|
||||
m_Config->Set("3_2_1_0_5", "t454j54hj5k32");
|
||||
auto getSomething4 = m_Config->Get("3_2_1_0_5", std::string(""));
|
||||
BOOST_CHECK(getSomething4 == "t454j54hj5k32");
|
||||
|
||||
//***check so outputwindow says: EE: Failed to find "DefaultConfigTestNotExists.ini"! Relying on hardcoded default values!
|
||||
auto m_Config2 = ResourceManager::Load<ConfigFile>("ConfigTestNotExists.ini");
|
||||
|
||||
//set value/savetodisk/load/checkvalue...
|
||||
m_Config->SaveToDisk();
|
||||
m_Config->Set("Test.4321", 145);
|
||||
m_Config->SaveToDisk();
|
||||
auto m_Config3 = ResourceManager::Load<ConfigFile>("ConfigTest.ini");
|
||||
auto getSomething5 = m_Config->Get("Test.4321", 0);
|
||||
BOOST_CHECK(getSomething5 == 145);
|
||||
|
||||
//***check so outputwindow says: EE: Failed to parse "DefaultConfigTestFailed.ini"
|
||||
//***check so outputwindow says: EE: Failed to parse "ConfigTestFailed.ini":
|
||||
auto m_Config4 = ResourceManager::Load<ConfigFile>("ConfigTestFailed.ini");
|
||||
|
||||
//reload,onchildreload unimplemented
|
||||
|
||||
//NOTE:still massive amount of memoryleaks from this method
|
||||
_CrtDumpMemoryLeaks();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifndef EVENTFIXTURE_H
|
||||
#define EVENTFIXTURE_H
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "Core\EventBroker.h"
|
||||
|
||||
template <typename EventType>
|
||||
struct EventFixture
|
||||
{
|
||||
EventFixture()
|
||||
{
|
||||
this->ventBroker = new EventBroker();
|
||||
m_EEventType = decltype(m_EEventType)(std::bind(&EventFixture::OnEvent, this, std::placeholders::_1));
|
||||
this->ventBroker->Subscribe(m_EEventType);
|
||||
Run();
|
||||
Check();
|
||||
}
|
||||
~EventFixture()
|
||||
{
|
||||
this->ventBroker->Unsubscribe(m_EEventType);
|
||||
delete this->ventBroker;
|
||||
}
|
||||
|
||||
EventBroker* ventBroker = nullptr;
|
||||
EventRelay<EventFixture, EventType> m_EEventType;
|
||||
bool m_EventRecieved = false;
|
||||
EventType Before;
|
||||
EventType After;
|
||||
|
||||
bool OnEvent(const EventType& event)
|
||||
{
|
||||
m_EventRecieved = true;
|
||||
After = event;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Run()
|
||||
{
|
||||
// Publish the event
|
||||
this->ventBroker->Publish(Before);
|
||||
// Clear to swap buffers
|
||||
this->ventBroker->Swap();
|
||||
// Process the event
|
||||
this->ventBroker->template Process<EventFixture>();
|
||||
}
|
||||
|
||||
void Check()
|
||||
{
|
||||
BOOST_CHECK(m_EventRecieved);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "EventFixture.h"
|
||||
|
||||
struct ETestEvent : public Event
|
||||
{
|
||||
int Int = 5;
|
||||
float Float = 1.33333f;
|
||||
double Double = 1.33333;
|
||||
std::string String = "Hello World";
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_CASE(EventBrokerTest)
|
||||
{
|
||||
EventFixture<ETestEvent> f;
|
||||
BOOST_CHECK(f.Before.Int == f.After.Int);
|
||||
BOOST_CHECK_CLOSE(f.Before.Float, f.After.Float, 0.00001f);
|
||||
BOOST_CHECK_CLOSE(f.Before.Double, f.After.Double, 0.00001f);
|
||||
BOOST_CHECK(f.Before.String == f.After.String);
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
|
||||
#include "HealthSystemTest.h"
|
||||
#include "Game/HealthSystem.h"
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(HealthSystemSuite)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(HealthSystemTest)
|
||||
{
|
||||
//this tests 2 healthevents and the healthsystem
|
||||
GameHealthSystemTest game;
|
||||
//100 loops will be more than enough to do the test
|
||||
int loops = 100;
|
||||
bool success = false;
|
||||
while (loops > 0) {
|
||||
game.Tick();
|
||||
if (game.TestSucceeded) {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
loops--;
|
||||
}
|
||||
//The system will process the events, hence it will take a while before we can read anything
|
||||
BOOST_TEST(success);
|
||||
}
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
GameHealthSystemTest::GameHealthSystemTest()
|
||||
{
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
|
||||
// Create the core event broker
|
||||
m_EventBroker = new EventBroker();
|
||||
|
||||
// Create a world
|
||||
m_World = new World();
|
||||
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
||||
if (!mapToLoad.empty()) {
|
||||
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
||||
}
|
||||
|
||||
// Create system pipeline
|
||||
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>(0);
|
||||
m_SystemPipeline->AddSystem<HealthSystem>(0);
|
||||
|
||||
//The Test
|
||||
//create entity which has transorm,player,model,health in it. i.e. is a player
|
||||
EntityID playerID = m_World->CreateEntity();
|
||||
ComponentWrapper transform = m_World->AttachComponent(playerID, "Transform");
|
||||
ComponentWrapper model = m_World->AttachComponent(playerID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||
ComponentWrapper player = m_World->AttachComponent(playerID, "Player");
|
||||
ComponentWrapper health = m_World->AttachComponent(playerID, "Health");
|
||||
healthsID = playerID;
|
||||
double currentHealth = (double)m_World->GetComponent(healthsID, "Health")["Health"];
|
||||
|
||||
//heal player with 40
|
||||
Events::PlayerHealthPickup e3;
|
||||
e3.HealthAmount = 40.0f;
|
||||
e3.PlayerHealedID = healthsID;
|
||||
m_EventBroker->Publish(e3);
|
||||
//damage player with 50
|
||||
Events::PlayerDamage e;
|
||||
e.DamageAmount = 50.0f;
|
||||
e.PlayerDamagedID = healthsID;
|
||||
m_EventBroker->Publish(e);
|
||||
//heal some other player with 40
|
||||
Events::PlayerHealthPickup e2;
|
||||
e2.HealthAmount = 40.0f;
|
||||
e2.PlayerHealedID = healthsID+1;
|
||||
m_EventBroker->Publish(e2);
|
||||
|
||||
EntityID playerID2 = m_World->CreateEntity();
|
||||
ComponentWrapper transform2 = m_World->AttachComponent(playerID2, "Transform");
|
||||
ComponentWrapper model2 = m_World->AttachComponent(playerID2, "Model");
|
||||
model2["Resource"] = "Models/Core/UnitSphere.obj";
|
||||
ComponentWrapper player2 = m_World->AttachComponent(playerID2, "Player");
|
||||
ComponentWrapper health2 = m_World->AttachComponent(playerID2, "Health");
|
||||
//END TEST
|
||||
}
|
||||
|
||||
GameHealthSystemTest::~GameHealthSystemTest()
|
||||
{
|
||||
delete m_SystemPipeline;
|
||||
delete m_World;
|
||||
delete m_EventBroker;
|
||||
}
|
||||
|
||||
void GameHealthSystemTest::Tick()
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
double currentTime = glfwGetTime();
|
||||
double dt = currentTime - m_LastTime;
|
||||
m_LastTime = currentTime;
|
||||
|
||||
// Iterate through systems and update world!
|
||||
m_SystemPipeline->Update(m_World, dt);
|
||||
|
||||
m_EventBroker->Swap();
|
||||
m_EventBroker->Clear();
|
||||
|
||||
//if health reaches 90 then we know the test has succeeded (start with 100hp, remove 50hp, add 40hp)
|
||||
double currentHealth = (double)m_World->GetComponent(healthsID, "Health")["Health"];
|
||||
if (currentHealth==90)
|
||||
TestSucceeded = true;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef HealthTest_h__
|
||||
#define HealthTest_h__
|
||||
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Core/ConfigFile.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Core/InputManager.h"
|
||||
#include "GUI/Frame.h"
|
||||
#include "Core/World.h"
|
||||
#include "Rendering/RenderQueueFactory.h"
|
||||
#include "Input/InputProxy.h"
|
||||
#include "Input/KeyboardInputHandler.h"
|
||||
#include "Input/MouseInputHandler.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
#include "PlayerSystem.h"
|
||||
#include "Editor/EditorSystem.h"
|
||||
|
||||
class GameHealthSystemTest
|
||||
{
|
||||
public:
|
||||
GameHealthSystemTest();
|
||||
~GameHealthSystemTest();
|
||||
|
||||
void Tick();
|
||||
bool TestSucceeded = false;
|
||||
|
||||
private:
|
||||
double m_LastTime;
|
||||
ConfigFile* m_Config = nullptr;
|
||||
EventBroker* m_EventBroker;
|
||||
World* m_World;
|
||||
SystemPipeline* m_SystemPipeline;
|
||||
int healthsID;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "Engine\Core\InputManager.h"
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(inputManagerTests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(inputManagerTest)
|
||||
{
|
||||
//already tested eventbroker so inputManager is indirectly already tested
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,476 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include "Engine/Core/ObjectPool.h"
|
||||
#include <ctime>
|
||||
|
||||
struct S
|
||||
{
|
||||
S() = default;
|
||||
S(int i, float ff) : k(i), f(ff) { }
|
||||
~S() { }
|
||||
int k;
|
||||
float f;
|
||||
};
|
||||
|
||||
//BOOST_GLOBAL_FIXTURE(S);
|
||||
BOOST_AUTO_TEST_SUITE(memProtoTypeTestSuite)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testPool)
|
||||
{
|
||||
ObjectPool<S> pool(32);//32 true/false values = 32 slots
|
||||
BOOST_CHECK(pool.empty() == true);
|
||||
|
||||
const size_t size = 12;//12 platser i structen addresses, som håller en int, en float vardera
|
||||
S* addresses[size];
|
||||
addresses[0] = pool.New(7, 0.035f);
|
||||
//"Not empty after allocating one element."
|
||||
BOOST_CHECK(!pool.empty());
|
||||
|
||||
//"Element created correctly with k==7"
|
||||
BOOST_CHECK(addresses[0]->k == 7);
|
||||
//"Element created correctly with f==0.035f"
|
||||
BOOST_CHECK_CLOSE_FRACTION(addresses[0]->f, 0.035f, 0.0001f);
|
||||
addresses[0]->k = 5;
|
||||
BOOST_CHECK(addresses[0]->k == 5);
|
||||
|
||||
//"Empty after delete"
|
||||
pool.Delete(addresses[0]);
|
||||
BOOST_CHECK(pool.empty());
|
||||
|
||||
addresses[0] = pool.New(7, 0.035f);
|
||||
addresses[1] = pool.New(5, 0.035f);
|
||||
pool.Delete(addresses[1]);
|
||||
BOOST_CHECK(!pool.empty());
|
||||
pool.Delete(addresses[0]);
|
||||
BOOST_CHECK(pool.empty());
|
||||
|
||||
//INT32_MAX, FLT_MAX test
|
||||
addresses[0] = pool.New(INT32_MAX, FLT_MAX);
|
||||
BOOST_CHECK(!pool.empty());
|
||||
BOOST_CHECK(addresses[0]->k == INT32_MAX);
|
||||
BOOST_CHECK_CLOSE_FRACTION(addresses[0]->f, FLT_MAX, 0.0001f);
|
||||
}
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE(testPoolArray)
|
||||
{
|
||||
ObjectPool<S> pool(32);
|
||||
S* addresses;
|
||||
|
||||
//Add array size 5 to pool."
|
||||
addresses = pool.NewArray(5);// <-> addresses = new S[5];
|
||||
addresses[0] = S(12, 0.030f);
|
||||
addresses[1] = S(13, 0.031f);
|
||||
addresses[2] = S(14, 0.032f);
|
||||
addresses[3] = S(15, 0.033f);
|
||||
addresses[4] = S(16, 0.034f);
|
||||
|
||||
//"Not empty after allocating
|
||||
BOOST_CHECK(!pool.empty());
|
||||
//"Element created correctly with k==12"
|
||||
BOOST_CHECK(addresses->k == 12);
|
||||
//"Element created correctly with f==0.030f"
|
||||
BOOST_CHECK_CLOSE_FRACTION(addresses->f, 0.030f, 0.0001f);
|
||||
|
||||
//add a few other structs so it becomes bigger than the original size (32),
|
||||
//which means it must push back the rest of the values into a vector
|
||||
S* test2, *test3, *test4, *test5;
|
||||
test2 = pool.NewArray(5);// <-> test2 = new S[5];
|
||||
test3 = pool.NewArray(40);//+40
|
||||
test4 = pool.NewArray(40);//+40
|
||||
test5 = pool.NewArray(40);//+40=120
|
||||
BOOST_CHECK(pool.ExtraSize() == 120);
|
||||
BOOST_CHECK(pool.PoolSize() == 10);
|
||||
BOOST_CHECK(pool.size() == 120 + 10);
|
||||
|
||||
//testar "perfekt delete", dvs bryr mig inte om att testa att deleta bara 38 om storleken egentligen är 40 osv
|
||||
pool.DeleteArray(test2, 5);//callar destructorn på test2 också
|
||||
pool.DeleteArray(test3, 40);
|
||||
|
||||
pool.DeleteArray(addresses, 5);
|
||||
|
||||
//add / del array
|
||||
S* another = pool.NewArray(64);
|
||||
for (int i = 0; i < 64; ++i)
|
||||
another[i] = S(i, 0.1f*i);
|
||||
pool.DeleteArray(another, 64);
|
||||
}
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(testIterationNormal)
|
||||
{
|
||||
//extra vector check
|
||||
S* test4, *test5;
|
||||
ObjectPool<S> pool(4);
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testOutOfScopeDelete)
|
||||
{
|
||||
//extra vector check
|
||||
S* test4, *test5;
|
||||
{
|
||||
ObjectPool<S> pool(4);
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
//pool goes out of scope here, and thus the test4 values become undefined (memory is killed at out of scope)
|
||||
BOOST_CHECK(test4[0].k != 14);
|
||||
BOOST_CHECK(test5[0].k != 15);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testIterationOneExtra)
|
||||
{
|
||||
//extra vector check
|
||||
ObjectPool<S> pool(1);
|
||||
S* test4, *test5;
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testIterationTwoExtra)
|
||||
{
|
||||
//extra vector check
|
||||
ObjectPool<S> pool(1);
|
||||
S* test4, *test5;
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE(releaseModeTest_RandomAllocateDeallocate)
|
||||
{
|
||||
//run this in releasemode
|
||||
struct I
|
||||
{
|
||||
I() = default;
|
||||
I(size_t i, size_t ff) : k(i), f(ff) { }
|
||||
~I() { }
|
||||
size_t k;
|
||||
size_t f;
|
||||
};
|
||||
srand((unsigned int)time(nullptr));
|
||||
|
||||
const size_t SIZE = 128;
|
||||
ObjectPool<I> pool(SIZE);
|
||||
I* addresses[SIZE];
|
||||
std::vector<bool> allocated(SIZE, false);
|
||||
std::vector<size_t> arrSizes(SIZE, 0);
|
||||
size_t slotsAlloced = 0;
|
||||
size_t superCount = 0;
|
||||
size_t slot;
|
||||
size_t i;
|
||||
|
||||
while (superCount++ < 1000) {
|
||||
if (rand() % 2 == 0) {
|
||||
i = 0;
|
||||
//ta slumpmässig slot som inte är allokerad
|
||||
do {
|
||||
slot = (size_t)((SIZE - 1) * ((float)rand() / RAND_MAX));
|
||||
} while (allocated[slot] && ++i < 512);
|
||||
|
||||
if (i < 512) {
|
||||
arrSizes[slot] = 1 + (size_t)((24 - 1) * ((float)rand() / RAND_MAX));
|
||||
addresses[slot] = pool.NewArray(arrSizes[slot]);
|
||||
for (size_t a = 0; a < arrSizes[slot]; ++a)
|
||||
addresses[slot][a] = I(slot, a);
|
||||
allocated[slot] = true;
|
||||
++slotsAlloced;
|
||||
}
|
||||
}
|
||||
//Deallocate
|
||||
else {
|
||||
i = 0;
|
||||
//ta slumpmässig slot som är allokerad
|
||||
do {
|
||||
slot = (size_t)((SIZE - 1) * ((float)rand() / RAND_MAX));
|
||||
} while (!allocated[slot] && ++i < 512);
|
||||
|
||||
if (i < 512) {
|
||||
pool.DeleteArray(addresses[slot], arrSizes[slot]);
|
||||
arrSizes[slot] = 0;
|
||||
allocated[slot] = false;
|
||||
--slotsAlloced;
|
||||
}
|
||||
}
|
||||
//Check content.
|
||||
for (size_t a = 0; a < SIZE; ++a) {
|
||||
if (allocated[a]) {
|
||||
for (size_t e = 0; e < arrSizes[a]; ++e) {
|
||||
BOOST_CHECK(!(addresses[a][e].k != a || addresses[a][e].f != e));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testConstructors)
|
||||
{
|
||||
//http://stackoverflow.com/questions/357929/is-it-important-to-unit-test-a-constructor
|
||||
//"If your constructor has, for example, an if (condition), you need to test both flows (true,false).
|
||||
//If your constructor does some kind of job before setting. You should check the job is done"
|
||||
|
||||
//testing the constructors with different T values and a small check so size is initialized to 0
|
||||
MemoryPool<int> memPoolI;
|
||||
BOOST_CHECK(memPoolI.empty());
|
||||
BOOST_CHECK(memPoolI.size() == 0);
|
||||
MemoryPool<float> memPoolF;
|
||||
BOOST_CHECK(memPoolF.empty());
|
||||
BOOST_CHECK(memPoolF.size() == 0);
|
||||
MemoryPool<double> memPoolD;
|
||||
BOOST_CHECK(memPoolD.empty());
|
||||
BOOST_CHECK(memPoolD.size() == 0);
|
||||
MemoryPool<S> memPoolS;
|
||||
BOOST_CHECK(memPoolS.empty());
|
||||
BOOST_CHECK(memPoolS.size() == 0);
|
||||
|
||||
ObjectPool<int> objPoolI(64);
|
||||
BOOST_CHECK(objPoolI.empty());
|
||||
BOOST_CHECK(objPoolI.size() == 0);
|
||||
ObjectPool<float> objPoolF(32);
|
||||
BOOST_CHECK(objPoolF.empty());
|
||||
BOOST_CHECK(objPoolF.size() == 0);
|
||||
ObjectPool<double> objPoolD(16);
|
||||
BOOST_CHECK(objPoolD.empty());
|
||||
BOOST_CHECK(objPoolD.size() == 0);
|
||||
ObjectPool<S> objPoolS(128);
|
||||
BOOST_CHECK(objPoolS.empty());
|
||||
BOOST_CHECK(objPoolS.size() == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testOperators)
|
||||
{
|
||||
ObjectPool<S> pool(100);
|
||||
// S* s[12] = pool.NewArray(12);
|
||||
S* s[12];
|
||||
s[0] = pool.New();
|
||||
s[11] = pool.New();
|
||||
|
||||
s[0]->k = 2;
|
||||
s[11]->k = 3;
|
||||
//testing operators: ++i,!=
|
||||
auto& iter = pool.begin();
|
||||
for (iter; iter != pool.end(); ++iter) {
|
||||
//testing operators:*,==
|
||||
auto dereferencedIterator = *iter;
|
||||
if (iter == pool.begin()) {
|
||||
BOOST_CHECK(dereferencedIterator.k == 2);
|
||||
}
|
||||
if (iter == pool.end()) {
|
||||
BOOST_CHECK(dereferencedIterator.k == 3);
|
||||
}
|
||||
//testing operators:->
|
||||
iter->k += 2;
|
||||
}
|
||||
BOOST_CHECK(s[0]->k == 4);
|
||||
BOOST_CHECK(s[11]->k == 5);
|
||||
BOOST_CHECK(iter == pool.end());
|
||||
|
||||
//testing operators:i++
|
||||
s[0]->k = 2;
|
||||
s[11]->k = 2;
|
||||
for (auto& iter = pool.begin(); iter != pool.end(); iter++)
|
||||
iter->k += 2;
|
||||
BOOST_CHECK(s[0]->k == 4);
|
||||
BOOST_CHECK(s[11]->k == 4);
|
||||
}
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE(testBranchFree)
|
||||
{
|
||||
//testing Free , which is the only untested
|
||||
//via delete/deletearray
|
||||
|
||||
//1. no extra memory delete
|
||||
ObjectPool<S> pool(32);//32 true/false values = 32 slots
|
||||
S* addresses[12];
|
||||
addresses[0] = pool.New(7, 0.035f);
|
||||
pool.Delete(addresses[0]);
|
||||
BOOST_CHECK(pool.empty());
|
||||
|
||||
//1b. no extra memory deleteArray
|
||||
ObjectPool<S> pool1b(32);//32 true/false values = 32 slots
|
||||
S* test1b;
|
||||
test1b = pool1b.NewArray(5);// <-> test2 = new S[5];
|
||||
BOOST_CHECK(pool1b.size() == 5);
|
||||
pool1b.DeleteArray(test1b, 5);//callar destructorn på test2 också
|
||||
BOOST_CHECK(pool1b.empty());
|
||||
|
||||
//2. extra memory delete
|
||||
ObjectPool<S> pool2(2);
|
||||
S* addresses2[12];
|
||||
addresses2[0] = pool2.New(7, 0.035f);
|
||||
addresses2[1] = pool2.New(7, 0.035f);
|
||||
addresses2[2] = pool2.New(7, 0.035f);
|
||||
addresses2[3] = pool2.New(7, 0.035f);
|
||||
addresses2[4] = pool2.New(7, 0.035f);
|
||||
BOOST_CHECK(pool2.size() == 5);
|
||||
pool2.Delete(addresses2[0]);
|
||||
BOOST_CHECK(pool2.size() == 4);
|
||||
pool2.Delete(addresses2[1]);
|
||||
BOOST_CHECK(pool2.size() == 3);
|
||||
pool2.Delete(addresses2[2]);
|
||||
BOOST_CHECK(pool2.size() == 2);
|
||||
pool2.Delete(addresses2[3]);
|
||||
BOOST_CHECK(pool2.size() == 1);
|
||||
pool2.Delete(addresses2[4]);
|
||||
BOOST_CHECK(pool2.empty());
|
||||
//reverse delete
|
||||
addresses2[0] = pool2.New(7, 0.035f);
|
||||
addresses2[1] = pool2.New(7, 0.035f);
|
||||
addresses2[2] = pool2.New(7, 0.035f);
|
||||
addresses2[3] = pool2.New(7, 0.035f);
|
||||
addresses2[4] = pool2.New(7, 0.035f);
|
||||
BOOST_CHECK(pool2.size() == 5);
|
||||
pool2.Delete(addresses2[4]);
|
||||
BOOST_CHECK(pool2.size() == 4);
|
||||
pool2.Delete(addresses2[3]);
|
||||
BOOST_CHECK(pool2.size() == 3);
|
||||
pool2.Delete(addresses2[2]);
|
||||
BOOST_CHECK(pool2.size() == 2);
|
||||
pool2.Delete(addresses2[1]);
|
||||
BOOST_CHECK(pool2.size() == 1);
|
||||
pool2.Delete(addresses2[0]);
|
||||
BOOST_CHECK(pool2.empty());
|
||||
|
||||
//2b. extra memory deleteArray
|
||||
ObjectPool<S> pool2b(32);//32 true/false values = 32 slots
|
||||
S* test2b,*test2bb;
|
||||
test2b = pool2b.NewArray(5);// <-> test2 = new S[5];
|
||||
BOOST_CHECK(pool2b.size() == 5);
|
||||
test2bb = pool2b.NewArray(40);// <-> test2 = new S[5];
|
||||
BOOST_CHECK(pool2b.size() == 45);
|
||||
pool2b.DeleteArray(test2b, 5);//callar destructorn på test2 också
|
||||
BOOST_CHECK(pool2b.size() == 40);
|
||||
pool2b.DeleteArray(test2bb, 40);//callar destructorn på test2 också
|
||||
BOOST_CHECK(pool2b.empty());
|
||||
}
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(testBranchAllocate)
|
||||
{
|
||||
//1 slot else many slots
|
||||
//see testBranchFree
|
||||
|
||||
//out of mem vs not out of mem allocate
|
||||
//see testBranchFree
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(testEdgeCase)
|
||||
{
|
||||
//test with a very small pool
|
||||
ObjectPool<S> pool(1);
|
||||
BOOST_CHECK(pool.empty());
|
||||
S* test4;
|
||||
test4 = pool.New(7, 0.035f);
|
||||
BOOST_CHECK(!pool.empty());
|
||||
BOOST_CHECK(test4->k == 7);
|
||||
BOOST_CHECK_CLOSE_FRACTION(test4->f, 0.035f, 0.0001f);
|
||||
|
||||
//test with a very small pool and array, iterating
|
||||
ObjectPool<S> poolA(1);
|
||||
S* test5;
|
||||
test5 = poolA.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : poolA)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testBadlyAlignedData)
|
||||
{
|
||||
//small test with non-aligned data 4+1bytes
|
||||
struct S
|
||||
{
|
||||
S() = default;
|
||||
S(float f, char c) : m_f(f), m_c(c) { }
|
||||
~S() { }
|
||||
float m_f;
|
||||
char m_c;
|
||||
};
|
||||
MemoryPool<S> memPoolS;
|
||||
BOOST_CHECK(memPoolS.empty());
|
||||
BOOST_CHECK(memPoolS.size() == 0);
|
||||
ObjectPool<S> objPoolS(64);
|
||||
BOOST_CHECK(objPoolS.empty());
|
||||
BOOST_CHECK(objPoolS.size() == 0);
|
||||
S* test4;
|
||||
test4 = objPoolS.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : objPoolS) {
|
||||
o.m_c = 'v';
|
||||
o.m_f = 0.15534543f;
|
||||
}
|
||||
for (size_t i = 0; i < 1; ++i) {
|
||||
BOOST_CHECK(test4[i].m_c == 'v');
|
||||
BOOST_CHECK_CLOSE_FRACTION(test4[i].m_f, 0.15534543f, 0.0001f);
|
||||
}
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(testBadlyAlignedData2)
|
||||
{
|
||||
//small test with non-aligned data 1+1+1bytes
|
||||
struct S
|
||||
{
|
||||
S() = default;
|
||||
S(char c, char c2, char c3) : m_c(c), m_c2(c2), m_c3(c3) { }
|
||||
~S() { }
|
||||
char m_c;
|
||||
char m_c2;
|
||||
char m_c3;
|
||||
};
|
||||
MemoryPool<S> memPoolS;
|
||||
BOOST_CHECK(memPoolS.empty());
|
||||
BOOST_CHECK(memPoolS.size() == 0);
|
||||
ObjectPool<S> objPoolS(64);
|
||||
BOOST_CHECK(objPoolS.empty());
|
||||
BOOST_CHECK(objPoolS.size() == 0);
|
||||
S* test4;
|
||||
test4 = objPoolS.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : objPoolS) {
|
||||
o.m_c = 'v';
|
||||
o.m_c2 = 'w';
|
||||
o.m_c3 = 'x';
|
||||
}
|
||||
for (size_t i = 0; i < 1; ++i) {
|
||||
BOOST_CHECK(test4[i].m_c == 'v');
|
||||
BOOST_CHECK(test4[i].m_c2 == 'w');
|
||||
BOOST_CHECK(test4[i].m_c3 == 'x');
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testWrongData)
|
||||
{
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(testFillDeleteFillAgain) {
|
||||
//already done in BOOST_AUTO_TEST_CASE(releaseModeTest_RandomAllocateDeallocate)
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,160 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include <stdlib.h>//srand
|
||||
|
||||
#include "Engine/Core/OctTree.h"
|
||||
#include "Engine/Core/Ray.h"
|
||||
#include "OldOctTree.h"
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(octTreeTestsW)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octSameRegionTest)
|
||||
{
|
||||
glm::vec3 mini = glm::vec3(-1, -1, -1);
|
||||
glm::vec3 maxi = glm::vec3(1, 1, 1);
|
||||
OctTree tree(AABB(mini, maxi), 2);
|
||||
AABB firstQuadrant(mini, 0.8f*mini);
|
||||
tree.AddStaticObject(firstQuadrant);
|
||||
AABB testBox(0.9f*mini, 0.8f*mini);
|
||||
std::vector<AABB> region;
|
||||
tree.BoxesInSameRegion(testBox, region);
|
||||
BOOST_REQUIRE(region.size() == 1);
|
||||
AABB& box = region[0];
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.Center().x, firstQuadrant.Center().x, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.Center().y, firstQuadrant.Center().y, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.Center().z, firstQuadrant.Center().z, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().x, firstQuadrant.HalfSize().x, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().y, firstQuadrant.HalfSize().y, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().z, firstQuadrant.HalfSize().z, 0.00001f);
|
||||
}
|
||||
|
||||
const int LEVEL_BOUNDS = 500;
|
||||
const int MAXSIZE = 50;
|
||||
const int BOXES = 400;
|
||||
const int NUM_DYNAMICS = 0;
|
||||
const int NUM_STATICS = BOXES - NUM_DYNAMICS;
|
||||
const int SEED = 6548;
|
||||
const int TEST_FRAMES = 300;
|
||||
const int NUM_FUNCTION_LOOPS = 25;
|
||||
const int TESTS = 0; //10
|
||||
|
||||
template<typename Tree>
|
||||
void RegionTest(Tree& tree)
|
||||
{
|
||||
AABB aabb;
|
||||
aabb.CreateFromCenter(glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS),
|
||||
glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE));
|
||||
std::vector<AABB> outVec;
|
||||
tree.BoxesInSameRegion(aabb, outVec);
|
||||
}
|
||||
|
||||
template<typename Tree>
|
||||
void RayTest(Tree& tree)
|
||||
{
|
||||
Tree::Output data;
|
||||
glm::vec3 rayStart = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
glm::vec3 rayEnd = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
tree.RayCollides({ rayStart , glm::normalize(rayEnd - rayStart) }, data);
|
||||
}
|
||||
|
||||
template<typename Tree>
|
||||
void BoxTest(Tree& tree)
|
||||
{
|
||||
AABB outBox;
|
||||
AABB aabb;
|
||||
aabb.CreateFromCenter(glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS),
|
||||
glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE));
|
||||
tree.BoxCollides(aabb, outBox);
|
||||
}
|
||||
|
||||
template<typename Tree>
|
||||
void NopTest(Tree& tree)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template<typename Tree, typename TestFunction>
|
||||
void TestLoop(TestFunction xTest)
|
||||
{
|
||||
srand(SEED);
|
||||
glm::vec3 mini = glm::vec3(0, 0, 0);
|
||||
glm::vec3 maxi = glm::vec3(LEVEL_BOUNDS, LEVEL_BOUNDS, LEVEL_BOUNDS);
|
||||
Tree tree(AABB(mini, maxi), 3);
|
||||
AABB aabb;
|
||||
glm::vec3 center;
|
||||
glm::vec3 size;
|
||||
for (int t = 0; t < TESTS; ++t) {
|
||||
for (int i = 0; i < NUM_STATICS; ++i) {
|
||||
center = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
size = glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE);
|
||||
aabb.CreateFromCenter(center, size);
|
||||
tree.AddStaticObject(aabb);
|
||||
}
|
||||
for (int fr = 0; fr < TEST_FRAMES; ++fr) {
|
||||
for (int i = 0; i < NUM_DYNAMICS; ++i) {
|
||||
center = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
size = glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE);
|
||||
aabb.CreateFromCenter(center, size);
|
||||
tree.AddDynamicObject(aabb);
|
||||
}
|
||||
|
||||
for (int fl = 0; fl < NUM_FUNCTION_LOOPS; ++fl) {
|
||||
xTest(tree);
|
||||
}
|
||||
|
||||
tree.ClearDynamicObjects();
|
||||
}
|
||||
tree.ClearObjects();
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRegionPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(RegionTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRegionPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(RegionTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octBoxPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(BoxTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octBoxPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(BoxTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRayPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(RayTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRayPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(RayTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octNopPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(NopTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octNopPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(NopTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include <stdlib.h>//srand
|
||||
|
||||
//#define private public//HACK! Needed for white box testing
|
||||
//#include "Engine/Core/OctTree.h"
|
||||
//#include "OldOctTree.h"
|
||||
//friend class and refactoringIntoNewClass is some extra work and needs to be updated when the original class is updated, and can contain bugs that
|
||||
//isnt in the original class
|
||||
//Reflection-inspection seems to be only available for C#
|
||||
//http://stackoverflow.com/questions/6778496/how-to-do-unit-testing-on-private-members-and-methods-of-c-classes
|
||||
//http://stackoverflow.com/questions/3676664/unit-testing-of-private-methods
|
||||
|
||||
#include "OctTreeTestGameClass.h"
|
||||
|
||||
#define private public//HACK! Needed for white box testing
|
||||
#include <Engine\Core\OctTree.h>
|
||||
//else we would have to "open up" the octTree class more with get/sets, public methods, etc. which is not good encapsulation-wise
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(octTreeTestsA)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octTreeTest)
|
||||
{
|
||||
//white box testing
|
||||
//http://softwaretestingfundamentals.com/differences-between-black-box-testing-and-white-box-testing/
|
||||
//http://technologyconversations.com/2013/12/11/black-box-vs-white-box-testing/
|
||||
|
||||
//simple AABB constructor check
|
||||
auto minCorner = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
auto maxCorner = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
auto someAABB = AABB(minCorner, maxCorner);
|
||||
BOOST_CHECK(someAABB.MinCorner() == minCorner);
|
||||
BOOST_CHECK(someAABB.MaxCorner() == maxCorner);
|
||||
BOOST_CHECK(someAABB.Center() == 0.5f * (minCorner + maxCorner));
|
||||
|
||||
//simple destructor check in the end, just look for memleaks, then it didnt clear the AABB structure
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octTreeTest2)
|
||||
{
|
||||
//octtree draw etc
|
||||
Game game(0, nullptr);
|
||||
while (game.Running()) {
|
||||
game.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,193 @@
|
||||
#include "OctTreeTestGameClass.h"
|
||||
|
||||
Game::Game(int argc, char* argv[]) : someOctTree(AABB(-0.5f*worldSize, 0.5f*worldSize), 2)
|
||||
{
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<Model>("Model");
|
||||
ResourceManager::RegisterType<Texture>("Texture");
|
||||
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||
ResourceManager::RegisterType<ShaderProgram>("ShaderProgram");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
|
||||
// Create the core event broker
|
||||
m_EventBroker = new EventBroker();
|
||||
|
||||
m_RenderQueueFactory = new RenderQueueFactory();
|
||||
|
||||
// Create the renderer
|
||||
m_Renderer = new Renderer(m_EventBroker);
|
||||
m_Renderer->SetFullscreen(m_Config->Get<bool>("Video.Fullscreen", false));
|
||||
m_Renderer->SetVSYNC(m_Config->Get<bool>("Video.VSYNC", false));
|
||||
m_Renderer->SetResolution(Rectangle(
|
||||
0,
|
||||
0,
|
||||
m_Config->Get<int>("Video.Width", 1280),
|
||||
m_Config->Get<int>("Video.Height", 720)
|
||||
));
|
||||
m_Renderer->Initialize();
|
||||
m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get<float>("Video.FOV", 90.f)));
|
||||
|
||||
// Create input manager
|
||||
m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker);
|
||||
m_InputProxy = new InputProxy(m_EventBroker);
|
||||
m_InputProxy->AddHandler<KeyboardInputHandler>();
|
||||
m_InputProxy->AddHandler<MouseInputHandler>();
|
||||
m_InputProxy->LoadBindings("Input.ini");
|
||||
|
||||
// Create the root level GUI frame
|
||||
m_FrameStack = new GUI::Frame(m_EventBroker);
|
||||
m_FrameStack->Width = m_Renderer->Resolution().Width;
|
||||
m_FrameStack->Height = m_Renderer->Resolution().Height;
|
||||
|
||||
// Create a TEST WORLD
|
||||
m_World = new HardcodedTestWorld();
|
||||
|
||||
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>(0);
|
||||
|
||||
m_LastTime = glfwGetTime();
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
delete m_FrameStack;
|
||||
delete m_EventBroker;
|
||||
}
|
||||
|
||||
void Game::Tick()
|
||||
{
|
||||
double currentTime = glfwGetTime();
|
||||
double dt = currentTime - m_LastTime;
|
||||
m_LastTime = currentTime;
|
||||
|
||||
// Handle input in a weird looking but responsive way
|
||||
m_EventBroker->Process<InputManager>();
|
||||
m_EventBroker->Swap();
|
||||
m_InputManager->Update(dt);
|
||||
m_EventBroker->Swap();
|
||||
m_InputProxy->Update(dt);
|
||||
m_EventBroker->Swap();
|
||||
m_InputProxy->Process();
|
||||
m_EventBroker->Swap();
|
||||
|
||||
#define TEST1
|
||||
//this draws the octTree and you can set the cube inside it and see what boxes in the tree that it belongs to
|
||||
#ifdef TEST1
|
||||
if (!m_UpdatedOnce) {
|
||||
m_UpdatedOnce = true;
|
||||
m_World->createTestEntitiesTest1();
|
||||
}
|
||||
|
||||
//add/move the trigger box
|
||||
auto pos = m_Renderer->Camera()->Forward() + m_Renderer->Camera()->Position();
|
||||
AABB boxi;
|
||||
boxi.CreateFromCenter(pos, maxPos - minPos);
|
||||
frameCounter++;
|
||||
if (frameCounter > 1) {
|
||||
m_World->someOctTree.ClearDynamicObjects();
|
||||
m_World->someOctTree.AddDynamicObject(boxi);
|
||||
frameCounter = 0;
|
||||
}
|
||||
ComponentWrapper transform = m_World->GetComponent(m_World->anotherBoxTransformId, "Transform");
|
||||
transform["Position"] = boxi.Center();
|
||||
|
||||
//check all children again in the tree if they have a box in them or not, and colormark them if they do
|
||||
//contentboxarna får man ut - inte childboxarna!
|
||||
std::vector<int> boxIndex;
|
||||
boxIndex = m_World->someOctTree.m_Root->childIndicesContainingBox(boxi);
|
||||
|
||||
for (auto& oneLinkedObject : m_World->linkOM)
|
||||
{
|
||||
ComponentWrapper model = m_World->GetComponent(oneLinkedObject.entId, "Model");
|
||||
model["Color"] = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
if (oneLinkedObject.child->m_DynamicObjIndices.size() != 0) {
|
||||
model["Color"] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
//next check if the childIndicesContainingBox method returns the correct boxes
|
||||
//REQUIRED: childIndicesContainingBox must be public to test this!
|
||||
for each (auto someBoxIndex in boxIndex)
|
||||
{
|
||||
glm::vec3 pos = m_World->someOctTree.m_Root->m_Children[someBoxIndex]->m_Box.Center();
|
||||
if (abs(pos.x - oneLinkedObject.posxyz.x) < 0.005f &&
|
||||
abs(pos.y - oneLinkedObject.posxyz.y) < 0.005f &&
|
||||
abs(pos.z - oneLinkedObject.posxyz.z) < 0.005f) {
|
||||
model["Color"] = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
|
||||
//wireframe
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
#endif
|
||||
//this tests AABB vs AABB collision and AABB vs OctTree with AABB in it
|
||||
#ifdef TEST2
|
||||
|
||||
//only add 1 for now...
|
||||
//grey box
|
||||
|
||||
const glm::vec4 redCol = glm::vec4(1, 0.2f, 0, 1);
|
||||
const glm::vec4 greenCol = glm::vec4(0.1f, 1.0f, 0.25f, 1);
|
||||
const glm::vec3 boxSize = 0.1f*glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
AABB aabb;
|
||||
aabb.CreateFromCenter(glm::vec3(0, 2.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
|
||||
if (m_UpdatedOnce) {
|
||||
//auto test = someOctTree.childIndicesContainingBox(aabb);
|
||||
std::vector<AABB> test2;
|
||||
someOctTree.BoxesInSameRegion(aabb, test2);
|
||||
}
|
||||
if (!m_UpdatedOnce) {
|
||||
m_UpdatedOnce = true;
|
||||
someOctTree.AddStaticObject(aabb);
|
||||
//create the "small red box"
|
||||
m_BoxID = m_World->CreateEntity();
|
||||
ComponentWrapper transform = m_World->AttachComponent(m_BoxID, "Transform");
|
||||
transform["Scale"] = boxSize;
|
||||
ComponentWrapper model = m_World->AttachComponent(m_BoxID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitBox.obj";
|
||||
m_World->createTestEntitiesTest2();
|
||||
}
|
||||
|
||||
//red box
|
||||
AABB redBox;
|
||||
auto boxPos = m_Renderer->Camera()->Position() + 1.2f*m_Renderer->Camera()->Forward();
|
||||
redBox.CreateFromCenter(boxPos, boxSize);
|
||||
ComponentWrapper transform = m_World->GetComponent(m_BoxID, "Transform");
|
||||
transform["Position"] = boxPos;
|
||||
ComponentWrapper model = m_World->GetComponent(m_BoxID, "Model");
|
||||
//this checks AABB vs an AABB in the octTree
|
||||
if (someOctTree.BoxCollides(redBox, AABB())) {
|
||||
//this checks AABB vs AABB
|
||||
//if (Collision::AABBVsAABB(redBox, aabb)) {
|
||||
//m_Renderer->Camera()->SetPosition(m_PrevPos);
|
||||
//m_Renderer->Camera()->SetOrientation(m_PrevOri);
|
||||
model["Color"] = greenCol;
|
||||
}
|
||||
else {
|
||||
model["Color"] = redCol;
|
||||
}
|
||||
|
||||
m_PrevPos = m_Renderer->Camera()->Position();
|
||||
m_PrevOri = m_Renderer->Camera()->Orientation();
|
||||
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
#endif
|
||||
|
||||
// Iterate through systems and update world!
|
||||
m_SystemPipeline->Update(m_World, dt);
|
||||
m_Renderer->Update(dt);
|
||||
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
GLERROR("Game::Tick m_RenderQueueFactory->Update");
|
||||
m_Renderer->Draw(m_RenderQueueFactory->RenderQueues());
|
||||
GLERROR("Game::Tick m_Renderer->Draw");
|
||||
m_EventBroker->Swap();
|
||||
m_EventBroker->Clear();
|
||||
|
||||
glfwPollEvents();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef Game_h__
|
||||
#define Game_h__
|
||||
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Core/ConfigFile.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Core/InputManager.h"
|
||||
#include "GUI/Frame.h"
|
||||
#include "Core/World.h"
|
||||
#include "Rendering/RenderQueueFactory.h"
|
||||
#include "Input/InputProxy.h"
|
||||
#include "Input/KeyboardInputHandler.h"
|
||||
#include "Input/MouseInputHandler.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
#include "PlayerSystem.h"
|
||||
#include "Editor/EditorSystem.h"
|
||||
|
||||
#include "OctTreeTestHardCodedTestWorld.h"
|
||||
#include "Collision/Collision.h"
|
||||
|
||||
class Game
|
||||
{
|
||||
public:
|
||||
Game(int argc, char* argv[]);
|
||||
~Game();
|
||||
|
||||
bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); }
|
||||
void Tick();
|
||||
|
||||
private:
|
||||
double m_LastTime;
|
||||
ConfigFile* m_Config = nullptr;
|
||||
EventBroker* m_EventBroker;
|
||||
IRenderer* m_Renderer;
|
||||
InputManager* m_InputManager;
|
||||
GUI::Frame* m_FrameStack;
|
||||
HardcodedTestWorld* m_World;
|
||||
RenderQueueFactory* m_RenderQueueFactory;
|
||||
InputProxy* m_InputProxy;
|
||||
SystemPipeline* m_SystemPipeline;
|
||||
|
||||
//Test1
|
||||
int frameCounter = 0;
|
||||
glm::vec3 minPos = glm::vec3(0.1f, 0.1f, 0.1f);
|
||||
glm::vec3 maxPos = glm::vec3(0.2f, 0.2f, 0.2f);
|
||||
|
||||
//Test2
|
||||
bool m_UpdatedOnce = false;
|
||||
unsigned int m_BoxID;
|
||||
glm::vec3 m_PrevPos;
|
||||
glm::quat m_PrevOri;
|
||||
|
||||
glm::vec3 worldSize = glm::vec3(50, 50, 50);
|
||||
OctTree someOctTree;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
//#define BOOST_TEST_MODULE collTest
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/execution_monitor.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include "Engine/Collision/Collision.h"
|
||||
#include "Engine/Core/AABB.h"
|
||||
#include "Engine/Core/Ray.h"
|
||||
#include <stdlib.h>//srand
|
||||
#include "Engine/Core/OctTree.h"
|
||||
|
||||
//vs memleaks
|
||||
//#define _CRTDBG_MAP_ALLOC
|
||||
//#include <stdlib.h>
|
||||
//#include <crtdbg.h>
|
||||
//#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
|
||||
//#define new DEBUG_CLIENTBLOCK
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(cTest)
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
#include <list>
|
||||
#include <tuple>
|
||||
#include <boost/any.hpp>
|
||||
#include "GLM.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/Util/Any.h"
|
||||
|
||||
#include <vector>
|
||||
//last!
|
||||
//#include "OldOctTree.h"
|
||||
#define private public
|
||||
#include <Engine\Core\OctTree.h>
|
||||
|
||||
class HardcodedTestWorld : public World
|
||||
{
|
||||
public:
|
||||
struct LinkOctTreeAndModel {
|
||||
EntityID entId;
|
||||
OctTree::OctChild* child;
|
||||
glm::vec3 posxyz;
|
||||
LinkOctTreeAndModel(EntityID eId, OctTree::OctChild* ch, glm::vec3 pos)
|
||||
{
|
||||
entId = eId;
|
||||
child = ch;
|
||||
posxyz = pos;
|
||||
}
|
||||
};
|
||||
EntityID anotherBoxTransformId;
|
||||
std::vector<LinkOctTreeAndModel> linkOM;
|
||||
OctTree someOctTree;
|
||||
|
||||
//constructor
|
||||
HardcodedTestWorld()
|
||||
: World()
|
||||
, someOctTree(AABB(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f)), 2)
|
||||
{
|
||||
registerTestComponents();
|
||||
//createTestEntities();
|
||||
}
|
||||
|
||||
private:
|
||||
void registerTestComponents()
|
||||
{
|
||||
ComponentWrapperFactory f;
|
||||
|
||||
|
||||
f = ComponentWrapperFactory("Test");
|
||||
f.AddProperty("TestInteger", 1337);
|
||||
f.AddProperty("TestFloat", 13.37f);
|
||||
f.AddProperty("TestString", std::string("Carlito"));
|
||||
RegisterComponent(f);
|
||||
|
||||
f = ComponentWrapperFactory("Debug");
|
||||
f.AddProperty("Name", std::string("Unnamed"));
|
||||
RegisterComponent(f);
|
||||
|
||||
f = ComponentWrapperFactory("Transform");
|
||||
f.AddProperty("Position", glm::vec3(0.f, 0.f, 0.f));
|
||||
f.AddProperty("Orientation", glm::quat());
|
||||
f.AddProperty("Scale", glm::vec3(1.f, 1.f, 1.f));
|
||||
RegisterComponent(f);
|
||||
|
||||
f = ComponentWrapperFactory("Model");
|
||||
f.AddProperty("Resource", std::string());
|
||||
f.AddProperty("Color", glm::vec4(1.f, 1.f, 1.f, 1.f));
|
||||
f.AddProperty("Visible", true);
|
||||
RegisterComponent(f);
|
||||
}
|
||||
|
||||
void createTestEntitiesTest1()
|
||||
{
|
||||
World& world = *this;
|
||||
EntityID tempId;
|
||||
//add octTree
|
||||
{
|
||||
//copy of mainbox
|
||||
auto someAABB = AABB(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
|
||||
//draw main box first
|
||||
AddBoxModel(someAABB.Center(), someAABB.HalfSize().x, someOctTree.m_Root, tempId);
|
||||
|
||||
//add anotherbox in octTree
|
||||
auto anotherBox = AABB(glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.2f, 0.2f, 0.2f));
|
||||
//note: have to delete the box in the tree first, since were trying to move the box
|
||||
someOctTree.AddDynamicObject(anotherBox);
|
||||
|
||||
//draw anotherbox and save it in anotherBoxTransformId
|
||||
AddBoxModel(anotherBox.Center(), anotherBox.HalfSize().x, someOctTree.m_Root, anotherBoxTransformId);
|
||||
|
||||
//draw the octTree
|
||||
for (size_t j = 0; j < 8; j++)
|
||||
{
|
||||
AddBoxModel(someOctTree.m_Root->m_Children[j]->m_Box.Center(),
|
||||
someOctTree.m_Root->m_Children[j]->m_Box.HalfSize().x, someOctTree.m_Root->m_Children[j], tempId);
|
||||
|
||||
auto someChild = someOctTree.m_Root->m_Children[j];
|
||||
|
||||
for (size_t i = 0; i < 8; i++)
|
||||
{
|
||||
AddBoxModel(someChild->m_Children[i]->m_Box.Center(),
|
||||
someChild->m_Children[i]->m_Box.HalfSize().x, someChild->m_Children[i], tempId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}//end CreateEnt
|
||||
|
||||
void createTestEntitiesTest2()
|
||||
{
|
||||
World& world = *this;
|
||||
|
||||
EntityID entityCollisionBox = world.CreateEntity();
|
||||
ComponentWrapper transform = world.AttachComponent(entityCollisionBox, "Transform");
|
||||
transform["Position"] = glm::vec3(0.f, 2.f, 0.f);
|
||||
ComponentWrapper model = world.AttachComponent(entityCollisionBox, "Model");
|
||||
model["Resource"] = "Models/Core/UnitBox.obj";
|
||||
}
|
||||
|
||||
void AddBoxModel(const glm::vec3 ¢er, const float &halfSize, OctTree::OctChild* child, EntityID &outEntityId) {
|
||||
World& world = *this;
|
||||
|
||||
EntityID entityDummyScene = world.CreateEntity();
|
||||
outEntityId = entityDummyScene;
|
||||
ComponentWrapper transform = world.AttachComponent(entityDummyScene, "Transform");
|
||||
transform["Position"] = center;
|
||||
transform["Scale"] = glm::vec3(1.0f, 1.0f, 1.0f)*halfSize*2.0f*0.97f;
|
||||
ComponentWrapper model = world.AttachComponent(entityDummyScene, "Model");
|
||||
model["Resource"] = "Models/Core/UnitBox.obj";
|
||||
model["Color"] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
if (child->m_DynamicObjIndices.size() != 0)
|
||||
model["Color"] = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
|
||||
linkOM.emplace_back(entityDummyScene, child, center);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,328 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <bitset>
|
||||
|
||||
#include "OldOctTree.h"
|
||||
#include "Collision/Collision.h"
|
||||
#include "Core/World.h"
|
||||
#include "Rendering/Camera.h"
|
||||
|
||||
namespace Old
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
//To be able to sort nodes based on distance to ray origin.
|
||||
struct ChildInfo
|
||||
{
|
||||
int Index;
|
||||
float Distance;
|
||||
};
|
||||
|
||||
bool isFirstLower(const ChildInfo& first, const ChildInfo& second)
|
||||
{
|
||||
return first.Distance < second.Distance;
|
||||
}
|
||||
|
||||
bool isSameBoxProbably(const AABB& first, const AABB& second)
|
||||
{
|
||||
const float EPS = 0.0001f;
|
||||
const auto& ma = first.MaxCorner();
|
||||
const auto& mi = first.MinCorner();
|
||||
return (std::abs(ma.x - mi.x) < EPS) &&
|
||||
(std::abs(ma.z - mi.z) < EPS) &&
|
||||
(std::abs(ma.y - mi.y) < EPS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
OctTree::OctTree()
|
||||
: OctTree(AABB(), 0)
|
||||
{}
|
||||
|
||||
OctTree::OctTree(const AABB& octTreeBounds, int subDivisions)
|
||||
: m_Box(octTreeBounds)
|
||||
, m_UpdatedOnce(false)
|
||||
{
|
||||
if (subDivisions == 0) {
|
||||
for (OctTree*& c : m_Children) {
|
||||
c = nullptr;
|
||||
}
|
||||
} else {
|
||||
--subDivisions;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
glm::vec3 minPos, maxPos;
|
||||
const glm::vec3& parentMin = m_Box.MinCorner();
|
||||
const glm::vec3& parentMax = m_Box.MaxCorner();
|
||||
const glm::vec3& parentCenter = m_Box.Center();
|
||||
std::bitset<3> bits(i);
|
||||
//If child is 4,5,6,7.
|
||||
if (bits.test(2)) {
|
||||
minPos.x = parentCenter.x;
|
||||
maxPos.x = parentMax.x;
|
||||
} else {
|
||||
minPos.x = parentMin.x;
|
||||
maxPos.x = parentCenter.x;
|
||||
}
|
||||
|
||||
//If child is 2,3,6,7
|
||||
if (bits.test(1)) {
|
||||
minPos.y = parentCenter.y;
|
||||
maxPos.y = parentMax.y;
|
||||
} else {
|
||||
minPos.y = parentMin.y;
|
||||
maxPos.y = parentCenter.y;
|
||||
}
|
||||
//If child is 1,3,5,7
|
||||
if (bits.test(0)) {
|
||||
minPos.z = parentCenter.z;
|
||||
maxPos.z = parentMax.z;
|
||||
} else {
|
||||
minPos.z = parentMin.z;
|
||||
maxPos.z = parentCenter.z;
|
||||
}
|
||||
m_Children[i] = new OctTree(AABB(minPos, maxPos), subDivisions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OctTree::~OctTree()
|
||||
{
|
||||
for (OctTree*& c : m_Children) {
|
||||
if (c != nullptr) {
|
||||
delete c;
|
||||
c = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::Update(float dt, World* world, Camera* cam)
|
||||
{
|
||||
AABB aabb;
|
||||
for (ComponentWrapper& c : *world->GetComponents("Collision")) {
|
||||
aabb.CreateFromCenter(c["BoxCenter"], c["BoxSize"]);
|
||||
AddStaticObject(aabb);
|
||||
}
|
||||
const glm::vec4 redCol = glm::vec4(1, 0.2f, 0, 1);
|
||||
const glm::vec4 greenCol = glm::vec4(0.1f, 1.0f, 0.25f, 1);
|
||||
const glm::vec3 boxSize = 0.1f*glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
|
||||
if (!m_UpdatedOnce) {
|
||||
m_BoxID = world->CreateEntity();
|
||||
ComponentWrapper transform = world->AttachComponent(m_BoxID, "Transform");
|
||||
transform["Scale"] = boxSize;
|
||||
ComponentWrapper model = world->AttachComponent(m_BoxID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitBox.obj";
|
||||
m_UpdatedOnce = true;
|
||||
}
|
||||
|
||||
AABB box;
|
||||
auto boxPos = cam->Position() + 1.2f*cam->Forward();
|
||||
box.CreateFromCenter(boxPos, boxSize);
|
||||
ComponentWrapper transform = world->GetComponent(m_BoxID, "Transform");
|
||||
transform["Position"] = boxPos;
|
||||
ComponentWrapper model = world->GetComponent(m_BoxID, "Model");
|
||||
//if (BoxCollides(box, AABB())) {
|
||||
if (Collision::AABBVsAABB(box, aabb)) {
|
||||
cam->SetPosition(m_PrevPos);
|
||||
cam->SetOrientation(m_PrevOri);
|
||||
model["Color"] = greenCol;
|
||||
} else {
|
||||
model["Color"] = redCol;
|
||||
}
|
||||
|
||||
m_PrevPos = cam->Position();
|
||||
m_PrevOri = cam->Orientation();
|
||||
ClearObjects();
|
||||
}
|
||||
|
||||
bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (int i : childIndicesContainingBox(boxToTest)) {
|
||||
if (m_Children[i]->BoxCollides(boxToTest, outBoxIntersected))
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
for (const auto& obj : m_StaticObjects) {
|
||||
if (Collision::AABBVsAABB(boxToTest, obj)) {
|
||||
outBoxIntersected = obj;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (const auto& obj : m_DynamicObjects) {
|
||||
//If there is a collision and it is not testing against itself.
|
||||
if (!isSameBoxProbably(boxToTest, obj) &&
|
||||
Collision::AABBVsAABB(boxToTest, obj)) {
|
||||
outBoxIntersected = obj;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool OctTree::RayCollides(const Ray& ray, Output& data) const
|
||||
{
|
||||
//If the node AABB is missed, everything it contains is missed.
|
||||
if (Collision::RayAABBIntr(ray, m_Box)) {
|
||||
//If the ray shoots the tree, and it is a parent to 8 children :o
|
||||
if (hasChildren()) {
|
||||
//Sort children according to their distance from the ray origin.
|
||||
std::vector<ChildInfo> childInfos;
|
||||
childInfos.reserve(8);
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
childInfos.push_back({ i, glm::distance(ray.Origin(), m_Children[i]->m_Box.Center()) });
|
||||
}
|
||||
std::sort(childInfos.begin(), childInfos.end(), isFirstLower);
|
||||
//Loop through the children, starting with the one closest to the ray origin. I.e the first to be hit.
|
||||
for (const ChildInfo& info : childInfos) {
|
||||
if (m_Children[info.Index]->RayCollides(ray, data)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//Check against boxes in the node.
|
||||
float minDist = INFINITY;
|
||||
bool intersected = false;
|
||||
for (const auto& obj : m_StaticObjects) {
|
||||
float dist;
|
||||
if (Collision::RayVsAABB(ray, obj, dist)) {
|
||||
minDist = std::min(dist, minDist);
|
||||
intersected = true;
|
||||
}
|
||||
}
|
||||
for (const auto& obj : m_DynamicObjects) {
|
||||
float dist;
|
||||
if (Collision::RayVsAABB(ray, obj, dist)) {
|
||||
minDist = std::min(dist, minDist);
|
||||
intersected = true;
|
||||
}
|
||||
}
|
||||
|
||||
data.CollideDistance = minDist;
|
||||
return intersected;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void OctTree::AddDynamicObject(const AABB& box)
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (auto i : childIndicesContainingBox(box)) {
|
||||
m_Children[i]->AddDynamicObject(box);
|
||||
}
|
||||
} else {
|
||||
m_DynamicObjects.push_back(box);
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::AddStaticObject(const AABB& box)
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (auto i : childIndicesContainingBox(box)) {
|
||||
m_Children[i]->AddStaticObject(box);
|
||||
}
|
||||
} else {
|
||||
m_StaticObjects.push_back(box);
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes) const
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (auto i : childIndicesContainingBox(box)) {
|
||||
m_Children[i]->BoxesInSameRegion(box, outBoxes);
|
||||
}
|
||||
} else {
|
||||
outBoxes.insert(outBoxes.end(), m_StaticObjects.begin(), m_StaticObjects.end());
|
||||
outBoxes.insert(outBoxes.end(), m_DynamicObjects.begin(), m_DynamicObjects.end());
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::ClearObjects()
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (OctTree*& c : m_Children) {
|
||||
c->ClearObjects();
|
||||
}
|
||||
} else {
|
||||
m_DynamicObjects.clear();
|
||||
m_StaticObjects.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::ClearDynamicObjects()
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (OctTree*& c : m_Children) {
|
||||
c->ClearObjects();
|
||||
}
|
||||
} else {
|
||||
m_DynamicObjects.clear();
|
||||
}
|
||||
}
|
||||
|
||||
//: 3 7
|
||||
//:
|
||||
//: 2 6
|
||||
//: |
|
||||
//: 1 5 \ y
|
||||
//: z
|
||||
//: 0 4 0 x-->
|
||||
//
|
||||
// child: 0 1 2 3 4 5 6 7
|
||||
// x : - - - - + + + +
|
||||
// y : - - + + - - + +
|
||||
// z : - + - + - + - +
|
||||
int OctTree::childIndexContainingPoint(const glm::vec3& point) const
|
||||
{
|
||||
const glm::vec3& c = m_Box.Center();
|
||||
return (1 << 2) * (point.x >= c.x) | (1 << 1) * (point.y >= c.y) | (point.z >= c.z);
|
||||
}
|
||||
|
||||
std::vector<int> OctTree::childIndicesContainingBox(const AABB& box) const
|
||||
{
|
||||
int minInd = childIndexContainingPoint(box.MinCorner());
|
||||
int maxInd = childIndexContainingPoint(box.MaxCorner());
|
||||
//Because of the predictable ordering of the child indices,
|
||||
//the number of bits set when xor:ing the indices will determine the number of children containing the box.
|
||||
std::bitset<3> bits(minInd ^ maxInd);
|
||||
switch (bits.count()) {
|
||||
//Box contained completely in one child.
|
||||
case 0:
|
||||
return{ minInd };
|
||||
//Two children.
|
||||
case 1:
|
||||
return{ minInd, maxInd };
|
||||
//Four children.
|
||||
case 2:
|
||||
{
|
||||
std::vector<int> ret;
|
||||
//Bit-hax to calculate the correct 4 children containing the box.
|
||||
//This works because of the childrens index determine what part of
|
||||
//the dimensions they are responsible for (which octant).
|
||||
bits.flip();
|
||||
//At this point the bits necessarily have exactly one bit set.
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
//If the child index have the same bit set as the bits, add box to it.
|
||||
if (bits.to_ulong() & c) {
|
||||
ret.push_back(c);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
case 3: //Eight children.
|
||||
return{ 0,1,2,3,4,5,6,7 };
|
||||
default:
|
||||
return std::vector<int>();
|
||||
}
|
||||
}
|
||||
|
||||
inline bool OctTree::hasChildren() const
|
||||
{
|
||||
return m_Children[0] != nullptr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#ifndef OldOctTree_h__
|
||||
#define OldOctTree_h__
|
||||
|
||||
#include "Core/AABB.h"
|
||||
|
||||
class Ray;
|
||||
class World;
|
||||
class Camera;
|
||||
|
||||
namespace Old
|
||||
{
|
||||
|
||||
class OctTree
|
||||
{
|
||||
public:
|
||||
struct Output
|
||||
{
|
||||
float CollideDistance;
|
||||
};
|
||||
OctTree();
|
||||
~OctTree();
|
||||
//For the root OctTree, [octTreeBounds] should be a box containing the entire level.
|
||||
OctTree(const AABB& octTreeBounds, int subDivisions);
|
||||
|
||||
//We should only ever need one OctTree in the game, and it should not need to be copied.
|
||||
//Define these if the OctTree suddenly needs to be copied, think of the children OctTree* ptrs.
|
||||
OctTree(const OctTree& other) = delete;
|
||||
OctTree(const OctTree&& other) = delete;
|
||||
OctTree& operator= (const OctTree& other) = delete;
|
||||
|
||||
void AddDynamicObject(const AABB& box);
|
||||
void AddStaticObject(const AABB& box);
|
||||
|
||||
void BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes) const;
|
||||
|
||||
void ClearObjects();
|
||||
void ClearDynamicObjects();
|
||||
|
||||
//Collision test function.
|
||||
void Update(float dt, World* world, Camera* cam);
|
||||
//Returns true if the ray collides with something in the tree. Result is written to [data].
|
||||
bool RayCollides(const Ray& ray, Output& data) const;
|
||||
//Returns true if the box collides with something in the tree.
|
||||
//On collision with a box, that box is written to [outBoxIntersected].
|
||||
//Note: More efficient than calling BoxesInSameRegion from outside and testing there.
|
||||
bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const;
|
||||
|
||||
private:
|
||||
OctTree* m_Children[8];
|
||||
std::vector<AABB> m_StaticObjects;
|
||||
std::vector<AABB> m_DynamicObjects;
|
||||
AABB m_Box;
|
||||
|
||||
bool m_UpdatedOnce;
|
||||
unsigned int m_BoxID;
|
||||
glm::vec3 m_PrevPos;
|
||||
glm::quat m_PrevOri;
|
||||
|
||||
inline bool hasChildren() const;
|
||||
int childIndexContainingPoint(const glm::vec3& point) const;
|
||||
std::vector<int> childIndicesContainingBox(const AABB& box) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "Core/World.h"
|
||||
|
||||
//private->public hack doesnt work, tons of link errors
|
||||
//so there is currently no good way to test this class
|
||||
//#define private public
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Core/ConfigFile.h"
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Engine\Rendering\Texture.h"
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(resourceManagerTests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(resourceManagerTest)
|
||||
{
|
||||
World m_World;
|
||||
|
||||
//private static metoder/variabler
|
||||
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
BOOST_CHECK(!ResourceManager::IsResourceLoaded("ConfigFile", "Config.ini"));
|
||||
auto m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
BOOST_CHECK(ResourceManager::IsResourceLoaded("ConfigFile", "Config.ini"));
|
||||
ResourceManager::Release("ConfigFile", "Config.ini");
|
||||
BOOST_CHECK(!ResourceManager::IsResourceLoaded("ConfigFile", "Config.ini"));
|
||||
|
||||
//configfile without register
|
||||
//check so output says "EE failed to load: type not registered..."
|
||||
auto m_ScreenQuadNoRegister = ResourceManager::Load<Model>("Models/Core/ScreenQuad.obj");
|
||||
BOOST_CHECK(!ResourceManager::IsResourceLoaded("Model", "Models/Core/ScreenQuad.obj"));
|
||||
|
||||
//there is no error feedback to check if you try to release the wrong resources - hence that cant be tested either
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(WorldTestMultipleAllocations, * utf::tolerance(0.00001))
|
||||
|
||||
// Loop through them and check data
|
||||
int i = 0;
|
||||
for (auto& c : w.GetComponents("Test")) {
|
||||
for (auto& c : *w.GetComponents("Test")) {
|
||||
BOOST_TEST((int)c["TestInteger"] == i);
|
||||
i++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user