Browse Source

Sector debug draw

Panagiotis Christopoulos Charitos 13 years ago
parent
commit
449dba656b

+ 8 - 0
include/anki/Config.h.cmake

@@ -32,4 +32,12 @@
 #define ANKI_FILE __FILE__
 #define ANKI_FUNC __func__
 
+#if defined(__GNUC__)
+#	define ANKI_LIKELY(x) __builtin_expect((x), 1)
+#	define ANKI_UNLIKELY(x) __builtin_expect((x), 0)
+#else
+#	define ANKI_LIKELY(x) ((x) == 1)
+#	define ANKI_UNLIKELY(x) ((x) == 1)
+#endif
+
 #endif

+ 6 - 1
include/anki/renderer/DebugDrawer.h

@@ -155,6 +155,7 @@ class Octree;
 class OctreeNode;
 class Renderer;
 class Camera;
+class Sector;
 
 /// This is a drawer for some scene nodes that need debug
 class SceneDebugDrawer: public Flags<U32>
@@ -164,7 +165,9 @@ public:
 	{
 		DF_NONE = 0,
 		DF_SPATIAL = 1 << 0,
-		DF_FRUSTUMABLE = 1 << 1
+		DF_FRUSTUMABLE = 1 << 1,
+		DF_SECTOR = 1 << 2,
+		DF_OCTREE = 1 << 3
 	};
 
 	SceneDebugDrawer(DebugDrawer* d)
@@ -178,6 +181,8 @@ public:
 
 	virtual void draw(const Octree& octree) const;
 
+	void draw(const Sector& sector);
+
 	void setViewProjectionMatrix(const Mat4& m)
 	{
 		dbg->setViewProjectionMatrix(m);

+ 0 - 10
include/anki/scene/Light.h

@@ -117,16 +117,6 @@ public:
 	}
 	/// @}
 
-	/// @name Spatial virtuals
-	/// @{
-
-	/// Override Spatial::getSpatialOrigin
-	const Vec3& getSpatialOrigin() const
-	{
-		return getWorldTransform().getOrigin();
-	}
-	/// @}
-
 private:
 	LightType type;
 	Vec4 color = Vec4(1.0);

+ 0 - 10
include/anki/scene/ModelNode.h

@@ -87,16 +87,6 @@ public:
 	}
 	/// @}
 
-	/// @name Spatial virtuals
-	/// @{
-
-	/// Override Spatial::getSpatialOrigin
-	const Vec3& getSpatialOrigin() const
-	{
-		return getWorldTransform().getOrigin();
-	}
-	/// @}
-
 private:
 	Obb obb; ///< In world space
 	const ModelPatch* modelPatch; ///< The resource

+ 0 - 10
include/anki/scene/ParticleEmitter.h

@@ -235,16 +235,6 @@ public:
 	void movableUpdate();
 	/// @}
 
-	/// @name Spatial virtuals
-	/// @{
-
-	/// Override Spatial::getSpatialOrigin
-	const Vec3& getSpatialOrigin() const
-	{
-		return getWorldTransform().getOrigin();
-	}
-	/// @}
-
 private:
 	ParticleEmitterResourcePointer particleEmitterResource;
 	std::unique_ptr<btCollisionShape> collShape = nullptr;

+ 25 - 3
include/anki/scene/Sector.h

@@ -21,6 +21,7 @@ struct Portal
 {
 	Array<Sector*, 2> sectors;
 	Obb shape;
+	Bool8 open;
 
 	Portal();
 };
@@ -34,9 +35,6 @@ public:
 	/// Default constructor
 	Sector(SectorGroup* group, const Aabb& box);
 
-	/// Called when a node was moved or a change in shape happened
-	Bool placeSceneNode(SceneNode* sp);
-
 	const Aabb& getAabb() const
 	{
 		return octree.getRoot().getAabb();
@@ -51,10 +49,24 @@ public:
 		return *group;
 	}
 
+	const Octree& getOctree() const
+	{
+		return octree;
+	}
+
+	Bool isVisible() const
+	{
+		return visible;
+	}
+
+	/// Called when a node was moved or a change in shape happened
+	Bool placeSceneNode(SceneNode* sp);
+
 private:
 	SectorGroup* group; ///< Know your father
 	Octree octree;
 	SceneVector<Portal*> portals;
+	Bool8 visible;
 };
 
 /// Sector group. This is supposed to represent the whole scene
@@ -77,6 +89,16 @@ public:
 	{
 		return *scene;
 	}
+
+	const SceneVector<Portal*>& getPortals() const
+	{
+		return portals;
+	}
+
+	const SceneVector<Sector*>& getSectors() const
+	{
+		return sectors;
+	}
 	/// @}
 
 	/// Called when a node was moved or a change in shape happened. The node 

+ 0 - 10
include/anki/scene/SkinNode.h

@@ -198,16 +198,6 @@ public:
 	}
 	/// @}
 
-	/// @name Spatial virtuals
-	/// @{
-
-	/// Override Spatial::getSpatialOrigin
-	const Vec3& getSpatialOrigin() const
-	{
-		return getWorldTransform().getOrigin();
-	}
-	/// @}
-
 private:
 	std::unique_ptr<SkinModelPatch> skinModelPatch;
 };

+ 8 - 1
include/anki/scene/Spatial.h

@@ -72,7 +72,12 @@ public:
 		return timestamp;
 	}
 
-	virtual const Vec3& getSpatialOrigin() const = 0;
+	/// Used for sorting spatials. In most object the origin is the center of
+	/// the bounding volume but for cameras the origin is the eye point
+	virtual const Vec3& getSpatialOrigin() const
+	{
+		return origin;
+	}
 
 	OctreeNode& getOctreeNode()
 	{
@@ -92,6 +97,7 @@ public:
 	{
 		timestamp = Timestamp::getTimestamp();
 		spatialCs->toAabb(aabb);
+		origin = (aabb.getMax() + aabb.getMin()) * 0.5;
 	}
 
 protected:
@@ -101,6 +107,7 @@ private:
 	U32 timestamp = Timestamp::getTimestamp();
 	OctreeNode* octreeNode = nullptr; ///< What octree node includes this
 	Aabb aabb; ///< A faster shape
+	Vec3 origin; ///< Cached value
 };
 /// @}
 

+ 2 - 1
include/anki/util/StdTypes.h

@@ -13,7 +13,7 @@ typedef int16_t I16;
 typedef int32_t I32;
 typedef int64_t I64;
 
-typedef int_fast32_t I; ///< Fast integer at least 32bit
+typedef int_fast32_t I; ///< Fast signed integer at least 32bit
 
 typedef uint8_t U8;
 typedef uint16_t U16;
@@ -28,6 +28,7 @@ typedef float F32;
 typedef double F64;
 
 typedef bool Bool;
+typedef U8 Bool8;
 /// @}
 
 } // end namespace anki

+ 7 - 3
src/renderer/Dbg.cpp

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

+ 31 - 0
src/renderer/DebugDrawer.cpp

@@ -4,6 +4,7 @@
 #include "anki/collision/Collision.h"
 #include "anki/scene/Frustumable.h"
 #include "anki/scene/Octree.h"
+#include "anki/scene/Sector.h"
 #include "anki/resource/Material.h"
 #include "anki/scene/Renderable.h"
 #include "anki/scene/Camera.h"
@@ -543,4 +544,34 @@ void SceneDebugDrawer::draw(const OctreeNode& octnode, U32 depth,
 	}
 }
 
+//==============================================================================
+void SceneDebugDrawer::draw(const Sector& sector)
+{
+	if(!isFlagEnabled(DF_SECTOR))
+	{
+		return;
+	}
+
+	// Draw the sector
+	dbg->setColor(Vec3(0.5, 0.5, 1.0));
+	CollisionDebugDrawer v(dbg);
+	sector.getAabb().accept(v);
+
+	// Draw the portals
+	dbg->setColor(Vec3(0.0, 0.0, 1.0));
+	for(const Portal* portal : sector.getSectorGroup().getPortals())
+	{
+		if(portal->sectors[0] == &sector || portal->sectors[1] == &sector)
+		{
+			portal->shape.accept(v);
+		}
+	}
+
+	// Draw the octree
+	if(isFlagEnabled(DF_OCTREE))
+	{
+		draw(sector.getOctree());
+	}
+}
+
 }  // end namespace anki

+ 14 - 0
src/scene/Sector.cpp

@@ -82,6 +82,7 @@ struct MaterialSortJob: ThreadJob
 Portal::Portal()
 {
 	sectors[0] = sectors[1] = nullptr;
+	open = true;
 }
 
 //==============================================================================
@@ -210,6 +211,12 @@ void SectorGroup::doVisibilityTests(SceneNode& sn, VisibilityTest test,
 	Frustumable* fr = sn.getFrustumable();
 	ANKI_ASSERT(fr != nullptr && "sn should be frustumable");
 
+	// Set all sectors to not visible
+	for(Sector* sector : sectors)
+	{
+		sector->visible = false;
+	}
+
 	//
 	// Find the visible sectors
 	//
@@ -221,11 +228,17 @@ void SectorGroup::doVisibilityTests(SceneNode& sn, VisibilityTest test,
 
 	// Find the sector that contains the frustumable
 	Sector& containerSector = sp->getOctreeNode().getOctree().getSector();
+	containerSector.visible = true;
 	visibleSectors.push_back(&containerSector);
 
 	// Loop all portals and add other sectors
 	for(Portal* portal : portals)
 	{
+		if(ANKI_UNLIKELY(!portal->open))
+		{
+			continue;
+		}
+
 		// Get the "other" sector of that portal
 		Sector* testAgainstSector;
 
@@ -253,6 +266,7 @@ void SectorGroup::doVisibilityTests(SceneNode& sn, VisibilityTest test,
 			{
 				if(r == nullptr || r->doVisibilityTests(portal->shape))
 				{
+					testAgainstSector->visible = true;
 					visibleSectors.push_back(testAgainstSector);
 				}
 			}

+ 9 - 4
testapp/Main.cpp

@@ -248,11 +248,16 @@ void init()
 
 	(void)sponzaModel;
 
-	//initPhysics();
+	initPhysics();
 
-	/*ParticleEmitter* pe = new ParticleEmitter("todo", "pe", &scene,
-		Movable::MF_NONE, nullptr);
-	(void)pe;*/
+	// Sectors
+	SectorGroup& sgroup = scene.getSectorGroup();
+
+	Sector* sectorA = sgroup.createNewSector(Aabb(Vec3(-100), Vec3(100)));
+	Sector* sectorB = sgroup.createNewSector(Aabb(Vec3(-10), Vec3(10)));
+
+	sgroup.createNewPortal(sectorA, sectorB, Obb(Vec3(0.0), 
+		Mat3::getIdentity(), Vec3(2.0, 2.0, 0.0)));
 }
 
 //==============================================================================