2016-03-07 22:56:16 +00:00
|
|
|
|
|
|
|
#include "SharedMemoryInProcessPhysicsC_API.h"
|
2017-02-07 16:08:55 +00:00
|
|
|
#include "../Utils/b3Clock.h"
|
2016-04-14 15:51:20 +00:00
|
|
|
|
2016-03-07 22:56:16 +00:00
|
|
|
#include "PhysicsClientSharedMemory.h"
|
2018-09-23 21:17:31 +00:00
|
|
|
#include "../ExampleBrowser/InProcessExampleBrowser.h"
|
2017-07-14 22:12:16 +00:00
|
|
|
#include <stdio.h>
|
2017-08-15 00:02:20 +00:00
|
|
|
#include <string.h>
|
2017-05-31 02:54:55 +00:00
|
|
|
#include "PhysicsServerExampleBullet2.h"
|
2018-05-07 22:57:36 +00:00
|
|
|
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
|
2017-05-13 18:15:20 +00:00
|
|
|
#include "../CommonInterfaces/CommonExampleInterface.h"
|
|
|
|
#include "InProcessMemory.h"
|
2016-04-14 15:51:20 +00:00
|
|
|
|
2017-03-26 20:06:46 +00:00
|
|
|
#include "Bullet3Common/b3Logging.h"
|
2016-04-14 15:51:20 +00:00
|
|
|
class InProcessPhysicsClientSharedMemoryMainThread : public PhysicsClientSharedMemory
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btInProcessExampleBrowserMainThreadInternalData* m_data;
|
|
|
|
b3Clock m_clock;
|
|
|
|
|
2016-04-14 15:51:20 +00:00
|
|
|
public:
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientSharedMemoryMainThread(int argc, char* argv[], bool useInProcessMemory)
|
|
|
|
{
|
|
|
|
int newargc = argc + 3;
|
|
|
|
char** newargv = (char**)malloc(sizeof(void*) * newargc);
|
2018-02-15 00:49:44 +00:00
|
|
|
char* t0 = (char*)"--unused";
|
2018-09-23 21:17:31 +00:00
|
|
|
newargv[0] = t0;
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
newargv[i + 1] = argv[i];
|
|
|
|
newargv[argc + 1] = (char*)"--logtostderr";
|
|
|
|
newargv[argc + 2] = (char*)"--start_demo_name=Physics Server";
|
|
|
|
|
|
|
|
m_data = btCreateInProcessExampleBrowserMainThread(newargc, newargv, useInProcessMemory);
|
|
|
|
SharedMemoryInterface* shMem = btGetSharedMemoryInterfaceMainThread(m_data);
|
|
|
|
|
|
|
|
setSharedMemoryInterface(shMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~InProcessPhysicsClientSharedMemoryMainThread()
|
|
|
|
{
|
|
|
|
setSharedMemoryInterface(0);
|
|
|
|
btShutDownExampleBrowserMainThread(m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return non-null if there is a status, nullptr otherwise
|
|
|
|
virtual const struct SharedMemoryStatus* processServerStatus()
|
|
|
|
{
|
2017-03-26 20:06:46 +00:00
|
|
|
{
|
|
|
|
if (btIsExampleBrowserMainThreadTerminated(m_data))
|
|
|
|
{
|
|
|
|
PhysicsClientSharedMemory::disconnectSharedMemory();
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
{
|
|
|
|
unsigned long int ms = m_clock.getTimeMilliseconds();
|
|
|
|
if (ms > 2)
|
|
|
|
{
|
2017-08-12 00:14:52 +00:00
|
|
|
B3_PROFILE("m_clock.reset()");
|
2017-03-26 20:06:46 +00:00
|
|
|
|
2017-08-12 00:14:52 +00:00
|
|
|
btUpdateInProcessExampleBrowserMainThread(m_data);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_clock.reset();
|
2017-03-26 20:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
b3Clock::usleep(0);
|
2017-02-16 22:19:09 +00:00
|
|
|
}
|
2017-03-26 20:06:46 +00:00
|
|
|
const SharedMemoryStatus* stat = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
stat = PhysicsClientSharedMemory::processServerStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool submitClientCommand(const struct SharedMemoryCommand& command)
|
|
|
|
{
|
|
|
|
// btUpdateInProcessExampleBrowserMainThread(m_data);
|
|
|
|
return PhysicsClientSharedMemory::submitClientCommand(command);
|
|
|
|
}
|
2016-04-14 15:51:20 +00:00
|
|
|
};
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2017-09-21 06:18:18 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThread(int argc, char* argv[])
|
2016-04-14 15:51:20 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientSharedMemoryMainThread* cl = new InProcessPhysicsClientSharedMemoryMainThread(argc, argv, 1);
|
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY + 1);
|
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle)cl;
|
2016-04-14 15:51:20 +00:00
|
|
|
}
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThreadSharedMemory(int argc, char* argv[])
|
2017-08-16 19:33:25 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientSharedMemoryMainThread* cl = new InProcessPhysicsClientSharedMemoryMainThread(argc, argv, 0);
|
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY + 1);
|
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle)cl;
|
2017-08-16 19:33:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 22:56:16 +00:00
|
|
|
class InProcessPhysicsClientSharedMemory : public PhysicsClientSharedMemory
|
|
|
|
{
|
|
|
|
btInProcessExampleBrowserInternalData* m_data;
|
2017-06-05 05:04:16 +00:00
|
|
|
char** m_newargv;
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2017-06-05 05:04:16 +00:00
|
|
|
public:
|
2017-08-15 00:02:20 +00:00
|
|
|
InProcessPhysicsClientSharedMemory(int argc, char* argv[], bool useInProcessMemory)
|
2016-03-07 22:56:16 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
int newargc = argc + 2;
|
|
|
|
m_newargv = (char**)malloc(sizeof(void*) * newargc);
|
2017-12-28 18:05:51 +00:00
|
|
|
char* t0 = (char*)"--unused";
|
|
|
|
m_newargv[0] = t0;
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
m_newargv[i + 1] = argv[i];
|
|
|
|
|
2016-03-07 22:56:16 +00:00
|
|
|
char* t1 = (char*)"--start_demo_name=Physics Server";
|
2018-09-23 21:17:31 +00:00
|
|
|
m_newargv[argc + 1] = t1;
|
|
|
|
m_data = btCreateInProcessExampleBrowser(newargc, m_newargv, useInProcessMemory);
|
2016-03-10 22:36:46 +00:00
|
|
|
SharedMemoryInterface* shMem = btGetSharedMemoryInterface(m_data);
|
|
|
|
setSharedMemoryInterface(shMem);
|
2016-03-07 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~InProcessPhysicsClientSharedMemory()
|
|
|
|
{
|
2016-03-10 22:36:46 +00:00
|
|
|
setSharedMemoryInterface(0);
|
2016-03-07 22:56:16 +00:00
|
|
|
btShutDownExampleBrowser(m_data);
|
2017-06-05 05:04:16 +00:00
|
|
|
free(m_newargv);
|
2016-03-07 22:56:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnect(int argc, char* argv[])
|
|
|
|
{
|
2017-08-16 19:33:25 +00:00
|
|
|
InProcessPhysicsClientSharedMemory* cl = new InProcessPhysicsClientSharedMemory(argc, argv, 1);
|
2018-09-23 21:17:31 +00:00
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY + 1);
|
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle)cl;
|
2017-08-16 19:33:25 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectSharedMemory(int argc, char* argv[])
|
|
|
|
{
|
2017-08-16 19:33:25 +00:00
|
|
|
InProcessPhysicsClientSharedMemory* cl = new InProcessPhysicsClientSharedMemory(argc, argv, 0);
|
2018-09-23 21:17:31 +00:00
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY + 1);
|
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle)cl;
|
2016-03-07 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 18:15:20 +00:00
|
|
|
class InProcessPhysicsClientExistingExampleBrowser : public PhysicsClientSharedMemory
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
CommonExampleInterface* m_physicsServerExample;
|
2017-05-13 18:15:20 +00:00
|
|
|
SharedMemoryInterface* m_sharedMem;
|
|
|
|
b3Clock m_clock;
|
|
|
|
unsigned long long int m_prevTime;
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2017-05-13 18:15:20 +00:00
|
|
|
public:
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientExistingExampleBrowser(struct GUIHelperInterface* guiHelper, bool useInProcessMemory, bool skipGraphicsUpdate)
|
2017-05-13 18:15:20 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
m_sharedMem = 0;
|
2017-05-13 18:15:20 +00:00
|
|
|
CommonExampleOptions options(guiHelper);
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
if (useInProcessMemory)
|
|
|
|
{
|
|
|
|
m_sharedMem = new InProcessMemory;
|
|
|
|
options.m_sharedMem = m_sharedMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.m_skipGraphicsUpdate = skipGraphicsUpdate;
|
|
|
|
m_physicsServerExample = PhysicsServerCreateFuncBullet2(options);
|
|
|
|
m_physicsServerExample->initPhysics();
|
|
|
|
m_physicsServerExample->resetCamera();
|
2017-05-13 18:15:20 +00:00
|
|
|
setSharedMemoryInterface(m_sharedMem);
|
|
|
|
m_clock.reset();
|
|
|
|
m_prevTime = m_clock.getTimeMicroseconds();
|
|
|
|
}
|
|
|
|
virtual ~InProcessPhysicsClientExistingExampleBrowser()
|
|
|
|
{
|
|
|
|
m_physicsServerExample->exitPhysics();
|
|
|
|
//s_instancingRenderer->removeAllInstances();
|
|
|
|
delete m_physicsServerExample;
|
|
|
|
delete m_sharedMem;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
// return non-null if there is a status, nullptr otherwise
|
|
|
|
virtual const struct SharedMemoryStatus* processServerStatus()
|
|
|
|
{
|
2017-07-14 22:12:16 +00:00
|
|
|
m_physicsServerExample->updateGraphics();
|
2017-05-13 18:15:20 +00:00
|
|
|
|
|
|
|
unsigned long long int curTime = m_clock.getTimeMicroseconds();
|
|
|
|
unsigned long long int dtMicro = curTime - m_prevTime;
|
|
|
|
m_prevTime = curTime;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
double dt = double(dtMicro) / 1000000.;
|
2017-05-13 18:15:20 +00:00
|
|
|
|
|
|
|
m_physicsServerExample->stepSimulation(dt);
|
|
|
|
{
|
|
|
|
b3Clock::usleep(0);
|
|
|
|
}
|
|
|
|
const SharedMemoryStatus* stat = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
stat = PhysicsClientSharedMemory::processServerStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
2017-05-13 18:15:20 +00:00
|
|
|
|
|
|
|
virtual void renderScene()
|
|
|
|
{
|
|
|
|
m_physicsServerExample->renderScene();
|
|
|
|
}
|
2017-05-13 20:37:49 +00:00
|
|
|
virtual void debugDraw(int debugDrawMode)
|
|
|
|
{
|
|
|
|
m_physicsServerExample->physicsDebugDraw(debugDrawMode);
|
|
|
|
}
|
|
|
|
virtual bool mouseMoveCallback(float x, float y)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
return m_physicsServerExample->mouseMoveCallback(x, y);
|
2017-05-13 20:37:49 +00:00
|
|
|
}
|
|
|
|
virtual bool mouseButtonCallback(int button, int state, float x, float y)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
return m_physicsServerExample->mouseButtonCallback(button, state, x, y);
|
2017-05-13 20:37:49 +00:00
|
|
|
}
|
2017-05-13 18:15:20 +00:00
|
|
|
};
|
|
|
|
|
2017-05-13 20:37:49 +00:00
|
|
|
void b3InProcessDebugDrawInternal(b3PhysicsClientHandle clientHandle, int debugDrawMode)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*)clientHandle;
|
2017-05-13 20:37:49 +00:00
|
|
|
cl->debugDraw(debugDrawMode);
|
|
|
|
}
|
2017-05-13 18:15:20 +00:00
|
|
|
void b3InProcessRenderSceneInternal(b3PhysicsClientHandle clientHandle)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*)clientHandle;
|
2017-05-13 18:15:20 +00:00
|
|
|
cl->renderScene();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int b3InProcessMouseMoveCallback(b3PhysicsClientHandle clientHandle, float x, float y)
|
2017-05-13 20:37:49 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*)clientHandle;
|
|
|
|
return cl->mouseMoveCallback(x, y);
|
2017-05-13 20:37:49 +00:00
|
|
|
}
|
|
|
|
int b3InProcessMouseButtonCallback(b3PhysicsClientHandle clientHandle, int button, int state, float x, float y)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*)clientHandle;
|
|
|
|
return cl->mouseButtonCallback(button, state, x, y);
|
2017-05-13 20:37:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerFromExistingExampleBrowserAndConnect(void* guiHelperPtr)
|
2017-05-13 18:15:20 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
static DummyGUIHelper noGfx;
|
|
|
|
|
|
|
|
GUIHelperInterface* guiHelper = (GUIHelperInterface*)guiHelperPtr;
|
|
|
|
if (!guiHelper)
|
|
|
|
{
|
|
|
|
guiHelper = &noGfx;
|
|
|
|
}
|
|
|
|
bool useInprocessMemory = true;
|
|
|
|
bool skipGraphicsUpdate = false;
|
|
|
|
|
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = new InProcessPhysicsClientExistingExampleBrowser(guiHelper, useInprocessMemory, skipGraphicsUpdate);
|
|
|
|
|
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle)cl;
|
2017-05-14 19:00:28 +00:00
|
|
|
}
|
2018-05-07 22:57:36 +00:00
|
|
|
|
2018-06-05 01:36:54 +00:00
|
|
|
extern int gSharedMemoryKey;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerFromExistingExampleBrowserAndConnect3(void* guiHelperPtr, int sharedMemoryKey)
|
2018-05-07 22:57:36 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
static DummyGUIHelper noGfx;
|
|
|
|
|
2018-06-05 01:36:54 +00:00
|
|
|
gSharedMemoryKey = sharedMemoryKey;
|
2018-09-23 21:17:31 +00:00
|
|
|
GUIHelperInterface* guiHelper = (GUIHelperInterface*)guiHelperPtr;
|
|
|
|
if (!guiHelper)
|
|
|
|
{
|
|
|
|
guiHelper = &noGfx;
|
|
|
|
}
|
|
|
|
bool useInprocessMemory = false;
|
|
|
|
bool skipGraphicsUpdate = true;
|
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = new InProcessPhysicsClientExistingExampleBrowser(guiHelper, useInprocessMemory, skipGraphicsUpdate);
|
|
|
|
|
|
|
|
cl->setSharedMemoryKey(sharedMemoryKey + 1);
|
|
|
|
cl->connect();
|
2018-06-06 02:44:33 +00:00
|
|
|
//backward compatiblity
|
|
|
|
gSharedMemoryKey = SHARED_MEMORY_KEY;
|
2018-09-23 21:17:31 +00:00
|
|
|
return (b3PhysicsClientHandle)cl;
|
2018-05-07 22:57:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 02:44:33 +00:00
|
|
|
//backward compatiblity
|
2018-09-23 21:17:31 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerFromExistingExampleBrowserAndConnect2(void* guiHelperPtr)
|
2018-06-06 02:44:33 +00:00
|
|
|
{
|
|
|
|
return b3CreateInProcessPhysicsServerFromExistingExampleBrowserAndConnect3(guiHelperPtr, SHARED_MEMORY_KEY);
|
|
|
|
}
|