Browse Source

Scene and portals

Panagiotis Christopoulos Charitos 13 years ago
parent
commit
979fd1abb3

+ 52 - 51
include/anki/collision/CollisionAlgorithmsMatrix.h

@@ -2,6 +2,7 @@
 #define ANKI_COLLISION_COLLISION_ALGORITHMS_MATRIX_H
 
 #include "anki/collision/Forward.h"
+#include "anki/util/StdTypes.h"
 
 namespace anki {
 
@@ -37,137 +38,137 @@ public:
 
 	/// Generic collide function. It doesn't uses visitor pattern for
 	/// speed reasons
-	static bool collide(const CollisionShape& a, const CollisionShape& b);
+	static Bool collide(const CollisionShape& a, const CollisionShape& b);
 
 	// 1st line (LS)
-	static bool collide(const Ls& a, const Ls& b);
-	static bool collide(const Ls& a, const Obb& b);
-	static bool collide(const Ls& a, const Frustum& b);
-	static bool collide(const Ls& a, const Plane& b);
-	static bool collide(const Ls& a, const Ray& b);
-	static bool collide(const Ls& a, const Sphere& b);
-	static bool collide(const Ls& a, const Aabb& b);
+	static Bool collide(const Ls& a, const Ls& b);
+	static Bool collide(const Ls& a, const Obb& b);
+	static Bool collide(const Ls& a, const Frustum& b);
+	static Bool collide(const Ls& a, const Plane& b);
+	static Bool collide(const Ls& a, const Ray& b);
+	static Bool collide(const Ls& a, const Sphere& b);
+	static Bool collide(const Ls& a, const Aabb& b);
 
 	// 2nd line (OBB)
-	static bool collide(const Obb& a, const Ls& b)
+	static Bool collide(const Obb& a, const Ls& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Obb& a, const Obb& b);
-	static bool collide(const Obb& a, const Frustum& b);
-	static bool collide(const Obb& a, const Plane& b);
-	static bool collide(const Obb& a, const Ray& b);
-	static bool collide(const Obb& a, const Sphere& b);
-	static bool collide(const Obb& a, const Aabb& b);
+	static Bool collide(const Obb& a, const Obb& b);
+	static Bool collide(const Obb& a, const Frustum& b);
+	static Bool collide(const Obb& a, const Plane& b);
+	static Bool collide(const Obb& a, const Ray& b);
+	static Bool collide(const Obb& a, const Sphere& b);
+	static Bool collide(const Obb& a, const Aabb& b);
 
 	// 3rd line (FRU)
-	static bool collide(const Frustum& a, const Ls& b)
+	static Bool collide(const Frustum& a, const Ls& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Frustum& a, const Obb& b)
+	static Bool collide(const Frustum& a, const Obb& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Frustum& a, const Frustum& b);
-	static bool collide(const Frustum& a, const Plane& b);
-	static bool collide(const Frustum& a, const Ray& b);
-	static bool collide(const Frustum& a, const Sphere& b);
-	static bool collide(const Frustum& a, const Aabb& b);
+	static Bool collide(const Frustum& a, const Frustum& b);
+	static Bool collide(const Frustum& a, const Plane& b);
+	static Bool collide(const Frustum& a, const Ray& b);
+	static Bool collide(const Frustum& a, const Sphere& b);
+	static Bool collide(const Frustum& a, const Aabb& b);
 
 	// 4th line (P)
-	static bool collide(const Plane& a, const Ls& b)
+	static Bool collide(const Plane& a, const Ls& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Plane& a, const Obb& b)
+	static Bool collide(const Plane& a, const Obb& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Plane& a,const Frustum& b)
+	static Bool collide(const Plane& a,const Frustum& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Plane& a, const Plane& b);
-	static bool collide(const Plane& a, const Ray& b);
-	static bool collide(const Plane& a, const Sphere& b);
-	static bool collide(const Plane& a, const Aabb& b);
+	static Bool collide(const Plane& a, const Plane& b);
+	static Bool collide(const Plane& a, const Ray& b);
+	static Bool collide(const Plane& a, const Sphere& b);
+	static Bool collide(const Plane& a, const Aabb& b);
 
 	// 5th line (R)
-	static bool collide(const Ray& a, const Ls& b)
+	static Bool collide(const Ray& a, const Ls& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Ray& a, const Obb& b)
+	static Bool collide(const Ray& a, const Obb& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Ray& a, const Frustum& b)
+	static Bool collide(const Ray& a, const Frustum& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Ray& a, const Plane& b)
+	static Bool collide(const Ray& a, const Plane& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Ray& a, const Ray& b);
-	static bool collide(const Ray& a, const Sphere& b);
-	static bool collide(const Ray& a, const Aabb& b);
+	static Bool collide(const Ray& a, const Ray& b);
+	static Bool collide(const Ray& a, const Sphere& b);
+	static Bool collide(const Ray& a, const Aabb& b);
 
 	// 6th line (S)
-	static bool collide(const Sphere& a, const Ls& b)
+	static Bool collide(const Sphere& a, const Ls& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Sphere& a, const Obb& b)
+	static Bool collide(const Sphere& a, const Obb& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Sphere& a, const Frustum& b)
+	static Bool collide(const Sphere& a, const Frustum& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Sphere& a, const Plane& b)
+	static Bool collide(const Sphere& a, const Plane& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Sphere& a, const Ray& b)
+	static Bool collide(const Sphere& a, const Ray& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Sphere& a, const Sphere& b);
-	static bool collide(const Sphere& a, const Aabb& b);
+	static Bool collide(const Sphere& a, const Sphere& b);
+	static Bool collide(const Sphere& a, const Aabb& b);
 
 	// 7th line (AABB)
-	static bool collide(const Aabb& a, const Ls& b)
+	static Bool collide(const Aabb& a, const Ls& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Aabb& a, const Obb& b)
+	static Bool collide(const Aabb& a, const Obb& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Aabb& a, const Frustum& b)
+	static Bool collide(const Aabb& a, const Frustum& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Aabb& a, const Plane& b)
+	static Bool collide(const Aabb& a, const Plane& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Aabb& a, const Ray& b)
+	static Bool collide(const Aabb& a, const Ray& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Aabb& a, const Sphere& b)
+	static Bool collide(const Aabb& a, const Sphere& b)
 	{
 		return collide(b, a);
 	}
-	static bool collide(const Aabb& a, const Aabb& b);
+	static Bool collide(const Aabb& a, const Aabb& b);
 
 private:
 	template<typename T>
-	static bool tcollide(const CollisionShape& a, const CollisionShape& b);
+	static Bool tcollide(const CollisionShape& a, const CollisionShape& b);
 };
 /// @}
 

+ 10 - 2
include/anki/scene/Frustumable.h

@@ -4,6 +4,7 @@
 #include "anki/collision/Frustum.h"
 #include "anki/scene/Spatial.h"
 #include "anki/scene/VisibilityTester.h"
+#include "anki/scene/Common.h"
 
 namespace anki {
 
@@ -18,8 +19,8 @@ public:
 	/// @{
 
 	/// Pass the frustum here so we can avoid the virtuals
-	Frustumable(Frustum* fr)
-		: frustum(fr)
+	Frustumable(const SceneAllocator<U8>& alloc, Frustum* fr)
+		: frustum(fr), renderables(alloc), lights(alloc)
 	{}
 	/// @}
 
@@ -89,6 +90,13 @@ protected:
 
 private:
 	U32 timestamp = Timestamp::getTimestamp();
+
+	// Visibility stuff
+	typedef SceneVector<SceneNode*> Renderables;
+	typedef SceneVector<SceneNode*> Lights;
+
+	Renderables renderables;
+	Lights lights;
 };
 
 } // end namespace anki

+ 53 - 0
include/anki/scene/Grid.h

@@ -0,0 +1,53 @@
+#ifndef ANKI_SCENE_GRID_H
+#define ANKI_SCENE_GRID_H
+
+#include "anki/collision/Collision.h"
+#include "anki/scene/Common.h"
+
+namespace anki {
+
+class SceneNode;
+class Frustumable;
+
+/// XXX
+class Grid
+{
+public:
+	/// Default constructor
+	Grid(const SceneAllocator<U8>& alloc, F32 cubeSize, const Aabb& aabb);
+
+	/// @name Accessors
+	/// @{
+	const Aabb& getAabb() const
+	{
+		return aabb;
+	}
+
+	SceneVector<SceneNode*>::iterator getSceneNodesBegin()
+	{
+		return sceneNodes.begin();
+	}
+	SceneVector<SceneNode*>::iterator getSceneNodesEnd()
+	{
+		return sceneNodes.end();
+	}
+	/// @}
+
+	/// Place a scene node in the grid
+	Bool placeSceneNode(SceneNode* sn);
+
+	/// Populate the fr with visible scene nodes and go up
+	void doVisibilityTests(Frustumable& fr);
+
+private:
+	Aabb aabb;
+	Array<U32, 3> cubesCount; ///< Cubes count in the 3 dimentions
+	F32 cubeSize;
+	SceneVector<SceneNode*> sceneNodes;
+
+	void removeSceneNode(SceneNode* sn);
+};
+
+} // end namespace anki
+
+#endif

+ 0 - 1
include/anki/scene/Movable.h

@@ -153,7 +153,6 @@ protected:
 
 	/// The frame where it was last moved
 	U32 timestamp = Timestamp::getTimestamp();
-	
 
 	/// Called for every frame. It updates the @a wTrf if @a shouldUpdateWTrf
 	/// is true. Then it moves to the children.

+ 5 - 2
include/anki/scene/Sector.h

@@ -2,17 +2,20 @@
 #define ANKI_SCENE_SECTOR_H
 
 #include "anki/scene/Octree.h"
+#include "anki/collision/Collision.h"
 
 namespace anki {
 
 // Forward
 class SceneNode;
+class Scene;
 class Sector;
 
 /// 2 way Portal
 struct Portal
 {
 	Array<Sector*, 2> sectors;
+	Obb shape;
 
 	Portal();
 };
@@ -40,7 +43,7 @@ class SectorGroup
 {
 public:
 	/// Default constructor
-	SectorGroup(const SceneAllocator<U8>& alloc);
+	SectorGroup(Scene* scene);
 
 	/// Destructor
 	~SectorGroup();
@@ -52,7 +55,7 @@ public:
 	Bool placeSceneNode(SceneNode* sp);
 
 private:
-	SceneAllocator<U8> alloc; ///< Keep a copy of the scene allocator
+	Scene* scene; ///< Keep it here to access various allocators
 	SceneVector<Sector*> sectors;
 	SceneVector<Portal*> portals;
 };

+ 3 - 0
include/anki/scene/Spatial.h

@@ -8,6 +8,7 @@
 namespace anki {
 
 class OctreeNode;
+class Grid;
 class SceneNode;
 
 /// @addtogroup Scene
@@ -19,6 +20,7 @@ class SceneNode;
 class Spatial: public Flags<U32>
 {
 	friend class OctreeNode;
+	friend class Grid;
 
 public:
 	/// Spatial flags
@@ -90,6 +92,7 @@ protected:
 private:
 	U32 timestamp = Timestamp::getTimestamp();
 	OctreeNode* octreeNode = nullptr; ///< What octree node includes this
+	Grid* grid = nullptr;
 	Aabb aabb; ///< A faster shape
 	SceneNode* sceneNode; ///< Know your father
 	Vec3 origin;

+ 2 - 2
src/renderer/Dbg.cpp

@@ -68,10 +68,10 @@ void Dbg::run()
 		}
 	}
 
-	for(const Sector* sector : scene.sectors)
+	/*for(const Sector* sector : scene.sectors)
 	{
 		sceneDrawer->draw(sector->getOctree());
-	}
+	}*/
 
 	scene.getPhysics().debugDraw();
 

+ 1 - 1
src/scene/Camera.cpp

@@ -14,7 +14,7 @@ Camera::Camera(CameraType type_,
 	:	SceneNode(name, scene),
 		Movable(movableFlags, movParent, *this, getSceneAllocator()),
 		Spatial(this, frustum),
-		Frustumable(frustum), 
+		Frustumable(getSceneAllocator(), frustum),
 		type(type_)
 {}
 

+ 68 - 0
src/scene/Grid.cpp

@@ -0,0 +1,68 @@
+#include "anki/scene/Grid.h"
+#include "anki/scene/Spatial.h"
+#include "anki/scene/SceneNode.h"
+
+namespace anki {
+
+//==============================================================================
+Grid::Grid(const SceneAllocator<U8> &alloc, F32 cubeSize_, const Aabb& aabb_)
+	: cubeSize(cubeSize_), sceneNodes(alloc)
+{
+	// Set the aabb by snaping it
+	for(U i = 0; i < 3; i++)
+	{
+		F32 d = aabb_.getMax()[i] - aabb_.getMin()[i];
+		ANKI_ASSERT(d > cubeSize);
+		F32 snap = ceil(d / cubeSize);
+
+		cubesCount[i] = snap;
+
+		snap *= cubeSize;
+		snap -= d;
+		snap *= 0.5;
+
+		aabb.getMin()[i] = aabb_.getMin()[i] - snap;
+		aabb.getMax()[i] = aabb_.getMax()[i] + snap;
+	}
+}
+
+//==============================================================================
+void Grid::removeSceneNode(SceneNode* sn)
+{
+	SceneVector<SceneNode*>::iterator it;
+
+	it = std::find(sceneNodes.begin(), sceneNodes.end(), sn);
+
+	ANKI_ASSERT(it != sceneNodes.end());
+	sceneNodes.erase(it);
+
+	(*it)->getSpatial()->grid = nullptr;
+}
+
+//==============================================================================
+Bool Grid::placeSceneNode(SceneNode* sn)
+{
+	Spatial* sp = sn->getSpatial();
+	ANKI_ASSERT(sp);
+
+	if(sp->grid == this)
+	{
+		// do nothing
+	}
+	else
+	{
+		sp->grid->removeSceneNode(sn);
+		sp->grid = this;
+		sceneNodes.push_back(sn);
+	}
+
+	return true;
+}
+
+//==============================================================================
+void doVisibilityTests(Frustumable& fr)
+{
+
+}
+
+} // end namespace anki

+ 1 - 1
src/scene/Light.cpp

@@ -48,7 +48,7 @@ PointLight::PointLight(const char* name, Scene* scene,
 SpotLight::SpotLight(const char* name, Scene* scene,
 	U32 movableFlags, Movable* movParent)
 	: 	Light(LT_SPOT, name, scene, movableFlags, movParent, &frustum),
-		Frustumable(&frustum)
+		Frustumable(getSceneAllocator(), &frustum)
 {
 	const F32 ang = toRad(45.0);
 	setOuterAngle(ang / 2.0);

+ 8 - 5
src/scene/Sector.cpp

@@ -1,6 +1,7 @@
 #include "anki/scene/Sector.h"
 #include "anki/scene/Spatial.h"
 #include "anki/scene/SceneNode.h"
+#include "anki/scene/Scene.h"
 #include "anki/collision/CollisionAlgorithmsMatrix.h"
 
 namespace anki {
@@ -44,21 +45,23 @@ Bool Sector::placeSceneNode(SceneNode* sn)
 //==============================================================================
 
 //==============================================================================
-SectorGroup::SectorGroup(const SceneAllocator<U8>& alloc_)
-	: alloc(alloc_)
-{}
+SectorGroup::SectorGroup(Scene* scene_)
+	: scene(scene_)
+{
+	ANKI_ASSERT(scene != nullptr);
+}
 
 //==============================================================================
 SectorGroup::~SectorGroup()
 {
 	for(Sector* sector : sectors)
 	{
-		ANKI_DELETE(sector, alloc);
+		ANKI_DELETE(sector, scene->getAllocator());
 	}
 
 	for(Portal* portal : portals)
 	{
-		ANKI_DELETE(portal, alloc);
+		ANKI_DELETE(portal, scene->getAllocator());
 	}
 }
 

+ 5 - 0
testapp/Main.cpp

@@ -393,6 +393,11 @@ void mainLoop()
 	HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
 	HighRezTimer::Scalar crntTime = prevUpdateTime;
 
+	std::cout << sizeof(SpotLight) << " " << sizeof(Light) << " "
+		<< sizeof(Frustumable) << " " << sizeof(VisibilityInfo) << " "
+		<< sizeof(SceneNode) << " " << sizeof(Movable) << " "
+		<< sizeof(Spatial) << std::endl;
+
 	while(1)
 	{
 		HighRezTimer timer;