Merge branch 'OctTree' of github.com:teamfisk/TacticalZ into OctTree

This commit is contained in:
William Moberg
2015-12-14 17:08:51 +01:00
9 changed files with 539 additions and 16 deletions
+10 -3
View File
@@ -86,10 +86,14 @@ bool RayVsModel(const Ray& ray,
glm::vec3 m = ray.Origin - v0;
glm::vec3 MxE1 = glm::cross(m, e1);
glm::vec3 DxE2 = glm::cross(ray.Direction, e2);
float DetInv = 1.0f / glm::dot(e1, DxE2);
float DetInv = glm::dot(e1, DxE2);
if (std::abs(DetInv) < FLT_EPSILON) {
continue;
}
DetInv = 1.0f / DetInv;
float u = glm::dot(m, DxE2) * DetInv;
float v = glm::dot(ray.Direction, MxE1) * DetInv;
if (u < 0 && v < 0 && 1 < u + v) {
if (u < 0 || v < 0 || 1 < u + v) {
continue;
}
//Here, u and v are positive, u+v <= 1, and if distance is positive - triangle is hit.
@@ -116,7 +120,10 @@ bool RayVsModel(const Ray& ray,
glm::vec3 m = ray.Origin - v0;
glm::vec3 MxE1 = glm::cross(m, e1);
glm::vec3 DxE2 = glm::cross(ray.Direction, e2);
float DetInv = 1.0f / glm::dot(e1, DxE2);
float DetInv = glm::dot(e1, DxE2);
if (std::abs(DetInv) < FLT_EPSILON) {
continue;
}
float dist = glm::dot(e2, MxE1) * DetInv;
if (dist >= outDistance) {
continue;
+1
View File
@@ -12,6 +12,7 @@ include_directories(
)
file(GLOB SOURCE_FILES
"*.h"
"*.cpp"
)
+94 -1
View File
@@ -3,11 +3,18 @@
#include <boost/test/execution_monitor.hpp>
using boost::unit_test_framework::test_suite;
using boost::unit_test_framework::test_case;
#include <Engine\Core\Collision.h>
#include "Engine\Core\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>
//ray vs model
#include "Engine\Core\ResourceManager.h"
#include "Engine\Rendering\Model.h"
#include "Engine\Core\Ray.h"
//vs memleaks
//#define _CRTDBG_MAP_ALLOC
@@ -87,6 +94,92 @@ BOOST_AUTO_TEST_CASE(collisionTest2)
BOOST_CHECK(test >= 0);
}
BOOST_AUTO_TEST_CASE(rayVsModelTest)
{
//simple test
Ray ray;
ray.Origin = glm::vec3(-50, 0, 0);
//ray.Direction = glm::vec3(-1, 0, 0);
ray.Direction = glm::normalize(glm::vec3(1, 0, 0));
//inte model, det kräver renderar grejs tydligen
ResourceManager::RegisterType<RawModel>("RawModel");
auto unitBox = ResourceManager::Load<RawModel>("Models/Core/UnitBox.obj");
BOOST_CHECK(unitBox != nullptr);
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
BOOST_CHECK(hit);
ray.Direction = glm::normalize(glm::vec3(-1, 0, 0));
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
BOOST_CHECK(!hit);
}
BOOST_AUTO_TEST_CASE(rayVsModelTest2)
{
//advanced test, based on ray vs AABB
srand(7676762);
Ray ray;
AABB someAABB;
glm::vec3 minPos;
glm::vec3 maxPos;
bool z;
int test = 0;
minPos = glm::vec3(-0.5f, -0.5f, -0.5f);
maxPos = glm::vec3(0.5f, 0.5f, 0.5f);
someAABB = AABB(minPos, maxPos);
ResourceManager::RegisterType<RawModel>("RawModel");
auto unitBox = ResourceManager::Load<RawModel>("Models/Core/UnitBox.obj");
BOOST_CHECK(unitBox != nullptr);
for (size_t i = 0; i < 1000000; i++)
{
ray.Origin.x = rand() % 100;
ray.Origin.y = rand() % 100;
ray.Origin.z = rand() % 100;
ray.Direction.x = rand() % 100;
ray.Direction.y = rand() % 100;
ray.Direction.z = rand() % 100;
ray.Origin /= 100;
ray.Origin = glm::vec3(-2, 0, 0);
ray.Direction /= 100;
ray.Direction = glm::normalize(ray.Direction);
z = Collision::RayVsAABB(ray, someAABB);
if (z) {
//hit
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
if (!hit) {
hit = hit;
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);
}
//
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
if (hit) {
//hit
z = Collision::RayVsAABB(ray, someAABB);
if (!z) {
z = z;
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(octTest)
{
glm::vec3 mini = glm::vec3(-1, -1, -1);
+2 -12
View File
@@ -2,21 +2,12 @@
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(octTreeTests)
BOOST_AUTO_TEST_CASE(octTreeTest)
{
}
BOOST_AUTO_TEST_CASE(octTreeTest2)
{
}
BOOST_AUTO_TEST_SUITE(octTreeTestsW)
BOOST_AUTO_TEST_CASE(octSameRegionTest)
{
@@ -167,4 +158,3 @@ BOOST_AUTO_TEST_CASE(octNopPerfTestNoDuplicates)
}
BOOST_AUTO_TEST_SUITE_END()
+53
View File
@@ -0,0 +1,53 @@
#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 OctTree constructor check
//OctTree someOctTree(someAABB, 5);
//BOOST_CHECK(someOctTree.m_Children[0] != nullptr);
//simple destructor check in the end, just look for memleaks, then it didnt clear the AABB structure
}
BOOST_AUTO_TEST_CASE(octTreeTest2)
{
//octtree ritningen osv
Game game(0, nullptr);
while (game.Running()) {
game.Tick();
}
}
BOOST_AUTO_TEST_SUITE_END()
+172
View File
@@ -0,0 +1,172 @@
#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");
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_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();
// Create input manager
m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker);
// 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_LastTime = glfwGetTime();
}
Game::~Game()
{
delete m_FrameStack;
delete m_EventBroker;
}
void Game::Tick()
{
double currentTime = glfwGetTime();
double dt = currentTime - m_LastTime;
m_LastTime = currentTime;
m_EventBroker->Swap();
m_InputManager->Update(dt);
m_Renderer->Update(dt);
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 > 50) {
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
m_Renderer->Draw(m_RenderQueueFactory->RenderQueues());
m_EventBroker->Swap();
m_EventBroker->Clear();
glfwPollEvents();
}
+52
View File
@@ -0,0 +1,52 @@
#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 "OctTreeTestHardCodedTestWorld.h"
#include "Core\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;
//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
+21
View File
@@ -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\Core\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()
+134
View File
@@ -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 &center, 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);
}
};