소스 검색

Collision fixes

Panagiotis Christopoulos Charitos 11 년 전
부모
커밋
91c731ce3a

+ 5 - 4
include/anki/collision/Frustum.h

@@ -9,7 +9,7 @@
 #include "anki/collision/CompoundShape.h"
 #include "anki/collision/CompoundShape.h"
 #include "anki/collision/Plane.h"
 #include "anki/collision/Plane.h"
 #include "anki/collision/Obb.h"
 #include "anki/collision/Obb.h"
-#include "anki/collision/LineSegment.h"
+#include "anki/collision/ConvexHullShape.h"
 #include "anki/Math.h"
 #include "anki/Math.h"
 #include "anki/util/Array.h"
 #include "anki/util/Array.h"
 
 
@@ -188,9 +188,9 @@ public:
 		m_frustumDirty = true;
 		m_frustumDirty = true;
 	}
 	}
 
 
-	const Array<LineSegment, 4>& getLineSegments() const
+	const Array<Vec4, 5>& getPoints() const
 	{
 	{
-		return m_segments;
+		return m_points;
 	}
 	}
 
 
 	/// Copy
 	/// Copy
@@ -216,7 +216,8 @@ private:
 
 
 	/// @name Shape
 	/// @name Shape
 	/// @{
 	/// @{
-	Array<LineSegment, 4> m_segments;
+	Array<Vec4, 5> m_points;
+	ConvexHullShape m_hull;
 	/// @}
 	/// @}
 
 
 	/// Implements Frustum::recalculate. Recalculates:
 	/// Implements Frustum::recalculate. Recalculates:

+ 1 - 1
include/anki/renderer/Tiler.h

@@ -106,7 +106,7 @@ private:
 		U& count) const;
 		U& count) const;
 
 
 	Bool testAgainstHull(const CollisionShape& cs, 
 	Bool testAgainstHull(const CollisionShape& cs, 
-		const U yFrom, const U yTo, const U xFrom, const U xTo);
+		const U yFrom, const U yTo, const U xFrom, const U xTo) const;
 
 
 	void update(U32 threadId, PtrSize threadsCount, 
 	void update(U32 threadId, PtrSize threadsCount, 
 		Camera& cam, Bool frustumChanged);
 		Camera& cam, Bool frustumChanged);

+ 6 - 0
include/anki/util/DArray.h

@@ -131,6 +131,12 @@ public:
 		return m_data[m_size - 1];
 		return m_data[m_size - 1];
 	}
 	}
 
 
+	/// Get last element.
+	ConstReference getBack() const
+	{
+		return m_data[m_size - 1];
+	}
+
 	/// Get last element.
 	/// Get last element.
 	ConstReference back() const
 	ConstReference back() const
 	{
 	{

+ 2 - 2
shaders/IsLp.frag.glsl

@@ -345,8 +345,8 @@ void main()
 		* vec3(0.01, 0.001, 0.001);
 		* vec3(0.01, 0.001, 0.001);
 #endif
 #endif
 
 
-#if 0
-	if(pointLightsCount != 0)
+#if 1
+	if(spotLightsCount != 0)
 	{
 	{
 		out_color += vec3(0.1);
 		out_color += vec3(0.1);
 	}
 	}

+ 2 - 1
shaders/PpsSslr.frag.glsl

@@ -9,6 +9,7 @@
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/Pack.glsl"
 
 
+const float PI = 3.14159265358979323846;
 const float ONE = 0.9;
 const float ONE = 0.9;
 
 
 layout(location = 0) in vec2 inTexCoords;
 layout(location = 0) in vec2 inTexCoords;
@@ -152,7 +153,7 @@ void main()
 				return;
 				return;
 			}
 			}
 
 
-			float factor = 1.0 - length(ndc);
+			float factor = sin(length(ndc) * PI);
 			factor *= 1.0 - length(pp0);
 			factor *= 1.0 - length(pp0);
 			factor *= specColor;
 			factor *= specColor;
 
 

+ 15 - 2
src/collision/ConvexHullShape.cpp

@@ -97,22 +97,35 @@ F32 ConvexHullShape::testPlane(const Plane& p) const
 	Plane pa = (m_trfIdentity) ? p : p.getTransformed(m_invTrf);
 	Plane pa = (m_trfIdentity) ? p : p.getTransformed(m_invTrf);
 
 
 	F32 minDist = MAX_F32;
 	F32 minDist = MAX_F32;
+	F32 maxDist = MIN_F32;
 
 
 	const Vec4* point = m_points;
 	const Vec4* point = m_points;
 	const Vec4* end = m_points + m_pointsCount;
 	const Vec4* end = m_points + m_pointsCount;
 	for(; point != end; ++point)
 	for(; point != end; ++point)
 	{
 	{
 		F32 test = pa.test(*point);
 		F32 test = pa.test(*point);
-		if(test == 0.0)
+		if(ANKI_UNLIKELY(test == 0.0))
 		{
 		{
 			// Early exit
 			// Early exit
 			return 0.0; 	
 			return 0.0; 	
 		}
 		}
 
 
 		minDist = min(minDist, test);
 		minDist = min(minDist, test);
+		maxDist = max(maxDist, test);
 	}
 	}
 
 
-	return minDist;
+	if(minDist > 0.0 && maxDist > 0.0)
+	{
+		return maxDist;
+	}
+	else if(minDist > 0.0 && maxDist > 0.0)
+	{
+		return minDist;
+	}
+	else
+	{
+		return 0.0;
+	}
 }
 }
 
 
 //==============================================================================
 //==============================================================================

+ 9 - 11
src/collision/Frustum.cpp

@@ -158,10 +158,8 @@ void Frustum::updateInternal()
 PerspectiveFrustum::PerspectiveFrustum()
 PerspectiveFrustum::PerspectiveFrustum()
 :	Frustum(Type::PERSPECTIVE)
 :	Frustum(Type::PERSPECTIVE)
 {
 {
-	for(LineSegment& ls : m_segments)
-	{
-		addShape(&ls);
-	}
+	addShape(&m_hull);
+	m_hull.initStorage(&m_points[0], m_points.getSize());
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -170,7 +168,7 @@ PerspectiveFrustum& PerspectiveFrustum::operator=(const PerspectiveFrustum& b)
 	Frustum::operator=(b);
 	Frustum::operator=(b);
 	m_fovX = b.m_fovX;
 	m_fovX = b.m_fovX;
 	m_fovY = b.m_fovY;
 	m_fovY = b.m_fovY;
-	m_segments = b.m_segments;
+	m_points = b.m_points;
 	return *this;
 	return *this;
 }
 }
 
 
@@ -202,19 +200,19 @@ void PerspectiveFrustum::recalculate(Bool planes, Bool other)
 	if(other)
 	if(other)
 	{
 	{
 		Vec4 eye = Vec4(0.0, 0.0, -m_near, 0.0);
 		Vec4 eye = Vec4(0.0, 0.0, -m_near, 0.0);
-		for(LineSegment& ls : m_segments)
+		for(Vec4& p : m_points)
 		{
 		{
-			ls.setOrigin(eye);
+			p = eye;
 		}
 		}
 
 
 		F32 x = m_far / tan((getPi<F32>() - m_fovX) / 2.0);
 		F32 x = m_far / tan((getPi<F32>() - m_fovX) / 2.0);
 		F32 y = tan(m_fovY / 2.0) * m_far;
 		F32 y = tan(m_fovY / 2.0) * m_far;
 		F32 z = -m_far;
 		F32 z = -m_far;
 
 
-		m_segments[0].setDirection(Vec4(x, y, z - m_near, 0.0)); // top right
-		m_segments[1].setDirection(Vec4(-x, y, z - m_near, 0.0)); // top left
-		m_segments[2].setDirection(Vec4(-x, -y, z - m_near, 0.0)); // bot left
-		m_segments[3].setDirection(Vec4(x, -y, z - m_near, 0.0)); // bot right
+		m_points[1] += Vec4(x, y, z - m_near, 0.0); // top right
+		m_points[2] += Vec4(-x, y, z - m_near, 0.0); // top left
+		m_points[3] += Vec4(-x, -y, z - m_near, 0.0); // bot left
+		m_points[4] += Vec4(x, -y, z - m_near, 0.0); // bot right
 	}
 	}
 }
 }
 
 

+ 2 - 2
src/renderer/Dbg.cpp

@@ -146,7 +146,7 @@ Error Dbg::run(GlCommandBufferHandle& cmdb)
 	}
 	}
 #endif
 #endif
 
 
-#if 1
+#if 0
 	{
 	{
 		Vec4 storage[] = {
 		Vec4 storage[] = {
 			Vec4(1.0, 1.0, 1.0, 0.0),
 			Vec4(1.0, 1.0, 1.0, 0.0),
@@ -170,7 +170,7 @@ Error Dbg::run(GlCommandBufferHandle& cmdb)
 		CompoundShape comp;
 		CompoundShape comp;
 		comp.addShape(&obb);
 		comp.addShape(&obb);
 		comp.addShape(&s);
 		comp.addShape(&s);
-		comp.addShape(&abb);
+		comp.addShape(&);
 
 
 		comp.transform(move.getWorldTransform());
 		comp.transform(move.getWorldTransform());
 
 

+ 2 - 1
src/renderer/Is.cpp

@@ -249,8 +249,9 @@ public:
 
 
 		for(U i = 0; i < 4; i++)
 		for(U i = 0; i < 4; i++)
 		{
 		{
+			// TODO that is wrong
 			Vec4 extendPoint = move.getWorldTransform().getOrigin() 
 			Vec4 extendPoint = move.getWorldTransform().getOrigin() 
-				+ frustum.getLineSegments()[i].getDirection();
+				+ frustum.getPoints()[i];
 
 
 			extendPoint = m_camFrustum->getViewMatrix() * extendPoint.xyz1();
 			extendPoint = m_camFrustum->getViewMatrix() * extendPoint.xyz1();
 			baseslight->m_extendPoints[i] = extendPoint;
 			baseslight->m_extendPoints[i] = extendPoint;

+ 20 - 2
src/renderer/Tiler.cpp

@@ -195,6 +195,7 @@ void Tiler::updateHullPoints(U32 threadId, PtrSize threadsCount, Camera& cam)
 		m_hullPoints[i] = finalPos;
 		m_hullPoints[i] = finalPos;
 	}
 	}
 
 
+	// The last point is the eye
 	m_hullPoints.getBack() = trf.getOrigin();
 	m_hullPoints.getBack() = trf.getOrigin();
 }
 }
 
 
@@ -293,7 +294,7 @@ Bool Tiler::test(
 
 
 //==============================================================================
 //==============================================================================
 Bool Tiler::testAgainstHull(const CollisionShape& cs, 
 Bool Tiler::testAgainstHull(const CollisionShape& cs, 
-	const U yFrom, const U yTo, const U xFrom, const U xTo)
+	const U yFrom, const U yTo, const U xFrom, const U xTo) const
 {
 {
 	Array<Vec4, 5> points;
 	Array<Vec4, 5> points;
 	const U countX = m_r->getTilesCount().x() + 1;
 	const U countX = m_r->getTilesCount().x() + 1;
@@ -302,7 +303,7 @@ Bool Tiler::testAgainstHull(const CollisionShape& cs,
 	points[1] = m_hullPoints[yFrom * countX + xTo];
 	points[1] = m_hullPoints[yFrom * countX + xTo];
 	points[2] = m_hullPoints[yTo * countX + xFrom];
 	points[2] = m_hullPoints[yTo * countX + xFrom];
 	points[3] = m_hullPoints[yTo * countX + xTo];
 	points[3] = m_hullPoints[yTo * countX + xTo];
-	points[3] = m_hullPoints.getBack();
+	points[4] = m_hullPoints.getBack();
 
 
 	ConvexHullShape hull;
 	ConvexHullShape hull;
 	hull.initStorage(&points[0], points.getSize());
 	hull.initStorage(&points[0], points.getSize());
@@ -368,6 +369,7 @@ void Tiler::testRange(const CollisionShape& cs, Bool nearPlane,
 
 
 	// Do the checks
 	// Do the checks
 	Bool inside[2][2] = {{false, false}, {false, false}};
 	Bool inside[2][2] = {{false, false}, {false, false}};
+	U touchingPlanes = 0;
 
 
 	// Top looking plane check
 	// Top looking plane check
 	if(my > 0)
 	if(my > 0)
@@ -386,6 +388,8 @@ void Tiler::testRange(const CollisionShape& cs, Bool nearPlane,
 		}
 		}
 		else
 		else
 		{
 		{
+			++touchingPlanes;
+
 			// Possibly all inside
 			// Possibly all inside
 			for(U i = 0; i < 2; i++)
 			for(U i = 0; i < 2; i++)
 			{
 			{
@@ -426,9 +430,23 @@ void Tiler::testRange(const CollisionShape& cs, Bool nearPlane,
 		else
 		else
 		{
 		{
 			// Do nothing and keep the top looking plane check results
 			// Do nothing and keep the top looking plane check results
+			++touchingPlanes;
 		}
 		}
 	}
 	}
 
 
+	// If touching both planes then we need detailed tests
+	//if(touchingPlanes > 2)
+	{
+		inside[0][0] = testAgainstHull(cs, 
+			yFrom, yFrom + my, xFrom, xFrom + mx);
+		inside[0][1] = testAgainstHull(cs, 
+			yFrom, yFrom + my, xFrom + mx, xTo);
+		inside[1][0] = testAgainstHull(cs, 
+			yFrom + my, yTo, xFrom, xFrom + mx);
+		inside[1][1] = testAgainstHull(cs, 
+			yFrom + my, yTo, xFrom + mx, xTo);
+	}
+
 	// Now move lower to the hierarchy
 	// Now move lower to the hierarchy
 	if(mx == 0)
 	if(mx == 0)
 	{
 	{

+ 3 - 3
testapp/Main.cpp

@@ -249,15 +249,15 @@ Error init()
 
 
 	if(1)
 	if(1)
 	{
 	{
-		err = scene.newSceneNode<PointLight>("plight0", point);
+		err = scene.newSceneNode<SpotLight>("plight0", spot);
 		if(err) return err;
 		if(err) return err;
 
 
-		lightc = point->tryGetComponent<LightComponent>();
+		lightc = spot->tryGetComponent<LightComponent>();
 		lightc->setRadius(6.0);
 		lightc->setRadius(6.0);
 		lightc->setDiffuseColor(Vec4(1.0));
 		lightc->setDiffuseColor(Vec4(1.0));
 		lightc->setSpecularColor(Vec4(0.6, 0.6, 0.3, 1.0));
 		lightc->setSpecularColor(Vec4(0.6, 0.6, 0.3, 1.0));
 
 
-		move = point->tryGetComponent<MoveComponent>();
+		move = spot->tryGetComponent<MoveComponent>();
 		move->setLocalOrigin(Vec4(2.0, 1.4, 0.6, 0.0));
 		move->setLocalOrigin(Vec4(2.0, 1.4, 0.6, 0.0));
 	}
 	}