Browse Source

Collision and tiler work

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
23dbb2c8cb

+ 1 - 0
include/anki/Collision.h

@@ -20,5 +20,6 @@
 
 
 #include "anki/collision/GjkEpa.h"
 #include "anki/collision/GjkEpa.h"
 #include "anki/collision/Functions.h"
 #include "anki/collision/Functions.h"
+#include "anki/collision/Tests.h"
 
 
 #endif
 #endif

+ 8 - 0
include/anki/renderer/Tiler.h

@@ -76,6 +76,8 @@ private:
 	Plane* m_nearPlanesW = nullptr;
 	Plane* m_nearPlanesW = nullptr;
 	Plane* m_farPlanesW = nullptr;
 	Plane* m_farPlanesW = nullptr;
 
 
+	DArray<Vec4> m_hullPoints;
+
 	/// A texture of tilesXCount * tilesYCount size and format RG32UI. Used to
 	/// A texture of tilesXCount * tilesYCount size and format RG32UI. Used to
 	/// calculate the near and far planes of the tiles
 	/// calculate the near and far planes of the tiles
 	GlTextureHandle m_rt;
 	GlTextureHandle m_rt;
@@ -97,14 +99,20 @@ private:
 	Timestamp m_planes4UpdateTimestamp = 0;
 	Timestamp m_planes4UpdateTimestamp = 0;
 
 
 	ANKI_USE_RESULT Error initInternal();
 	ANKI_USE_RESULT Error initInternal();
+	ANKI_USE_RESULT Error initHullPoints();
 
 
 	void testRange(const CollisionShape& cs, Bool nearPlane,
 	void testRange(const CollisionShape& cs, Bool nearPlane,
 		U iFrom, U iTo, U jFrom, U jTo, VisibleTiles* visible, 
 		U iFrom, U iTo, U jFrom, U jTo, VisibleTiles* visible, 
 		U& count) const;
 		U& count) const;
 
 
+	Bool testAgainstHull(const CollisionShape& cs, 
+		const U yFrom, const U yTo, const U xFrom, const U xTo);
+
 	void update(U32 threadId, PtrSize threadsCount, 
 	void update(U32 threadId, PtrSize threadsCount, 
 		Camera& cam, Bool frustumChanged);
 		Camera& cam, Bool frustumChanged);
 
 
+	void updateHullPoints(U32 threadId, PtrSize threadsCount, Camera& cam);
+
 	/// Calculate and set a top looking plane
 	/// Calculate and set a top looking plane
 	void calcPlaneY(U i, const F32 o6, const F32 near);
 	void calcPlaneY(U i, const F32 o6, const F32 near);
 
 

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

@@ -321,6 +321,55 @@ private:
 		m_alloc = b.m_alloc;
 		m_alloc = b.m_alloc;
 	}
 	}
 };
 };
+
+/// Array with preallocated memory.
+template<typename T>
+class SArray: public DArray<T>
+{
+public:
+	using Base = DArray<T>;
+	using Value = T;
+
+	SArray(void* mem, PtrSize size)
+	:	Base()
+	{
+		ANKI_ASSERT(mem);
+		ANKI_ASSERT(size);
+		Base::m_data = static_cast<Value*>(mem);
+		Base::m_size = size;
+	}
+
+	/// Move.
+	SArray(SArray&& b)
+	:	SArray()
+	{
+		move(b);
+	}
+
+	~SArray()
+	{
+#if ANKI_ASSERTIONS
+		Base::m_data = nullptr;
+		Base::m_size = 0;
+#endif
+	}
+
+	/// Move.
+	SArray& operator=(SArray&& b)
+	{
+		move(b);
+		return *this;
+	}
+
+private:
+	void move(SArray& b)
+	{
+		Base::m_data = b.m_data;
+		b.m_data = nullptr;
+		Base::m_size = b.m_size;
+		b.m_size = 0;
+	}
+};
 /// @}
 /// @}
 
 
 } // end namespace anki
 } // end namespace anki

+ 1 - 1
run_callgrind.sh

@@ -1,4 +1,4 @@
 #!/bin/bash
 #!/bin/bash
 set -v
 set -v
 
 
-ANKI_DATA_PATH=$PWD/assets valgrind --tool=callgrind --callgrind-out-file=callgrind.out.tmp --collect-jumps=yes --branch-sim=yes --cache-sim=yes --simulate-wb=yes --cacheuse=yes --simulate-hwpref=yes --separate-threads=yes --I1=32768,2,64 --D1=32768,2,64 --LL=1048576,16,64 $1
+PROFILE=1 ANKI_DATA_PATH=$PWD/assets valgrind --tool=callgrind --callgrind-out-file=callgrind.out.tmp --collect-jumps=yes --branch-sim=yes --cache-sim=yes --simulate-wb=yes --cacheuse=yes --simulate-hwpref=yes --separate-threads=yes --I1=32768,2,64 --D1=32768,2,64 --LL=1048576,16,64 $1

+ 1 - 1
shaders/IsLp.frag.glsl

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

+ 5 - 0
src/collision/Tests.cpp

@@ -314,5 +314,10 @@ static const Callback matrix[COUNT][COUNT] = {
 /* PL   */ {txp<Aabb>,            tcx,  txp<LineSegment>,       txp<Obb>,             nullptr,            txp<Sphere>            },
 /* PL   */ {txp<Aabb>,            tcx,  txp<LineSegment>,       txp<Obb>,             nullptr,            txp<Sphere>            },
 /* S    */ {t<Aabb, Sphere>,      tcx,  t<LineSegment, Sphere>, gjk,                  tpx<Sphere>,        t<Sphere, Sphere>      }};
 /* S    */ {t<Aabb, Sphere>,      tcx,  t<LineSegment, Sphere>, gjk,                  tpx<Sphere>,        t<Sphere, Sphere>      }};
 
 
+Bool testCollisionShapes(const CollisionShape& a, const CollisionShape& b)
+{
+	return false;
+}
+
 } // end namespace anki
 } // end namespace anki
 
 

+ 5 - 2
src/core/Counters.cpp

@@ -258,8 +258,11 @@ void CountersManager::flush()
 			{
 			{
 				if(inf.m_flags & CF_FPS)
 				if(inf.m_flags & CF_FPS)
 				{
 				{
-					err = m_perrunFile.writeText("%" MAX_NAME "f", 
-						(F64)(*m_globalTimestamp) / m_perrunValues[i].m_float);
+					F32 fps = 
+						(F64)(*m_globalTimestamp) 
+						/ (m_perrunValues[i].m_float / 1000.0);
+
+					err = m_perrunFile.writeText("%" MAX_NAME "f", fps);
 				}
 				}
 				else
 				else
 				{
 				{

+ 1 - 1
src/renderer/Renderer.cpp

@@ -159,7 +159,7 @@ Error Renderer::render(SceneGraph& scene,
 	if(m_projectionParamsUpdateTimestamp 
 	if(m_projectionParamsUpdateTimestamp 
 			< m_scene->getActiveCameraChangeTimestamp()
 			< m_scene->getActiveCameraChangeTimestamp()
 		|| m_projectionParamsUpdateTimestamp < camUpdateTimestamp
 		|| m_projectionParamsUpdateTimestamp < camUpdateTimestamp
-		|| m_projectionParamsUpdateTimestamp == 1)
+		|| m_projectionParamsUpdateTimestamp == 0)
 	{
 	{
 		ANKI_ASSERT(cam.getCameraType() == Camera::Type::PERSPECTIVE);
 		ANKI_ASSERT(cam.getCameraType() == Camera::Type::PERSPECTIVE);
 		computeProjectionParams(fr.getProjectionMatrix());
 		computeProjectionParams(fr.getProjectionMatrix());

+ 77 - 0
src/renderer/Tiler.cpp

@@ -139,9 +139,65 @@ Error Tiler::initInternal()
 
 
 	cmdBuff.flush();
 	cmdBuff.flush();
 
 
+	// Hull
+	err = initHullPoints();
+
 	return err;
 	return err;
 }
 }
 
 
+//==============================================================================
+ANKI_USE_RESULT Error Tiler::initHullPoints()
+{
+	const U countX = m_r->getTilesCount().x();
+	const U countY = m_r->getTilesCount().y();
+
+	// Grid points + eye
+	const U count = (countX + 1) * (countY + 1) + 1;
+
+	return m_hullPoints.create(getAllocator(), count);
+}
+
+//==============================================================================
+void Tiler::updateHullPoints(U32 threadId, PtrSize threadsCount, Camera& cam)
+{
+	const U countX = m_r->getTilesCount().x() + 1;
+	const U countY = m_r->getTilesCount().y() + 1;
+	const U count = countX * countY;
+	PtrSize start, end;
+	Threadpool::Task::choseStartEnd(threadId, threadsCount, count, start, end);
+
+	const Vec4& projParams = m_r->getProjectionParameters();
+	const Transform& trf = 
+		cam.getComponent<MoveComponent>().getWorldTransform();
+
+	for(U i = start; i < end; ++i)
+	{
+		const U x = i % countX;
+		const U y = i / countX;
+
+		// Calculate the view space position
+		Vec2 clip = Vec2(
+			F32(x) / (countX - 1), 
+			F32(y) / (countY - 1));
+
+		Vec4 view;
+		const F32 depth = 1.0;
+		view.z() = projParams.z() / (projParams.w() + depth);
+		Vec2 viewxy = (clip * 2.0 - 1.0) * projParams.xy() * view.z();
+		view.x() = viewxy.x();
+		view.y() = viewxy.y();
+		view.w() = 0.0;
+
+		// Transform it
+		Vec4 finalPos = trf.transform(view);
+
+		// Store
+		m_hullPoints[i] = finalPos;
+	}
+
+	m_hullPoints.getBack() = trf.getOrigin();
+}
+
 //==============================================================================
 //==============================================================================
 void Tiler::runMinMax(GlTextureHandle& depthMap,
 void Tiler::runMinMax(GlTextureHandle& depthMap,
 	GlCommandBufferHandle& cmd)
 	GlCommandBufferHandle& cmd)
@@ -235,6 +291,25 @@ Bool Tiler::test(
 	return count > 0;
 	return count > 0;
 }
 }
 
 
+//==============================================================================
+Bool Tiler::testAgainstHull(const CollisionShape& cs, 
+	const U yFrom, const U yTo, const U xFrom, const U xTo)
+{
+	Array<Vec4, 5> points;
+	const U countX = m_r->getTilesCount().x() + 1;
+
+	points[0] = m_hullPoints[yFrom * countX + xFrom];
+	points[1] = m_hullPoints[yFrom * countX + xTo];
+	points[2] = m_hullPoints[yTo * countX + xFrom];
+	points[3] = m_hullPoints[yTo * countX + xTo];
+	points[3] = m_hullPoints.getBack();
+
+	ConvexHullShape hull;
+	hull.initStorage(&points[0], points.getSize());
+
+	return testCollisionShapes(hull, cs);
+}
+
 //==============================================================================
 //==============================================================================
 void Tiler::testRange(const CollisionShape& cs, Bool nearPlane,
 void Tiler::testRange(const CollisionShape& cs, Bool nearPlane,
 	U yFrom, U yTo, U xFrom, U xTo, VisibleTiles* visible, U& count) const
 	U yFrom, U yTo, U xFrom, U xTo, VisibleTiles* visible, U& count) const
@@ -548,6 +623,8 @@ void Tiler::update(U32 threadId, PtrSize threadsCount,
 		++farPlanesW;
 		++farPlanesW;
 	}
 	}
 #endif
 #endif
+
+	updateHullPoints(threadId, threadsCount, cam);
 }
 }
 
 
 //==============================================================================
 //==============================================================================

+ 12 - 5
testapp/Main.cpp

@@ -79,6 +79,13 @@ Error init()
 		1.0));
 		1.0));
 	scene.setActiveCamera(cam);
 	scene.setActiveCamera(cam);
 
 
+#if NO_PLAYER
+	cam->getComponent<MoveComponent>().
+		setLocalTransform(Transform(Vec4(-5.0, 10.0, -3.0, 0.0),
+		Mat3x4(Euler(toRad(-26.0), toRad(-90.0), toRad(0.0))),
+		1.0));
+#endif
+
 	// lights
 	// lights
 #if 0
 #if 0
 	Vec3 lpos(-24.0, 0.1, -10.0);
 	Vec3 lpos(-24.0, 0.1, -10.0);
@@ -224,7 +231,7 @@ Error init()
 	}
 	}
 #endif
 #endif
 
 
-#if 1
+#if 0
 	// horse
 	// horse
 	err = scene.newSceneNode<ModelNode>("horse", horse, 
 	err = scene.newSceneNode<ModelNode>("horse", horse, 
 		"models/horse/horse.ankimdl");
 		"models/horse/horse.ankimdl");
@@ -240,7 +247,7 @@ Error init()
 	}
 	}
 #endif
 #endif
 
 
-	if(1)
+	if(0)
 	{
 	{
 		err = scene.newSceneNode<PointLight>("plight0", point);
 		err = scene.newSceneNode<PointLight>("plight0", point);
 		if(err) return err;
 		if(err) return err;
@@ -254,7 +261,7 @@ Error init()
 		move->setLocalOrigin(Vec4(2.0, 1.4, 0.6, 0.0));
 		move->setLocalOrigin(Vec4(2.0, 1.4, 0.6, 0.0));
 	}
 	}
 
 
-#if 0
+#if 1
 	{
 	{
 		ScriptResourcePointer script;
 		ScriptResourcePointer script;
 
 
@@ -472,7 +479,7 @@ Error mainLoopExtra(App& app, void*, Bool& quit)
 
 
 	//execStdinScpripts();
 	//execStdinScpripts();
 
 
-	if(getenv("PROFILE") && app.getGlobalTimestamp() == 2000)
+	if(getenv("PROFILE") && app.getGlobalTimestamp() == 500)
 	{
 	{
 		quit = true;
 		quit = true;
 		return err;
 		return err;
@@ -525,7 +532,7 @@ Error initSubsystems(int argc, char* argv[])
 
 
 	//config.set("maxTextureSize", 256);
 	//config.set("maxTextureSize", 256);
 
 
-	config.set("fullscreenDesktopResolution", false);
+	config.set("fullscreenDesktopResolution", true);
 	config.set("debugContext", false);
 	config.set("debugContext", false);
 
 
 	app = new App;
 	app = new App;