From f7bbbd88e605a813a43eadc520494c2bc7455b08 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Thu, 29 Oct 2015 11:25:50 -0700 Subject: [PATCH] preparation for CommandLogging for PhysicsServer. --- .../ExampleBrowser/OpenGLExampleBrowser.cpp | 2 +- examples/SharedMemory/PhysicsServer.cpp | 55 ++++++++++++++++++- examples/SharedMemory/PhysicsServer.h | 2 + .../SharedMemory/PhysicsServerExample.cpp | 33 +++++++---- examples/SharedMemory/PhysicsServerExample.h | 4 ++ 5 files changed, 81 insertions(+), 15 deletions(-) diff --git a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp index 87e235c96..66b64e99e 100644 --- a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp +++ b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp @@ -1041,7 +1041,7 @@ void OpenGLExampleBrowser::update(float deltaTime) } static int toggle = 1; - if (1) + if (renderGrid) { if (!pauseSimulation) processProfileData(s_profWindow,false); diff --git a/examples/SharedMemory/PhysicsServer.cpp b/examples/SharedMemory/PhysicsServer.cpp index b93ca04b3..9f3948da3 100644 --- a/examples/SharedMemory/PhysicsServer.cpp +++ b/examples/SharedMemory/PhysicsServer.cpp @@ -108,6 +108,24 @@ struct InternalBodyHandle : public InteralBodyData return m_nextFreeHandle; } }; +struct CommandLogger +{ + FILE* m_file; + + void logCommand(SharedMemoryBlock* testBlock1) + { + //fwrite(buf,buffSize+sizeof(int),1,m_file); + } + + CommandLogger(const char* fileName) + { + m_file = fopen(fileName,"wb"); + } + virtual ~CommandLogger() + { + fclose(m_file); + } +}; struct PhysicsServerInternalData { @@ -200,6 +218,7 @@ struct PhysicsServerInternalData SharedMemoryInterface* m_sharedMemory; SharedMemoryBlock* m_testBlock1; + CommandLogger* m_commandLogger; bool m_isConnected; btScalar m_physicsDeltaTime; btAlignedObjectArray m_multiBodyJointFeedbacks; @@ -240,6 +259,7 @@ struct PhysicsServerInternalData PhysicsServerInternalData() :m_sharedMemory(0), m_testBlock1(0), + m_commandLogger(0), m_isConnected(false), m_physicsDeltaTime(1./240.), m_dynamicsWorld(0), @@ -332,6 +352,12 @@ PhysicsServerSharedMemory::PhysicsServerSharedMemory() PhysicsServerSharedMemory::~PhysicsServerSharedMemory() { deleteDynamicsWorld(); + if (m_data->m_commandLogger) + { + delete m_data->m_commandLogger; + m_data->m_commandLogger = 0; + } + delete m_data; } @@ -451,7 +477,7 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* } bool allowCreation = true; - bool allowConnectToExistingSharedMemory = false; + if (m_data->m_isConnected) { @@ -489,6 +515,7 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* b3Error("Cannot connect to shared memory"); m_data->m_isConnected = false; } + return m_data->m_isConnected; } @@ -723,11 +750,17 @@ void PhysicsServerSharedMemory::processClientCommands() ///we ignore overflow of integer for now if (m_data->m_testBlock1->m_numClientCommands> m_data->m_testBlock1->m_numProcessedClientCommands) { - + + //until we implement a proper ring buffer, we assume always maximum of 1 outstanding commands btAssert(m_data->m_testBlock1->m_numClientCommands==m_data->m_testBlock1->m_numProcessedClientCommands+1); const SharedMemoryCommand& clientCmd =m_data->m_testBlock1->m_clientCommands[0]; + if (m_data->m_commandLogger) + { + m_data->m_commandLogger->logCommand(m_data->m_testBlock1); + } + m_data->m_testBlock1->m_numProcessedClientCommands++; //no timestamp yet @@ -1678,3 +1711,21 @@ void PhysicsServerSharedMemory::removePickingConstraint() m_data->m_pickingMultiBodyPoint2Point = 0; } } + +void PhysicsServerSharedMemory::enableCommandLogging(bool enable, const char* fileName) +{ + if (enable) + { + if (0==m_data->m_commandLogger) + { + m_data->m_commandLogger = new CommandLogger(fileName); + } + } else + { + if (0!=m_data->m_commandLogger) + { + delete m_data->m_commandLogger; + m_data->m_commandLogger = 0; + } + } +} diff --git a/examples/SharedMemory/PhysicsServer.h b/examples/SharedMemory/PhysicsServer.h index 96011606a..e3980792b 100644 --- a/examples/SharedMemory/PhysicsServer.h +++ b/examples/SharedMemory/PhysicsServer.h @@ -54,6 +54,8 @@ public: void physicsDebugDraw(int debugDrawFlags); void renderScene(); + void enableCommandLogging(bool enable, const char* fileName); + }; diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index a2239221b..9cf3b2049 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -29,7 +29,10 @@ public: virtual void stepSimulation(float deltaTime); - + void enableCommandLogging() + { + m_physicsServer.enableCommandLogging(true,"BulletPhysicsCommandLog.bin"); + } virtual void resetCamera() { @@ -190,18 +193,7 @@ void PhysicsServerExample::physicsDebugDraw(int debugDrawFlags) } -extern int gSharedMemoryKey; -class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options) -{ - PhysicsServerExample* example = new PhysicsServerExample(options.m_guiHelper); - if (gSharedMemoryKey>=0) - { - example->setSharedMemoryKey(gSharedMemoryKey); - } - return example; - -} btVector3 PhysicsServerExample::getRayTo(int x,int y) { @@ -267,3 +259,20 @@ btVector3 PhysicsServerExample::getRayTo(int x,int y) return rayTo; } + +extern int gSharedMemoryKey; + +class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options) +{ + PhysicsServerExample* example = new PhysicsServerExample(options.m_guiHelper); + if (gSharedMemoryKey>=0) + { + example->setSharedMemoryKey(gSharedMemoryKey); + } + if (options.m_option & PHYSICS_SERVER_ENABLE_COMMAND_LOGGING) + { + example->enableCommandLogging(); + } + return example; + +} diff --git a/examples/SharedMemory/PhysicsServerExample.h b/examples/SharedMemory/PhysicsServerExample.h index 15e393db8..fbd65b43b 100644 --- a/examples/SharedMemory/PhysicsServerExample.h +++ b/examples/SharedMemory/PhysicsServerExample.h @@ -1,6 +1,10 @@ #ifndef PHYSICS_SERVER_EXAMPLE_H #define PHYSICS_SERVER_EXAMPLE_H +enum PhysicsServerOptions +{ + PHYSICS_SERVER_ENABLE_COMMAND_LOGGING=1, +}; class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options);