Browse Source

Refactoring and adding a nice func in collision

Panagiotis Christopoulos Charitos 12 years ago
parent
commit
4d610115f5

+ 75 - 0
docs/drafts/tiler_optimized.txt

@@ -0,0 +1,75 @@
+
+algorithm cs aabb fr
+	MAX_POINTS = 8
+
+	pointsWorld[MAX_POINTS]
+
+	if cs.type is pespective frustum
+		pointsWorld[...] = get the 5 points
+		n = 5
+	else
+		pointsWorld[...] = get the 8 aabb points
+		n = 8
+	end
+
+	pointsNdc[MAX_POINTS]
+
+	minZ, maxZ
+	for i in (0, n)
+		pointsNdc[i] = fr.projectionMat * pointsWorld[i]
+		pointsNdc[i] /= pointsNdc[i].w
+
+		update minZ and maxZ
+	end
+
+	edges = convexHull2D(pointsNdc, n)
+
+
+	depth = log 2 TILES
+
+	for edge in edges
+		do_edge edge -1..1, -1..1 depth
+	end
+
+do_edge edge min max depth
+	if depth == 0
+		what?
+	endif
+
+	center = (min + max) / 2
+
+	if center left of edge
+		// right up
+		inside[4] = false
+		p = max
+		if p is left of edge
+			inside[0] = true;
+		end
+
+		// center top
+		p = center.x, max.y
+		if p is left of edge
+			inside[0] = true;
+			inside[1] = true;
+		end
+
+		// left top
+		p = min.x, max.y
+		if p is left of edge
+			inside[1] = true;
+		end
+
+		// blah blah
+
+		for all that inside
+			do edge ......... TODO
+		end
+ 
+	else
+		do edge min center depth - 1
+		do edge center max depth - 1
+		do edge min.x,center.y center.x,max.y depth - 1
+		do edge center.x,min.y max.x,center.y depth - 1
+	end
+end
+

+ 2 - 0
include/anki/collision/Collision.h

@@ -11,4 +11,6 @@
 #include "anki/collision/Frustum.h"
 #include "anki/collision/Aabb.h"
 
+#include "anki/collision/Functions.h"
+
 #endif

+ 25 - 0
include/anki/collision/Functions.h

@@ -0,0 +1,25 @@
+#ifndef ANKI_COLLISION_FUNCTIONS_H
+#define ANKI_COLLISION_FUNCTIONS_H
+
+#include "anki/collision/Plane.h"
+#include "anki/collision/Frustum.h"
+
+namespace anki {
+
+/// @addtogroup Collision
+/// @{
+
+/// Extract the clip planes using an MVP matrix
+///
+/// @param[in] mvp The MVP matrix.
+/// @param[out] planes Pointers to the planes. Elements can be nullptr
+///
+/// @note plane_count * 8 muls, plane_count sqrt
+extern void extractClipPlanes(const Mat4& mvp, 
+	Plane* planes[Frustum::FP_COUNT]);
+
+/// @}
+
+} // end namespace anki
+
+#endif

+ 2 - 2
include/anki/event/Event.h

@@ -2,7 +2,7 @@
 #define ANKI_EVENT_EVENT_H
 
 #include "anki/scene/Common.h"
-#include "anki/util/Flags.h"
+#include "anki/util/Bitset.h"
 
 namespace anki {
 
@@ -10,7 +10,7 @@ namespace anki {
 class EventManager;
 
 /// Abstract class for all events
-class Event: public Flags<U8>
+class Event: public Bitset<U8>
 {
 	friend class EventManager;
 

+ 15 - 1
include/anki/gl/Fbo.h

@@ -2,6 +2,7 @@
 #define ANKI_GL_FBO_H
 
 #include "anki/gl/GlObject.h"
+#include "anki/util/Array.h"
 #include <initializer_list>
 
 namespace anki {
@@ -16,6 +17,8 @@ class Texture;
 class Fbo: public GlObjectContextNonSharable
 {
 public:
+	static const U MAX_ATTACHMENTS = 8;
+
 	typedef GlObjectContextNonSharable Base;
 
 	/// FBO target
@@ -49,12 +52,14 @@ public:
 	{
 		destroy();
 		Base::operator=(std::forward<Base>(b));
+		attachmentsCount = b.attachmentsCount;
+		b.attachmentsCount = 0;
 		return *this;
 	}
 	/// @}
 
 	/// Binds FBO
-	void bind(const FboTarget target = FT_ALL) const;
+	void bind(const FboTarget target = FT_ALL, Bool noReadbacks = false) const;
 
 	/// Unbind all targets. Unbinds both draw and read FBOs so the active is
 	/// the default FBO
@@ -92,6 +97,9 @@ private:
 	static thread_local const Fbo* currentRead;
 	static thread_local const Fbo* currentDraw;
 
+	Array<GLenum, MAX_ATTACHMENTS> attachments;
+	U8 attachmentsCount;
+
 	static GLuint getCurrentFboGlId()
 	{
 		GLint i;
@@ -112,6 +120,12 @@ private:
 		glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &i);
 		return i;
 	}
+
+	void invalidateInternal() const
+	{
+		glInvalidateFramebuffer(GL_FRAMEBUFFER, attachmentsCount, 
+			&attachments[0]);
+	}
 };
 /// @}
 

+ 3 - 2
include/anki/renderer/Dbg.h

@@ -3,13 +3,14 @@
 
 #include "anki/renderer/RenderingPass.h"
 #include "anki/gl/Fbo.h"
-#include <memory>
 #include "anki/renderer/DebugDrawer.h"
+#include "anki/util/Bitset.h"
+#include <memory>
 
 namespace anki {
 
 /// Debugging stage
-class Dbg: public SwitchableRenderingPass, public Flags<U8>
+class Dbg: public SwitchableRenderingPass, public Bitset<U8>
 {
 public:
 	enum DebugFlag

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

@@ -7,7 +7,6 @@
 #include "anki/resource/Resource.h"
 #include "anki/collision/CollisionShape.h"
 #include "anki/scene/SceneNode.h"
-#include "anki/util/Flags.h"
 #include "anki/util/Array.h"
 #include <unordered_map>
 #include <LinearMath/btIDebugDraw.h>

+ 3 - 3
include/anki/scene/Movable.h

@@ -2,7 +2,7 @@
 #define ANKI_SCENE_MOVABLE_H
 
 #include "anki/util/Object.h"
-#include "anki/util/Flags.h"
+#include "anki/util/Bitset.h"
 #include "anki/math/Math.h"
 #include "anki/core/Timestamp.h"
 #include "anki/scene/Common.h"
@@ -16,7 +16,7 @@ class PropertyMap;
 
 /// Interface for movable scene nodes
 class Movable: public Object<Movable, SceneAllocator<Movable>>,
-	public Flags<U8>
+	public Bitset<U8>
 {
 public:
 	typedef Object<Movable, SceneAllocator<Movable>> Base;
@@ -161,7 +161,7 @@ protected:
 	void movableMarkForUpdate()
 	{
 		timestamp = Timestamp::getTimestamp();
-		enableFlags(MF_TRANSFORM_DIRTY);
+		enableBits(MF_TRANSFORM_DIRTY);
 	}
 };
 /// @}

+ 5 - 5
include/anki/scene/Spatial.h

@@ -3,7 +3,7 @@
 
 #include "anki/scene/Common.h"
 #include "anki/collision/Collision.h"
-#include "anki/util/Flags.h"
+#include "anki/util/Bitset.h"
 #include "anki/core/Timestamp.h"
 
 namespace anki {
@@ -18,7 +18,7 @@ class SceneNode;
 /// Spatial "interface" for scene nodes. It indicates scene nodes that need to 
 /// be placed in the scene's octree and they participate in the visibility 
 /// tests
-class Spatial: public Flags<U8>
+class Spatial: public Bitset<U8>
 {
 	friend class OctreeNode;
 	friend class Grid;
@@ -46,7 +46,7 @@ public:
 		U32 flags = SF_NONE)
 		: spatialProtected(cs, alloc)
 	{
-		enableFlags(flags);
+		enableBits(flags);
 	}
 
 	// Remove from current OctreeNode
@@ -146,11 +146,11 @@ public:
 
 	void resetFrame()
 	{
-		disableFlags(SF_VISIBLE_ANY);
+		disableBits(SF_VISIBLE_ANY);
 
 		for(Spatial* subsp : spatialProtected.subSpatials)
 		{
-			subsp->disableFlags(SF_VISIBLE_ANY);
+			subsp->disableBits(SF_VISIBLE_ANY);
 		}
 	}
 

+ 69 - 0
include/anki/util/Bitset.h

@@ -0,0 +1,69 @@
+#ifndef ANKI_BITSET_H
+#define ANKI_BITSET_H
+
+#include "anki/util/StdTypes.h"
+
+namespace anki {
+
+/// @addtogroup util
+/// @{
+/// @addtogroup containers
+/// @{
+
+/// Easy bit manipulation
+template<typename T>
+class Bitset
+{
+public:
+	typedef T Value;
+
+	Bitset()
+		: bitmask(0)
+	{}
+
+	Bitset(T bitmask_)
+		: bitmask(bitmask_)
+	{}
+
+	/// @name Bits manipulation
+	/// @{
+	void enableBits(Value mask)
+	{
+		bitmask |= mask;
+	}
+	void enableBits(Value mask, Bool enable)
+	{
+		bitmask = (enable) ? bitmask | mask : bitmask & ~mask;
+	}
+
+	void disableBits(Value mask)
+	{
+		bitmask &= ~mask;
+	}
+
+	void switchBits(Value mask)
+	{
+		bitmask ^= mask;
+	}
+
+	Bool bitsEnabled(Value mask) const
+	{
+		return bitmask & mask;
+	}
+
+	Value getBitmask() const
+	{
+		return bitmask;
+	}
+	/// @}
+
+protected:
+	Value bitmask;
+};
+
+/// @}
+/// @}
+
+} // end namespace anki
+
+#endif

+ 0 - 60
include/anki/util/Flags.h

@@ -1,60 +0,0 @@
-#ifndef ANKI_UTIL_FLAGS_H
-#define ANKI_UTIL_FLAGS_H
-
-#include "anki/util/StdTypes.h"
-
-namespace anki {
-
-/// Easy flag manipulation
-template<typename T>
-class Flags
-{
-public:
-	typedef T Value;
-
-	Flags()
-	{}
-
-	Flags(T bitmask_)
-		: bitmask(bitmask_)
-	{}
-
-	/// @name Flag manipulation
-	/// @{
-	void enableFlags(Value mask)
-	{
-		bitmask |= mask;
-	}
-	void enableFlags(Value mask, Bool enable)
-	{
-		bitmask = (enable) ? bitmask | mask : bitmask & ~mask;
-	}
-
-	void disableFlags(Value mask)
-	{
-		bitmask &= ~mask;
-	}
-
-	void switchFlags(Value mask)
-	{
-		bitmask ^= mask;
-	}
-
-	Bool flagsEnabled(Value mask) const
-	{
-		return bitmask & mask;
-	}
-
-	Value getFlagsBitbitmask() const
-	{
-		return bitmask;
-	}
-	/// @}
-
-protected:
-	Value bitmask = 0;
-};
-
-} // end namespace anki
-
-#endif

+ 72 - 0
src/collision/Functions.cpp

@@ -0,0 +1,72 @@
+#include "anki/collision/Functions.h"
+
+namespace anki {
+
+//==============================================================================
+void extractClipPlanes(const Mat4& mvp, Plane* planes[Frustum::FP_COUNT])
+{
+	// Plane equation coefficients
+	F32 a, b, c, d;
+
+	if(planes[Frustum::FP_NEAR])
+	{
+		a = mvp(3, 0) + mvp(2, 0);
+		b = mvp(3, 1) + mvp(2, 1);
+		c = mvp(3, 2) + mvp(2, 2);
+		d = mvp(3, 3) + mvp(2, 3);
+
+		*planes[Frustum::FP_NEAR] = Plane(a, b, c, d);
+	}
+
+	if(planes[Frustum::FP_FAR])
+	{
+		a = mvp(3, 0) - mvp(2, 0);
+		b = mvp(3, 1) - mvp(2, 1);
+		c = mvp(3, 2) - mvp(2, 2);
+		d = mvp(3, 3) - mvp(2, 3);
+
+		*planes[Frustum::FP_FAR] = Plane(a, b, c, d);
+	}
+
+	if(planes[Frustum::FP_LEFT])
+	{
+		a = mvp(3, 0) + mvp(0, 0);
+		b = mvp(3, 1) + mvp(0, 1);
+		c = mvp(3, 2) + mvp(0, 2);
+		d = mvp(3, 3) + mvp(0, 3);
+
+		*planes[Frustum::FP_LEFT] = Plane(a, b, c, d);
+	}
+
+	if(planes[Frustum::FP_RIGHT])
+	{
+		a = mvp(3, 0) - mvp(0, 0);
+		b = mvp(3, 1) - mvp(0, 1);
+		c = mvp(3, 2) - mvp(0, 2);
+		d = mvp(3, 3) - mvp(0, 3);
+
+		*planes[Frustum::FP_RIGHT] = Plane(a, b, c, d);
+	}
+
+	if(planes[Frustum::FP_TOP])
+	{
+		a = mvp(3, 0) - mvp(1, 0);
+		b = mvp(3, 1) - mvp(1, 1);
+		c = mvp(3, 2) - mvp(1, 2);
+		d = mvp(3, 3) - mvp(1, 3);
+
+		*planes[Frustum::FP_TOP] = Plane(a, b, c, d);
+	}
+
+	if(planes[Frustum::FP_BOTTOM])
+	{
+		a = mvp(3, 0) + mvp(1, 0);
+		b = mvp(3, 1) + mvp(1, 1);
+		c = mvp(3, 2) + mvp(1, 2);
+		d = mvp(3, 3) + mvp(1, 3);
+
+		*planes[Frustum::FP_BOTTOM] = Plane(a, b, c, d);
+	}	
+}
+
+} // end namespace anki

+ 1 - 1
src/event/Event.cpp

@@ -6,7 +6,7 @@ namespace anki {
 
 //==============================================================================
 Event::Event(F32 startTime_, F32 duration_, EventManager* manager_, U8 flags)
-	:	Flags<U8>(flags), 
+	:	Bitset<U8>(flags), 
 		startTime(startTime_),
 		duration(duration_),
 		manager(manager_)

+ 1 - 1
src/event/EventManager.cpp

@@ -79,7 +79,7 @@ void EventManager::updateAllEvents(F32 prevUpdateTime_, F32 crntTime_)
 		}
 		else
 		{
-			if(pevent->flagsEnabled(Event::EF_REANIMATE))
+			if(pevent->bitsEnabled(Event::EF_REANIMATE))
 			{
 				pevent->startTime = prevUpdateTime;
 				pevent->update(prevUpdateTime, crntTime);

+ 24 - 1
src/gl/Fbo.cpp

@@ -36,7 +36,7 @@ void Fbo::destroy()
 }
 
 //==============================================================================
-void Fbo::bind(const FboTarget target) const
+void Fbo::bind(const FboTarget target, Bool noReadbacks) const
 {
 	ANKI_ASSERT(isCreated());
 	checkNonSharable();
@@ -48,6 +48,14 @@ void Fbo::bind(const FboTarget target) const
 			glBindFramebuffer(GL_FRAMEBUFFER, glId);
 			currentDraw = currentRead = this;
 		}
+
+#if ANKI_GL == ANKI_GL_ES
+		if(noReadbacks)
+		{
+			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT 
+				| GL_STENCIL_BUFFER_BIT);
+		}
+#endif
 	}
 	else if(target == FT_DRAW)
 	{
@@ -56,6 +64,14 @@ void Fbo::bind(const FboTarget target) const
 			glBindFramebuffer(GL_DRAW_FRAMEBUFFER, glId);
 			currentDraw = this;
 		}
+
+#if ANKI_GL == ANKI_GL_ES
+		if(noReadbacks)
+		{
+			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT 
+				| GL_STENCIL_BUFFER_BIT);
+		}
+#endif
 	}
 	else
 	{
@@ -65,6 +81,8 @@ void Fbo::bind(const FboTarget target) const
 			glBindFramebuffer(GL_READ_FRAMEBUFFER, glId);
 			currentRead = this;
 		}
+
+		ANKI_ASSERT(noReadbacks == false && "Dosn't make sense");
 	}
 }
 
@@ -124,6 +142,9 @@ void Fbo::setColorAttachments(const std::initializer_list<const Texture*>&
 	{
 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
 			GL_TEXTURE_2D, tex->getGlId(), 0);
+
+		attachments[attachmentsCount++] = GL_COLOR_ATTACHMENT0 + i;
+
 		++i;
 	}
 }
@@ -158,6 +179,8 @@ void Fbo::setOtherAttachment(GLenum attachment, const Texture& tex,
 		ANKI_ASSERT(0);
 		break;
 	}
+
+	attachments[attachmentsCount++] = attachment;
 }
 
 } // end namespace anki

+ 1 - 1
src/gl/ShaderProgram.cpp

@@ -768,7 +768,7 @@ void ShaderProgram::initUniformBlocks()
 	{
 		/* Block index */
 		GLint blockIndex;
-		glGetActiveUniformsiv(glId, 1, &(uni.index),  GL_UNIFORM_BLOCK_INDEX, 
+		glGetActiveUniformsiv(glId, 1, &(uni.index), GL_UNIFORM_BLOCK_INDEX, 
 			&blockIndex);
 
 		if(blockIndex == -1)

+ 1 - 1
src/renderer/Dbg.cpp

@@ -15,7 +15,7 @@ Dbg::~Dbg()
 void Dbg::init(const Renderer::Initializer& initializer)
 {
 	enabled = initializer.dbg.enabled;
-	enableFlags(DF_ALL);
+	enableBits(DF_ALL);
 
 	try
 	{

+ 2 - 2
src/renderer/DebugDrawer.cpp

@@ -491,7 +491,7 @@ void SceneDebugDrawer::draw(SceneNode& node)
 
 	Spatial* sp;
 	if((sp = node.getSpatial())
-		&& sp->flagsEnabled(Spatial::SF_VISIBLE_CAMERA))
+		&& sp->bitsEnabled(Spatial::SF_VISIBLE_CAMERA))
 	{
 		draw(*sp);
 	}
@@ -517,7 +517,7 @@ void SceneDebugDrawer::draw(Spatial& x) const
 	dbg->setColor(Vec3(0.25, 0.0, 0.25));
 	for(auto it = x.getSubSpatialsBegin(); it != x.getSubSpatialsEnd(); ++it)
 	{
-		if((*it)->flagsEnabled(Spatial::SF_VISIBLE_CAMERA))
+		if((*it)->bitsEnabled(Spatial::SF_VISIBLE_CAMERA))
 		{
 			(*it)->getAabb().accept(coldraw);
 		}

+ 4 - 4
src/scene/Movable.cpp

@@ -6,7 +6,7 @@ namespace anki {
 //==============================================================================
 Movable::Movable(U32 flags_, Movable* parent, PropertyMap& pmap,
 	const SceneAllocator<Movable>& alloc)
-	: Base(parent, alloc), Flags(flags_)
+	: Base(parent, alloc), Bitset(flags_)
 {
 	pmap.addNewProperty(
 		new ReadWritePointerProperty<Transform>("localTransform", &lTrf));
@@ -35,14 +35,14 @@ void Movable::updateWorldTransform()
 {
 	prevWTrf = wTrf;
 	const Movable* parent = getParent();
-	const Bool dirty = flagsEnabled(MF_TRANSFORM_DIRTY);
+	const Bool dirty = bitsEnabled(MF_TRANSFORM_DIRTY);
 
 	// If dirty then update world transform
 	if(dirty)
 	{
 		if(parent)
 		{
-			if(flagsEnabled(MF_IGNORE_LOCAL_TRANSFORM))
+			if(bitsEnabled(MF_IGNORE_LOCAL_TRANSFORM))
 			{
 				wTrf = parent->getWorldTransform();
 			}
@@ -74,7 +74,7 @@ void Movable::updateWorldTransform()
 	}
 
 	// Now it's a good time to cleanse parent
-	disableFlags(MF_TRANSFORM_DIRTY);
+	disableBits(MF_TRANSFORM_DIRTY);
 }
 
 } // end namespace anki

+ 4 - 4
src/scene/Visibility.cpp

@@ -64,7 +64,7 @@ struct VisibilityTestJob: ThreadJob
 					subsp->getOptimalCollisionShape()))
 				{
 					subSpatialsMask |= 1 << i;
-					subsp->enableFlags(Spatial::SF_VISIBLE_CAMERA);
+					subsp->enableBits(Spatial::SF_VISIBLE_CAMERA);
 				}
 				++i;
 			}
@@ -106,7 +106,7 @@ struct VisibilityTestJob: ThreadJob
 				}
 			}
 
-			sp->enableFlags(Spatial::SF_VISIBLE_CAMERA);
+			sp->enableBits(Spatial::SF_VISIBLE_CAMERA);
 		} // end for
 	}
 
@@ -155,7 +155,7 @@ struct VisibilityTestJob: ThreadJob
 				if(frustumableSn->getFrustumable()->insideFrustum(*subsp))
 				{
 					subSpatialsMask |= 1 << i;
-					subsp->enableFlags(Spatial::SF_VISIBLE_LIGHT);
+					subsp->enableBits(Spatial::SF_VISIBLE_LIGHT);
 				}
 				++i;
 			}
@@ -165,7 +165,7 @@ struct VisibilityTestJob: ThreadJob
 				continue;
 			}
 
-			sp->enableFlags(Spatial::SF_VISIBLE_LIGHT);
+			sp->enableBits(Spatial::SF_VISIBLE_LIGHT);
 
 			Renderable* r = node->getRenderable();
 			if(r)

+ 4 - 4
testapp/Main.cpp

@@ -350,22 +350,22 @@ void mainLoopExtra()
 	}
 	if(in.getKey(KC_F2) == 1)
 	{
-		MainRendererSingleton::get().getDbg().switchFlags(
+		MainRendererSingleton::get().getDbg().switchBits(
 			Dbg::DF_SPATIAL);
 	}
 	if(in.getKey(KC_F3) == 1)
 	{
-		MainRendererSingleton::get().getDbg().switchFlags(
+		MainRendererSingleton::get().getDbg().switchBits(
 			Dbg::DF_PHYSICS);
 	}
 	if(in.getKey(KC_F4) == 1)
 	{
-		MainRendererSingleton::get().getDbg().switchFlags(
+		MainRendererSingleton::get().getDbg().switchBits(
 			Dbg::DF_SECTOR);
 	}
 	if(in.getKey(KC_F5) == 1)
 	{
-		MainRendererSingleton::get().getDbg().switchFlags(
+		MainRendererSingleton::get().getDbg().switchBits(
 			Dbg::DF_OCTREE);
 	}
 	if(in.getKey(KC_F12) == 1)