Browse Source

Physics work

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
a8beeb2b45

+ 3 - 3
include/anki/math/Mat3x4.h

@@ -47,11 +47,11 @@ public:
 	/// @name Constructors
 	/// @name Constructors
 	/// @{
 	/// @{
 	explicit TMat3x4()
 	explicit TMat3x4()
-		: Base()
+	:	Base()
 	{}
 	{}
 
 
 	TMat3x4(const TMat3x4& b)
 	TMat3x4(const TMat3x4& b)
-		: Base(b)
+	:	Base(b)
 	{}
 	{}
 
 
 	explicit TMat3x4(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, 
 	explicit TMat3x4(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, 
@@ -73,7 +73,7 @@ public:
 	}
 	}
 
 
 	explicit TMat3x4(const T f)
 	explicit TMat3x4(const T f)
-		: Base(f)
+	:	Base(f)
 	{}
 	{}
 
 
 	explicit TMat3x4(const TMat3<T>& m3)
 	explicit TMat3x4(const TMat3<T>& m3)

+ 1 - 1
include/anki/math/Transform.h

@@ -33,7 +33,7 @@ public:
 
 
 	explicit TTransform(const Mat4& m4)
 	explicit TTransform(const Mat4& m4)
 	{
 	{
-		m_rotation = m4.getRotationPart();
+		m_rotation = TMat3x4<T>(m4.getRotationPart());
 		m_origin = m4.getTranslationPart();
 		m_origin = m4.getTranslationPart();
 		m_scale = 1.0;
 		m_scale = 1.0;
 		ANKI_CHECK_W();
 		ANKI_CHECK_W();

+ 2 - 0
include/anki/physics/Common.h

@@ -15,6 +15,8 @@ namespace anki {
 
 
 // Forward
 // Forward
 class PhysicsWorld;
 class PhysicsWorld;
+class PhysicsCollisionShape;
+class SceneGraph;
 
 
 /// @addtogroup physics
 /// @addtogroup physics
 /// @{
 /// @{

+ 23 - 2
include/anki/physics/PhysicsBody.h

@@ -13,17 +13,38 @@ namespace anki {
 /// @addtogroup physics
 /// @addtogroup physics
 /// @{
 /// @{
 
 
+/// Body initializer.
+struct PhysicsBodyInitializer
+{
+	PhysicsWorld* m_world = nullptr;
+	PhysicsCollisionShape* m_shape = nullptr;
+	F32 m_mass = 0.0;
+	Transform m_startTrf = Transform::getIdentity();
+	Bool m_kinematic = false;
+	Bool m_gravity = true;
+};
+
 /// Rigid body.
 /// Rigid body.
 class PhysicsBody
 class PhysicsBody
 {
 {
 public:
 public:
+	using Initializer = PhysicsBodyInitializer;
+
 	PhysicsBody();
 	PhysicsBody();
 
 
 	~PhysicsBody();
 	~PhysicsBody();
 
 
+	ANKI_USE_RESULT Error create(const Initializer& init);
+
 private:
 private:
-	NewtonBody* m_body;
-	Transform m_trf;
+	NewtonBody* m_body = nullptr;
+	Transform m_trf = Transform::getIdentity();
+
+	// Newton callback.
+	static void onTransform(
+		const NewtonBody* const body, 
+		const dFloat* const matrix, 
+		int threadIndex);
 };
 };
 /// @}
 /// @}
 
 

+ 7 - 0
include/anki/physics/PhysicsCollisionShape.h

@@ -42,8 +42,15 @@ public:
 	ANKI_USE_RESULT Error createStaticTriangleSoup(Initializer& init,
 	ANKI_USE_RESULT Error createStaticTriangleSoup(Initializer& init,
 		const Vec3* positions, U32 positionsStride, U16* indices);
 		const Vec3* positions, U32 positionsStride, U16* indices);
 
 
+	NewtonCollision* _getNewtonShape()
+	{
+		ANKI_ASSERT(m_shape);
+		return m_shape;
+	}
+
 private:
 private:
 	NewtonCollision* m_shape = nullptr;
 	NewtonCollision* m_shape = nullptr;
+	static I32 id;
 };
 };
 /// @}
 /// @}
 
 

+ 9 - 0
include/anki/physics/PhysicsWorld.h

@@ -20,7 +20,16 @@ public:
 	PhysicsWorld();
 	PhysicsWorld();
 	~PhysicsWorld();
 	~PhysicsWorld();
 
 
+	ANKI_USE_RESULT Error create(SceneGraph* scene);
+
+	NewtonWorld* _getNewtonWorld()
+	{
+		ANKI_ASSERT(m_world);
+		return m_world;
+	}
+
 private:
 private:
+	SceneGraph* m_scene = nullptr;
 	NewtonWorld* m_world = nullptr;
 	NewtonWorld* m_world = nullptr;
 };
 };
 
 

+ 5 - 5
include/anki/renderer/Ssao.h

@@ -25,6 +25,11 @@ class Ssao: public RenderingPass
 	friend class Sslr;
 	friend class Sslr;
 	friend class MainRenderer;
 	friend class MainRenderer;
 
 
+	GlTextureHandle& _getRt()
+	{
+		return m_vblurRt;
+	}
+
 private:
 private:
 	U32 m_width, m_height; ///< Blur passes size
 	U32 m_width, m_height; ///< Blur passes size
 	U8 m_blurringIterationsCount;
 	U8 m_blurringIterationsCount;
@@ -52,11 +57,6 @@ private:
 	ANKI_USE_RESULT Error init(const ConfigSet& initializer);
 	ANKI_USE_RESULT Error init(const ConfigSet& initializer);
 	ANKI_USE_RESULT Error run(GlCommandBufferHandle& cmdBuff);
 	ANKI_USE_RESULT Error run(GlCommandBufferHandle& cmdBuff);
 
 
-	GlTextureHandle& getRt()
-	{
-		return m_vblurRt;
-	}
-
 	ANKI_USE_RESULT Error createFb(
 	ANKI_USE_RESULT Error createFb(
 		GlFramebufferHandle& fb, GlTextureHandle& rt);
 		GlFramebufferHandle& fb, GlTextureHandle& rt);
 	ANKI_USE_RESULT Error initInternal(const ConfigSet& initializer);
 	ANKI_USE_RESULT Error initInternal(const ConfigSet& initializer);

+ 1 - 1
src/CMakeLists.txt

@@ -35,7 +35,7 @@ endif()
 
 
 add_library(anki Dummy.cpp "${_SYS_SRC}")
 add_library(anki Dummy.cpp "${_SYS_SRC}")
 
 
-target_link_libraries(anki ankicore ankiscript ankirenderer ankiscene ankievent ankiinput ankiphysics ankiresource ankimisc ankigl ankicollision ankimath ankiutil ankitinyxml2 ankilua ankiz ${LIB_SDL} ${ANKI_GPERFTOOLS_LIBS} ${_SYS})
+target_link_libraries(anki ankicore ankiscript ankirenderer ankiscene ankievent ankiinput ankiphysics ankiresource ankimisc ankigl ankicollision ankimath ankiutil ankitinyxml2 ankilua ankiz ankinewton ${LIB_SDL} ${ANKI_GPERFTOOLS_LIBS} ${_SYS})
 
 
 set_target_properties(anki PROPERTIES LINKER_LANGUAGE CXX)
 set_target_properties(anki PROPERTIES LINKER_LANGUAGE CXX)
 
 

+ 69 - 1
src/physics/PhysicsBody.cpp

@@ -4,6 +4,8 @@
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
 #include "anki/physics/PhysicsBody.h"
 #include "anki/physics/PhysicsBody.h"
+#include "anki/physics/PhysicsWorld.h"
+#include "anki/physics/PhysicsCollisionShape.h"
 
 
 namespace anki {
 namespace anki {
 
 
@@ -13,6 +15,72 @@ PhysicsBody::PhysicsBody()
 
 
 //==============================================================================
 //==============================================================================
 PhysicsBody::~PhysicsBody()
 PhysicsBody::~PhysicsBody()
-{}
+{
+	if(m_body)
+	{
+		NewtonDestroyBody(m_body);
+	}
+}
+
+//==============================================================================
+Error PhysicsBody::create(const Initializer& init)
+{
+	ANKI_ASSERT(init.m_world);
+	ANKI_ASSERT(init.m_shape);
+
+	Mat4 trf = Mat4(init.m_startTrf);
+	if(init.m_kinematic)
+	{
+		// TODO
+	}
+	else
+	{
+		m_body = NewtonCreateDynamicBody(init.m_world->_getNewtonWorld(),
+			init.m_shape->_getNewtonShape(), &trf(0, 0));
+	}
+
+	if(!m_body)
+	{
+		ANKI_LOGE("NewtonCreateXXBody() failed");
+		return ErrorCode::FUNCTION_FAILED;
+	}
+
+	// User data & callbacks
+	NewtonBodySetUserData(m_body, this);
+	NewtonBodySetTransformCallback(m_body, onTransform);
+
+	// Set mass
+	NewtonCollision* shape = NewtonBodyGetCollision(m_body);
+	NewtonBodySetMassProperties(m_body, init.m_mass, shape);
+
+	NewtonBodySetSimulationState(m_body, true);
+
+	if(init.m_gravity)
+	{
+		//Vec3 force = ;
+		//NewtonBodyAddForce(m_body, );
+	}
+
+	return ErrorCode::NONE;
+}
+
+//==============================================================================
+void PhysicsBody::onTransform(
+	const NewtonBody* const body, 
+	const dFloat* const matrix, 
+	int threadIndex)
+{
+	ANKI_ASSERT(body);
+	ANKI_ASSERT(matrix);
+
+	void* ud = NewtonBodyGetUserData(body);
+	ANKI_ASSERT(ud);
+
+	PhysicsBody* self = reinterpret_cast<PhysicsBody*>(ud);
+
+	Mat4 trf;
+	memcpy(&trf, matrix, sizeof(Mat4));
+	self->m_trf = Transform(trf);
+}
 
 
 } // end namespace anki
 } // end namespace anki

+ 39 - 0
src/physics/PhysicsCollisionShape.cpp

@@ -4,9 +4,13 @@
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
 #include "anki/physics/PhysicsCollisionShape.h"
 #include "anki/physics/PhysicsCollisionShape.h"
+#include "anki/physics/PhysicsWorld.h"
 
 
 namespace anki {
 namespace anki {
 
 
+//==============================================================================
+I32 PhysicsCollisionShape::id = 0;
+
 //==============================================================================
 //==============================================================================
 PhysicsCollisionShape::PhysicsCollisionShape()
 PhysicsCollisionShape::PhysicsCollisionShape()
 {}
 {}
@@ -20,5 +24,40 @@ PhysicsCollisionShape::~PhysicsCollisionShape()
 	}
 	}
 }
 }
 
 
+//==============================================================================
+Error PhysicsCollisionShape::createSphere(Initializer& init, F32 radius)
+{
+	ANKI_ASSERT(init.m_world);
+	Error err = ErrorCode::NONE;
+
+	m_shape = NewtonCreateSphere(
+		init.m_world->_getNewtonWorld(), radius, id++, nullptr);
+	if(!m_shape)
+	{
+		ANKI_LOGE("NewtonCreateSphere() failed");
+		err = ErrorCode::FUNCTION_FAILED;
+	}
+
+	return err;
+}
+
+//==============================================================================
+Error PhysicsCollisionShape::createBox(Initializer& init, const Vec3& extend)
+{
+	ANKI_ASSERT(init.m_world);
+	Error err = ErrorCode::NONE;
+
+	m_shape = NewtonCreateBox(
+		init.m_world->_getNewtonWorld(), extend.x(), extend.y(), extend.z(),
+		id++, nullptr);
+	if(!m_shape)
+	{
+		ANKI_LOGE("NewtonCreateBox() failed");
+		err = ErrorCode::FUNCTION_FAILED;
+	}
+
+	return err;
+}
+
 } // end namespace anki
 } // end namespace anki
 
 

+ 44 - 1
src/physics/PhysicsWorld.cpp

@@ -4,15 +4,58 @@
 // http://www.anki3d.org/LICENSE
 // http://www.anki3d.org/LICENSE
 
 
 #include "anki/physics/PhysicsWorld.h"
 #include "anki/physics/PhysicsWorld.h"
+#include "anki/scene/SceneGraph.h"
 
 
 namespace anki {
 namespace anki {
 
 
+//==============================================================================
+// Ugly but there is no other way
+static SceneGraph* gScene = nullptr;
+
+static void* newtonAlloc(int size)
+{
+	return gScene->getAllocator().allocate(size);
+}
+
+static void newtonFree(void* const ptr, int size)
+{
+	gScene->getAllocator().deallocate(ptr, size);
+}
+
 //==============================================================================
 //==============================================================================
 PhysicsWorld::PhysicsWorld()
 PhysicsWorld::PhysicsWorld()
 {}
 {}
 
 
 //==============================================================================
 //==============================================================================
 PhysicsWorld::~PhysicsWorld()
 PhysicsWorld::~PhysicsWorld()
-{}
+{
+	if(m_world)
+	{
+		NewtonDestroy(m_world);
+	}
+}
+
+//==============================================================================
+Error PhysicsWorld::create(SceneGraph* scene)
+{
+	Error err = ErrorCode::NONE;
+
+	ANKI_ASSERT(scene);
+	m_scene = scene;
+	
+	// Set allocators
+	gScene = scene;
+	NewtonSetMemorySystem(newtonAlloc, newtonFree);
+
+	// Initialize world
+	m_world = NewtonCreate();
+	if(!m_world)
+	{
+		ANKI_LOGE("NewtonCreate() failed");
+		return ErrorCode::FUNCTION_FAILED;
+	}
+
+	return err;
+}
 
 
 } // end namespace anki
 } // end namespace anki

+ 1 - 1
src/renderer/MainRenderer.cpp

@@ -94,7 +94,7 @@ Error MainRenderer::render(SceneGraph& scene)
 			rt = &getIs()._getRt();
 			rt = &getIs()._getRt();
 		}
 		}
 
 
-		//rt = &getMs()._getRt0();
+		//rt = &getPps().getSsao()._getRt();
 		//rt = &getIs()._getRt();
 		//rt = &getIs()._getRt();
 
 
 		rt->setFilter(lastJobs, GlTextureHandle::Filter::LINEAR);
 		rt->setFilter(lastJobs, GlTextureHandle::Filter::LINEAR);

+ 1 - 1
src/renderer/Pps.cpp

@@ -159,7 +159,7 @@ Error Pps::run(GlCommandBufferHandle& cmdBuff)
 
 
 	if(m_ssao.getEnabled())
 	if(m_ssao.getEnabled())
 	{
 	{
-		m_ssao.getRt().bind(cmdBuff, 1);
+		m_ssao._getRt().bind(cmdBuff, 1);
 	}
 	}
 
 
 	if(m_lf.getEnabled())
 	if(m_lf.getEnabled())

+ 1 - 1
testapp/Main.cpp

@@ -229,7 +229,7 @@ Error init()
 	{
 	{
 		ScriptResourcePointer script;
 		ScriptResourcePointer script;
 
 
-		err = script.load("maps/sponza/scene.lua", &resources);
+		err = script.load("maps/adis/scene.lua", &resources);
 		if(err) return err;
 		if(err) return err;
 
 
 		err = app->getScriptManager().evalString(script->getSource());
 		err = app->getScriptManager().evalString(script->getSource());