From 8a956318e56f3848e295192c3142ac8deec5399a Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Wed, 28 Oct 2015 16:02:55 -0700 Subject: [PATCH 1/5] fix case sensitive issue --- examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp | 2 +- test/SharedMemory/premake4.lua | 10 +++++----- test/SharedMemory/test.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp b/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp index 10bd55e39..cfedc3a9a 100644 --- a/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp +++ b/examples/Importers/ImportURDFDemo/ImportURDFSetup.cpp @@ -367,7 +367,7 @@ void ImportUrdfSetup::initPhysics() m_guiHelper->createCollisionShapeGraphicsObject(box); btTransform start; start.setIdentity(); btVector3 groundOrigin(0,0,0); - groundOrigin[upAxis]=-2;//.5; + groundOrigin[upAxis]=-2.5; start.setOrigin(groundOrigin); btRigidBody* body = createRigidBody(0,start,box); //m_dynamicsWorld->removeRigidBody(body); diff --git a/test/SharedMemory/premake4.lua b/test/SharedMemory/premake4.lua index 1eeb98178..9ef8f6c93 100644 --- a/test/SharedMemory/premake4.lua +++ b/test/SharedMemory/premake4.lua @@ -49,10 +49,10 @@ project ("Test_PhysicsLoopBack") "../../examples/SharedMemory/PhysicsClient.h", "../../examples/SharedMemory/PhysicsServer.cpp", "../../examples/SharedMemory/PhysicsServer.h", - "../../examples/SharedMemory/PhysicsLoopback.cpp", - "../../examples/SharedMemory/PhysicsLoopback.h", - "../../examples/SharedMemory/PhysicsLoopbackC_Api.cpp", - "../../examples/SharedMemory/PhysicsLoopbackC_Api.h", + "../../examples/SharedMemory/PhysicsLoopBack.cpp", + "../../examples/SharedMemory/PhysicsLoopBack.h", + "../../examples/SharedMemory/PhysicsLoopBackC_Api.cpp", + "../../examples/SharedMemory/PhysicsLoopBackC_Api.h", "../../examples/SharedMemory/PhysicsClientSharedMemory.cpp", "../../examples/SharedMemory/PhysicsClientSharedMemory.h", "../../examples/SharedMemory/PhysicsClientC_API.cpp", @@ -78,4 +78,4 @@ project ("Test_PhysicsLoopBack") "../../examples/Importers/ImportURDFDemo/UrdfParser.cpp", "../../examples/Importers/ImportURDFDemo/urdfStringSplit.cpp", } - \ No newline at end of file + diff --git a/test/SharedMemory/test.c b/test/SharedMemory/test.c index d3939583f..c7347ae00 100644 --- a/test/SharedMemory/test.c +++ b/test/SharedMemory/test.c @@ -2,7 +2,7 @@ #include "PhysicsClientC_API.h" #ifdef PHYSICS_LOOP_BACK -#include "PhysicsLoopbackC_API.h" +#include "PhysicsLoopBackC_API.h" #endif //PHYSICS_LOOP_BACK #include "SharedMemoryPublic.h" From 650189d50bf18944f451a7e75b9b30b24e637acf Mon Sep 17 00:00:00 2001 From: erwincoumans Date: Thu, 29 Oct 2015 08:28:49 -0700 Subject: [PATCH 2/5] Allow both remote debug drawer and local debug drawer for PhysicsServer (shared memory etc) --- examples/SharedMemory/PhysicsServer.cpp | 71 +++++++++++++++---------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/examples/SharedMemory/PhysicsServer.cpp b/examples/SharedMemory/PhysicsServer.cpp index 63bc65275..b93ca04b3 100644 --- a/examples/SharedMemory/PhysicsServer.cpp +++ b/examples/SharedMemory/PhysicsServer.cpp @@ -217,8 +217,8 @@ struct PhysicsServerInternalData btMultiBodyConstraintSolver* m_solver; btDefaultCollisionConfiguration* m_collisionConfiguration; btMultiBodyDynamicsWorld* m_dynamicsWorld; - SharedMemoryDebugDrawer* m_debugDrawer; - + SharedMemoryDebugDrawer* m_remoteDebugDrawer; + @@ -243,7 +243,7 @@ struct PhysicsServerInternalData m_isConnected(false), m_physicsDeltaTime(1./240.), m_dynamicsWorld(0), - m_debugDrawer(0), + m_remoteDebugDrawer(0), m_guiHelper(0), m_sharedMemoryKey(SHARED_MEMORY_KEY), m_verboseOutput(false), @@ -356,8 +356,8 @@ void PhysicsServerSharedMemory::createEmptyDynamicsWorld() m_data->m_dynamicsWorld = new btMultiBodyDynamicsWorld(m_data->m_dispatcher, m_data->m_broadphase, m_data->m_solver, m_data->m_collisionConfiguration); - m_data->m_debugDrawer = new SharedMemoryDebugDrawer(); - m_data->m_dynamicsWorld->setDebugDrawer(m_data->m_debugDrawer); + m_data->m_remoteDebugDrawer = new SharedMemoryDebugDrawer(); + m_data->m_dynamicsWorld->setGravity(btVector3(0, 0, 0)); } @@ -424,8 +424,8 @@ void PhysicsServerSharedMemory::deleteDynamicsWorld() delete m_data->m_dynamicsWorld; m_data->m_dynamicsWorld=0; - delete m_data->m_debugDrawer; - m_data->m_debugDrawer=0; + delete m_data->m_remoteDebugDrawer; + m_data->m_remoteDebugDrawer =0; delete m_data->m_solver; m_data->m_solver=0; @@ -445,6 +445,11 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* { m_data->m_guiHelper = guiHelper; + if (m_data->m_guiHelper) + { + m_data->m_guiHelper->createPhysicsDebugDrawer(m_data->m_dynamicsWorld); + } + bool allowCreation = true; bool allowConnectToExistingSharedMemory = false; @@ -490,6 +495,13 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* void PhysicsServerSharedMemory::disconnectSharedMemory(bool deInitializeSharedMemory) { + if (m_data->m_guiHelper) + { + if (m_data->m_guiHelper && m_data->m_dynamicsWorld && m_data->m_dynamicsWorld->getDebugDrawer()) + { + m_data->m_dynamicsWorld->setDebugDrawer(0); + } + } if (m_data->m_verboseOutput) { b3Printf("releaseSharedMemory1\n"); @@ -717,6 +729,7 @@ void PhysicsServerSharedMemory::processClientCommands() const SharedMemoryCommand& clientCmd =m_data->m_testBlock1->m_clientCommands[0]; m_data->m_testBlock1->m_numProcessedClientCommands++; + //no timestamp yet int timeStamp = 0; @@ -749,7 +762,7 @@ void PhysicsServerSharedMemory::processClientCommands() } case CMD_REQUEST_DEBUG_LINES: { - int curFlags =m_data->m_debugDrawer->getDebugMode(); + int curFlags =m_data->m_remoteDebugDrawer->getDebugMode(); int debugMode = clientCmd.m_requestDebugLinesArguments.m_debugMode;//clientCmd.btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb; int startingLineIndex = clientCmd.m_requestDebugLinesArguments.m_startingLineIndex; @@ -761,23 +774,26 @@ void PhysicsServerSharedMemory::processClientCommands() if (clientCmd.m_requestDebugLinesArguments.m_startingLineIndex==0) { - m_data->m_debugDrawer->m_lines2.resize(0); + m_data->m_remoteDebugDrawer->m_lines2.resize(0); //|btIDebugDraw::DBG_DrawAabb| // btIDebugDraw::DBG_DrawConstraints |btIDebugDraw::DBG_DrawConstraintLimits ; - m_data->m_debugDrawer->setDebugMode(debugMode); + m_data->m_remoteDebugDrawer->setDebugMode(debugMode); + btIDebugDraw* oldDebugDrawer = m_data->m_dynamicsWorld->getDebugDrawer(); + m_data->m_dynamicsWorld->setDebugDrawer(m_data->m_remoteDebugDrawer); m_data->m_dynamicsWorld->debugDrawWorld(); - m_data->m_debugDrawer->setDebugMode(curFlags); + m_data->m_dynamicsWorld->setDebugDrawer(oldDebugDrawer); + m_data->m_remoteDebugDrawer->setDebugMode(curFlags); } //9 floats per line: 3 floats for 'from', 3 floats for 'to' and 3 floats for 'color' int maxNumLines = SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE/(sizeof(float)*9)-1; - if (startingLineIndex >m_data->m_debugDrawer->m_lines2.size()) + if (startingLineIndex >m_data->m_remoteDebugDrawer->m_lines2.size()) { b3Warning("m_startingLineIndex exceeds total number of debug lines"); - startingLineIndex =m_data->m_debugDrawer->m_lines2.size(); + startingLineIndex =m_data->m_remoteDebugDrawer->m_lines2.size(); } - int numLines = btMin(maxNumLines,m_data->m_debugDrawer->m_lines2.size()-startingLineIndex); + int numLines = btMin(maxNumLines,m_data->m_remoteDebugDrawer->m_lines2.size()-startingLineIndex); if (numLines) { @@ -788,24 +804,24 @@ void PhysicsServerSharedMemory::processClientCommands() for (int i=0;im_debugDrawer->m_lines2[i+startingLineIndex].m_from.x(); - linesTo[i*3] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_to.x(); - linesColor[i*3] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_color.x(); + linesFrom[i*3] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_from.x(); + linesTo[i*3] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_to.x(); + linesColor[i*3] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_color.x(); - linesFrom[i*3+1] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_from.y(); - linesTo[i*3+1] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_to.y(); - linesColor[i*3+1] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_color.y(); + linesFrom[i*3+1] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_from.y(); + linesTo[i*3+1] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_to.y(); + linesColor[i*3+1] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_color.y(); - linesFrom[i*3+2] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_from.z(); - linesTo[i*3+2] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_to.z(); - linesColor[i*3+2] = m_data->m_debugDrawer->m_lines2[i+startingLineIndex].m_color.z(); + linesFrom[i*3+2] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_from.z(); + linesTo[i*3+2] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_to.z(); + linesColor[i*3+2] = m_data->m_remoteDebugDrawer->m_lines2[i+startingLineIndex].m_color.z(); } } SharedMemoryStatus& status = m_data->createServerStatus(CMD_DEBUG_LINES_COMPLETED,clientCmd.m_sequenceNumber,timeStamp); status.m_sendDebugLinesArgs.m_numDebugLines = numLines; status.m_sendDebugLinesArgs.m_startingLineIndex = startingLineIndex; - status.m_sendDebugLinesArgs.m_numRemainingDebugLines = m_data->m_debugDrawer->m_lines2.size()-(startingLineIndex+numLines); + status.m_sendDebugLinesArgs.m_numRemainingDebugLines = m_data->m_remoteDebugDrawer->m_lines2.size()-(startingLineIndex+numLines); m_data->submitServerStatus(status); break; @@ -1533,17 +1549,14 @@ void PhysicsServerSharedMemory::renderScene() void PhysicsServerSharedMemory::physicsDebugDraw(int debugDrawFlags) { -#if 0 if (m_data->m_dynamicsWorld) { if (m_data->m_dynamicsWorld->getDebugDrawer()) { - //m_data->m_debugDrawer->m_lines.clear(); - //m_data->m_dynamicsWorld->getDebugDrawer()->setDebugMode(debugDrawFlags); + m_data->m_dynamicsWorld->getDebugDrawer()->setDebugMode(debugDrawFlags); + m_data->m_dynamicsWorld->debugDrawWorld(); } - m_data->m_dynamicsWorld->debugDrawWorld(); } -#endif } From 9442791cf1791cbdba916452ba391f5b1c15b38f Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Thu, 29 Oct 2015 11:18:26 -0700 Subject: [PATCH 3/5] Fix PhysicsServer connection issue, in cases where shared memory was not properly shut down. --- examples/SharedMemory/PhysicsServer.cpp | 51 +++++++++++++++---------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/examples/SharedMemory/PhysicsServer.cpp b/examples/SharedMemory/PhysicsServer.cpp index b93ca04b3..9dfc8e413 100644 --- a/examples/SharedMemory/PhysicsServer.cpp +++ b/examples/SharedMemory/PhysicsServer.cpp @@ -460,34 +460,43 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* } - m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE,allowCreation); - if (m_data->m_testBlock1) - { - int magicId =m_data->m_testBlock1->m_magicId; - if (m_data->m_verboseOutput) + int counter = 0; + do + { + + m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE,allowCreation); + if (m_data->m_testBlock1) { - b3Printf("magicId = %d\n", magicId); - } - - if (m_data->m_testBlock1->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER) - { - InitSharedMemoryBlock(m_data->m_testBlock1); + int magicId =m_data->m_testBlock1->m_magicId; if (m_data->m_verboseOutput) { - b3Printf("Created and initialized shared memory block\n"); + b3Printf("magicId = %d\n", magicId); } - m_data->m_isConnected = true; - } else + + if (m_data->m_testBlock1->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER) + { + InitSharedMemoryBlock(m_data->m_testBlock1); + if (m_data->m_verboseOutput) + { + b3Printf("Created and initialized shared memory block\n"); + } + m_data->m_isConnected = true; + } else + { + m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE); + m_data->m_testBlock1 = 0; + m_data->m_isConnected = false; + } + } else { - b3Error("Server cannot connect to existing shared memory, disconnecting shared memory.\n"); - m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE); - m_data->m_testBlock1 = 0; - m_data->m_isConnected = false; + b3Error("Cannot connect to shared memory"); + m_data->m_isConnected = false; } - } else + } while (counter++ < 10 && !m_data->m_isConnected); + + if (!m_data->m_isConnected) { - b3Error("Cannot connect to shared memory"); - m_data->m_isConnected = false; + b3Error("Server cannot connect to shared memory.\n"); } return m_data->m_isConnected; } From f7bbbd88e605a813a43eadc520494c2bc7455b08 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Thu, 29 Oct 2015 11:25:50 -0700 Subject: [PATCH 4/5] 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); From c68c215eadfbfc44903e02432ce5479244c0030d Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Fri, 30 Oct 2015 10:30:48 -0700 Subject: [PATCH 5/5] add rudimentary command logging for shared memory physics server --- examples/ExampleBrowser/ExampleEntries.cpp | 5 + .../ExampleBrowser/OpenGLExampleBrowser.cpp | 4 +- examples/SharedMemory/PhysicsServer.cpp | 123 +++++++++++++++++- examples/SharedMemory/PhysicsServer.h | 2 + .../SharedMemory/PhysicsServerExample.cpp | 11 ++ examples/SharedMemory/PhysicsServerExample.h | 1 + examples/SharedMemory/SharedMemoryBlock.h | 2 +- examples/SharedMemory/SharedMemoryCommands.h | 2 +- 8 files changed, 146 insertions(+), 4 deletions(-) diff --git a/examples/ExampleBrowser/ExampleEntries.cpp b/examples/ExampleBrowser/ExampleEntries.cpp index 17b920fa9..e6f84bab3 100644 --- a/examples/ExampleBrowser/ExampleEntries.cpp +++ b/examples/ExampleBrowser/ExampleEntries.cpp @@ -215,6 +215,11 @@ static ExampleEntry gDefaultExamples[]= ExampleEntry(1,"Physics Server", "Create a physics server that communicates with a physics client over shared memory", PhysicsServerCreateFunc), + ExampleEntry(1,"Physics Server (Logging)", "Create a physics server that communicates with a physics client over shared memory. It will log all commands to a file.", + PhysicsServerCreateFunc,PHYSICS_SERVER_ENABLE_COMMAND_LOGGING), + ExampleEntry(1,"Physics Server (Replay Log)", "Create a physics server that replay a command log from disk.", + PhysicsServerCreateFunc,PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG), + ExampleEntry(1, "Physics Client", "Create a physics client that can communicate with a physics server over shared memory", PhysicsClientCreateFunc), #ifdef ENABLE_LUA ExampleEntry(1,"Lua Script", "Create the dynamics world, collision shapes and rigid bodies using Lua scripting", diff --git a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp index 66b64e99e..bc3356380 100644 --- a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp +++ b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp @@ -71,6 +71,7 @@ extern bool useShadowMap; static bool visualWireframe=false; static bool renderVisualGeometry=true; static bool renderGrid = true; +static bool renderGui = true; static bool enable_experimental_opencl = false; int gDebugDrawFlags = 0; @@ -170,6 +171,7 @@ void MyKeyboardCallback(int key, int state) if (key=='g' && state) { renderGrid = !renderGrid; + renderGui = !renderGui; } @@ -1041,7 +1043,7 @@ void OpenGLExampleBrowser::update(float deltaTime) } static int toggle = 1; - if (renderGrid) + if (renderGui) { if (!pauseSimulation) processProfileData(s_profWindow,false); diff --git a/examples/SharedMemory/PhysicsServer.cpp b/examples/SharedMemory/PhysicsServer.cpp index 154249c39..7218f0ae3 100644 --- a/examples/SharedMemory/PhysicsServer.cpp +++ b/examples/SharedMemory/PhysicsServer.cpp @@ -108,18 +108,82 @@ struct InternalBodyHandle : public InteralBodyData return m_nextFreeHandle; } }; + +class btCommandChunk +{ +public: + int m_chunkCode; + int m_length; + void *m_oldPtr; + int m_dna_nr; + int m_number; +}; + struct CommandLogger { FILE* m_file; + + void writeHeader(unsigned char* buffer) const + { +#ifdef BT_USE_DOUBLE_PRECISION + memcpy(buffer, "BT3CMDd", 7); +#else + memcpy(buffer, "BT3CMDf", 7); +#endif //BT_USE_DOUBLE_PRECISION + + int littleEndian= 1; + littleEndian= ((char*)&littleEndian)[0]; + + if (sizeof(void*)==8) + { + buffer[7] = '-'; + } else + { + buffer[7] = '_'; + } + + if (littleEndian) + { + buffer[8]='v'; + } else + { + buffer[8]='V'; + } + + buffer[9] = 0; + buffer[10] = 0; + buffer[11] = 0; + + int ver = btGetVersion(); + if (ver>=0 && ver<999) + { + sprintf((char*)&buffer[9],"%d",ver); + } + + } + void logCommand(SharedMemoryBlock* testBlock1) { - //fwrite(buf,buffSize+sizeof(int),1,m_file); + btCommandChunk chunk; + chunk.m_chunkCode = testBlock1->m_clientCommands[0].m_type; + chunk.m_oldPtr = 0; + chunk.m_dna_nr = 0; + chunk.m_length = sizeof(SharedMemoryCommand); + chunk.m_number = 1; + fwrite((const char*)&chunk,sizeof(btCommandChunk), 1,m_file); + fwrite((const char*)&testBlock1->m_clientCommands[0],sizeof(SharedMemoryCommand),1,m_file); } CommandLogger(const char* fileName) { m_file = fopen(fileName,"wb"); + unsigned char buf[15]; + buf[12] = 12; + buf[13] = 13; + buf[14] = 14; + writeHeader(buf); + fwrite(buf,12,1,m_file); } virtual ~CommandLogger() { @@ -127,6 +191,41 @@ struct CommandLogger } }; + +struct CommandLogPlayback +{ + unsigned char* m_header[12]; + FILE* m_file; + + CommandLogPlayback(const char* fileName) + { + m_file = fopen(fileName,"rb"); + if (m_file) + { + fread(m_header,12,1,m_file); + } + } + virtual ~CommandLogPlayback() + { + if (m_file) + { + fclose(m_file); + m_file=0; + } + } + bool processNextCommand(SharedMemoryCommand* cmd) + { + btCommandChunk chunk; + size_t s = fread((void*)&chunk,sizeof(btCommandChunk),1,m_file); + if (s==1) + { + s = fread(cmd,sizeof(SharedMemoryCommand),1,m_file); + return (s==1); + } + return false; + } +}; + struct PhysicsServerInternalData { ///handle management @@ -219,6 +318,8 @@ struct PhysicsServerInternalData SharedMemoryInterface* m_sharedMemory; SharedMemoryBlock* m_testBlock1; CommandLogger* m_commandLogger; + CommandLogPlayback* m_logPlayback; + bool m_isConnected; btScalar m_physicsDeltaTime; btAlignedObjectArray m_multiBodyJointFeedbacks; @@ -260,6 +361,7 @@ struct PhysicsServerInternalData :m_sharedMemory(0), m_testBlock1(0), m_commandLogger(0), + m_logPlayback(0), m_isConnected(false), m_physicsDeltaTime(1./240.), m_dynamicsWorld(0), @@ -756,6 +858,19 @@ void PhysicsServerSharedMemory::processClientCommands() { if (m_data->m_isConnected && m_data->m_testBlock1) { + if (m_data->m_logPlayback) + { + if (m_data->m_testBlock1->m_numServerCommands>m_data->m_testBlock1->m_numProcessedServerCommands) + { + m_data->m_testBlock1->m_numProcessedServerCommands++; + } + //push a command from log file + bool hasCommand = m_data->m_logPlayback->processNextCommand(&m_data->m_testBlock1->m_clientCommands[0]); + if (hasCommand) + { + m_data->m_testBlock1->m_numClientCommands++; + } + } ///we ignore overflow of integer for now if (m_data->m_testBlock1->m_numClientCommands> m_data->m_testBlock1->m_numProcessedClientCommands) { @@ -1738,3 +1853,9 @@ void PhysicsServerSharedMemory::enableCommandLogging(bool enable, const char* fi } } } + +void PhysicsServerSharedMemory::replayFromLogFile(const char* fileName) +{ + CommandLogPlayback* pb = new CommandLogPlayback(fileName); + m_data->m_logPlayback = pb; +} \ No newline at end of file diff --git a/examples/SharedMemory/PhysicsServer.h b/examples/SharedMemory/PhysicsServer.h index e3980792b..d5730aad4 100644 --- a/examples/SharedMemory/PhysicsServer.h +++ b/examples/SharedMemory/PhysicsServer.h @@ -55,6 +55,8 @@ public: void renderScene(); void enableCommandLogging(bool enable, const char* fileName); + void replayFromLogFile(const char* fileName); + }; diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index 9cf3b2049..2d9fed993 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -33,6 +33,13 @@ public: { m_physicsServer.enableCommandLogging(true,"BulletPhysicsCommandLog.bin"); } + + void replayFromLogFile() + { + m_physicsServer.replayFromLogFile("BulletPhysicsCommandLog.bin"); + } + + virtual void resetCamera() { @@ -273,6 +280,10 @@ class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOpt { example->enableCommandLogging(); } + if (options.m_option & PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG) + { + example->replayFromLogFile(); + } return example; } diff --git a/examples/SharedMemory/PhysicsServerExample.h b/examples/SharedMemory/PhysicsServerExample.h index fbd65b43b..e88074230 100644 --- a/examples/SharedMemory/PhysicsServerExample.h +++ b/examples/SharedMemory/PhysicsServerExample.h @@ -4,6 +4,7 @@ enum PhysicsServerOptions { PHYSICS_SERVER_ENABLE_COMMAND_LOGGING=1, + PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG=2, }; class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options); diff --git a/examples/SharedMemory/SharedMemoryBlock.h b/examples/SharedMemory/SharedMemoryBlock.h index 36cd60f35..fa80c36c9 100644 --- a/examples/SharedMemory/SharedMemoryBlock.h +++ b/examples/SharedMemory/SharedMemoryBlock.h @@ -2,7 +2,7 @@ #define SHARED_MEMORY_BLOCK_H #define SHARED_MEMORY_MAGIC_NUMBER 64738 -#define SHARED_MEMORY_MAX_COMMANDS 32 +#define SHARED_MEMORY_MAX_COMMANDS 4 #define SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE (256*1024) #include "SharedMemoryCommands.h" diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index 6016a3ba1..11a99917e 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -26,7 +26,7 @@ #define SHARED_MEMORY_SERVER_TEST_C -#define MAX_DEGREE_OF_FREEDOM 256 +#define MAX_DEGREE_OF_FREEDOM 64 #define MAX_NUM_SENSORS 256 #define MAX_URDF_FILENAME_LENGTH 1024 #define MAX_FILENAME_LENGTH MAX_URDF_FILENAME_LENGTH